Repository navigation
Expand file tree
/
Copy pathStockAccount.java
More file actions
137 lines (115 loc) · 5.08 KB
/
Copy pathStockAccount.java
File metadata and controls
137 lines (115 loc) · 5.08 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class StockAccount extends Account{
JSONObject UserAccountData,USERDATA,dataUAD,dataUD;
private final String UserID;
StockAccount(String UserID) throws JSONException, IOException {
super(UserID);
String s="";
this.UserID = UserID;
FileReader reader = new FileReader("UserAccount_Data.json");
Scanner scan = new Scanner(reader);
while(scan.hasNext()){
s = s + scan.nextLine();
}
reader.close();
dataUAD = new JSONObject(s);
UserAccountData = dataUAD.getJSONObject(UserID);
String s3 = "";
FileReader readerx = new FileReader("USERDATA.json");
Scanner scanx = new Scanner(readerx);
while(scanx.hasNext()){
s3 = s3 + scanx.nextLine();
}
readerx.close();
dataUD = new JSONObject(s3);
USERDATA = dataUD.getJSONObject(UserID);
}
public String getUserID(){
return this.UserID;
}
public String getUsername() throws JSONException {
return USERDATA.getString("userName");
}
public double getBalance() throws JSONException {
return Double.valueOf(USERDATA.getString("balance"));
}
public LinkedList<String> StockList(){
LinkedList<String> listOfStocks = new LinkedList<String>();
Iterator it = UserAccountData.keys();
while(it.hasNext()){
listOfStocks.add(it.next().toString());
}
return listOfStocks;
}
public void addMoney(double amount) throws JSONException, IOException {
double currentBalance = Double.valueOf(USERDATA.get("balance").toString()) ;
USERDATA.put("balance",currentBalance + amount);
dataUD.put(this.getUserID(),USERDATA);
FileWriter file = new FileWriter("USERDATA.json");
file.write(dataUD.toString());
file.close();
}
public void deductMoney(double amount) throws IOException, JSONException {
double currentBalance = Double.valueOf(USERDATA.get("balance").toString()) ;
USERDATA.put("balance",currentBalance - amount);
dataUD.put(this.getUserID(),USERDATA);
FileWriter file = new FileWriter("USERDATA.json");
file.write(dataUD.toString());
file.close();
}
public void buyStock(String symbol, int noOfShares) throws JSONException, IOException {
Stock stock = new Stock(symbol);
LinkedList<String> listOfStocks = this.StockList();
boolean exists = false;
for(int i=0;i<listOfStocks.size();i++){
if(listOfStocks.get(i).toString().equals(symbol)){
exists = true;
break;
}
}
if(exists){
UserAccountData.getJSONObject(symbol).getJSONObject("buys").put(String.valueOf(LocalDateTime.now().minusDays((long) 1)).substring(0,10),noOfShares);
int currentHoldings = Integer.parseInt(UserAccountData.getJSONObject(symbol).get("CurrentHoldings").toString());
UserAccountData.getJSONObject(symbol).put("CurrentHoldings",currentHoldings + noOfShares);
}
else{
JSONObject stockJSONObject = new JSONObject();
JSONObject buys = new JSONObject();
JSONObject sells = new JSONObject();
buys.put(String.valueOf(LocalDateTime.now().minusDays((long) 1)).substring(0,10),noOfShares);
stockJSONObject.put("buys",buys);
stockJSONObject.put("sells",sells);
stockJSONObject.put("CurrentHoldings",noOfShares);
UserAccountData.put(symbol,stockJSONObject);
}
double currPrice = new Stock(symbol).getClosePrice();
this.deductMoney(currPrice*noOfShares);
dataUAD.put(this.getUserID(),UserAccountData);
FileWriter file = new FileWriter("UserAccount_Data.json");
file.write(dataUAD.toString());
file.close();
}
public void sellStock(String symbol,int noOfShares) throws JSONException, IOException {
Stock stock = new Stock(symbol);
UserAccountData.getJSONObject(symbol).getJSONObject("sells").put(String.valueOf(LocalDateTime.now().minusDays((long) 1)).substring(0,10),noOfShares);
int currentHoldings = Integer.parseInt(UserAccountData.getJSONObject(symbol).get("CurrentHoldings").toString());
UserAccountData.getJSONObject(symbol).put("CurrentHoldings",currentHoldings - noOfShares);
dataUAD.put(this.getUserID(),UserAccountData);
FileWriter file = new FileWriter("UserAccount_Data.json");
file.write(dataUAD.toString());
file.close();
}
// public static void main(String[] args) throws JSONException, IOException {
// StockAccount pie = new StockAccount("ID1");
// pie.addMoney(200000);
//
// }
}