-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockTradingPlatform.java
More file actions
218 lines (161 loc) · 7.19 KB
/
Copy pathStockTradingPlatform.java
File metadata and controls
218 lines (161 loc) · 7.19 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.util.ArrayList;
import java.util.Scanner;
public class StockTradingPlatform {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Create user
System.out.print("Enter your name: ");
String userName = sc.nextLine();
User user = new User(userName, 10000);
// Create stocks
ArrayList<Stock> stocks = new ArrayList<>();
stocks.add(new Stock("AAPL", "Apple", 180.50));
stocks.add(new Stock("TSLA", "Tesla", 245.30));
stocks.add(new Stock("MSFT", "Microsoft", 420.75));
stocks.add(new Stock("AMZN", "Amazon", 185.20));
// Store transactions
ArrayList<Transaction> transactions = new ArrayList<>();
// Store user's stock holdings
Portfolio portfolio = new Portfolio();
int choice;
do {
System.out.println("\n========================================");
System.out.println(" STOCK TRADING PLATFORM");
System.out.println("========================================");
System.out.println("1. View Market");
System.out.println("2. Buy Stock");
System.out.println("3. Sell Stock");
System.out.println("4. View Balance");
System.out.println("5. View Portfolio");
System.out.println("6. View Transactions");
System.out.println("7. Exit");
System.out.println("========================================");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
// Display market
System.out.println("\n============== MARKET DATA ==============");
System.out.printf("%-10s %-15s %-15s%n",
"Symbol", "Company", "Price");
System.out.println("-----------------------------------------");
for (Stock stock : stocks) {
stock.displayStock();
}
break;
case 2:
// Buy stock
System.out.print("\nEnter stock symbol to buy: ");
String buySymbol = sc.next().toUpperCase();
Stock buyStock = findStock(stocks, buySymbol);
if (buyStock == null) {
System.out.println("Stock not found.");
break;
}
System.out.print("Enter quantity: ");
int buyQuantity = sc.nextInt();
if (buyQuantity <= 0) {
System.out.println("Quantity must be greater than 0.");
break;
}
double buyAmount = buyStock.getPrice() * buyQuantity;
if (user.deductBalance(buyAmount)) {
Transaction buyTransaction = new Transaction(
"BUY",
buyStock.getSymbol(),
buyQuantity,
buyStock.getPrice()
);
transactions.add(buyTransaction);
portfolio.addStock(buyStock.getSymbol(), buyQuantity);
System.out.printf(
"Successfully purchased %d shares of %s.%n",
buyQuantity,
buyStock.getName()
);
System.out.printf(
"Total Amount: ₹%.2f%n",
buyAmount
);
} else {
System.out.println("Insufficient balance.");
}
break;
case 3:
// Sell stock
System.out.print("\nEnter stock symbol to sell: ");
String sellSymbol = sc.next().toUpperCase();
Stock sellStock = findStock(stocks, sellSymbol);
if (sellStock == null) {
System.out.println("Stock not found.");
break;
}
System.out.print("Enter quantity: ");
int sellQuantity = sc.nextInt();
if (sellQuantity <= 0) {
System.out.println("Quantity must be greater than 0.");
break;
}
double sellAmount =
sellStock.getPrice() * sellQuantity;
if (!portfolio.removeStock(sellStock.getSymbol(), sellQuantity)) {
System.out.println("You do not own enough shares to sell.");
break;
}
user.addBalance(sellAmount);
Transaction sellTransaction = new Transaction(
"SELL",
sellStock.getSymbol(),
sellQuantity,
sellStock.getPrice()
);
transactions.add(sellTransaction);
System.out.printf(
"Successfully sold %d shares of %s.%n",
sellQuantity,
sellStock.getName()
);
System.out.printf(
"Total Amount Added: ₹%.2f%n",
sellAmount
);
break;
case 4:
// Display balance
System.out.println("\n============== USER ACCOUNT ==============");
user.displayUser();
break;
case 5:
// Display portfolio
portfolio.displayPortfolio(stocks);
break;
case 6:
// Display transactions
System.out.println("\n============== TRANSACTIONS ==============");
if (transactions.isEmpty()) {
System.out.println("No transactions yet.");
} else {
for (Transaction transaction : transactions) {
transaction.displayTransaction();
}
}
break;
case 7:
System.out.println("\nThank you for using Stock Trading Platform!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 7);
sc.close();
}
// Find stock using symbol
public static Stock findStock(ArrayList<Stock> stocks, String symbol) {
for (Stock stock : stocks) {
if (stock.getSymbol().equalsIgnoreCase(symbol)) {
return stock;
}
}
return null;
}
}