返回顶部

Java模拟简单的KFC程序

我们可以把KFC看做一个一个具体的工厂,然后,我们去抽象一个工厂,在抽象工厂中指明了生产各种抽象食物的方法,如生产汉堡、
鸡翅等,肯德基店就需要实现这个抽象工厂,生产具体的食品,如生产麻辣鸡腿汉堡、生产奥尔良鸡翅等。除此之外,我们还要“抽象食物”,
对每个具体的食物添加抽象父类,如汉堡就是抽象父类,麻辣鸡腿汉堡就是汉堡的一个子类,依次类推。每一种食物又都存在着一些共同的属性,
如风味、单价、数量等,因此,我们继续进行抽象,所有的抽象食物都继承一个抽象父类
将抽象工厂作为客户类中的一个实例变量,客户需要任何产品的时候,只需要向工厂请求即可
,这就是抽象工厂模式的应用方式。客户类和工厂类分开,客户无须修改就可以获得新产品。

//饮料基类

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());
    }

}
//可乐实现类

public class RealBeverage extends Beverage{
    public RealBeverage(int num) {
        this.kind = "可乐";
        this.price = 10;
        this.num = num;
    }
}
//鸡翅基类

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());
    }

}


//鸡翅实现类

public class RealChickenWings extends ChickenWings{
    public RealChickenWings(int num) {
        this.kind = "奥尔良";
        this.price = 5;
        this.num = num;
    }
}

//汉堡基类

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());
    }

}
//汉堡实现类

public class RealHamburg extends Hamburg{
    public RealHamburg(int num) {
        this.kind = "香辣";
        this.price = 26;
        this.num = num;
    }
}
//套餐1基类

public abstract class TaoCan1 extends AbstractBaseFood implements IFood{
    public void printMessage(){
        System.out.println("--"+this.kind+"套餐1,\t单价:"+this.price+
                ",\t数量:"+this.num+",\t合计:"+this.totalPrice());
    }
}
//套餐1实现类

public class RealTaoCan1 extends TaoCan1{
    public RealTaoCan1(int num) {
        this.kind = "超值";
        this.price = 50;
        this.num = num;
    }
}
//套餐2基类

public abstract class TaoCan2 extends AbstractBaseFood implements IFood{
    public void printMessage(){
        System.out.println("--"+this.kind+"套餐2,\t单价:"+this.price+
                ",\t数量:"+this.num+",\t合计:"+this.totalPrice());
    }
}
//套餐2实现类

public class RealTaoCan2 extends TaoCan2{
    public RealTaoCan2(int num) {
        this.kind = "特惠";
        this.price = 42;
        this.num = num;
    }
}
//KFC抽象工厂
public interface IKfcFactory {
    public Hamburg createHamburg(int num); //汉堡
    public FrenchFries createFrenchFries(int num);//薯条
    public ChickenWings createChickenWings(int num);//鸡翅
    public Beverage createBeverage(int num); //饮料
    public TaoCan1 createTaoCan1(int num);//套餐1
    public TaoCan2 createTaoCan2(int num);//套餐2
}
````java
public interface IFood {
    //打印输出食物信息
    void printMessage();
}

//肯德基具体工厂
public class KfcFactory implements IKfcFactory{

    //生产汉堡
    public Hamburg createHamburg(int num) {
        return new RealHamburg(num);
    }

    //生产薯条
    public FrenchFries createFrenchFries(int num) {
        return new RealFrenchFries(num);
    }

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

    //生产饮料
    public Beverage createBeverage(int num) {
        return new RealBeverage(num);
    }

    //生产套餐1
    public TaoCan1 createTaoCan1(int num) {
        return new RealTaoCan1(num);
    }

    //生产套餐2
    public TaoCan2 createTaoCan2(int num) {
        return new RealTaoCan2(num);
    }
}

//客户类
public class Customer {
    //抽象工厂
    private IKfcFactory kfcFactory;
    //构造方法将抽象工厂作为参数传入
    public Customer(IKfcFactory kfcFactory){
        this.kfcFactory = kfcFactory;
    }

    //订购老八秘制小汉堡
    public int orderHamburg(int num){
        Hamburg hamburg = kfcFactory.createHamburg(num);
        hamburg.printMessage();
        return hamburg.totalPrice();
    }

    //订购奥尔良烤翅
    public int orderChickenWings(int num){
        ChickenWings chickenWings = kfcFactory.createChickenWings(num);
        chickenWings.printMessage();
        return chickenWings.totalPrice();
    }

