日期:2014-05-20 浏览次数:20680 次
import java.util.*; public class 美味餐厅 { public static void main(String[] args) { BOSS boss = new BOSS(); boss.eat(); } } class 菜谱 { String name; double price; int number; int 点菜数; } class 餐厅 { 菜谱[] cp = new 菜谱[3]; public void initial() { 菜谱 蒸排骨 = new 菜谱(); 蒸排骨.name = "蒸排骨"; 蒸排骨.price = 15; 蒸排骨.number = 4; 蒸排骨.点菜数 = 0; 菜谱 白切鸡 = new 菜谱(); 白切鸡.name = "白切鸡"; 白切鸡.price = 15; 白切鸡.number = 10; 白切鸡.点菜数 = 0; 菜谱 齿牛肉 = new 菜谱(); 齿牛肉.name = "齿牛肉"; 齿牛肉.price = 20; 齿牛肉.number = 2; 齿牛肉.点菜数 = 0; cp[0] = 蒸排骨; cp[1] = 白切鸡; cp[2] = 齿牛肉; } } class BOSS { 餐厅 ct = new 餐厅(); String name = ""; public void eat() { ct.initial(); 功能选择(); } public void 功能选择() { 主界面(); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); switch (n) { case 1: System.out.print("请输入菜名:"); name = scn.next(); 点菜(name); 返回主界面(); break; case 2: 查看(); 返回主界面(); break; case 3: 买单(); 返回主界面(); break; case 4: System.out.print("欢迎下次光临!!"); break; } } public void 返回主界面() { System.out.print("输入0返回:"); Scanner scn = new Scanner(System.in); int n = scn.nextInt(); if (n == 0) { 功能选择(); } else { System.out.print("输入异常,请重新输入:"); 返回主界面(); } } public void 主界面() { System.out.println("*********欢迎来到美味餐厅**********"); System.out.println("1、点菜"); System.out.println("2、查看菜谱&数量"); System.out.println("3、买单"); System.out.println("4、退出"); System.out.println("***********************************"); System.out.print("请输入功能编号:"); } public void 点菜(String name) { for (int i = 0; i < ct.cp.length; i++) { if (ct.cp[i] != null) { if (name.equals(ct.cp[i].name)) { if (ct.cp[i].number > 0) { ct.cp[i].number--; ct.cp[i].点菜数++; System.out.println("点菜成功,蒸排骨还剩" + ct.cp[i].number + "份。"); return; } } } } System.out.print("点菜失败.."); } public void 查看() { System.out.println("菜名\t价格\t份量"); for (int i = 0; i < ct.cp.length; i++) { if (ct.cp[i] != null) { System.out.println(ct.cp[i].name + "\t" + ct.cp[i].price + "\t" + ct.cp[i].number); } } } public void 买单() { Scanner scn = new Scanner(System.in); double 菜价 = 0; System.out.println("您点的菜详细信息如下:"); System.out.println("菜名\t价格\t份量"); for (int i = 0; i < ct.cp.length; i++) { if (ct.cp[i].name != null) { if (ct.cp[i].点菜数 > 0) { 菜价 = ct.cp[i].price * ct.cp[i].点菜数+菜价; System.out.println(ct.cp[i].name + "\t" + ct.cp[i].price + "\t" + ct.cp[i].点菜数); continue; } } } System.out.println("共计:" + 菜价 + "元"); System.out.print("请输入付款金额:"); int 付款 = scn.nextInt(); double 找零 = 付款 - 菜价; System.out.println("\n应收:" + 菜价 + "元,实收" + 付款 + "元,找零:" + 找零 + "元"); } }