0

This is a minimized version to a school project I am working on. Details of the project are listed below. OK. So I am new and rusty at Java so I could really use some help. How can I view the values in my Map> ? I have coders-block! I have tried peek, all sorts of System.out.println()'s I cannot remember how to do this.

Working on a school project. The project thus far asks to either buy, sell, or exit. Upon buy choice it asks for a stock code, quantity to purchase, and price per share. Then it asks again to buy, sell, or exit. Upon buy choice again it will again ask for stock code, quantity, price per share. And again asks to but, sell, or exit. Upon choosing to sell all my purchased stock (first round input + second round input) it asks for stock code, quantity to sell, price per share, after typing in the stock code, quantity to sell, and the price per share it will tell me I only have y amount of shares, y being the first round quantity input. I cannot figure how to update the maps values to equal all my input quantity, prices. Can someone help me?

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;
        }
    }
}
David Iles
  • 31
  • 2

2 Answers2

0

To get a particular stock Code, just do this.

Deque<Block> blocks = getStockCode("stockCode");
if (blocks == null) {
     System.out.println("StockCode `" + s + "` does not exist.");
} else {
     for (Block blk : blocks) {
         System.out.println(blk);
     }
}

Here is your method

private static Deque<Block> getStockCode(String s) {
         return maintainStocks.get(s);
}

In your Block class, add a toString function to list the info when you print the the block. You can change it as you see fit but here is one example.

@Override
public String toString() {
    return "[" + stockCode  ", " + quantity + ", " + price +"]";
}
WJS
  • 36,363
  • 4
  • 24
  • 39
0

You can retrieve the Queue from the map like this:

    for (String key : maintainStocks.keySet()) {
      System.out.printf("%s", key);
      Deque<Block> queue = maintainStocks.get(key);
      for (Block block : queue) {
        // operation
      }
    }
0xh3xa
  • 4,801
  • 2
  • 14
  • 28