-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockTradingPlatform.java
More file actions
186 lines (171 loc) · 6.82 KB
/
Copy pathStockTradingPlatform.java
File metadata and controls
186 lines (171 loc) · 6.82 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
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
class Stock {
private String symbol;
private String name;
private double price;
private int quantity;
public Stock(String symbol, String name, double price) {
this.symbol = symbol;
this.name = name;
this.price = price;
this.quantity = 0;
}
public String getSymbol() {
return symbol;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
class Portfolio {
private Map<String, Stock> stocksOwned;
private double cashBalance;
public Portfolio(double initialCash) {
this.stocksOwned = new HashMap<>();
this.cashBalance = initialCash;
}
public void buyStock(Stock stock, int quantity) {
double totalPrice = stock.getPrice() * quantity;
if (totalPrice <= cashBalance) {
cashBalance -= totalPrice;
if (stocksOwned.containsKey(stock.getSymbol())) {
Stock existingStock = stocksOwned.get(stock.getSymbol());
existingStock.setQuantity(existingStock.getQuantity() + quantity);
} else {
Stock newStock = new Stock(stock.getSymbol(), stock.getName(), stock.getPrice());
newStock.setQuantity(quantity);
stocksOwned.put(stock.getSymbol(), newStock);
}
System.out.println(quantity + " shares of " + stock.getName() + " bought successfully.");
} else {
System.out.println("Insufficient funds to buy " + quantity + " shares of " + stock.getName() + ".");
}
}
public void sellStock(Stock stock, int quantity) {
if (stocksOwned.containsKey(stock.getSymbol())) {
Stock ownedStock = stocksOwned.get(stock.getSymbol());
if (ownedStock.getQuantity() >= quantity) {
double totalPrice = stock.getPrice() * quantity;
cashBalance += totalPrice;
ownedStock.setQuantity(ownedStock.getQuantity() - quantity);
if (ownedStock.getQuantity() == 0) {
stocksOwned.remove(stock.getSymbol());
}
System.out.println(quantity + " shares of " + stock.getName() + " sold successfully.");
} else {
System.out.println("You do not own enough shares of " + stock.getName() + " to sell " + quantity + ".");
}
} else {
System.out.println("You do not own any shares of " + stock.getName() + ".");
}
}
public double getPortfolioValue() {
double portfolioValue = cashBalance;
for (Stock stock : stocksOwned.values()) {
portfolioValue += stock.getPrice() * stock.getQuantity();
}
return portfolioValue;
}
public double getCashBalance() {
return cashBalance;
}
public Map<String, Stock> getStocksOwned() {
return stocksOwned;
}
}
class MarketData {
private Map<String, Double> stockPrices;
private Random random;
public MarketData() {
this.stockPrices = new HashMap<>();
this.random = new Random();
initializeStockPrices();
}
private void initializeStockPrices() {
stockPrices.put("AAPL", 150.0);
stockPrices.put("GOOGL", 2500.0);
stockPrices.put("AMZN", 3500.0);
}
public void updateStockPrices() {
for (String symbol : stockPrices.keySet()) {
double currentPrice = stockPrices.get(symbol);
double newPrice = currentPrice * (1 + (random.nextDouble() - 0.5) * 0.1);
stockPrices.put(symbol, newPrice);
}
}
public double getPrice(String symbol) {
return stockPrices.getOrDefault(symbol, 0.0);
}
}
public class StockTradingPlatform {
public static void main(String[] args) {
Portfolio portfolio = new Portfolio(10000.0);
MarketData marketData = new MarketData();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n=== Stock Trading Platform ===");
System.out.println("1. Buy Stock");
System.out.println("2. Sell Stock");
System.out.println("3. View Portfolio");
System.out.println("4. Quit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
buyStock(portfolio, marketData, scanner);
break;
case 2:
sellStock(portfolio, marketData, scanner);
break;
case 3:
viewPortfolio(portfolio);
break;
case 4:
System.out.println("Exiting the program...");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number between 1 and 4.");
}
marketData.updateStockPrices();
}
}
private static void buyStock(Portfolio portfolio, MarketData marketData, Scanner scanner) {
System.out.print("Enter stock symbol to buy: ");
String symbol = scanner.next().toUpperCase();
double price = marketData.getPrice(symbol);
System.out.print("Enter quantity to buy: ");
int quantity = scanner.nextInt();
Stock stock = new Stock(symbol, "Dummy Stock", price);
portfolio.buyStock(stock, quantity);
}
private static void sellStock(Portfolio portfolio, MarketData marketData, Scanner scanner) {
System.out.print("Enter stock symbol to sell: ");
String symbol = scanner.next().toUpperCase();
double price = marketData.getPrice(symbol);
System.out.print("Enter quantity to sell: ");
int quantity = scanner.nextInt();
Stock stock = new Stock(symbol, "Dummy Stock", price);
portfolio.sellStock(stock, quantity);
}
private static void viewPortfolio(Portfolio portfolio) {
System.out.println("\n=== Portfolio Summary ===");
System.out.println("Cash Balance: $" + portfolio.getCashBalance());
System.out.println("Stocks Owned:");
for (Stock stock : portfolio.getStocksOwned().values()) {
System.out.println("- " + stock.getName() + " (" + stock.getSymbol() + "): " + stock.getQuantity() + " shares");
}
System.out.println("Total Portfolio Value: $" + portfolio.getPortfolioValue());
}
}