赞
踩
同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++或Java或Python结合设计模式(2种以上)至少实现系统的以下功能:
1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动(自行设计或参考官网信息)。
5.模拟打印小票的功能(写到文件中)。
本题采用工厂方法模式和抽象工厂模式:
工厂方法模式:
核心工厂类不再负责所有产品的创建,而是将具体创建的工作交给子类去做,成为一个抽象工厂角色,仅负责给出具体工厂类必须实现的接口,而不接触哪一个产品类应当被实例化这种细节。
抽象工厂模式:
客户类和工厂类分开。消费者任何时候需要某种产品,只需向工厂请求即可。消费者无须修改就可以接纳新产品。缺点是当产品修改时,工厂类也要做相应的修改。如:如何创建及如何向客户端提供。
抽象食物——AbstractFood
食物接口——Food
抽象基类——汉堡类,鸡肉类,甜点类,饮品类和套餐
具体食物类
抽象工厂——生产汉堡类,鸡肉类,甜点类,饮品类和套餐
具体工厂——生产具体食物
客户类——调用抽象工厂
测试类——建立客户点餐
public abstract class AbstractFood { protected String kind; //数量 protected int num; //价格 protected float price; //合计 public float totalPrice(){ return this.num * this.price; }
public abstract class Chicken extends
AbstractFood implements Food{
public void printMessage(){
System.out.println("--"+this.kind+" 单价:"+this.price+
" 数量:"+this.num+" 合计:"+this.totalPrice());
}
}
public class ChickenWings extends Chicken{
public ChickenWings(int num) {
this.kind = "奥尔良烤翅";
this.price = 6f;
this.num = num;
}
}
public abstract class Drinks extends AbstractFood implements Food{
public void printMessage(){
System.out.println("--"+this.kind+"
单价:"+this.price+
"
数量:"+this.num+" 合计:"+this.totalPrice());
}
}
public class Coffee extends Drinks{
public Coffee(int num) {
this.kind = "咖啡";
this.price = 12f;
this.num = num;
}
public abstract class Dessert extends AbstractFood implements Food{
public void printMessage(){
System.out.println("--"+this.kind+"
单价:"+this.price+
"
数量:"+this.num+" 合计:"+this.totalPrice());
}
}
public class Customer { //抽象工厂 private Factory factory; //构造方法将抽象工厂作为参数传入 public Customer(Factory factory){ this.factory = factory; } /** * 点餐 */ public float orderZingerBurger(int num){ //获得香辣鸡腿堡 Hamburger zingerBurger = factory.createZingerBurger(num); //输出订购信息 zingerBurger.printMessage(); //返回总价 return zingerBurger.totalPrice(); } public float orderBeefBurger(int num){ //获得超级牛肉堡 Hamburger beefBurger = factory.createBeefBurger(num); //输出订购信息 beefBurger.printMessage(); //返回总价 return beefBurger.totalPrice(); } public float orderOrleansBurger(int num){ //获得奥尔良烤鸡腿堡 Hamburger orleansBurger = factory.createOrleansBurger(num); //输出订购信息 orleansBurger.printMessage(); //返回总价 return orleansBurger.totalPrice(); } public float orderShrimpBurger(int num){ //获得全虾堡 Hamburger shrimpBurger = factory.createShrimpBurger(num); //输出订购信息 shrimpBurger.printMessage(); //返回总价 return shrimpBurger.totalPrice(); } public float orderChickenWings(int num){ //获得奥尔良烤翅 Chicken chickenWings = factory.createChickenWings(num); //输出订购信息 chickenWings.printMessage(); //返回总价 return chickenWings.totalPrice(); } public float orderDrumStick(int num){ //获得 烤鸡腿 Chicken drumStick = factory.createDrumStick(num); //输出订购信息 drumStick.printMessage(); //返回总价 return drumStick.totalPrice(); } public float orderPopcornChicken(int num){ //获得鸡米花 Chicken popcornChicken = factory.createPopcornChicken(num); //输出订购信息 popcornChicken.printMessage(); //返回总价 return popcornChicken.totalPrice(); } public float orderChips(int num){ //获得薯条 Dessert chips = factory.createChips(num); //输出订购信息 chips.printMessage(); //返回总价 return chips.totalPrice(); } public float orderEggTart(int num){ //获得蛋挞 Dessert eggTart = factory.createEggTart(num); //输出订购信息 eggTart.printMessage(); //返回总价 return eggTart.totalPrice(); } public float orderSundae(int num){ //获得圣代 Dessert sundae = factory.createSundae(num); //输出订购信息 sundae.printMessage(); //返回总价 return sundae.totalPrice(); } public float orderMilky(int num){ //获得奶茶 Drinks milky = factory.createMilky(num); //输出订购信息 milky.printMessage(); //返回总价 return milky.totalPrice(); } public float orderKola(int num){ //获得可乐 Drinks kola = factory.createKola(num); //输出订购信息 kola.printMessage(); //返回总价 return kola.totalPrice(); } public float orderCoffee(int num){ //获得咖啡 Drinks coffee = factory.createCoffee(num); //输出订购信息 coffee.printMessage(); //返回总价 return coffee.totalPrice(); } public float orderOneCombo(int num) { //单人套餐 Combo oneCombo = factory.createOneCombo(num); //输出订购信息 oneCombo.printMessage(); //返回总价 return oneCombo.totalPrice(); } public float orderTwoCombo(int num) { //双人套餐 Combo twoCombo = factory.createTwoCombo(num); //输出订购信息 twoCombo.printMessage(); //返回总价 return twoCombo.totalPrice(); } public float orderThreeCombo(int num) { //三人套餐 Combo threeCombo = factory.createThreeCombo(num); //输出订购信息 threeCombo.printMessage(); //返回总价 return threeCombo.totalPrice(); } }
import java.util.Scanner; public class Test { public static void main(String[] args) { /** * 定义一个肯德基(factory类型) */ Factory factory = new KFCFactory(); /** * 创建客户 */ Customer customer = new Customer(factory); /** * 客户点餐 */ Scanner input =new Scanner(System.in); float ZingerBurger = 0;float OrleansBurger= 0; float BeefBurger= 0;float ShrimpBurger = 0; float ChickenWings= 0;float DrumStick = 0; float PopcornChicken = 0; float Chips= 0; float EggTart= 0;float Sundae = 0; float Kola= 0; float Milky= 0; float Coffee = 0;float OneCombo= 0; float TwoCombo= 0;float ThreeCombo = 0; System.out.println("欢迎进入KFC点餐系统!\n" +"******************菜单******************\n" +"汉堡类:\n" + "1.香辣鸡腿堡--15.0元 2.奥尔良烤鸡腿堡--17.0元 \n" + "3.超级牛肉堡--17.0元 4.全虾堡--17.0元\n" +"鸡肉类:\n" + "5.奥尔良烤翅--6.0元 6.烤鸡腿--12.0元\n" + "7.鸡米花--11.0元\n" + "甜点类:\n" + "8.薯条--8元 9.蛋挞--4元\n" + "10.圣代--6元\n" +"饮品类:\n" + "11.可乐--6元 12.奶茶--6元\n" + "13.咖啡--12元\n" +"套餐类:\n" +"14.单人套餐--33元\n" + " 1个香辣鸡腿堡+1个烤鸡腿+1包薯条+1杯可乐/奶茶(任点)\n" +"15.双人套餐:--54元\n" + " 1个香辣鸡腿堡+1个奥尔良烤鸡腿堡/超级牛肉堡/全虾堡(任点)\n" + " 1对奥尔良烤翅+2个蛋挞+2杯可乐/奶茶(任点)\n" +"16.三人套餐--109元\n" + " 3个奥尔良烤鸡腿堡/超级牛肉堡/全虾堡(任点)\n" + " 1对奥尔良烤翅+1个烤鸡腿+1包鸡米花\n" + " 1包薯条+2杯圣代+2杯可乐/奶茶(任点)+1杯咖啡\n" +"*********************************************\n"); while(true){ System.out.println("请选择序号点餐"); int a=input.nextInt(); System.out.println("请选择数量"); int b=input.nextInt(); switch(a){ case 1: //香辣鸡腿堡 ZingerBurger = customer.orderZingerBurger(b); break; case 2: //奥尔良烤鸡腿堡 OrleansBurger = customer.orderOrleansBurger(b); break; case 3: //超级牛肉堡 BeefBurger = customer.orderBeefBurger(b); break; case 4: //全虾堡 ShrimpBurger = customer.orderShrimpBurger(b); break; case 5: //奥尔良烤鸡翅 ChickenWings = customer.orderChickenWings(b); break; case 6: //烤鸡腿 DrumStick = customer.orderDrumStick(b); break; case 7: //鸡米花 PopcornChicken = customer.orderPopcornChicken(b); break; case 8: //薯条 Chips = customer.orderChips(b); break; case 9: //蛋挞 EggTart = customer.orderEggTart(b); break; case 10: //圣代 Sundae = customer.orderSundae(b); break; case 11: //可乐 Kola = customer.orderKola(b); break; case 12: //奶茶 Milky = customer.orderMilky(b); break; case 13: //咖啡 Coffee = customer.orderCoffee(b); break; case 14: //单人套餐 OneCombo = customer.orderOneCombo(b); break; case 15: //双人套餐 TwoCombo = customer.orderTwoCombo(b); break; case 16: //三人套餐 ThreeCombo = customer.orderThreeCombo(b); break; } System.out.println("请选择是否继续点餐,1.是 2.否"); int c=input.nextInt(); if(c!=1){ break; } } float money1=ZingerBurger+OrleansBurger+BeefBurger+ShrimpBurger; float money2=+ChickenWings+DrumStick+PopcornChicken; float money3=+Chips+EggTart+Sundae; float money4=Kola+Milky+Coffee; float money5=OneCombo+TwoCombo+ThreeCombo; float money=money1+money2+money3+money4+money5; System.out.println("此次点餐共计:"+money); System.out.println("请问您是否具有优惠券? 1.是 2.否"); int c=input.nextInt(); int d=0; if(c==1){ System.out.println("请输入优惠券金额"); d=input.nextInt(); } System.out.println("请输入实付金额"); int e=input.nextInt(); float changes=e-money-d; System.out.println("找零:"+changes); System.out.println("欢迎下次点餐,祝您用餐愉快!"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。