    //订购薯条
    public int orderFrenchFries(int num){
        FrenchFries frenchFries = kfcFactory.createFrenchFries(num);
        frenchFries.printMessage();
        return frenchFries.totalPrice();
    }

    //订购可乐
    public int orderBeverage(int num){
        Beverage beverage = kfcFactory.createBeverage(num);
        beverage.printMessage();
        return beverage.totalPrice();
    }

    //订购套餐1
    public int orderTaoCan1(int num){
        TaoCan1 taocan1=kfcFactory.createTaoCan1(num);
        taocan1.printMessage();
        return taocan1.totalPrice();
    }

    //订购套餐2
    public int orderTaoCan2(int num){
        TaoCan2 taocan2=kfcFactory.createTaoCan2(num);
        taocan2.printMessage();
        return taocan2.totalPrice();
    }
}

//单品基本信息抽象类
public abstract class AbstractBaseFood {
    protected String kind;//类别
    protected int num;//数量
    protected int price; //价格
    public int totalPrice()//合计
    {
        return this.num * this.price;
    }
}
public interface IFood {
    //打印输出食物信息
    void printMessage();
}

//菜单
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Date;
import java.io.*;
import java.io.FileWriter;

public class MainMenu {
    //定义一个肯德基(IKfcFactory类型)
    IKfcFactory kfcFactory = new KfcFactory();
    //创建客户
    Customer customer = new Customer(kfcFactory);
    Scanner s=new Scanner(System.in);
    public int a;
    public int num1,num2,num3,num4,num5,num6;
    int hamburgMoney=customer.orderHamburg(num1);
    int chickenWingsMoney = customer.orderChickenWings(num2);
    int frenchFriesMoney = customer.orderFrenchFries(num3);
    int singleMoney = customer.orderBeverage(num4);
    int taocan1Money =customer.orderTaoCan1(num5);
    int taocan2Money =customer.orderTaoCan2(num6);
    public int price;
    public String b;
    public int c;
    public int d;    //付款金额
    public int e;    //找零
    public void menu()
    {
        System.out.println("* * * * * 欢迎进入肯德基炸鸡店 * * * * *");
        System.out.println("* 您可以选择:1.单点      2.套餐                   *");
        System.out.println("* 请您选择:");
        a=s.nextInt();
        switch(a)
        {
            case 1:
                System.out.println("- - - - - - - - - - - - - - - - - - - ");
                System.out.println("1.汉堡类:老八秘制小汉堡_26元");
                System.out.println("- - - - - - - - - - - - - - - - - - - ");
                System.out.println("2.鸡翅类:奥尔良烤翅_5元");
                System.out.println("- - - - - - - - - - - - - - - - - - -  ");
                System.out.println("3.小食类:薯条_8元");
                System.out.println("- - - - - - - - - - - - - - - - - - -  ");
                System.out.println("4.饮料类:可乐_10元");
                System.out.println("- - - - - - - - - - - - - - - - - - -  ");
                System.out.print("请您选择老八秘制小汉堡的数量:");
                num1=s.nextInt();
                hamburgMoney = customer.orderHamburg(num1);
                System.out.print("请您选择奥尔良烤翅的数量:");
                num2=s.nextInt();
                chickenWingsMoney = customer.orderChickenWings(num2);
                System.out.print("请您选择薯条的数量:");
                num3=s.nextInt();
                frenchFriesMoney = customer.orderFrenchFries(num3);
                System.out.print("请您选择可乐的数量:");
                num4=s.nextInt();
                singleMoney = customer.orderBeverage(num4);
                price=hamburgMoney+chickenWingsMoney+frenchFriesMoney+singleMoney;
                System.out.println("总计:"+price);
                break;
            case 2:
                System.out.println("- - - - - - - - - - - - - - - - - - - - ");
                System.out.println("套餐1:老八秘制小汉堡+薯条+可乐_50元");
                System.out.println("- - - - - - - - - - - - - - - - - - - -  ");
                System.out.println("套餐2:香辣鸡腿堡+麻辣烤翅+可乐_42元");
                System.out.println("- - - - - - - - - - - - - - - - - - - - ");
                System.out.print("请您选择套餐1的数量:");
                num5=s.nextInt();
                taocan1Money =customer.orderTaoCan1(num5);
                System.out.print("请您选择套餐2的数量:");
                num6=s.nextInt();
                taocan2Money =customer.orderTaoCan2(num6);
                price=taocan1Money+taocan2Money;
                System.out.println("总计:"+price);
        }
    }

