赞
踩
Java SE
使用选择结构,循环结构,数组的知识实现一个外卖订餐系统
public class Food { //菜单类:序号、菜名、单价 private int id; private String name; private double price; //无参构造方法 public Food() { } //有参构造方法 public Food(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } //重写toString方法 @Override public String toString() { return id+"\t"+name+"\t"+price+"元"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
public class Memu { //订单类:序号、订餐人、餐品信息、送餐日期、送餐地址、总金额、订单状态 private int id; private String name; private String foodMes; private int time; private String address; private double money; private String now; //无参构造方法 public Memu() { } //有参构造方法 public Memu(int id, String name, String foodMes, int time, String address, double money, String now) { this.id = id; this.name = name; this.foodMes = foodMes; this.time = time; this.address = address; this.money = money; this.now = now; } //重写toString方法 @Override public String toString() { return id+"\t"+name+"\t"+foodMes+"\t"+time+"点\t"+address+"\t"+money+"元\t"+now; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFoodMes() { return foodMes; } public void setFoodMes(String foodMes) { this.foodMes = foodMes; } public int getTime() { return time; } //增加时间输入的判断 public void setTime(int time) { if (time>=10&&time<=20){ this.time = time; }else{ System.out.println("输入错误,默认12点送餐"); this.time = 12; } } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public String getNow() { return now; } public void setNow(String now) { this.now = now; } }
import java.util.Scanner; public class OrderMange { //外卖订餐系统测试类 public static void main(String[] args) { //1.定义Food对象数组并赋值 Food[] foods=new Food[3]; foods[0]=new Food(1,"红烧茄子",24.0); foods[1]=new Food(2,"红烧排骨",36.0); foods[2]=new Food(3,"鱼香肉丝",32.0); //2.定义Scanner对象 Scanner sc = new Scanner(System.in); //3.定义菜单对象和订单序号 Memu memu1 = new Memu(); memu1.setId(1); //4.定义循环控制变量 boolean isFood=true; //5.定义返回0循环控制变量和num0 boolean is0=true; int num0=1; //6.定义点赞数和数组 int[] goodNum={0,0,0}; System.out.println("欢迎使用“外卖订餐系统”"); while (isFood){ System.out.println("*************************************"); System.out.println("1.我要订餐"); System.out.println("2.查看餐袋"); System.out.println("3.签收订单"); System.out.println("4.删除订单"); System.out.println("5.我要点赞"); System.out.println("6.退出系统"); System.out.println("*************************************"); System.out.print("请选择:"); int choseNum = sc.nextInt();//7.定义选择的数字 switch (choseNum){ case 1: System.out.println("***我要订餐***"); System.out.print("请输入订餐人姓名:"); memu1.setName(sc.next()); //8.菜单显示 System.out.println("序号\t"+"菜名\t"+"单价"); for (int i = 0; i < foods.length; i++) { System.out.println(foods[i].toString()); } System.out.print("请选您要点的菜品编号:"); int foodId = sc.nextInt(); //9.菜品编号判断1-3,否则返回 if (foodId>=1&&foodId<=3){ }else{ System.out.println("输入错误,菜品编号需要输入1-3的整数"); break; } System.out.print("请选择您需要的份数:"); int foodNumber = sc.nextInt(); if(foodId==1){ memu1.setFoodMes("红烧茄子\t"+foodNumber+"份"); }else if(foodId==2){ memu1.setFoodMes("红烧排骨\t"+foodNumber+"份"); }else if(foodId==3){ memu1.setFoodMes("鱼香肉丝\t"+foodNumber+"份"); } System.out.print("请输入送餐时间(送餐时间是10点至20点间整点送餐)"); memu1.setTime(sc.nextInt()); System.out.print("请输入送餐地址:"); memu1.setAddress(sc.next()); System.out.println("订餐成功!"); System.out.println("您订的是:"+memu1.getFoodMes()); System.out.println("送餐时间:"+memu1.getTime()+"点"); memu1.setMoney(foods[foodId-1].getPrice()*foodNumber+6.0); System.out.println("餐费:"+foods[foodId-1].getPrice()*foodNumber+"元,送餐费:6.0元,总计:"+memu1.getMoney()+"元."); memu1.setNow("已预订"); //10.输入0才返回判断输入的值为0才返回 while (is0){ System.out.print("输入0返回:"); num0 = sc.nextInt(); if (num0==0){ is0=false; } } break; case 2: System.out.println("***查看餐袋***"); System.out.println("序号\t"+"订餐人\t"+"餐品信息\t"+"送餐日期\t"+"送餐地址\t"+"总金额\t"+"订单状态"); System.out.println(memu1.toString()); //循环变量初始化 is0=true; //判断输入的值为0才返回 while (is0){ System.out.print("输入0返回:"); num0 = sc.nextInt(); if (num0==0){ is0=false; } } break; case 3: System.out.println("***签收订单***"); System.out.print("请选择要签收的订单序号:"); int num3 = sc.nextInt(); //和当前订单序号进行判断 if (num3==memu1.getId()){ System.out.println("订单签收成功!"); }else{ System.out.println("签收失败!没有这个订单!"); } //循环变量初始化,判断输入的值为0才返回 is0=true; while (is0){ System.out.print("输入0返回:"); num0 = sc.nextInt(); if (num0==0){ is0=false; } } break; case 4: System.out.println("***删除订单***"); System.out.print("请输入要删除的订单序号:"); int num4 = sc.nextInt(); //和当前订单序号进行判断 if (num4==memu1.getId()){ System.out.println("删除订单成功!"); }else{ System.out.println("删除订单失败!没有这个订单!"); } //循环变量初始化,判断输入的值为0才返回. is0=true; while (is0){ System.out.print("输入0返回:"); num0 = sc.nextInt(); if (num0==0){ is0=false; } } break; case 5: System.out.println("***我要点赞***"); //6.菜单显示 System.out.println("序号\t"+"菜名\t"+"单价"); for (int i = 0; i < foods.length; i++) { System.out.println(foods[i].toString()+"\t"+goodNum[i]+"赞"); } System.out.print("请输入要点赞的菜品序号:"); int num5 = sc.nextInt(); if (num5>=1&&num5<=3){ }else{ System.out.println("输入错误,菜品序号需要输入1-3的整数"); break; } for (int i = 0; i <foods.length ; i++) { if(num5==foods[i].getId()){ goodNum[i]++; } } //循环变量初始化,判断输入的值为0才返回 is0=true; while (is0){ System.out.print("输入0返回:"); num0 = sc.nextInt(); if (num0==0){ is0=false; } } break; case 6: System.out.println("谢谢使用,欢迎下次光临!"); isFood=false; break; default: break; } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。