import java.util.Arrays;标签:String,car,System,二维,数组,println,点餐,out From: https://www.cnblogs.com/19981206-zxb/p/16893530.html
import java.util.Scanner;
/**
* @Author:Zxb
* @Version:1.0
* @Date:2022/11/15-18:59
* @Since:jdk1.8
* @Description:
*/
public class Exercise1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//使用二维数组初始化菜单列表
String[][] menus = {
{"鱼香肉丝", "20", "微辣"},
{"红烧鱼", "30", "特辣"},
{"糖醋里脊", "25"},
{"酸辣土豆丝", "18"},
{"醋溜白菜", "15"},
{"红烧牛肉面", "16"}
};
//购物车
String[][] car = new String[0][];
boolean b = true;
while (b) {
System.out.println("*****************欢迎使用XXX点餐系统*******************");
System.out.println("\t\t1、显示所有菜单");
System.out.println("\t\t2、点餐(加入到购物车)");
System.out.println("\t\t3、显示购物车");
System.out.println("\t\t0、退出系统");
System.out.println("请选择:");
int choose = scanner.nextInt();
switch (choose) {
case 1:
System.out.println("点餐系统>>>显示菜单列表");
for (String[] menu : menus) {
System.out.println(Arrays.toString(menu));
}
break;
case 2:
System.out.println("点餐系统>>>点餐");
while(b){
System.out.println("请输入点餐名称:");
String inputName = scanner.next();
//根据名称查找一道菜
String[] buyMenu = null;
for (String[] menu : menus) {
if (inputName.equals(menu[0])) {
buyMenu = menu;
}
}
// int index = -1;
// for (int i = 0; i < menus.length; i++) {
// String [] menu = menus[i];
// if(inputName.equals(menu[0])){
// index = i;
// }
// }
//先判断
if (buyMenu == null) {
System.out.println("[点餐失败,该菜品是不存在!]");
} else {
//先扩容
car = Arrays.copyOf(car, car.length + 1);
//将点的菜加入到购物车、
car[car.length - 1] = buyMenu;
System.out.println("[点餐成功,已将<" + buyMenu[0] + ">菜加入购物车。价格为<" + buyMenu[1] + ">]");
}
System.out.println("是否继续点餐?y/n");
String input=scanner.next();
if("n".equals(input)||"N".equals(input)){
break;
}
}
break;
case 3:
System.out.println("点餐系统>>>显示购物车");
if (car.length == 0) {
System.out.println("[你还未添加任何商品!]");
} else {
int pay = 0;
for (String[] strings : car) {
System.out.println(Arrays.toString(strings));
//计算价格:将字符串转成int类型!
int price = Integer.parseInt(strings[1]);
pay += price;
}
System.out.println("需支付<" + pay + ">元!");
}
break;
case 0:
System.out.println("[谢谢使用,系统已退出!]");
b = false;
break;
}
}
}
}