因此,我正在制作一个Java程序,该程序接受客户购买的商品,并使用数组和哈希图来计算CSE作业的费用(也正在与此同时打印其他内容)。但是现在我希望用户能够先输入产品,然后输入数量,以便制作一个简单,用户友好的“收款机”程序。基本上只需要system.out的有关如何执行此操作的想法?
不确定使用多个scan / system.in代码行的循环是否可以工作,我不确定如何解决此问题。
哦,如果有人提出类似的要求,请告诉我!我找不到类似的东西,但我可能会误会。
import java.util.HashMap;
import java.util.ArrayList;
public class preLab3 {
// PreLab Q1: Compute the price of buying a specific quantity of a single item
public static double getPriceSingleItemType(HashMap<String, Double> priceList, String itemType, int quantity) {
double itemPrice = priceList.get(itemType);
return itemPrice * quantity;
}
// PreLab Q2: Compute the price of a single order
public static double getPrice(HashMap<String, Double> priceList, ArrayList<String> basket) {
double totalCost = 0.0;
for(String item: basket) {
double itemPrice = priceList.get(item);
totalCost = totalCost + itemPrice;
}
return totalCost;
}
public static HashMap<String, Double> getPriceList() {
HashMap<String, Double> priceList = new HashMap<String, Double>();
priceList.put("eggs", 1.79); // per dozen
priceList.put("butter", 3.19);
priceList.put("cheese", 5.99);
priceList.put("apple", 0.99); // per pound
priceList.put("banana", 0.39); // per pound
priceList.put("strawberries", 1.99);
priceList.put("peppers", 0.99);
priceList.put("tomato sauce", 0.5);
priceList.put("chocolate chips", 2.29);
return priceList;
}
public static void main(String[] args) {
System.out.println(getPriceList());
// PreLab Q3: I want to buy 3 lbs of strawberries
double price = getPriceSingleItemType(getPriceList(), "strawberries", 3);
System.out.println(price);
ArrayList<String> cart = new ArrayList<>();
cart.add("strawberries");
cart.add("strawberries");
cart.add("strawberries");
cart.add("chocolate chips");
cart.add("banana");
cart.add("banana");
cart.add("apple");
cart.add("eggs");
cart.add("butter");
double totalCost = getPrice(getPriceList(), cart);
System.out.println("The customer must pay: " + totalCost + " USD");
ArrayList<HashMap<String, Integer>> orders = new ArrayList<>();
for(HashMap<String, Integer> maps : orders) {
}
}
}
最佳答案
首先创建一个用于用户输入的Scanner
对象,然后使用您已经获得的示例代码,其中cart.add(item)
添加用户输入item
,并循环用户输入quantity
的每个项目。所以总共是这样的:
// ... same code above
ArrayList<String> cart = new ArrayList<>();
Scanner input = new Scanner(System.in);
String user_item = "";
int user_quantity = 0;
System.out.println("What items would you like to buy?");
// Loop to repeatedly get user input and add items to cart
// until user enters "q" for item to quit.
do {
System.out.print("Item? (q to quit) ");
user_item = input.nextLine();
if (user_item.equals("q")){
break;
}
System.out.print("How many " + user_item + "? ");
user_quantity = input.nextInt();
System.out.println(user_quantity + " " + user_item);
for(int q = 0; q <= user_quantity; q++){
cart.add(user_item);
}
} while(!input.nextLine().equals("q"));
input.close();
// Remove these
// cart.add("strawberries");
// cart.add("strawberries");
// cart.add("strawberries");
// cart.add("chocolate chips");
// cart.add("banana");
// cart.add("banana");
// cart.add("apple");
// cart.add("eggs");
// cart.add("butter");
double totalCost = getPrice(getPriceList(), cart);
// ... same code below
示例运行:
{banana=0.39, eggs=1.79, apple=0.99, butter=3.19, peppers=0.99, chocolate chips=2.29, tomato sauce=0.5, strawberries=1.99, cheese=5.99}
What items would you like to buy?
Item? (q to quit) butter
How many butter? 1
1 butter
Item? (q to quit) eggs
How many eggs? 2
2 eggs
Item? (q to quit) q
The customer must pay: 11.75 USD
当然,您可能还想为无效的用户输入添加错误/异常处理。
希望这可以帮助。