-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock.java
35 lines (29 loc) · 1.02 KB
/
Stock.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
import java.util.HashMap;
public class Stock {
private String stockName;
private double currentPrice;
private HashMap<String, Double> priceHistory; // Date -> Price
public Stock(String stockName, double initialPrice) {
this.stockName = stockName;
this.currentPrice = initialPrice;
this.priceHistory = new HashMap<>();
this.priceHistory.put("2023-01-01", initialPrice); // Example starting point
}
// Update the price on a specific date
public void updatePrice(String date, double newPrice) {
this.currentPrice = newPrice;
priceHistory.put(date, newPrice);
}
// Get the current price
public double getCurrentPrice() {
return currentPrice;
}
// Get the price history
public HashMap<String, Double> getPriceHistory() {
return priceHistory;
}
@Override
public String toString() {
return "Stock: " + stockName + ", Current Price: " + currentPrice + ", Price History: " + priceHistory.toString();
}
}