-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecuritiesAccount.java
59 lines (51 loc) · 2.61 KB
/
SecuritiesAccount.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
import java.util.*;
public class SecuritiesAccount extends Account {
// a collection that stores all the stocks owned
private ArrayList<Stock> stocks = new ArrayList<Stock>();
// a collection that stores all the open positions
private ArrayList<Stock> openPositions = new ArrayList<Stock>();
public SecuritiesAccount(String currencyType) {
super(currencyType);
setAccountType("securities");
}
public boolean buy(Stock stock, int numShares) {
boolean transactionSuccessful = false;
if (getAmount().getValue() >= stock.getCurrentPrice().getValue()*numShares) { // if account has enough money to buy stock
Currency convertedCurrency = stock.getCurrentPrice().convertTo(getCurrencyType());
double totalAmount = getAmount().getValue() - convertedCurrency.getValue()*numShares; // deduct price of total purchase from account's balance
Currency updatedCurrency = new Dollar(totalAmount);
switch(getCurrencyType()) {
case "euro":
updatedCurrency = new Euro(totalAmount);
case "yen":
updatedCurrency = new Yen(totalAmount);
}
setAmount(updatedCurrency); // update the account's new balance
stocks.add(stock); // add the stock to the account's stocks
transactionSuccessful = true;
} else {
System.out.println("This account does not have enough money for this transaction");
}
return transactionSuccessful;
}
public boolean sell(Stock stock, int numShares) {
boolean transactionSuccessful = false;
if (stocks.contains(stock)) { // if the account actually owns the stock attempted to be sold
Currency convertedCurrency = stock.getCurrentPrice().convertTo(getCurrencyType());
double totalAmount = getAmount().getValue() + convertedCurrency.getValue()*numShares; // add price of total sale to account's balance
Currency updatedCurrency = new Dollar(totalAmount);
switch(getCurrencyType()) {
case "euro":
updatedCurrency = new Euro(totalAmount);
case "yen":
updatedCurrency = new Yen(totalAmount);
}
setAmount(updatedCurrency); // update the account's new balance
stocks.remove(stock); // add the stock to the account's stocks
transactionSuccessful = true;
} else {
System.out.println("This account does not own this stock.");
}
return transactionSuccessful;
}
}