Repository navigation
Expand file tree
/
Copy pathPortfolio.java
More file actions
199 lines (137 loc) · 5.07 KB
/
Copy pathPortfolio.java
File metadata and controls
199 lines (137 loc) · 5.07 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
192
193
194
195
196
197
198
199
import java.util.HashMap;
import java.util.Map;
public class Portfolio {
private Map<String, Holding> holdings;
public Portfolio() {
holdings = new HashMap<>();
}
public void buyStock(String symbol, int quantity, double price) {
if (holdings.containsKey(symbol)) {
Holding holding = holdings.get(symbol);
double totalOldInvestment =
holding.quantity * holding.averageBuyPrice;
double newInvestment = quantity * price;
int newQuantity = holding.quantity + quantity;
holding.averageBuyPrice =
(totalOldInvestment + newInvestment) / newQuantity;
holding.quantity = newQuantity;
} else {
holdings.put(
symbol,
new Holding(quantity, price)
);
}
}
public boolean sellStock(String symbol, int quantity) {
if (!holdings.containsKey(symbol)) {
return false;
}
Holding holding = holdings.get(symbol);
if (quantity > holding.quantity) {
return false;
}
holding.quantity -= quantity;
if (holding.quantity == 0) {
holdings.remove(symbol);
}
return true;
}
public boolean ownsStock(String symbol) {
return holdings.containsKey(symbol);
}
public int getQuantity(String symbol) {
if (!holdings.containsKey(symbol)) {
return 0;
}
return holdings.get(symbol).quantity;
}
public double getAverageBuyPrice(String symbol) {
if (!holdings.containsKey(symbol)) {
return 0;
}
return holdings.get(symbol).averageBuyPrice;
}
public Map<String, Holding> getHoldings() {
return holdings;
}
public double getTotalInvestment() {
double total = 0;
for (Holding holding : holdings.values()) {
total += holding.quantity * holding.averageBuyPrice;
}
return total;
}
public double getCurrentValue(Map<String, Stock> marketStocks) {
double total = 0;
for (Map.Entry<String, Holding> entry : holdings.entrySet()) {
String symbol = entry.getKey();
Holding holding = entry.getValue();
Stock stock = marketStocks.get(symbol);
if (stock != null) {
total += holding.quantity * stock.getPrice();
}
}
return total;
}
public double getProfitLoss(Map<String, Stock> marketStocks) {
return getCurrentValue(marketStocks) - getTotalInvestment();
}
public void displayPortfolio(Map<String, Stock> marketStocks) {
if (holdings.isEmpty()) {
System.out.println("\nYour portfolio is empty.");
return;
}
System.out.println("\n==============================================================");
System.out.println(" MY PORTFOLIO");
System.out.println("==============================================================");
System.out.printf(
"%-10s %-10s %-15s %-15s %-15s%n",
"Stock",
"Quantity",
"Avg Price",
"Current",
"P/L"
);
System.out.println("--------------------------------------------------------------");
for (Map.Entry<String, Holding> entry : holdings.entrySet()) {
String symbol = entry.getKey();
Holding holding = entry.getValue();
Stock stock = marketStocks.get(symbol);
if (stock != null) {
double currentPrice = stock.getPrice();
double profitLoss =
(currentPrice - holding.averageBuyPrice)
* holding.quantity;
System.out.printf(
"%-10s %-10d $%-14.2f $%-14.2f $%-14.2f%n",
symbol,
holding.quantity,
holding.averageBuyPrice,
currentPrice,
profitLoss
);
}
}
System.out.println("--------------------------------------------------------------");
double investment = getTotalInvestment();
double currentValue = getCurrentValue(marketStocks);
double profitLoss = getProfitLoss(marketStocks);
System.out.printf("Total Investment : $%.2f%n", investment);
System.out.printf("Current Value : $%.2f%n", currentValue);
System.out.printf("Unrealized P/L : $%.2f%n", profitLoss);
}
public static class Holding {
private int quantity;
private double averageBuyPrice;
public Holding(int quantity, double averageBuyPrice) {
this.quantity = quantity;
this.averageBuyPrice = averageBuyPrice;
}
public int getQuantity() {
return quantity;
}
public double getAverageBuyPrice() {
return averageBuyPrice;
}
}
}