1. 程式人生 > >java作業練習4:KFC前臺訂餐系統(版本1)

java作業練習4:KFC前臺訂餐系統(版本1)

作業題目:KFC前臺訂餐系統(版本1)

所用語言:java

所用類:

              

其他檔案:bills.txt

程式介紹:使用者根據目前電腦時段選擇早餐,正餐,宵夜的不同選單,使用者只能在這個範圍內點餐,每次加餐會更新顧客的選單最後輸入給的錢自動找零。將“發票”儲存至bills.txt中,整個類的設計過程採用了簡單工廠模式和單例模式。程式碼如下:

import KFCMenus.BreakfastMenu;
import KFCMenus.LateNightSnacksMenu;
import KFCMenus.Menu;
import KFCMenus.RegularMealsMenu;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        System.out.println("------\\(˙<>˙)/------");
        System.out.println("!!!!Welcome to KFC!!!!");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式
        System.out.println("Current time is:" + df.format(new Date()));// new Date()為獲取當前系統時間
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        Menu menu;
        if (hour >= 6 && hour < 8) {
            System.out.print(">▽<早上好!這是您的早餐選單哦!");
            menu = BreakfastMenu.getInstance();
            Frame(menu);
        } else if (hour >= 8 && hour < 11 || hour >= 11 && hour < 13 || hour >= 13 && hour < 18) {
            if (hour >= 8 && hour < 11)
                System.out.println("上午好");
            if (hour >= 11 && hour < 13)
                System.out.println("中午好");
            if (hour >= 13 && hour < 18)
                System.out.println("下午好");
            System.out.println(">▽<這是您的主食選單!");
            menu = RegularMealsMenu.getInstance();
            Frame(menu);
        } else {
            System.out.println("晚上好(-_-)zZ,夜宵選單給你看~");
            menu = LateNightSnacksMenu.getInstance();
            Frame(menu);
        }
    }

    public static void Frame(Menu menu) throws IOException {

        Set<String> foodSet = menu.menu.keySet();//set中無法取出某個制定的元素,所以將其轉換成List
        List<String> foodlist = new ArrayList<String>(foodSet);
        System.out.println("
[email protected]
@[email protected]@[email protected]@"); for (int i = 0; i < foodlist.size(); i++) { System.out.println((i + 1) + foodlist.get(i)); } System.out.println("[email protected]@[email protected]@[email protected]@"); Scanner sc = new Scanner(System.in); boolean flag = true; System.out.println("想吃什麼嘞? -▽-y"); float totalPrice = 0; Map<String, Float> orderList = new HashMap<>(); while (flag) { switch (sc.nextInt()) { case 1: orderList.put(foodlist.get(0), menu.getMenu().get(foodlist.get(0))); totalPrice += menu.getMenu().get(foodlist.get(0)); break; case 2: orderList.put(foodlist.get(1), menu.getMenu().get(foodlist.get(1))); totalPrice += menu.getMenu().get(foodlist.get(1)); break; case 3: orderList.put(foodlist.get(2), menu.getMenu().get(foodlist.get(2))); totalPrice += menu.getMenu().get(foodlist.get(2)); break; case 0: flag=false; break; } System.out.println("已點:" + orderList + "總價:" + totalPrice + "就這些嗎? <(‵▽′)> (按0結束按其他鍵繼續)"); } System.out.println("KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*"); System.out.println("這是您的選單!請過目:" + orderList + "總價:" + totalPrice + "請給現金(/><)/"); System.out.println("KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*KFC*"); float money = 0; do { money = sc.nextFloat(); if (money < totalPrice) { System.out.println("沒給夠哦!⊙ . ⊙"); } } while (money < totalPrice); System.out.println("收現金:" + money + "找零:" + (money - totalPrice) + "發票已列印 ≧ω≦"); File f = new File("E:/Java Projects/Homework4/src/file/bills"); FileWriter fw = new FileWriter(f); BufferedWriter bw = new BufferedWriter(fw); bw.write("食品:" + String.valueOf(orderList) + "總價" + totalPrice + "\t"); bw.flush(); } }

main類

package Foods;
//使用簡單的工廠模式生成各種食品
public interface Food {
    void setPirce(float p);
    float getPrice();
    String getName();
}

Foods介面,用於建立工程

package Foods;

public class FoodFactory  {
    public Food getFood(String name){
        if(name=="null")
        {
            System.out.println("there isn't this name ,≡(▔﹏▔)≡");
            return null;
        }
        if(name.equalsIgnoreCase("冰淇淋")){
            return new IceCream();
        } else if(name.equalsIgnoreCase("嫩牛五方")){
            return new BeefRoulade();
        } else if(name.equalsIgnoreCase("薯條")){
            return new FrenchFries();
        }else if(name.equalsIgnoreCase("香辣雞腿堡")){
            return new ZingerBurger();
        }else if(name.equalsIgnoreCase("可樂")){
            return new Cola();
        }
        return null;
    }
}

工廠類 

所有食物類:

package Foods;

public class BeefRoulade extends FoodInfo implements Food{
    public BeefRoulade(){
        price=15;
        name="嫩牛五方";
    }
    @Override
    public void setPirce(float p) {
        this.price=p;
    }

    @Override
    public float getPrice() {
        return price;
    }

    @Override
    public String getName() {
        return name;
    }
}
package Foods;

public class Cola extends FoodInfo implements  Food {
    public Cola(){
        price=12;
        name="可樂";
    }
    @Override
    public void setPirce(float p) {
        this.price=p;
    }

    @Override
    public float getPrice() {
        return this.price;
    }

    @Override
    public String getName() {
        return name;
    }
}
package Foods;

public class FrenchFries extends FoodInfo implements Food {
    public FrenchFries() {
        price= 19;
        name = "薯條";
    }

    @Override
    public void setPirce(float p) {
        this.price=p;
    }

    @Override
    public float getPrice() {
        return this.price;
    }
    @Override
    public String getName() {
        return name;
    }
}
package Foods;

public class IceCream extends FoodInfo implements Food {
    public IceCream(){
        price=9;
        name="冰淇淋";
    }
    @Override
    public void setPirce(float p) {
        this.price=p;
    }

    @Override
    public float getPrice() {
        return this.price;
    }
    @Override
    public String getName() {
        return name;
    }
}
package Foods;

public class ZingerBurger extends FoodInfo implements Food{
    public ZingerBurger(){
        price=20;
        name="香辣雞腿堡";
    }
    @Override
    public void setPirce(float p) {
        this.price=p;
    }

    @Override
    public float getPrice() {
        return this.price;
    }
    @Override
    public String getName() {
        return name;
    }
}

結束後的bills:

 Coder:Flyige