    //领取优惠券并付款找零
    public void other(){
        System.out.println("为迎接新老顾客的到来,本店特地准备了两种优惠券");
        System.out.println("---1.满45减5   ---2.满65减8");
        System.out.print("您是否需要优惠券?(y/n)");
        String b=s.next();
        if("y".equals(b))
        {
            if(price>=45&&price<65)   //领取5元优惠券
            {
                price-=5;
                c=5;    //5元优惠券
            }
            else if(price>=65)  //领取8元优惠券
            {
                price-=8;
                c=8;
            }
            else
                System.out.println("您不符合使用优惠券的要求哦!");
            System.out.println("领取"+c+"元优惠券成功!您一共消费:"+price+"元");

        }
        System.out.print("付款:");
        d=s.nextInt();
        e=d-price;
        System.out.println("找零:"+e);
    }

    //打印账单
    public void paint(){
        System.out.print("您需要打印账单吗?(y/n)");
        String m=s.next();
        if("y".equals(m)){        //用户需要打印小票
            System.out.println("您的账单为:");
            switch(a){
                case 1:
                    SimpleDateFormat t1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println(t1.format(new Date()));		//输出时间戳
                    hamburgMoney=customer.orderHamburg(num1);
                    chickenWingsMoney = customer.orderChickenWings(num2);
                    frenchFriesMoney = customer.orderFrenchFries(num3);
                    singleMoney = customer.orderBeverage(num4);
                    System.out.println("领取"+c+"元优惠券");
                    System.out.println("总计:"+price);
                    System.out.println("付款:"+d);
                    System.out.println("找零:"+e);
                    break;
                case 2:
                    SimpleDateFormat t2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    System.out.println(t2.format(new Date()));		//输出时间戳
                    taocan1Money =customer.orderTaoCan1(num5);
                    taocan2Money =customer.orderTaoCan2(num6);
                    System.out.println("领取"+c+"元优惠券");
                    System.out.println("总计:"+price);
                    System.out.println("付款:"+d);
                    System.out.println("找零:"+e);
                    break;
            }
            try{
                fully();
            }catch(Exception e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else if("n".equals(m)){
            System.out.println("欢迎下次光临~");
        }
    }

    //在文件中打印小票
    public void fully() throws Exception{
        FileWriter pw=new FileWriter("zhangdan.txt",true);
        BufferedWriter bw=new BufferedWriter(pw);
        try {
            switch(a)
            {
                case 1:
                    bw.write("-------- 欢迎光临肯德基炸鸡店--------\r\n");
                    bw.write("老八秘制小汉堡      "+"单价:26   数量:"+num1+"合计:"+hamburgMoney+"\r\n");
                    bw.write("奥尔良烤翅             "+"单价:5     数量:"+num2+"合计:"+chickenWingsMoney+"\r\n");
                    bw.write("薯条          "+"单价:8     数量:"+num3+"合计:"+frenchFriesMoney+"\r\n");
                    bw.write("可乐饮料                 "+"单价:10   数量:"+num4+"合计:"+singleMoney+"\r\n");
                    bw.write("总计:"+price+"\r\n");
                    bw.write("付款:"+d+"\r\n");
                    bw.write("找零:"+e+"\r\n");
                    bw.flush();
                    bw.close();
                    break;
                case 2:
                    bw.write("--------欢迎光临肯德基炸鸡店---------\r\n");
                    bw.write("超值套餐1      "+"单价:50     数量:"+num5+"合计:"+taocan1Money+"\r\n");
                    bw.write("特惠套餐2      "+"单价:42     数量:"+num6+"合计:"+taocan2Money+"\r\n");
                    bw.write("总计:"+price+"\r\n");
                    bw.write("付款:"+d+"\r\n");
                    bw.write("找零:"+e+"\r\n");
                    bw.flush();
                    bw.close();
                    break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("打印成功!欢迎下次光临~");
    }
}

import java.text.*;
import java.util.*;

public class MainApp {
    public static void main(String[] args) {
        Date date=new Date();
        DateFormat fullFormat=DateFormat.getDateInstance(DateFormat.FULL);
        System.out.println("当前日期:"+fullFormat.format(date));
        //引用菜单类
        MainMenu mainmenu=new MainMenu();
        mainmenu.menu();
        mainmenu.other();
        mainmenu.paint();
    }
}
posted @ 2020-06-07 21:02  violet-evergarden  阅读(329)  评论(1编辑  收藏  举报