-
Notifications
You must be signed in to change notification settings - Fork 0
/
Portfolio.java
151 lines (126 loc) · 4.73 KB
/
Portfolio.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
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
/**
* @version 1.0.0, 18 May 2023
* @author Andrew Kim and Dylan Nguyen
*/
import java.util.Hashtable;
public class Portfolio {
// ticker, shares owned
private static Hashtable<String, Integer> ownedStocks;
private static Hashtable<String, Double> spentOnStocks;
private static final String SYSTEM_NAME = System.getProperty("user.name");
private static double balance;
public Portfolio() {
/**
* Number of shares of each stock
*/
ownedStocks = new Hashtable<>();
for(Stock stock : Broker.getStocks()){
ownedStocks.put(stock.getTicker(), 0);
}
/**
* Amount of money spent on each stock
* Used to calculate the average amount spent on each share
*/
spentOnStocks = new Hashtable<>();
System.out.println(Broker.getStocks().toString());
for(Stock stock : Broker.getStocks()) {
spentOnStocks.put(stock.getTicker(), 0.0);
}
balance = Options.getNetWorth();
}
/**
* Buys an amount of stock specified. Returns -1 if there is not enough money.
* @param ticker
* @param amount
* @return cost of buying those stocks
*/
public static double buyStock(String ticker, int amount){
if(amount == 0) return 0;
double currentStockPrice = Broker.getStock(ticker).getTransactionPrice();
if(currentStockPrice * amount <= balance){
if(ownedStocks.get(ticker) == null) {
ownedStocks.put(ticker, 0);
}
if(spentOnStocks.get(ticker) == null) {
spentOnStocks.put(ticker, 0.0);
}
// increase number of owned shares
ownedStocks.put(ticker, ownedStocks.get(ticker) + amount);
// amount of money spent for transaction
double moneySpent = currentStockPrice * amount;
balance -= Stock.round(moneySpent);
balance = Stock.round(balance);
// add money spent on each stock
spentOnStocks.put(ticker, spentOnStocks.get(ticker) + moneySpent);
// change share counts in Options
Options.setStockCount(ticker, ownedStocks.get(ticker));
return Stock.round(currentStockPrice * amount);
}
return -1;
}
/**
* Sells an amount of stock specified. Returns -1 if there is not enough of that stock owned.
* @param ticker
* @param amount
* @return double based on profit!
*/
public static double sellStock(String ticker, int amount){
double currentStockPrice = Broker.getStock(ticker).getTransactionPrice();
if(ownedStocks.get(ticker) >= amount){
// number of stock owned
int numSharesOwned = ownedStocks.get(ticker);
// decrease number of owned shares
ownedStocks.put(ticker, numSharesOwned - amount);
// add realized money to balance
balance += Stock.round(currentStockPrice * amount);
balance = Stock.round(balance);
/**
* Change amount of money spent on stock
* Multiply amount by fraction of shares sold in transaction
*/
double fractionSold = amount / numSharesOwned;
spentOnStocks.put(ticker, Stock.round(spentOnStocks.get(ticker) * fractionSold));
// change share counts in Options
Options.setStockCount(ticker, ownedStocks.get(ticker));
return Stock.round(currentStockPrice * amount);
}
return -1;
}
/**
* Returns the stock amount using ticker.
* @param ticker
* @return amount of stock owned. Returns 0 if ticker does not exist in program
*/
public static int getStockAmount(String ticker){
if(ownedStocks.get(ticker) != null)
return ownedStocks.get(ticker);
return 0;
}
public static Hashtable<String, Integer> getOwnedStocks() {
return ownedStocks;
}
public static void addMoney(int n){
balance += n;
balance = Stock.round(balance);
}
public static void removeMoney(int n){
balance -= n;
balance = Stock.round(balance);
}
public static double getBalance() {
return Stock.round(balance);
}
public static String getSystemName() {
return SYSTEM_NAME;
}
public static double averageSpentOnStock(String ticker) {
return Stock.round(spentOnStocks.get(ticker) / ownedStocks.get(ticker));
}
public static double getTotalNetworth() {
double networth = balance;
for(String key : ownedStocks.keySet()){
networth += Broker.getStock(key).getTransactionPrice() * ownedStocks.get(key);
}
return Stock.round(networth);
}
}