-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockTraderAPI.java
114 lines (90 loc) · 3.7 KB
/
StockTraderAPI.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
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
// Repository Name: StockTraderAPI
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/trade")
public class TradingController {
// Simulated user data
private double balance = 10.00; // User's cash balance
private final Map<String, Stock> portfolio = new HashMap<>(); // User's portfolio
// Inner class to represent a stock
static class Stock {
String symbol;
double price;
int quantity;
Stock(String symbol, double price, int quantity) {
this.symbol = symbol;
this.price = price;
this.quantity = quantity;
}
}
// Mocked method to simulate fetching a stock's price
private double fetchStockPrice(String symbol) {
// Mock prices for simplicity
switch (symbol.toUpperCase()) {
case "AAPL": return 9.50;
case "TSLA": return 7.25;
case "AMZN": return 6.80;
default: return -1; // Indicates stock not found
}
}
// Endpoint to buy stocks
@PostMapping("/buy")
public ResponseEntity<String> buyStock(@RequestParam String symbol, @RequestParam int quantity) {
double price = fetchStockPrice(symbol);
if (price == -1) {
return ResponseEntity.badRequest().body("Stock symbol not found: " + symbol);
}
double totalCost = price * quantity;
if (totalCost > balance) {
return ResponseEntity.badRequest().body("Insufficient funds. Available balance: $" + balance);
}
// Update balance and portfolio
balance -= totalCost;
portfolio.putIfAbsent(symbol, new Stock(symbol, price, 0));
Stock stock = portfolio.get(symbol);
stock.quantity += quantity;
return ResponseEntity.ok("Bought " + quantity + " shares of " + symbol + " for $" + totalCost);
}
// Endpoint to sell stocks
@PostMapping("/sell")
public ResponseEntity<String> sellStock(@RequestParam String symbol, @RequestParam int quantity) {
Stock stock = portfolio.get(symbol);
if (stock == null || stock.quantity < quantity) {
return ResponseEntity.badRequest().body("Not enough shares of " + symbol + " to sell.");
}
double earnings = stock.price * quantity;
stock.quantity -= quantity;
balance += earnings;
if (stock.quantity == 0) {
portfolio.remove(symbol); // Remove stock if fully sold
}
return ResponseEntity.ok("Sold " + quantity + " shares of " + symbol + " for $" + earnings);
}
// Endpoint to view current portfolio
@GetMapping("/portfolio")
public ResponseEntity<Map<String, String>> viewPortfolio() {
Map<String, String> portfolioSummary = new HashMap<>();
portfolio.forEach((symbol, stock) -> {
portfolioSummary.put(symbol, "Shares: " + stock.quantity + ", Price: $" + stock.price);
});
portfolioSummary.put("Cash Balance", "$" + balance);
return ResponseEntity.ok(portfolioSummary);
}
// Endpoint to check a stock's price
@GetMapping("/price")
public ResponseEntity<String> getStockPrice(@RequestParam String symbol) {
double price = fetchStockPrice(symbol);
if (price == -1) {
return ResponseEntity.badRequest().body("Stock symbol not found: " + symbol);
}
return ResponseEntity.ok("Current price of " + symbol + ": $" + price);
}
// Endpoint to check cash balance
@GetMapping("/balance")
public ResponseEntity<String> getBalance() {
return ResponseEntity.ok("Current cash balance: $" + balance);
}
}