-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShareImpl.java
More file actions
73 lines (60 loc) · 2.46 KB
/
ShareImpl.java
File metadata and controls
73 lines (60 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.Serializable;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static java.util.concurrent.TimeUnit.*;
public class ShareImpl implements Share, Serializable {
private String shareName;
private int noOfSharesAvailable;
private float sharePrice;
private transient final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private long timeNow;
private long timeToPriceUpdate;
public ShareImpl(String shareName, int noOfSharesAvailable, float sharePrice) {
this.shareName = shareName;
this. noOfSharesAvailable = noOfSharesAvailable;
this.sharePrice = sharePrice;
// create a new timer object and set it its first run of the updatePrice method to be in one minute and schedule the method to be invoked again in another minute
timeNow = System.currentTimeMillis();
timeToPriceUpdate = System.currentTimeMillis() + 60000;
scheduler();
}
// schedule a timer to update the price of a share every minute by adding a value in range -30 to 30 to its price. append 60000 milliseconds (1 min) to timeNow
// schedule a second timer to update the timeNow every second
private void scheduler() {
final Runnable updatePrice = new Runnable() {
@Override
public void run() {
Random random = new Random();
sharePrice += -30 + random.nextFloat() * (60);
timeToPriceUpdate = timeNow + 60000;
}
};
final Runnable updateTime = new Runnable() {
@Override
public void run() {
timeNow = System.currentTimeMillis();
}
};
scheduler.scheduleAtFixedRate(updatePrice, 60, 60, SECONDS);
scheduler.scheduleAtFixedRate(updateTime, 0, 1, SECONDS);
}
@Override
public String getShareName() {
return shareName;
}
@Override
public int getVolumeOfSharesAvailable() {
return noOfSharesAvailable;
}
@Override
public float getSharePrice() {
return sharePrice;
}
@Override
// return the difference between the two time values to get the time left until the share price will be updated.
public double getTimeRemainingToPriceUpdate() {
//DecimalFormat df = new DecimalFormat("#.##");
return Double.valueOf((timeToPriceUpdate/1000) - (timeNow/1000));
}
}