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;
}
}
}