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.*;
import java.util.Objects;
public class Stocks {
public static Map<String, Deque<Block>> maintainStocks = new LinkedHashMap<>();
public static void main (String[]args) {
var exit = false;
while (!exit) {
var BseChoice = getBseChoice("Would you like to (B)uy, (S)ell, or (E)xit? ");
var validate = getValidate(BseChoice);
}
}
public static String getValidate(String bseChoice) {
var isValid = false;
do {
if (bseChoice.equalsIgnoreCase("B")) {
System.out.println(bseChoice);
getBuyStocks();
isValid = true;
} else {
if (bseChoice.equalsIgnoreCase("S")) {
System.out.println(bseChoice);
getSellStocks(maintainStocks);
} else {
if (bseChoice.equalsIgnoreCase("E")) {
System.out.println("Thank you for your consideration. Solution exiting.");
isValid = true;
} else {
System.out.println("Invalid choice.");
System.out.print("Would you like to (B)uy, (S)ell stock or (E)xit? ");
var input = new Scanner(System.in);
bseChoice = input.nextLine();
}
}
}
} while (!isValid);
return null;
}
public static String getBseChoice (String prompt){
var input = new Scanner(System.in);
System.out.print(prompt);
return input.nextLine();
}
public static void getBuyStocks () {
System.out.print("Please enter the 3-letter stock code. > ");
var scanner = new Scanner(System.in);
var stockCode = scanner.next();
if (stockCode.length() != 3) {
System.out.println("Invalid stock code.");
getBuyStocks();
}
var quantity = getInteger("Please enter the quantity to purchase. > ");
if (quantity < 0) {
System.out.println("Invalid entry.");
getInteger("Please enter the quantity to purchase. >");
scanner.nextLine();
}
var price = getDouble("Please enter the purchase price per share. > ");
if (price < 0) {
System.out.println("Invalid entry.");
getDouble("Please enter the purchase price per share. > ");
scanner.nextLine();
}
var block = new Block(stockCode, quantity, price);
if (!maintainStocks.containsKey(stockCode)) {
maintainStocks.put(stockCode, new ArrayDeque<>());
}
maintainStocks.get(stockCode).add(block);
var isValid = false;
//getBseChoice("Would you like to (B)uy, (S)ell, or (E)xit? ");
}
private static int getInteger (String prompt){
System.out.print(prompt);
var scanner = new Scanner(System.in);
while (!scanner.hasNextInt()) {
System.out.println("Invalid integer!");
System.out.print(prompt);
scanner.nextLine();
}
return scanner.nextInt();
}
private static double getDouble (String prompt){
System.out.print(prompt);
var scanner = new Scanner(System.in);
while (!scanner.hasNextDouble()) {
System.out.println("Invalid Double!");
System.out.print(prompt);
scanner.nextLine();
}
return scanner.nextDouble();
}
private static void getSellStocks (Map<String, Deque<Block>> maintainStocks) {
//System.out.println(maintainStocks);
System.out.print("Please enter the 3-letter stock code. > ");
var scanner = new Scanner(System.in);
var stockCode = scanner.next();
var quantityToSell = getInteger("Please enter the quantity sold. > ");
var price = getDouble("Please enter the price per share. > ");
while (!maintainStocks.containsKey(stockCode)) {
System.out.print("Sorry, that stock code does not exist.");
scanner.nextLine();
System.out.print("Please enter the 3-letter stock code. > ");
}
if (maintainStocks.containsKey(stockCode)) {
//var sell = maintainStocks.get(stockCode).peek();
for (var stocks : maintainStocks.entrySet()) {
for (var value : stocks.getValue()) {
var valid = false;
while (!valid) {
if (quantityToSell > value.getQuantity()) {
System.out.printf("I'm sorry but you only have %d shares to sell.%n", value.getQuantity());
//getSellStocks();
}
valid = true;
}
var profitLoss = (quantityToSell * price) - (value.getQuantity() * value.getPrice());
System.out.printf("Your profit / loss is $%.2f.", profitLoss);
System.exit(0);
}
}
}
}
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;
}
}
}