本文主要是介绍同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++/python/Java,结合设计模式(2种以上)至少实现系统的以下功能: 1.正常餐品结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.基本要求
同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++/python/Java,结合设计模式(2种以上)至少实现系统的以下功能:
1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动(自行设计或参考官网信息)。
5.模拟打印小票的功能(写到文件中)。
**
2.设计模式
2.1工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
2.2抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
**
3.程序实现
3.1食物接口IFood
package com.kfc.factory.model;
/** 抽象食物接口*/
public interface IFood {/** 打印食物信息*/void printMessage();
}
3.2 抽象事物的建立
(1)食物基类
package com.kfc.factory.model;
/** 食物基类*/
public abstract class AbstractBaseFood {protected String kind;//类别protected int num;//数量protected float price;//价格public float totalPrice(){return this.num * this.price;}
}
(2)饮料基类
package com.kfc.factory.model;
/** 饮料基类*/
public abstract class Beverage extends AbstractBaseFood implements IFood {public void printMessage() {System.out.println("--" + this.kind + "饮料:\t" + this.price + "\t" + this.num + "\t" + this.totalPrice());}
}
(3)汉堡基类
package com.kfc.factory.model;
/** 汉堡基类*/
public abstract class Hamburg extends AbstractBaseFood implements IFood {public void printMessage() {System.out.println("--"+this.kind + "风味汉堡: \t" + this.price + " \t" + this.num + " \t" + this.totalPrice());}
}
(4)鸡翅基类
package com.kfc.factory.model;
/** 鸡翅基类*/
public abstract class ChickenWings extends AbstractBaseFood implements IFood {public void printMessage() {System.out.println("--" + this.kind + "风味鸡翅:\t" + this.price + "\t" + this.num + "\t" + this.totalPrice());}
}
(5)薯条基类
package com.kfc.factory.model;
/** 薯条基类*/
public abstract class FrenchFries extends AbstractBaseFood implements IFood {public void printMessage() {System.out.println("--" + this.kind + "风味薯条:\t" + this.price + "\t" + this.num + "\t" + this.totalPrice());}
}
(6)套餐1基类
package com.kfc.factory.model;public class Num1 extends AbstractBaseFood implements IFood {public void printMessage() {System.out.println("---"+this.kind + "套餐: " + " "+this.price + " " + this.num + " " + this.totalPrice());}}
(7)套餐2基类
package com.kfc.factory.model;public class Num2 extends AbstractBaseFood implements IFood{public void printMessage() {System.out.println("---"+this.kind + "套餐: " + ""+this.price + " " + this.num + " " + this.totalPrice());}
}
(8)套餐3基类
package com.kfc.factory.model;/** 套餐3*/
public class Num3 extends AbstractBaseFood implements IFood{public void printMessage() {System.out.println("---"+this.kind + "套餐: " + ""+this.price + " " + this.num + " " + this.totalPrice());}
}
3.3具体食物的建立
(9)饮料实现类
package com.kfc.factory.model;
import com.kfc.factory.model.Beverage;
/** 饮料实现类*/
public class KeleBeverage extends Beverage {public KeleBeverage(int num) {this.kind = "可乐";this.price = 7.0f;this.num = num;}
}
(10)汉堡实现类
package com.kfc.factory.model;
import com.kfc.factory.model.Hamburg;
/** 麻辣鸡腿汉堡*/
public class ChinaHanburm extends Hamburg{/** 构造方法*/public ChinaHanburm(int num) {this.kind = "麻辣";this.price = 14.0f;this.num = num;}
}
(11)薯条实现类
package com.kfc.factory.model;
import com.kfc.factory.model.FrenchFries;
/** 薯条实现类*/
public class ChinaFrenchFries extends FrenchFries {public ChinaFrenchFries(int num) {this.kind = "番茄";this.price = 8.0f;this.num = num;}
}
(12)鸡翅实现类
package com.kfc.factory.model;
import com.kfc.factory.model.ChickenWings;
public class ChinaChickenWings extends ChickenWings{/** 鸡翅实现类*/public ChinaChickenWings(int num) {this.kind = "奥尔良";this.price = 2.5f;this.num = num;}
}
(13)套餐1实现类
package com.kfc.factory.model;
import com.kfc.factory.model.Num1;
/** 套餐1*/
public class Num1Kind extends Num1{public Num1Kind(int num) {this.kind = "全家桶";this.price = 62.0f;this.num = num;}
}
(14)套餐2实现类
package com.kfc.factory.model;
import com.kfc.factory.model.Num2;
/** 套餐2*/
public class Num2Kind extends Num2{public Num2Kind(int num) {this.kind = "原味鸡桶";this.price = 88.0f;this.num = num;}
}
(15)套餐3实现类
package com.kfc.factory.model;
import com.kfc.factory.model.Num3;
/** 套餐3的类型实现*/
public class Num3Kind extends Num3{/** 构造函数*/public Num3Kind(int num) {this.kind = "翅桶辣堡";this.price = 99.0f;this.num = num;}
}
3.4 建立工厂
(16)创建抽象肯德基工厂——IKfcFactory 生产抽象食物
package com.kfc.factory.itf;
import com.kfc.factory.model.Beverage;
import com.kfc.factory.model.ChickenWings;
import com.kfc.factory.model.FrenchFries;
import com.kfc.factory.model.Hamburg;
import com.kfc.factory.model.Num1;
import com.kfc.factory.model.Num2;
import com.kfc.factory.model.Num3;
/** 肯德基抽象工厂基类*/
public interface IKfcFactory {public Hamburg createHamburg(int num);// 生产汉堡public FrenchFries createFrenchFries(int num);// 生产薯条public ChickenWings createChickenWings(int num);// 生产鸡翅public Beverage createBeverage(int num); // 生产饮料public Num1 createNum1(int num);//生产套餐1public Num2 createNum2(int num);//生产套餐2public Num3 createNum3(int num);//生产套餐3}
(17)创建具体肯德基工厂——ChinaKfcFactory 生产具体食物
package com.kfc.factory.itf;
import com.kfc.factory.itf.IKfcFactory;
import com.kfc.factory.model.Beverage;
import com.kfc.factory.model.ChickenWings;
import com.kfc.factory.model.FrenchFries;
import com.kfc.factory.model.Hamburg;
import com.kfc.factory.model.KeleBeverage;
import com.kfc.factory.model.Num1;
import com.kfc.factory.model.Num1Kind;
import com.kfc.factory.model.Num2;
import com.kfc.factory.model.Num2Kind;
import com.kfc.factory.model.Num3;
import com.kfc.factory.model.Num3Kind;
import com.kfc.factory.model.ChinaChickenWings;
import com.kfc.factory.model.ChinaFrenchFries;
import com.kfc.factory.model.ChinaHanburm;public class ChinaKfcFactory implements IKfcFactory{// 生产可乐public Beverage createBeverage(int num){return new KeleBeverage(num);}// 生产奥尔良烤鸡翅public ChickenWings createChickenWings(int num){return new ChinaChickenWings(num);}// 生产薯条public FrenchFries createFrenchFries(int num){return new ChinaFrenchFries(num);}// 生产麻辣风味鸡腿汉堡public Hamburg createHamburg(int num){return new ChinaHanburm(num);}//生产套餐1public Num1 createNum1(int num) {return new Num1Kind(num);}public Num2 createNum2(int num) {return new Num2Kind(num);}@Overridepublic Num3 createNum3(int num) {return new Num3Kind(num);}}
3.5 创建客户类
(18)创建客户类——Customer
package com.kfc.factory.custom;
import com.kfc.factory.itf.IKfcFactory;
import com.kfc.factory.model.Beverage;
import com.kfc.factory.model.ChickenWings;
import com.kfc.factory.model.FrenchFries;
import com.kfc.factory.model.Hamburg;
import com.kfc.factory.model.Num1;
import com.kfc.factory.model.Num2;
import com.kfc.factory.model.Num3;/** 客户类*/public class Customer{// 抽象工厂private IKfcFactory kfcFactory;// 构造方法将抽象工厂作为参数传入public Customer(IKfcFactory kfcFactory){this.kfcFactory = kfcFactory;}/*** 订购食物*/// 订购麻辣鸡腿汉堡public float orderHamburg(int num){// 获得麻辣鸡腿汉堡Hamburg hamburg = kfcFactory.createHamburg(num);// 输出订购信息hamburg.printMessage();// 返回总价return hamburg.totalPrice();}// 订购奥尔良烤鸡翅public float orderChickenWings(int num){// 获得奥尔良烤鸡翅ChickenWings chickenWings = kfcFactory.createChickenWings(num);// 输出订购信息chickenWings.printMessage();// 返回总价return chickenWings.totalPrice();}// 订购薯条public float orderFrenchFries(int num){// 获得薯条FrenchFries frenchFries = kfcFactory.createFrenchFries(num);// 输出订购信息frenchFries.printMessage();// 返回总价return frenchFries.totalPrice();}// 订购可乐public float orderBeverage(int num){// 获得可乐Beverage beverage = kfcFactory.createBeverage(num);// 输出订购信息beverage.printMessage();// 返回总价return beverage.totalPrice();}/** 订购套餐1*/public float orderNum1(int num) {Num1 num1 = kfcFactory.createNum1(num);num1.printMessage();return num1.totalPrice();}/** 订购套餐2*/public float orderNum2(int num) {Num2 num2 = kfcFactory.createNum2(num);num2.printMessage();return num2.totalPrice();}/** 订购套餐3*/public float orderNum3(int num) {Num3 num3 = kfcFactory.createNum3(num);num3.printMessage();return num3.totalPrice();}}
3.6主函数界面的实现
/*
* author:软工1604 徐於敏 16408070731
*/
package com.kfc.factory;import com.kfc.factory.custom.Customer;
import com.kfc.factory.itf.ChinaKfcFactory;
import com.kfc.factory.itf.IKfcFactory;
import java.util.Date;
import java.util.Scanner;
import java.io.IOException;
import java.text.SimpleDateFormat;
public class OrderApp {@SuppressWarnings("resource")public static void main(String[] args) throws IOException {int a=0,b=0,c=0,d=0,e=0,f=0,g=0,n=0;//定义变量,控制食品数量int flag = 1;/** 定义一个肯德基(IKfcFactory类型)*/IKfcFactory kfcFactory = new ChinaKfcFactory();Customer customer = new Customer(kfcFactory);//开始点餐/** 点餐过程实现*/SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式while(flag !=0) {Scanner sc = new Scanner(System.in);System.out.println("欢迎进入肯德基点餐系统!");System.out.println("1.单点"+"2.套餐");int m = sc.nextInt();switch(m) {case 1:System.out.println("有如下食品:1.麻辣风味汉堡 2.奥尔良风味鸡翅 3.番茄风味薯条 4.可乐");System.out.println("麻辣风味汉堡个数:");a = sc.nextInt();System.out.println("奥尔良风味鸡翅:");b = sc.nextInt();System.out.println("番茄风味薯条:");c = sc.nextInt();System.out.println("可乐:");d = sc.nextInt();System.out.println("付款金额:");n = sc.nextInt();System.out.println("********************************************************************");System.out.println(" 欢迎光临肯德基 ");System.out.println("订餐时间:"+df.format(new Date()));System.out.println("\t名称"+"\t单价"+"\t数量"+"\t合计");float hamhurgMoney = customer.orderHamburg(a);// 麻辣鸡腿汉堡float chickenWingsMoney = customer.orderChickenWings(b); // 奥尔良烤鸡翅float frenchFriesMoney = customer.orderFrenchFries(c);// 薯条float beverageMoney = customer.orderBeverage(d);// 可乐System.out.println("总计:" + (hamhurgMoney + chickenWingsMoney + frenchFriesMoney + beverageMoney));System.out.println("收款:"+n+" "+"找零:"+(n - hamhurgMoney - chickenWingsMoney - frenchFriesMoney - beverageMoney));System.out.println("当前收银员:徐於敏");System.out.println("欢迎下次光临!");System.out.println("********************************************************************");break;case 2:System.out.println("有如下套餐:1.全家桶套餐 2.万圣节辣堡桶 3.翅桶辣堡套餐 ");System.out.println("1.全家桶套餐的个数:");e = sc.nextInt();System.out.println("万圣节日辣堡桶个数:");f = sc.nextInt();System.out.println("翅桶辣堡套餐:");g = sc.nextInt();System.out.println("付款金额:");n = sc.nextInt();System.out.println("********************************************************************");System.out.println(" 欢迎光临肯德基 ");System.out.println("订餐时间:"+df.format(new Date()));System.out.println("\t名称"+" "+"\t单价"+"\t数量"+"\t合计");float num1Money = customer.orderNum1(e);float num2Money = customer.orderNum2(f);float num3Money = customer.orderNum3(g);System.out.println("总计:" + (num1Money+num2Money+num3Money));System.out.println("收款:"+n+" "+"找零:"+(n - num1Money - num2Money - num3Money));System.out.println("当前收银员:徐於敏");System.out.println("欢迎下次光临!");System.out.println("********************************************************************");break;}System.out.println("继续点餐? 1——yes 0——no:");flag = sc.nextInt(); }/* 将小票信息输入到文件中File fp = new File("./1.txt");PrintWriter pw =new PrintWriter(fp);pw.write("********************************************************************");pw.println();pw.write(" 欢迎光临肯德基 ");pw.println();pw.write("订餐时间:"+df.format(new Date()));pw.println();pw.write("\t名称"+"\t单价"+"\t数量"+"\t合计");pw.println();pw.write("总计:" + (ham + chicken + Fries + beverage));pw.println();pw.write("当前收银员:徐於敏");pw.println();pw.write("欢迎下次光临!");pw.println();pw.write("********************************************************************");pw.close();*/}
}
4.运行截图
5.个人总结
由于个人能力有限,仅完成单品点餐收银,套餐点餐收银结账,小票的打出。通过本次作业我对各个设计模式有了更深的了解,也进一步提升了自己的编程设计能力。
6.参考资料
参考网站https://m.jb51.net/article/140406.htm
抽象工厂模式资料:http://www.runoob.com/design-pattern/abstract-factory-pattern.html
工厂模式:http://www.runoob.com/design-pattern/factory-pattern.html
这篇关于同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++/python/Java,结合设计模式(2种以上)至少实现系统的以下功能: 1.正常餐品结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!