欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现KFC点餐系统

程序员文章站 2024-02-22 18:02:05
同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用c++/python/java,结合设计模式(2种以上)至少实现系统...

同学们应该都去麦当劳或肯德基吃过快餐吧?请同学们参考肯德基官网的信息模拟肯德基快餐店的收银系统,合理使用c++/python/java,结合设计模式(2种以上)至少实现系统的以下功能:

1.正常餐品结算和找零。
2.基本套餐结算和找零。
3.使用优惠劵购买餐品结算和找零。
4.可在一定时间段参与店内活动(自行设计或参考官网信息)。
5.模拟打印小票的功能(写到文件中)。

类图:

java实现KFC点餐系统

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

运行结果展示:

java实现KFC点餐系统

文件存储:

java实现KFC点餐系统

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。