从事学校项目。到目前为止,该项目要求购买、出售或退出。在购买选择时,它会询问股票代码、购买数量和每股价格。然后它再次要求买入、卖出或退出。再次选择购买时,它将再次询问股票代码、数量、每股价格。并再次要求但、出售或退出。在选择出售我所有购买的股票(第一轮输入+第二轮输入)时,它会询问股票代码、要出售的数量、每股价格,在输入股票代码、要出售的数量和每股价格后它会告诉我只有y股,y是第一轮数量输入。我不知道如何更新地图值以等于我所有的输入数量和价格。有人能帮我吗?
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;
}
}
}