Repository navigation
Expand file tree
/
Copy pathUser.java
More file actions
191 lines (142 loc) · 4.37 KB
/
Copy pathUser.java
File metadata and controls
191 lines (142 loc) · 4.37 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.util.ArrayList;
public class User {
private String username;
private double balance;
private Portfolio portfolio;
private ArrayList<Transaction> transactions;
private double realizedProfitLoss;
public User(String username, double balance) {
this.username = username;
this.balance = balance;
portfolio = new Portfolio();
transactions = new ArrayList<>();
realizedProfitLoss = 0;
}
public String getUsername() {
return username;
}
public double getBalance() {
return balance;
}
public Portfolio getPortfolio() {
return portfolio;
}
public ArrayList<Transaction> getTransactions() {
return transactions;
}
public double getRealizedProfitLoss() {
return realizedProfitLoss;
}
public boolean buyStock(Stock stock, int quantity) {
if (quantity <= 0) {
return false;
}
double totalCost = stock.getPrice() * quantity;
if (totalCost > balance) {
return false;
}
balance -= totalCost;
portfolio.buyStock(
stock.getSymbol(),
quantity,
stock.getPrice()
);
Transaction transaction = new Transaction(
"BUY",
stock.getSymbol(),
quantity,
stock.getPrice(),
0
);
transactions.add(transaction);
return true;
}
public double calculateSellProfit(
Stock stock,
int quantity) {
double averagePrice =
portfolio.getAverageBuyPrice(
stock.getSymbol()
);
return (stock.getPrice() - averagePrice) * quantity;
}
public boolean sellStock(Stock stock, int quantity) {
if (quantity <= 0) {
return false;
}
if (!portfolio.ownsStock(stock.getSymbol())) {
return false;
}
if (portfolio.getQuantity(stock.getSymbol()) < quantity) {
return false;
}
double totalReceived =
stock.getPrice() * quantity;
double profitLoss =
calculateSellProfit(stock, quantity);
portfolio.sellStock(
stock.getSymbol(),
quantity
);
balance += totalReceived;
realizedProfitLoss += profitLoss;
Transaction transaction = new Transaction(
"SELL",
stock.getSymbol(),
quantity,
stock.getPrice(),
profitLoss
);
transactions.add(transaction);
return true;
}
public void displayBalance() {
System.out.println("\n========================================");
System.out.println(" ACCOUNT BALANCE");
System.out.println("========================================");
System.out.printf(
"Available Cash : $%.2f%n",
balance
);
System.out.printf(
"Realized P/L : $%.2f%n",
realizedProfitLoss
);
}
public void displayTransactions() {
if (transactions.isEmpty()) {
System.out.println(
"\nNo transactions found."
);
return;
}
System.out.println(
"\n================================================================================"
);
System.out.println(
" TRANSACTION HISTORY"
);
System.out.println(
"================================================================================"
);
System.out.printf(
"%-6s %-8s %-8s %-11s %-13s %-13s %s%n",
"Type",
"Stock",
"Qty",
"Price",
"Total",
"P/L",
"Date & Time"
);
System.out.println(
"--------------------------------------------------------------------------------"
);
for (Transaction transaction : transactions) {
transaction.displayTransaction();
}
}
public void setRealizedProfitLoss(double realizedProfitLoss) {
this.realizedProfitLoss = realizedProfitLoss;
}
}