学习java中的类图-KFC点餐系统

一.问题描述
同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用C++或Java或Python结合设计模式(2种以上)至少实现系统的以下功能:
1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动(自行设计或参考官网信息)。
5.模拟打印小票的功能(写到文件中)。
基本要求:
1.程序设计风格良好,控制台界面友好。
2.实现功能测试代码,确保程序的健壮性。
3.画出使用的设计模式图。
二.类图

学习java中的类图-KFC点餐系统_第1张图片

学习java中的类图-KFC点餐系统_第2张图片
学习java中的类图-KFC点餐系统_第3张图片
学习java中的类图-KFC点餐系统_第4张图片
学习java中的类图-KFC点餐系统_第5张图片
三.代码
1.AbstractFood

public  abstract class AbstractFood {
    //类别
    protected String kind;
    //数量
    protected int num;
    //价格
    protected float price;
    //合计
    public float totalPrice(){
        return this.num*this.price;
    }
}

2.Food

public interface Food {
    //打印输出食物信息
   String printMessage();
}

3.Factory

public interface Factory {
    public interface  factory{
        //生产汉堡
        public Hamburger createHamburger(int num);

        //生产鸡肉类食物
        public Chicken createChicken(int num);

        //生产甜点
        public Dessert createDessert(int num);

        //生产饮品
        public Drinks createDrinks(int num);
    }
    public Hamburger createZingerBurger(int num);
    public Hamburger createBeefBurger(int num);
    public Hamburger createOrleansBurger(int num);
    public Hamburger createShrimpBurger(int num);
    public Chicken createDrumStick(int num);
    public Chicken createChickenWings(int num);
    public Chicken createPopcornChicken(int num);
    public Dessert createChips(int num);
    public Dessert createEggTart(int num);
    public Dessert createSundae(int num);
    public Drinks createMilky(int num);
    public Drinks createKola(int num);
    public Drinks createCoffee(int num);
    public Combo createOneCombo(int num);
    public Combo createTwoCombo(int num);
    public Combo createThreeCombo(int num);

}

4.KFCFactory

//具体工厂
public class KFCFactory implements Factory {
    //生产香辣鸡腿堡
    public ZingerBurger createZingerBurger(int num){
        return new ZingerBurger(num);
    }

    //生产超级牛肉堡
    public BeefBurger createBeefBurger(int num){
        return new BeefBurger(num);
    }

    //生产奥尔良烤鸡腿堡
    public OrleansBurger createOrleansBurger(int num){
        return new OrleansBurger(num);
    }

    //生产全虾堡
    public ShrimpBurger createShrimpBurger(int num){
        return new ShrimpBurger(num);
    }

    //生产奥尔良鸡翅
    public ChickenWings createChickenWings(int num){
        return new ChickenWings(num);
    }

    //生产烤鸡腿
    public DrumStick createDrumStick(int num){
        return new DrumStick(num);
    }

    //生产鸡米花
    public PopcornChicken createPopcornChicken(int num){
        return new PopcornChicken(num);
    }

    //生产薯条
     public Chips createChips(int num){
        return new Chips(num);
     }

    //生产蛋挞
    public EggTart createEggTart(int num){
        return new EggTart(num);
    }

    //生产圣代
    public Sundae createSundae(int num){
        return new Sundae(num);
    }

    //生产奶茶
    public Milky createMilky(int num){
        return new Milky(num);
    }

    //生产可乐
    public Kola createKola(int num){
        return new Kola(num);
    }

    //生产咖啡
    public Coffee createCoffee(int num){
        return new Coffee(num);
    }

    //生产单人套餐
    public OneCombo createOneCombo(int num){
        return new OneCombo(num);
    }
    //生产双人套餐
    public TwoCombo createTwoCombo(int num){
        return new TwoCombo(num);
    }

    //生产三人套餐
    public ThreeCombo createThreeCombo(int num){
        return new ThreeCombo(num);
    }
}

5.Hamburger

//汉堡类
public abstract class Hamburger extends AbstractFood implements Food{
    public String printMessage(){
        return("--"+this.kind+"  单价:"+this.price+
                "  数量:"+this.num+"  合计:"+this.totalPrice());
    }
}

6.Chicken

//鸡肉类
public abstract class Chicken extends AbstractFood implements Food {
    public String printMessage(){
        return("--"+this.kind+"  单价:"+this.price+
                "  数量:"+this.num+"  合计:"+this.totalPrice());
    }
}

7.Combo

//套餐
public abstract class Combo extends AbstractFood implements Food {
    public String printMessage(){
        return("--"+this.kind+"  单价:"+this.price+
                "  数量:"+this.num+"  合计:"+this.totalPrice());
    }
}

8.BeefBurger

//超级牛肉堡
public class BeefBurger extends Hamburger {
    public BeefBurger(int num){
        this.kind="超级牛肉堡";
        this.price=17f;
        this.num=num;
    }
}

9.ChickenWings

//奥尔良烤翅
public class ChickenWings extends Chicken {
    public ChickenWings(int num){
        this.kind="奥尔良烤翅";
        this.price=6f;
        this.num=num;
    }
}

10.EggTart


    //蛋挞
public class EggTart extends Dessert {
    public EggTart(int num){
        this.kind="蛋挞";
        this.price=6f;
        this.num=num;
    }
}

11.ThreeCombo

//三人套餐
public class ThreeCombo extends Combo {
    public ThreeCombo(int num){
        this.kind="三人套餐";
        this.price=109f;
        this.num=num;
    }
}

