同为西科沦落人,相逢何必曾相识

一、题目名称:KFC点餐系统设计
二、题目内容:
同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网信息模拟肯德基快餐店的收银系统,合理使用C++或Java或Python结合设计模式(2种以上)至少实现系统的以下功能:
1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动(自行设计或参考官网的信息)。
5.模拟打印小票的功能(写到文件中)。
基本要求:
程序设计风格良好,控制台界面友好,最多两人一组完成任务。
实现功能测试代码,确保程序的健壮性。
画出使用的设计模式图。
提高要求:
实现可视化界面(使用MFC)。
实现会员储值卡功能,完成储值卡消费。
实现当天营业额和餐品销量计算和统计,用数据库记录。
思路分析:
采用简单工厂模式将肯德基的食物设为一个接口,其他四类汉堡,小吃,甜点和饮料实现食物这个接口,具体食物由子类继承父类。
采用抽象工厂模式生产食物,工厂为接口,具体的工厂KFC工厂实现接口。
二.UML类图设计
1.工厂模式

2.策略模式

三.具体设计细节
1.系统设计的具体结构

2.菜单的具体设计

3.程序具体设计
1.基本食物类
食物基本信息类
具体代码:public class baseFood {
// 类别
protected String kind;
// 数量
protected int num;
// 价格
protected float price;
// 合计
public float totalPrice()
{
return this.num * this.price;
}
}
2.打印食物接口
打印食物信息接口
具体代码:public interface IFood {
String printMessage();
}
3.抽象肯德基工厂类
定义抽象方法,将参数传入食品代号和食品数量
集体代码:
public interface KFCFactoryImpl {
public Hamburg createHamburg(int kind,int num);
public IceCream createIceCream(int kind, int num);
public Beverage createBeverage(int kind, int num);
public Chicken createChicken(int kind, int num);
}
4.具体肯德基厨房类
具体肯德基厨房实现抽象接口生产所需的食物,实现抽象产品。
具体代码://具体肯德基厨房工厂
public class KFCFoodFactory implements KFCFactoryImpl {
@Override
//制作汉堡包
public Hamburg createHamburg(int kind, int num) {
if(1 == kind){
return new BigMacHam(num);//巨无霸
}else if(2 == kind){
return new SpicyChickenHam(num);//香辣鸡腿堡
}else if(3 == kind){
return new OrleansChickenHam(num);//奥尔良鸡腿堡
}else{
return null;
}
}

//制作冰激凌
@Override
public IceCream createIceCream(int kind, int num) {
    if(1 == kind){
        return new strawberrySundae(num);//草莓圣代
    }else if(2 == kind){
        return new blueberrySundae(num);//蓝莓圣代
    }else if(3 == kind){
        return new iceCreamCone(num);//原味甜筒
    }else{
        return null;
    }
}

//制作饮料
@Override
public Beverage createBeverage(int kind, int num) {
    if(1 == kind){
        return new CocaCola(num);//可口可乐
    }else if(2 == kind){
        return new juice(num);//九珍果汁
    }else{
        return null;
    }
}

//制作炸鸡
@Override
public Chicken createChicken(int kind, int num) {
    if(1 == kind){
        return new chickenNugget(num);//上校鸡块
    }else if(2 == kind){
        return new chickenRolls(num);//鸡肉卷
    }else if(3 == kind){
        return new popcornChicken(num);//鸡米花
    }else{
        return null;
    }
}

}
5.汉堡基类

这里既用到了extends继承食物信息基类又用到了implements实现打印食物接口。
集体代码:abstract class Hamburg extends baseFood implements IFood{
@Override
public String printMessage() {
return (this.kind+"\t"+this.price+"\t"+this.num+"\t"+this.totalPrice());
}
}
6.汉堡派生类

具体代码:
public class BigMacHam extends Hamburg{
public BigMacHam(int num) {
this.kind = “巨无霸大汉堡”;
this.num = num;
this.price = 25.0f;
}
}
对于其他的食物,果汁,鸡翅,冰淇淋等类也可以通过这类方法实现。
7.顾客类

