-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeAlpha_Stock_Trading_Platform.java
More file actions
195 lines (175 loc) · 6.48 KB
/
CodeAlpha_Stock_Trading_Platform.java
File metadata and controls
195 lines (175 loc) · 6.48 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
package CodeAlpha_Stock_Trading_Platform;
import java.util.*;
public class CodeAlpha_Stock_Trading_Platform{
//stockmarket info
static class StockMarket{
private HashMap<String , Double> stocks;
public StockMarket(){
stocks = new HashMap<>();
}
//add a new stock
public void addStock(String stockName, double price){
stocks.put(stockName, price);
System.out.println("stock added with name : " + stockName);
}
//get stock price
public double getStockPrice(String stockName){
if (!stocks.containsKey(stockName)){
System.out.println(stockName + " not found");
return 0.0;
}
else{
return stocks.get(stockName);
}
}
//change stock price
public void changeStockPrice(String stockName , double newPrice){
stocks.put(stockName, newPrice);
System.out.println("Stock Price Updated");
}
//delete stock
public void deleteStock(String stockName){
if (!stocks.containsKey(stockName)){
System.out.println(stockName + " not found");
}
else{
stocks.remove(stockName);
System.out.println(stockName + " deleted");
}
}
//print all available stocks
public void printAllStocks(){
System.out.println("******All Stocks******");
Set<String> keySet = stocks.keySet();
for (String key : keySet){
System.out.println(key + " : " + stocks.get(key));
}
}
}
static class Info{
private double balance;
private HashMap<String, Integer> portfolio;//stockname , qty of that stock
public Info(double balance){
this.balance = balance;
portfolio = new HashMap<>();
}
}
//user info
static class User{
private StockMarket stock;
private HashMap<String, Info> users;
public User(){
users = new HashMap<>();
stock = new StockMarket();
}
//add your account
public void addUser(String name, double balance){
users.put(name, new Info(balance));
System.out.println("Account added with name : " + name);
}
//get balance
public double getBalance(String name){
if (!users.containsKey(name)){
System.out.println(name + " not found");
return 0.0;
}
else{
return users.get(name).balance;
}
}
//change balance
public void changeBalance(String name, double newBalance){
if (!users.containsKey(name)){
System.out.println(name + " not found");
}
else{
users.get(name).balance = newBalance;
System.out.println("Balance Updated");
}
}
//get portfolio
public HashMap<String , Integer> getPortfolio(String name){
if (!users.containsKey(name)){
System.out.println(name + " not found");
return new HashMap<>();
}
else{
return users.get(name).portfolio;
}
}
//buy stock
public void buyStock(String name, String stockSymbol, int qty){
double price = stock.getStockPrice(stockSymbol);
if (!users.containsKey(name)){
System.out.println(name + " not found");
}
else{
if (users.get(name).balance > price * qty){
users.get(name).balance -= price* qty;
users.get(name).portfolio.put(stockSymbol, users.get(name).portfolio.getOrDefault(stockSymbol, 0) + qty);
System.out.println(stockSymbol + " bought");
}
else{
System.out.println("Not enough balance to buy stocks");
}
}
}
//sell stocks
public void sellStock(String name, String stockSymbol, int qty){
double price = stock.getStockPrice(stockSymbol);
if (!users.containsKey(name)){
System.out.println(name + " not found");
}
else{
if (users.get(name).portfolio.get(stockSymbol) >= qty){
users.get(name).balance += price* qty;
users.get(name).portfolio.put(stockSymbol, users.get(name).portfolio.get(stockSymbol) - qty);
System.out.println(stockSymbol + " sold");
}
else{
System.out.println("Not enough stocks to sell");
}
}
}
//print info about user
public void printInfo(String name){
if (!users.containsKey(name)){
System.out.println(name + " not found");
}
else{
System.out.println("******User Info******");
System.out.println("Username : " + name);
System.out.println("Balance amount : " + users.get(name).balance);
Set<String> keyset = users.get(name).portfolio.keySet();
for (String key : keyset){
System.out.println(key + " : " + users.get(name).portfolio.get(key));
}
}
}
//delete user account
public void deleteAccount(String name){
users.remove(name);
System.out.println("Account deleted with name : " + name);
}
}
public static void main(String[] args) {
User users = new User();
users.addUser("Abc", 100000);
users.addUser("xyz", 50000);
users.stock.addStock("Apple", 10000);
users.stock.addStock("CodeAlpha", 5000);
users.stock.addStock("Google", 7000);
users.buyStock("Abc", "Apple", 5);
users.buyStock("Abc", "Google", 2);
users.buyStock("Abc", "CodeAlpha", 3);
users.buyStock("Def", "Apple", 5);
users.buyStock("xyz", "CodeAlpha", 7);
users.buyStock("xyz", "Google", 10);
users.sellStock("Abc", "Apple", 4);
System.out.println(users.getPortfolio("Abc"));
System.out.println("Balance = " + users.getBalance("Abc"));
users.printInfo("Abc");
users.deleteAccount("xyz");
users.stock.deleteStock("Google");
}
}