12.Quest

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.util.Scanner;
import 程序设计方法与优化.肯德基点餐系统.Customer;
public class Quest {
    public static void main(String[] args) {
        try{
            Factory factory = new KFCFactory();
            Customer customer = new Customer(factory);

            //创建输出流对象
            FileWriter file = new FileWriter("E:\\大二下\\程序设计优化\\肯德基点餐系统\\Information.txt");
            //字符缓冲输出流
            BufferedWriter bw = new BufferedWriter(file);

            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;
            Scanner input = new Scanner(System.in);
            bw.write("-------------------------------欢迎光临肯德基店----------------------------------");
            bw.newLine();
            bw.write("*******************************菜单******************************************");
            bw.newLine();
            bw.write("========================汉堡类:===================");
            bw.newLine();
            bw.write("----1.香辣鸡腿堡-----------------------------15.0元");
            bw.newLine();
            bw.write( "----2.奥尔良烤鸡腿堡------------------------17.0元");
            bw.newLine();
            bw.write("----3.超级牛肉堡-----------------------------17.0元");
            bw.newLine();
            bw.write( "----4.全虾堡---------------------------------17.0元");
            bw.newLine();
            bw.write("========================鸡肉类:===================");
            bw.newLine();
            bw.write("----5.奥尔良烤翅------------------------------6.0元");
            bw.newLine();
            bw.write("----6.烤鸡腿----------------------------------12.0元");
            bw.newLine();
            bw.write( "----7.鸡米花--------------------------------11.0元");
            bw.newLine();
            bw.write("========================甜点类:===================");
            bw.newLine();
            bw.write("----8.薯条------------------------------------8.0元");
            bw.newLine();
            bw.write("----9.蛋挞------------------------------------4.0元");
            bw.newLine();
            bw.write("----10.圣代-----------------------------------6.0元");
            bw.newLine();
            bw.write("========================饮品类:===================");
            bw.newLine();
            bw.write("----11.可乐-----------------------------------6.0元");
            bw.newLine();
            bw.write("----12.奶茶-----------------------------------6.0元");
            bw.newLine();
            bw.write("----13.咖啡----------------------------------12.0元");
            bw.newLine();
            bw.write("========================套餐类:===================");
            bw.newLine();
            bw.write("----14.单人套餐------------------------------33.0元");
            bw.newLine();
            bw.write(" 1个香辣鸡腿堡+1个烤鸡腿+1包薯条+1杯可乐/奶茶(任点)     ");
            bw.newLine();
            bw.write("----15.双人套餐-------------------------------54.0元");
            bw.newLine();
            bw.write(" 1个香辣鸡腿堡+1个奥尔良烤鸡腿堡/超级牛肉堡/全虾堡(任点)");
            bw.newLine();
            bw.write( " 1对奥尔良鸡翅+2个蛋挞+2杯可乐/奶茶(任点)"            );
            bw.newLine();
            bw.write("----16.三人套餐------------------------------109.0元");
            bw.newLine();
            bw.write(" 3个奥尔良烤鸡腿堡/超级牛肉堡/全虾堡(任点)          ");
            bw.newLine();
            bw.write( " 1对奥尔良鸡翅+1个烤鸡腿+1包鸡米花"                  );
            bw.newLine();
            bw.write( " 1包薯条+2杯圣代+2杯可乐/奶茶(任点)+1杯咖啡"    );
            bw.newLine();
            bw.write("***********************************************************************************");
            bw.newLine();
            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();
            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 m = input.nextInt();
            double d = 0;
            if (m == 1) {
                System.out.println("请输入优惠券金额:");
                d=input.nextDouble();
            }
            System.out.println("您的支付金额为:");
            double f = input.nextDouble();
            double changes = f - money + d;
            System.out.println("找零:" + changes);
            /* if (c != 1) {
                    break;
                }*/
            bw.write("######################账单如下#########################");
            bw.newLine();
            bw.write("您的餐品为:");
            bw.newLine();
            bw.write("您本次餐品总计:"+money+"元");
            bw.newLine();
            bw.write("您此次优惠券金额为:"+d+"元");
            bw.newLine();
            bw.write("您的实付金额为:"+f+"元");
            bw.newLine();
            bw.write("找零:"+changes+"元");
            bw.newLine();
            bw.write("                   欢迎下次光临!                      ");
            bw.flush();
            bw.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}


这里我只列举了部分代码,其他部分与其类似。
四.调试
学习java中的类图-KFC点餐系统_第6张图片
学习java中的类图-KFC点餐系统_第7张图片
五.测试
1.菜单
学习java中的类图-KFC点餐系统_第8张图片
2.小票
学习java中的类图-KFC点餐系统_第9张图片

六.总结
首先,我创建了一个类AbstractFactory:在这个类里面实现各种食品的数量、类别、单价、总价。其次是Food接口:在这个接口里面打印食品信息。在接口Factory工厂里面实现各个食品的生产,同时也包括食品下的各个类别的生产。然后有各个实现接口Food的食品类:Hamburger,Chicken,Dessert,Drinks,Combo这些。KFCFactory类实现接口Factory里具体食品的生产数量。
然后下面就是具体的食品类:
Hamburger类下面有四个子类:ZungerBurger,BeefBurger,OrleanBurger,ShrimpBurger;
Chicken类下面有三个子类:ChickenWings,DrumStick,PopcornChicken
Dessert下面有三个子类:Chips,EggTart,Sundae
Drinks下面有三个子类:Kola,Milky,Coffee
Combo下面有三个子类:OneCombo,TwoCombo,ThreeCombo
用到的关系模式有:继承,接口,聚合。
通过这个程序,能够更好地理解简单工厂模式,工厂方法模式以及抽象工厂。

你可能感兴趣的:(实现小功能的程序,uml类图,java)