java实现KFC点餐系统
时间:2020-10-31 19:14:02|栏目:JAVA代码|点击: 次
同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++/python/Java,结合设计模式(2种以上)至少实现系统的以下功能:
1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠?还郝虿推方崴愫驼伊恪?
4.可在一定时间段参与店内活动(自行设计或参考官网信息)。
5.模拟打印小票的功能(写到文件中)。
类图:
建立IFood接口实现各类食物信息的打印:
public interface IFood { /** * 打印输出食物信息 * @return */ String printMesage(); }
抽象类AbstractBaseFood
public class AbstractBaseFood { // 类别 protected String kind; // 数量 protected int num; // 价格 protected float price; //找零 // 合计 public float totalPrice() { return this.num * this.price; } }
各类果汁的基类Baverage:
public abstract class Beverage extends AbstractBaseFood implements IFood { public String printMesage() { return ("--" + this.kind + "饮料,\t单价:" + this.price + ",\t数量:" + this.num + ",\t合计:" + this.totalPrice()); } }
建立Baverage的具体实现类ChinaBaverage:
public class ChinaBeverage extends Beverage { public ChinaBeverage(int num) { this.kind = "可乐"; this.price = 6.0f; this.num = num; } }
以此类推分别建立 ChickenWing,FrenchFries,Hamburg抽象类和它们的实现类ChinaChickenWing,FrenchFries,Hamburg
建立抽象工厂IKfcFactory:
public interface IKfcFactory { // 生产汉堡 public ChinaHamburg createHamburg(int num); // 生产薯条 public xtx.FrenchFries createFrenchFries(int num); // 生产鸡翅 public ChinaChickenWings createChickenWings(int num); // 生产饮料 public ChinaBeverage createBeverage(int num); }
建立IKfcFactory的实现类ChinaFactory:
public class ChinaKfcFactory implements IKfcFactory { // 生产可乐 public ChinaBeverage createBeverage(int num) { return new ChinaBeverage(num); } // 生产奥尔良烤鸡翅 public ChinaChickenWings createChickenWings(int num) { return new ChinaChickenWings(num); } // 生产薯条 public ChinaFrenchFries createFrenchFries(int num) { return new ChinaFrenchFries(num); } // 生产麻辣风味鸡腿汉堡 public ChinaHamburg createHamburg(int num) { return new ChinaHamburg(num); } }
建立Customer类实现食物的选择和文件存储:
package xtx.factory.custom; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import xtx.ChinaBeverage; import xtx.ChinaChickenWings; import xtx.ChinaFrenchFries; import xtx.IKfcFactory; import xtx.ChinaHamburg; public class Customer { // 抽象工厂 private IKfcFactory kfcFactory; // 构造方法将抽象工厂作为参数传入 public Customer(IKfcFactory kfcFactory2) { this.kfcFactory = kfcFactory2; } /** * 订购食物 * @throws IOException */ private String s[] =new String[5]; public void showbill() throws IOException{ BufferedWriter bw=new BufferedWriter(new FileWriter("D://workspace2Eclipse//xtx//src//xtx//factory//custom//show.txt",true)); bw.write("---------------------账单如下---------------------"); bw.newLine(); for(int i=0;i<5;i++){ bw.write(s[i]); bw.newLine(); bw.flush(); } } // 订购麻辣鸡腿汉堡 public float orderHamburg(int num) throws IOException { // 获得麻辣鸡腿汉堡 ChinaHamburg hamburg = kfcFactory.createHamburg(num); // 输出订购信息 System.out.print(hamburg.printMesage()); s[0]=hamburg.printMesage(); System.out.print("\n"); // 返回总价 return hamburg.totalPrice(); } // 订购奥尔良烤鸡翅 public float orderChickenWings(int num) { // 获得奥尔良烤鸡翅 ChinaChickenWings chickenWings = kfcFactory.createChickenWings(num); // 输出订购信息 System.out.print(chickenWings.printMesage()); s[1]=chickenWings.printMesage(); System.out.print("\n"); // 返回总价 return chickenWings.totalPrice(); } // 订购薯条 public float orderFrenchFries(int num) { // 获得薯条 ChinaFrenchFries frenchFries = (ChinaFrenchFries) ((IKfcFactory) kfcFactory).createFrenchFries(num); // 输出订购信息 System.out.print(frenchFries.printMesage()); s[2]=frenchFries.printMesage(); System.out.print("\n"); // 返回总价 return frenchFries.totalPrice(); } // 订购可乐 public float orderBeverage(int num) { // 获得可乐 ChinaBeverage beverage = kfcFactory.createBeverage(num); // 输出订购信息 System.out.print(beverage.printMesage()); s[3]=beverage.printMesage(); System.out.print("\n"); return beverage.totalPrice(); } //订购套餐一 public float ordercombo1(int num) { // 获得可乐 ChinaBeverage beverage = kfcFactory.createBeverage(num); // 获得麻辣鸡腿汉堡 ChinaHamburg hamburg = kfcFactory.createHamburg(num); s[4]=("--套餐一,\t单价:21,\t数量:"+num+"\t\t合计:"+(beverage.totalPrice()+hamburg.totalPrice())+"\n"); System.out.print("--套餐一,\t单价:21,\t数量:"+num+"\t\t合计:"+(beverage.totalPrice()+hamburg.totalPrice())+"\n"); return beverage.totalPrice()+hamburg.totalPrice(); } }
MainApp:
package xtx.factory.itf; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; import xtx.IKfcFactory; import xtx.factory.custom.Customer; public class MainApp { /** * 主应用程序方法 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { /** * 定义一个肯德基(IKfcFactory类型) */ IKfcFactory kfcFactory = (IKfcFactory) new ChinaKfcFactory(); Customer customer = new Customer(kfcFactory); /** * 开始点餐 */ // 一个麻辣鸡腿汉堡 Scanner in =new Scanner(System.in); //System.out.print("请输入付款金额"); System.out.print("-----现有如下产品-----\n"); System.out.print("--麻辣风味汉堡\t单价:15.0.\n--奥尔良风味鸡翅\t单价:3.0\n--普通风味薯条\t单价:8.0\n--可乐饮料\t单价:6.0\n--套餐一(麻辣风味汉堡+可乐饮料)\t单价:21\n"); System.out.print("\n-----------------------"); System.out.print("\n请点餐:\n"); System.out.print("请输入麻辣风味汉堡数量---:"); int a1=in.nextInt(); System.out.print("请输入奥尔良风味鸡翅数量-:"); int a2=in.nextInt(); System.out.print("普通入风味薯条数量------:"); int a3=in.nextInt(); System.out.print("请输入可乐饮料数量------:"); int a4=in.nextInt(); System.out.print("请输入套餐份数---------:"); int a5=in.nextInt(); System.out.print("\n------账单如下-----\n"); float hamhurgMoney = customer.orderHamburg(a1); // 四个奥尔良烤鸡翅 float chickenWingsMoney = customer.orderChickenWings(a2); // 一包薯条 float frenchFriesMoney = customer.orderFrenchFries(a3); // 两杯可乐 float beverageMoney = customer.orderBeverage(a4); float combo1=customer.ordercombo1(a5); // float sum=hamhurgMoney + chickenWingsMoney + frenchFriesMoney + beverageMoney+combo1; customer.showbill(); System.out.println("总计:" + (sum)); System.out.print("请输入付款金额:"); int a=in.nextInt(); System.out.print("找零:"+(a-sum)); customer.showbill(); BufferedWriter bw=new BufferedWriter(new FileWriter("D://workspace2Eclipse//xtx//src//xtx//factory//custom//show.txt",true)); bw.write("总计: "+sum); bw.newLine(); bw.write("付款:"+a); bw.newLine(); float y=a-sum; bw.write("找零:"+y); bw.newLine(); bw.flush(); bw.close(); } }
运行结果展示:
文件存储:
更多学习资料请关注专题《管理系统开发》。
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机