-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPortfolio.java
More file actions
112 lines (97 loc) · 3.66 KB
/
Portfolio.java
File metadata and controls
112 lines (97 loc) · 3.66 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
package account;
import org.decimal4j.util.DoubleRounder;
import stocks.Stock;
import stocks.Transaction;
import utilities.Console;
import utilities.Messages;
import java.util.ArrayList;
public class Portfolio {
private ArrayList<Stock> mainPortfolio;
Console console = new Console(System.in,System.out);
private Double buyingPower;
private Double currentPortfolioValue;
public Portfolio() {
this.mainPortfolio = new ArrayList<>();
this.buyingPower = 2500.00;
this.currentPortfolioValue = 0.00;
}
public Double getBuyingPower() {
return buyingPower;
}
public Double getCurrentPortfolioValue() {
return currentPortfolioValue + buyingPower;
}
public void addStockToPortfolio(Stock stock){
mainPortfolio.add(stock);
}
public void purchaseStock(String stockSymbol, String month, Integer numOfShares){
Double costOfPurchase = DoubleRounder.round(Stock.checkStockPrice(stockSymbol,month) * numOfShares,2);
if(costOfPurchase < buyingPower){
if(checkToSeeIfOwnStock(stockSymbol)){
getStockFromPortfolio(stockSymbol).addTransaction(Transaction.makeTransaction(stockSymbol,month,numOfShares));
} else {
//TODO resolve way to get name for stock per symbol
Stock stock = new Stock(stockSymbol,"TempName");
stock.addTransaction(Transaction.makeTransaction(stockSymbol,month,numOfShares));
addStockToPortfolio(stock);
}
buyingPower = buyingPower - costOfPurchase;
} else{
console.println(Messages.notEnough);
}
}
public Boolean checkToSeeIfOwnStock(String stockSymbol){
Boolean result = false;
for (Stock s : mainPortfolio) {
if(s.symbol.equalsIgnoreCase(stockSymbol)){
result = true;
break;
}
}
return result;
}
public Stock getStockFromPortfolio(String stockSymbol){
for (Stock s: mainPortfolio) {
if(s.symbol.equalsIgnoreCase(stockSymbol)){
return s;
}
}
return null;
}
public void updateCurrentPortfolioValue(String month){
Double newCurrentValue = 0.0;
for (Stock s: mainPortfolio) {
s.updateCurrentStockPrice(s.symbol,month);
newCurrentValue += getEquityOfStock(s);
}
currentPortfolioValue = newCurrentValue;
}
public Double getEquityOfStock(Stock stock){
return DoubleRounder.round((stock.getCurrentStockPrice() * stock.getTotalNumOfShares()),2);
}
public String getPositionOfStock(String stockSymbol){
StringBuilder builder = new StringBuilder();
Stock stock = getStockFromPortfolio(stockSymbol);
builder.append("Name : ")
.append(stock.name)
.append("\nSymbol : ")
.append(stock.symbol)
.append("\nNumber of Shares : ")
.append(stock.totalNumOfShares)
.append("\nCurrent Price : ")
.append(stock.currentStockPrice)
.append("\nEquity : ")
.append(stock.valueOfPosition);
return builder.toString();
}
public String getAllPositionsFromPortfolio(){
StringBuilder builder = new StringBuilder();
for (Stock s : mainPortfolio) {
builder.append("*******************")
.append("\n")
.append(getPositionOfStock(s.symbol))
.append("\n");
}
return builder.toString();
}
}