java Map<String,Deque<Block>>需要值帮助
我是初学者,所以请对我放松点。我必须创建一个存储不同3个字母的股票代码、股票价格和购买价格的股票程序。然后召回它们,以不同的价格出售。我的问题是,我似乎无法在地图上适当地增加更多的股票价值。当我在其中放入一个不同的值时,它会擦除第一个值,或者至少看起来是这样。My Block类(未列出)将最新的键和值返回到下面列出的代码。我的代码和结果如下所示:
private String getPurchaseMap(String stockCode, Double quantityPurchased, Double purchasePrice) {
var stock = new Block(stockCode, quantityPurchased, purchasePrice);
var deque = new ArrayDeque<Block>();
if (!maintainStocks.containsKey(stockCode)) {
deque = new ArrayDeque<Block>();
deque.offerFirst(stock);
maintainStocks.put(stockCode, deque);
} else {
deque.offer(stock);
maintainStocks.put(stockCode, deque);
}
var tempObj = maintainStocks.get(stockCode).peekFirst();
for (var stocks : maintainStocks.entrySet()) {
System.out.printf("%s%n", stocks.getKey());
for (var test : stocks.getValue()) {
System.out.println(tempObj.getQuantity());
System.out.println(tempObj.getPrice());
}
}
return null;
}
The results are:
-Would you like to (B)uy, (S)ell stock or (E)xit? > b
-Please enter the 3-letter stock code > aaa
-Please enter the quantity to purchase > 10
-Please enter the price per share > 15
-aaa
-10.0
-15.0
-Would you like to pursue another transaction? (Y)es or (N)o > y
-Would you like to (B)uy, (S)ell stock or (E)xit? > b
-Please enter the 3-letter stock code > aaa
-Please enter the quantity to purchase > 11
-Please enter the price per share > 16
-aaa
-11.0
-16.0
-Would you like to pursue another transaction? (Y)es or (N)o > n
-Thank you for your consideration. Solution exiting.
共 (0) 个答案