这里顾客类的成员函数ShowBill实现打印小票文件,OutBill显示订单,这里利用集合动态数组添加订单信息。
具体代码:
public class Customer {
private KFCFactoryImpl kfcFactory;
private ArrayList billList = new ArrayList();

public Customer(KFCFactoryImpl kfcFactory) {
    this.kfcFactory = kfcFactory;
}

public void showBill() throws IOException {
    BufferedWriter bw = new BufferedWriter(new FileWriter("C://kfc.txt", true));
    bw.write("*****************************************************\r\n");
    bw.write("             **欢迎光KFC**             \r\n");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    bw.write("订餐时间:" + df.format(new Date()) + "\r\n");
    bw.write("名称    \t\t单价\t数量\t价格\r\n");
    Iterator var3 = this.billList.iterator();

    while(var3.hasNext()) {
        String str = (String)var3.next();
        bw.write(str);
        bw.newLine();
        bw.flush();
    }

}

public void outBill() {
    Iterator var1 = this.billList.iterator();

    while(var1.hasNext()) {
        String str = (String)var1.next();
        System.out.println(str);
    }

}

public float getHamburg(int choose, int num) {
    Hamburg hamburg = this.kfcFactory.createHamburg(choose, num);
    System.out.print(hamburg.printMessage());
    this.billList.add(hamburg.printMessage());
    return hamburg.totalPrice();
}

public float getIceCream(int choose, int num) {
    IceCream icecream = this.kfcFactory.createIceCream(choose, num);
    System.out.print(icecream.printMessage());
    this.billList.add(icecream.printMessage());
    return icecream.totalPrice();
}

public float getBeverage(int choose, int num) {
    Beverage beverage = this.kfcFactory.createBeverage(choose, num);
    System.out.print(beverage.printMessage());
    this.billList.add(beverage.printMessage());
    return beverage.totalPrice();
}

public float getChicken(int choose, int num) {
    Chicken chicken = this.kfcFactory.createChicken(choose, num);
    System.out.print(chicken.printMessage());
    this.billList.add(chicken.printMessage());
    return chicken.totalPrice();
}

public float getSetMeal(int choose, int num) {
    Hamburg hamburg;
    Chicken chicken;
    Beverage beverage;
    float money;
    if (1 == choose) {
        hamburg = this.kfcFactory.createHamburg(3, 1);
        beverage = this.kfcFactory.createBeverage(2, 1);
        chicken = this.kfcFactory.createChicken(1, 1);
        this.billList.add(hamburg.printMessage());
        this.billList.add(beverage.printMessage());
        this.billList.add(chicken.printMessage());
        money = (float)Math.round(0.9D * (double)(hamburg.totalPrice() + beverage.totalPrice() + chicken.totalPrice()) * (double)num);
        return money;
    } else {
        IceCream iceCream;
        if (2 == choose) {
            hamburg = this.kfcFactory.createHamburg(2, 3);
            beverage = this.kfcFactory.createBeverage(2, 3);
            chicken = this.kfcFactory.createChicken(2, 2);
            iceCream = this.kfcFactory.createIceCream(1, 1);
            this.billList.add(hamburg.printMessage());
            this.billList.add(beverage.printMessage());
            this.billList.add(chicken.printMessage());
            this.billList.add(iceCream.printMessage());
            money = (float)Math.round(0.9D * (double)(hamburg.totalPrice() + beverage.totalPrice() + chicken.totalPrice() + iceCream.totalPrice()) * (double)num);
            return money;
        } else if (3 == choose) {
            hamburg = this.kfcFactory.createHamburg(1, 2);
            iceCream = this.kfcFactory.createIceCream(3, 2);
            beverage = this.kfcFactory.createBeverage(2, 2);
            chicken = this.kfcFactory.createChicken(1, 1);
            this.billList.add(hamburg.printMessage());
            this.billList.add(beverage.printMessage());
            this.billList.add(chicken.printMessage());
            this.billList.add(iceCream.printMessage());
            money = (float)Math.round(0.9D * (double)(hamburg.totalPrice() + beverage.totalPrice() + chicken.totalPrice() + iceCream.totalPrice()) * (double)num);
            return money;
        } else {
            System.out.println("没有该套餐!!!");
            return 0.0F;
        }
    }
}

}
8.菜单主类

