-
Notifications
You must be signed in to change notification settings - Fork 0
/
Market.java
88 lines (69 loc) · 2.35 KB
/
Market.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Market implements Serializable, Savable, Printable{
private String name;
private ArrayList<Asset> assets;
public Market(String name, ArrayList<Asset> assets){
this.name = name;
this.assets = assets;
}
// prints all the assets in market
public void print(){
System.out.println("<>".repeat(50));
System.out.println(this.name.toUpperCase());
for(Asset asset: this.assets){
asset.print();
}
System.out.println("<>".repeat(50));
}
// save market to file
public void save(){
try{
FileOutputStream file = new FileOutputStream(this.name + Constants.FILE_MARK_EXTENTION.getFull());
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(this);
out.close();
file.close();
}catch(IOException e){
System.out.println("IOException is caught");
}
}
// retrieve market from file
public static Market retrieve(String fileName){
Market market = null;
try{
FileInputStream file = new FileInputStream(fileName + Constants.FILE_MARK_EXTENTION.getFull());
ObjectInputStream in = new ObjectInputStream(file);
market = (Market) in.readObject();
in.close();
file.close();
}catch (IOException | ClassNotFoundException e){
}
return market;
}
// class variate in all the assets in the market
public void variate(boolean isPrint){
for(int i=0; i<assets.size(); i++){
assets.get(i).variatePrice(Math.random() * 5, isPrint);
}
}
// returns asset based on ticker (or null == NOT FOUND)
public Asset getAssetByTicker(String key){
List<Asset> stream = this.assets.stream().filter(s -> s.getTicker().equals(key)).collect(Collectors.toList());
if (stream.isEmpty()){
System.out.println("Asset does not exists");
return null;
}
return stream.get(0);
}
// add asset to market
public void addAsset(Asset asset){
this.assets.add(asset);
}
// remove asset to market
public void removeAsset(Asset asset){
this.assets.remove(asset);
}
}