java - 如何使用从 Hashmap 上的 Arraylist 中选择的值?
问题描述
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
TheMenu();
}
public static List<String> initPizzas() {
ArrayList<String> thePizza = new ArrayList<String>();
thePizza.add("Cheese");
thePizza.add("Mixed");
thePizza.add("Mushroom");
thePizza.add("Meat");
return thePizza;
}
public static void viewAllPizza(List<String> pizzas,String CustomerRef[]) {
ListIterator<String> it = pizzas.listIterator(); //to enumerate the pizzas in the menu
while(it.hasNext()) {
System.out.println(it.nextIndex() + " " + it.next());
}
}
public static void addPizza(List<String> pizzas, String CustomerRef[]) {
Scanner input = new Scanner(System.in);
String Exit = "E";
while(true) {
System.out.println("Enter the pizza you would like to add and E to exit");
String choice = input.next();
if(choice.equals(Exit)) {
break;
}else {
pizzas.add(choice);
; }
}
}
public static void TheMenu() {
String Customer[] = new String[10];
Scanner input = new Scanner(System.in);
List<String> Pizzas = initPizzas();
String option;
do {
System.out.println("\nMenu");
System.out.println("V: Views all Pizza");
System.out.println("A: To add a Pizza to the list");
System.out.println("O: To order Pizza");
System.out.println("Q: To exit");
option = input.next();
if (option.charAt(0) == 'V' )
{
viewAllPizza(Pizzas,Customer);
}
if (option.charAt(0) == 'A' )
{
addPizza(Pizzas,Customer);
}
if (option.charAt(0) == 'O') {
orderPizza(Pizzas, Customer);
}
}while (option.charAt(0) != 'Q');
}
public static void orderPizza(List<String> viewAllPizza,String CustomerRef[]) {
Map<String, Integer> counts = new HashMap<String, Integer>();
for(String str: viewAllPizza()) {
if(counts.containsKey(str)) {
counts.put(str, counts.get(str) + 1);
}else {
counts.put(str, 1);
}
}
for(Map.Entry<String, Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
如果我犯了太多错误,我深表歉意,但我对 java 没有太多经验。我一直在尝试使用 Hashmap 来订购我从 Arraylist 中选择的披萨,并使用 Scanner 保留我订购的数量,但我无法弄清楚。
我期望的输出:
假设我想订购 4 块肉和 1 块奶酪比萨(我想使用扫描仪输入这些值)
-
Orders
-
Meat 4
Cheese 1
但是当我使用 orderPizza 方法时,它会获取 Arraylist 中的所有比萨饼并添加 +1(有问题的部分)
-
Orders
-
Cheese 1
Mixed 1
Mushroom 1
Meat 1
解决方案
Implement a new method addPizzaForOrder
to add a pizza to the list of pizzas you would like to order. You need to update counts
map inside that method.
public static void addPizzaForOrder(String CustomerRef[],
Map<String, Integer> counts) {
Scanner input = new Scanner(System.in);
String Exit = "E";
while(true) {
System.out.println("Enter the pizza you would like to order and E to exit");
String choice = input.next();
if(choice.equals(Exit)) {
break;
}else {
if(counts.containsKey(choice)) {
counts.put(choice, counts.get(choice) + 1);
} else {
counts.put(choice, 1);
}
}
}
}
public static void orderPizza(Map<String, Integer> counts,String CustomerRef[]) {
Map<String, Integer> counts = new HashMap<String, Integer>();
addPizzaForOrder(Customer, counts);
for(Map.Entry<String, Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
推荐阅读
- java - 如何将 UTC 转换为 SGT
- java - 添加 ImageView 会导致 android 应用程序小部件崩溃
- ajax - 如何使用 Spring Boot 更新页面上的 div?
- actions-on-google - 模拟器中忽略第二个简单响应的 displayText
- angular - 如何在 Ag-grid 中获取更新的行
- c# - “object”不包含“Skip”的定义,并且没有扩展方法“Skip”接受“object”类型的第一个参数 - 在 Razor 中
- php - 我想在 laravel 中使用用户名或电子邮件更新登录
- java - 无法解析包含 java 中的 html 字符实体的 xml
- java - Gradle:maven 存储库上的条件基础发布包
- javascript - Javascript 代码块作为函数的参数