由菜单类实现主界面的选择显示界面
具体代码:
public class Menu {
Scanner input;
private KFCFactoryImpl kfcFactory;
private Customer customer;
private float hamburgMoney;
private float iceCreamMoney;
private float beverageMoney;
private float chickenFiresMoney;
private float setMealMoney;
private float sumMoney;

public Menu(KFCFactoryImpl kfcFactory, Customer customer) {
    this.input = new Scanner(System.in);
    this.kfcFactory = kfcFactory;
    this.customer = customer;
}

public void menu() throws IOException {
    System.out.println("******欢迎光临KFC********");
    System.out.println("    ***          ***   ");
    System.out.println("  ********    ********  ");
    System.out.println("***         *        ***");
    System.out.println("  ***  1.开始点餐   *** ");
    System.out.println("    ** 2.退出系统  **   ");
    System.out.println("      **         **    ");
    System.out.println("         **   **");
    System.out.println("            **         ");
    System.out.print("请选择>");
    int choose = this.input.nextInt();
    switch(choose) {
        case 1:
            this.productionMenu();
            int pay = this.payMoney(this.customer, this.getMoney());
            printBill(this.customer, this.getMoney(), pay);
            break;
        case 2:
            System.out.println("您已成功退出点餐系统...");
        default:
            System.out.println("选择错误....");
    }

}

public void productionMenu() throws IOException {
    boolean flag = true;

    while(flag) {
        System.out.println("当前商品如下:");
        System.out.println("==================");
        System.out.println("1.汉堡");
        System.out.println("==================");
        System.out.println("2.冰淇淋");
        System.out.println("==================");
        System.out.println("3.饮料");
        System.out.println("==================");
        System.out.println("4.烤鸡");
        System.out.println("==================");
        System.out.println("5.套餐");
        System.out.println("==================");
        System.out.println("6.结束订餐,查看订单");
        System.out.println("==================");
        System.out.print("请选择>");
        int choose = this.input.nextInt();
        switch(choose) {
            case 1:
                this.hamburgMenu();
                break;
            case 2:
                this.iceCreamMenu();
                break;
            case 3:
                this.beverageMenu();
                break;
            case 4:
                this.chickenFiresMenu();
                break;
            case 5:
                this.setMeal();
                break;
            case 6:
                float money = this.getMoney();
                showBills(this.customer, money);
                break;
            default:
                System.out.println("选择错误...");
        }

        if (choose == 6) {
            flag = false;
        }
    }

}

public void hamburgMenu() {
    System.out.println("1.巨无霸汉堡--------25元");
    System.out.println("2.香辣鸡腿堡--------12元");
    System.out.println("3.奥尔良鸡腿堡------10元");
    System.out.print("请选择>");
    int choose = this.input.nextInt();
    System.out.print("请输入数量>");
    int num = this.input.nextInt();
    this.hamburgMoney = this.customer.getHamburg(choose, num);
}

public void iceCreamMenu() {
    System.out.println("1.草莓圣代------12元");
    System.out.println("2.蓝莓圣代------12元");
    System.out.println("3.  甜筒--------6元");
    System.out.print("请选择>");
    int choose = this.input.nextInt();
    System.out.print("请输入数量>");
    int num = this.input.nextInt();
    this.iceCreamMoney = this.customer.getIceCream(choose, num);
}

public void beverageMenu() {
    System.out.println("1.可口可乐------12元");
    System.out.println("2.九珍果汁------11元");
    System.out.println("3.雪碧----------10元");
    System.out.print("请选择>");
    int choose = this.input.nextInt();
    System.out.print("请输入数量>");
    int num = this.input.nextInt();
    this.beverageMoney = this.customer.getBeverage(choose, num);
}

public void chickenFiresMenu() {
    System.out.println("1.上校鸡块------12元");
    System.out.println("2. 鸡肉卷 ------10元");
    System.out.println("3. 鸡米花 ------8元");
    System.out.print("请选择>");
    int choose = this.input.nextInt();
    System.out.print("请输入数量>");
    int num = this.input.nextInt();
    this.chickenFiresMoney = this.customer.getChicken(choose, num);
}

public void setMeal() {
    System.out.println("1.儿童套餐(奥尔良鸡腿堡+九珍果汁+上校鸡块)------35元");
    System.out.println("2.家庭套餐(香辣鸡腿堡(3个)+九珍果汁(3杯)+鸡肉卷(2个)+草莓圣代)------118元");
    System.out.println("3.情侣套餐(巨无霸(2个)+甜筒(2个)+九珍果汁(2杯)+上校鸡块)------99元");
    System.out.print("请选择>");
    int choose = this.input.nextInt();
    System.out.print("请输入数量>");
    int num = this.input.nextInt();
    this.setMealMoney = this.customer.getSetMeal(choose, num);
}

public float getMoney() {
    this.sumMoney = this.hamburgMoney + this.iceCreamMoney + this.beverageMoney + this.chickenFiresMoney + this.setMealMoney;
    return this.sumMoney;
}

public static void printBill(Customer customer, float sumMoney, int pay) throws IOException {
    customer.showBill();
    BufferedWriter bw = new BufferedWriter(new FileWriter("C://kfc.txt", true));
    bw.write("总计: " + sumMoney);
    bw.newLine();
    bw.write("付款:" + pay);
    bw.newLine();
    float y = (float)pay - sumMoney;
    bw.write("找零:" + y);
    bw.newLine();
    bw.write("当前收银员:小猫咪为你服务\r\n");
    bw.write("                         欢迎下次光临!^_^                        \r\n");
    bw.write("******************************************************\r\n");
    bw.flush();
    bw.close();
}

public static void showBills(Customer customer, float sumMoney) throws IOException {
    customer.outBill();
    System.out.println("总计:" + sumMoney);
}

public int payMoney(Customer customer, float sumMoney) {
    System.out.print("请输入付款金额:");
    int pay = this.input.nextInt();
    if ((float)pay - sumMoney >= 0.0F) {
        if (pay >= 100) {
            System.out.println("您要使用优惠卷结账吗?(y/n)");
            System.out.println("优惠卷:满100减20,满200减30!!!");
            if (this.input.next().equals("y")) {
                if (pay >= 200) {
                    System.out.println("找零:" + ((float)pay - sumMoney + 30.0F));
                } else {
                    System.out.println("找零:" + ((float)pay - sumMoney + 20.0F));
                }
            } else {
                System.out.println("找零:" + ((float)pay - sumMoney));
            }
        } else {
            System.out.println("找零:" + ((float)pay - sumMoney));
        }
    } else {
        System.out.println("不够哦!!!");
    }

    return pay;
}

}
9.运行界面

四.总结
模拟肯德基订餐的系统是运用了工厂方法设计模式设计的一个简单的点餐系统,该系统可以实现正常商品的结算,套餐商品结算,优惠卷的使用以及在文件中记录订餐账单(即模拟订餐小票的打印)。
存在问题:
编写过程中,思路不清晰。刚开始在写的时候,想法过于简单,所以在实际编写的过程中,一度发现问题并不断迭代。所以在编写代码之前要理清自己的思路,数据的处理,功能实现的具体方法。
设计模式的应用不熟练导致的代码内聚度不够高,耦合不够低。书本上学习的设计模式运用到实际中还是挺困难的,所对于设计模式的学习还是要多与实际问题结合应用。
本篇文章基于我们学姐的一片文章,下面是原著。[这是学姐的链接]
(%E6%9C%AC%E6%96%87%E9%93%BE%E6%8E%A5%EF%BC%9Ahttps://blog.csdn.net/qq_42611113/article/details/90143130)

你可能感兴趣的:(同为西科沦落人,相逢何必曾相识)