-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTradingFirm.java
More file actions
128 lines (104 loc) · 3.8 KB
/
Copy pathTradingFirm.java
File metadata and controls
128 lines (104 loc) · 3.8 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
package given;
import java.math.BigDecimal;
import java.util.*;
class Position {
private String accountId;
private String symbol;
private long shares;
public Position(String accountId, String symbol, long shares) {
this.accountId = accountId;
this.symbol = symbol;
this.shares = shares;
}
public String getAccountId() {
return accountId;
}
public String getSymbol() {
return symbol;
}
public long getShares() {
return shares;
}
}
class Quote {
private String symbol;
private BigDecimal price;
public Quote(String symbol, double price) {
this.symbol = symbol;
this.price = BigDecimal.valueOf(price);
}
public String getSymbol() {
return symbol;
}
public BigDecimal getPrice() {
return price;
}
}
class PositionService {
private List<Position> positions;
public PositionService() {
// Initializing the list dynamically
this.positions = new ArrayList<>();
// Adding positions dynamically
this.positions.add(new Position("A123", "WMT", 10));
this.positions.add(new Position("A123", "MCD", 5));
this.positions.add(new Position("B456", "AAPL", 15));
this.positions.add(new Position("B456", "GOOGL", 7));
this.positions.add(new Position("C789", "TSLA", 20));
}
public List<Position> getAllPositions() {
return positions;
}
}
class QuoteService {
private List<Quote> previousClosedQuotes;
public QuoteService() {
// Initializing the list dynamically
this.previousClosedQuotes = new ArrayList<>();
// Adding previous close quotes dynamically using double values
this.previousClosedQuotes.add(new Quote("WMT", 140.50));
this.previousClosedQuotes.add(new Quote("MCD", 210.00));
this.previousClosedQuotes.add(new Quote("AAPL", 144.00));
this.previousClosedQuotes.add(new Quote("GOOGL", 2700.00));
this.previousClosedQuotes.add(new Quote("TSLA", 730.00));
this.previousClosedQuotes.add(new Quote("PB", 138.33)); // Additional example
}
public List<Quote> getAllPreviousClosedQuotes() {
return previousClosedQuotes;
}
// Write this method to get the latest price of a given symbol
public BigDecimal getLatestPrice(String symbol) {
// Look for the latest price in previousClosedQuotes
for (Quote quote : previousClosedQuotes) {
if (quote.getSymbol().equals(symbol)) {
return quote.getPrice();
}
}
// Default price if not found
return BigDecimal.ZERO;
}
}
public class TradingFirm {
public static void main(String[] args) {
PositionService positionService = new PositionService();
QuoteService quoteService = new QuoteService();
List<Position> positions = positionService.getAllPositions();
//List<Quote> previousQuotes = quoteService.getAllPreviousClosedQuotes();
// Map to store total account values
Map<String, BigDecimal> accountValues = new HashMap<>();
// Calculate total value per account
for (Position position : positions) {
BigDecimal price = quoteService.getLatestPrice(position.getSymbol());
BigDecimal totalValue = price.multiply(BigDecimal.valueOf(position.getShares()));
accountValues.put(
position.getAccountId(),
accountValues.getOrDefault(position.getAccountId(), BigDecimal.ZERO).add(totalValue)
);
}
// Display results
System.out.println("Account ID : Total Value");
for (Map.Entry<String, BigDecimal> entry : accountValues.entrySet()) {
System.out.printf("%-10s : $%.2f%n", entry.getKey(), entry.getValue());
}
}
}