无法更新我的地图 >

问题描述 投票:0回答:1

这是我正在从事的学校项目的最小化版本。下面列出了该项目的详细信息。好。所以我对Java感到新手和生疏,所以我真的可以使用一些帮助。如何在我的Map>中查看值?我有编码器块!我尝试过偷看,各种各样的System.out.println()都不记得该怎么做。

[从事学校项目。到目前为止,该项目要求购买,出售或退出。根据购买选择,它会询问股票代码,要购买的数量以及每股价格。然后,它再次要求购买,出售或退出。再次选择购买时,它将再次询问股票代码,数量,每股价格。并再次要求但出售或退出。选择出售我所有购买的股票(第一轮输入+第二轮输入)后,它会询问股票代码,出售数量,每股价格,输入股票代码,出售数量以及每股价格我只有y份股票,y是第一轮输入量。我无法弄清楚如何将地图值更新为等于我所有的输入数量,价格。有人可以帮我吗?

import java.util.*;
class LinkedHashMap2{
    public static Map<String, Deque<Block>> maintainStocks = new LinkedHashMap<>();

    public static void main(String args[]){

        var x = 0;
        while (x<2) {

            System.out.print("Enter the stock code: ");
            var scanner = new Scanner(System.in);
            var stockCode = scanner.nextLine();

            System.out.print("Enter quantity to purchase: ");
            scanner = new Scanner(System.in);
            var quantity = scanner.nextInt();

            System.out.print("Enter stock price per share: ");
            var price = scanner.nextDouble();

            var block = new Block(stockCode, quantity, price);
            if (!maintainStocks.containsKey(stockCode)) {
                maintainStocks.put(stockCode, new ArrayDeque<>());
            }
            maintainStocks.get(stockCode).add(block);
            x++;
        }

        for (var stockName : maintainStocks.entrySet()) {
            System.out.printf("%s", stockName.getKey());
            for (var stockQuantity : stockName.getValue()){
                System.out.println();
            }
        }
    }




    private static void getStockCode(String s) {

    }


    public static class Block {
        private String stockCode;
        private double price;
        private int quantity;

        public Block(String stockCode, int quantity, double price) {
            this.setCode(stockCode);
            this.setQuantity(quantity);
            this.setPrice(price);
        }

        private void setPrice(double price) {

            this.price = price;
        }

        private void setQuantity(int quantity) {

            this.quantity = quantity;
        }

        private void setCode(String stockCode) {
            Objects.requireNonNull(stockCode);
            this.stockCode = stockCode;
        }

        public int getQuantity() {

            return this.quantity;
        }

        public double getPrice() {

            return this.price;
        }
    }
}
java deque linkedhashmap
1个回答
0
投票

您需要Override类中的equals() hashCode()Block,对于equals()

所有属性参与确定两个对象是否相等。

并且在哈希中,它是stockCode,我想每个对象都是唯一的

,您可以像这样从地图中检索:

Deque<Block> queue = map.get(key);
// Operation on queue

,将这些方法equals()hashCode()添加到您的Block类中

         @Override
         public boolean equals(Object o) {
             if (this == o) {
                 return true;
             }
             if (o == null || getClass() != o.getClass()) {
                 return false;
             }

             Block block = (Block) o;

             if (Double.compare(block.price, price) != 0) {
                 return false;
             }
             if (quantity != block.quantity) {
                 return false;
             }
             return stockCode != null ? stockCode.equals(block.stockCode) : block.stockCode == null;
         }

         @Override
         public int hashCode() {
             return stockCode != null ? stockCode.hashCode() : 0;
         }
© www.soinside.com 2019 - 2024. All rights reserved.