作业:使用类和对象更改吃货系统
package com.java_eat; /** * 订单类 * @author Administrator * */ public class Order { private String name;//订单者姓名 private String dishes;//菜名 private double amount;//金额 private int dayTime;//送餐时间 private String address;//送餐地址 private String state;//订单状态 public Order() {} public Order(String name, String dishes, double amount, int dayTime, String address, String state) { super(); this.name = name; this.dishes = dishes; this.amount = amount; this.dayTime = dayTime; this.address = address; this.state = state; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDishes() { return dishes; } public void setDishes(String dishes) { this.dishes = dishes; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public int getDayTime() { return dayTime; } public void setDayTime(int dayTime) throws Exception { if(dayTime>8&&dayTime<20) { this.dayTime = dayTime; }else { throw new Exception("送餐时间:8点到20点整点送餐"); } } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
package com.java_eat; /** * 菜品类 * @author Administrator * */ public class Menu { private String dishes;//菜名 private double price;//单价 private int evaluate;//评价 public Menu() {} public Menu(String dishes, double price, int evaluate) { this.dishes = dishes; this.price = price; this.evaluate = evaluate; } public String getDishes() { return dishes; } public void setDishes(String dishes) { this.dishes = dishes; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getEvaluate() { return evaluate; } public void setEvaluate(int evaluate) { this.evaluate = evaluate; } }
package com.java_eat; import java.util.ArrayList; import java.util.Scanner; public class EatTest { public static void main(String[] args) { Scanner cxj = new Scanner(System.in); Menu m1 = new Menu("红烧鱼",28,2); Menu m2 = new Menu("客家豆腐",16,2); Menu m3 = new Menu("蛋蒸肉",18,0); Menu m4 = new Menu("青菜",12,4); ArrayList al = new ArrayList();//记录菜单 al.add(m1); al.add(m2); al.add(m3); al.add(m4); ArrayList alr = new ArrayList();//记录订单 Order o1 = new Order("李一","红烧鱼",28,12,"厦门思明区","已预定"); Order o2 = new Order("李二","客家豆腐",16,10,"厦门尖尾部","已完成"); Order or = new Order();//添加新订单 alr.add(o1); alr.add(o2); boolean ver = false; do { System.out.println("**欢迎使用订餐系统**"); 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 a = cxj.nextInt(); switch(a) { case 1: if(alr.size()>=4) { System.out.println("报歉,订单已满!无法订餐!"); System.out.println("按0返回:"); int k1 = cxj.nextInt(); if(k1==0) { ver = true; break; } } System.out.print("编号\t"+"菜名\t"+"单价\t"+"评价\n"); for(int i = 0;i<al.size();i++) { Menu me = (Menu)al.get(i); System.out.println((i+1)+"\t"+me.getDishes()+"\t"+me.getPrice()+"\t"+me.getEvaluate()); } System.out.print("请选择:"); int t = cxj.nextInt(); Menu mu = (Menu)al.get(t-1); or.setDishes(mu.getDishes()); System.out.print("请输入订餐者姓名:"); or.setName(cxj.next()); System.out.print("请问您需要几份?"); int c = cxj.nextInt(); or.setAmount(mu.getPrice()*c); System.out.print("请选择送餐时间(8点到20点整点送餐):"); try { or.setDayTime(cxj.nextInt()); } catch (Exception e) { System.err.println("输入的送餐时间有误!"); } System.out.print("请输入送餐地址:"); or.setAddress(cxj.next()); System.out.println("1、确定\t2、取消"); int temp = cxj.nextInt(); if(temp ==1) { or.setState("已预定"); System.out.println("马上为您配送!"); } alr.add(or); System.out.println("按0返回:"); int k1 = cxj.nextInt(); if(k1==0) { ver = true; break; } case 2: System.out.println("******当前餐袋*******"); System.out.println("编号\t姓名\t餐品信息\t送餐日期\t送餐地址\t\t总金额\t订单状态"); for(int i = 0;i<alr.size();i++) { Order order = (Order)alr.get(i); System.out.println((i+1)+"\t"+order.getName()+"\t"+order.getDishes()+"\t"+order.getDayTime()+"\t"+order.getAddress()+"\t"+order.getAmount()+"\t"+order.getState()); } System.out.println("按0返回:"); int k2 = cxj.nextInt(); if(k2==0) { ver = true; break; } case 3: boolean b1 = false; System.out.println("******当前订单*******"); System.out.println("编号\t姓名\t餐品信息\t送餐日期\t送餐地址\t\t总金额\t订单状态"); for(int i = 0;i<alr.size();i++) { Order order = (Order)alr.get(i); System.out.println((i+1)+"\t"+order.getName()+"\t"+order.getDishes()+"\t"+order.getDayTime()+"\t"+order.getAddress()+"\t"+order.getAmount()+"\t"+order.getState()); } if(alr.size()<1) { System.out.println("当前无订单,无需签收!"); }else { for(int i = 0;i<alr.size();i++) { Order order = (Order)alr.get(i); if(order.getState().equals("已预定")){ b1 = true; break; } } } if(b1) { System.out.print("请输入您要签收的订单编号:"); int r = cxj.nextInt(); Order order = (Order)alr.get(r-1); order.setState("已完成"); System.out.println("已签收!"); }else { System.out.println("当前无需要签收的订单!"); } System.out.println("按0返回:"); int k3 = cxj.nextInt(); if(k3==0) { ver = true; break; } case 4: boolean b2 = false; System.out.println("******当前订单*******"); System.out.println("编号\t姓名\t餐品信息\t送餐日期\t送餐地址\t\t总金额\t订单状态"); for(int i = 0;i<alr.size();i++) { Order order = (Order)alr.get(i); System.out.println((i+1)+"\t"+order.getName()+"\t"+order.getDishes()+"\t"+order.getDayTime()+"\t"+order.getAddress()+"\t"+order.getAmount()+"\t"+order.getState()); } if(alr.size()<1) { System.out.println("当前无订单,无需签收!"); }else { System.out.println("请输入要删除的订单编号:"); int a1 = cxj.nextInt(); Order order = (Order)alr.get(a1-1); if(order.getState().equals("已预定")) { System.out.println("此订单未签收,无法删除!"); }else { alr.remove(a1-1); System.out.println("已删除!"); } } System.out.println("按0返回:"); int k4 = cxj.nextInt(); if(k4==0) { ver = true; break; } case 5: System.out.println("******我要点赞*******"); System.out.print("编号\t"+"菜名\t"+"单价\t"+"评价\n"); for(int i = 0;i<al.size();i++) { Menu me = (Menu)al.get(i); System.out.println((i+1)+"\t"+me.getDishes()+"\t"+me.getPrice()+"\t"+me.getEvaluate()); } System.out.println("请输入您要点赞的菜品:"); int i = cxj.nextInt(); Menu me = (Menu)al.get(i-1); me.setEvaluate(me.getEvaluate()+1); System.out.println("点赞成功!"); System.out.println("按0返回:"); int k5 = cxj.nextInt(); if(k5==0) { ver = true; break; } case 6: System.out.println("谢谢光临,下次再见!"); ver = false; break; } }while(ver); } }
测试结果示例: