java實現KFC點餐系統


這篇文章主要為大家詳細介紹了java實現KFC點餐系統,模擬肯德基快餐店的收銀系統,具有一定的參考價值,感興趣的小伙伴們可以參考一下

同學們應該都去麥當勞或肯德基吃過快餐吧?請同學們參考肯德基官網的信息模擬肯德基快餐店的收銀系統,合理使用C++/python/Java,結合設計模式(2種以上)至少實現系統的以下功能:

1.正常餐品結算和找零。
2.基本套餐結算和找零。
3.使用優惠劵購買餐品結算和找零。
4.可在一定時間段參與店內活動(自行設計或參考官網信息)。
5.模擬打印小票的功能(寫到文件中)。

類圖:

建立IFood接口實現各類食物信息的打印:

1
2
3
4
5
6
7
8
public interface IFood {
  /**
  * 打印輸出食物信息
  * @return
  */
  String printMesage();
 
}

抽象類AbstractBaseFood

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class AbstractBaseFood {
  // 類別
  protected String kind;
  // 數量
  protected int num;
  // 價格
  protected float price;
  //找零
  // 合計
  public float totalPrice()
  {
  return this .num * this .price;
  }
  
}

各類果汁的基類Baverage:

1
2
3
4
5
6
7
8
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:

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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類實現食物的選擇和文件存儲:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();
  }
}

運行結果展示:

文件存儲:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM