KFC簡易點餐系統作業
阿新 • • 發佈:2018-12-17
KFC點餐系統作業說明: 思路:用了兩種設計模式,一種是工廠模式,一種是抽象工廠。 工廠模式:用來建立漢堡,飲料物件。 抽象工廠:用來建立套餐。 首先建立一個食物介面,再建立一個漢堡和飲料介面繼承食物介面。 然後建立具體的漢堡類,和飲料類例項化介面。 運用工廠模式建立漢堡和飲料物件。 執行抽象工廠建立套餐物件。 將點餐記錄在檔案中,並列印。
類:
抽象工廠:
工廠模式:
執行結果:
public class Function { /** * @param args */ static double Money(double money,int count) { Date dNow = new Date(); SimpleDateFormat ft = new SimpleDateFormat ("hh:mm"); int datecount=10; try{ Date dt1 = ft.parse("6:00");//將字串轉換為date型別 Date dt2 = ft.parse("10:00"); Date dt3=ft.parse(ft.format(dNow)); if(dt3.getTime()>dt1.getTime()&&dt3.getTime()<dt2.getTime()) datecount=8; }catch(Exception e) { e.printStackTrace(); } return money/10*count/10*datecount; } static void write(String str){ try { FileWriter fw = new FileWriter(new File("D:note.txt"),true); fw.write(str); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { double money=0; Date dNow = new Date(); SimpleDateFormat ft = new SimpleDateFormat ("hh:mm"); System.out.println("當前時間為: " + ft.format(dNow)); Scanner scanner =new Scanner(System.in); System.out.println("請您選擇單點或者套餐-----1.單點 2.套餐"); int choose=scanner.nextInt(); if(choose==1){ System.out.println("-----------------------------------"); System.out.println(" 請輸您要選擇的餐品 "); System.out.println("漢堡系列:1.田園雞腿堡 ¥18 2.香辣雞腿堡 ¥25"); System.out.println("-----------------------------------"); int foodchoose=scanner.nextInt(); System.out.println("-----------------------------------"); System.out.println("請問您要什麼口味的漢堡"); String burgertaste=scanner.next(); System.out.println("-----------------------------------"); System.out.println(" 請選擇您需要點的飲品 "); System.out.println("飲品系列:1.可樂 ¥10 2.咖啡 ¥15"); int drinkchoose=scanner.nextInt(); System.out.println("-----------------------------------"); System.out.println("請問您要大杯中杯還是小杯"); System.out.println("-----------------------------------"); String drinktype=scanner.next(); //foodchoose drinkchoose分別代表選擇的漢堡和飲品 //您點的餐品: switch(foodchoose){ case 1: hamburgerfactory fac1=new PastoralChickenFactory(); PastoralChicken pc=(PastoralChicken)fac1.factoryMethod(burgertaste); System.out.println(pc); write(pc.toString()); money=money+18; break; case 2: hamburgerfactory fac2=new SpicyChickenFactory(); SpicyChicken sc=(SpicyChicken)fac2.factoryMethod(burgertaste); System.out.println(sc); write(sc.toString()); money=money+25; break; default: System.out.println("沒有此類餐品"); } switch(drinkchoose){ case 1: drinksfactory fac1=new ColaFactory(); Cola cola=(Cola)fac1.factoryMethod(drinktype); System.out.println(cola); write(cola.toString()); money=money+10; break; case 2: drinksfactory fac2=new CoffeeFactory(); Coffee coffee=(Coffee)fac2.factoryMethod(drinktype); System.out.println(coffee); write(coffee.toString()); money=money+15; break; default: System.out.println("沒有此類餐品"); } System.out.println("請問您有幾折卷"); int count=scanner.nextInt(); System.out.println("您本次一共消費"+Money(money, count)); write("您本次一共消費"+Money(money, count)); } else if(choose==2) { System.out.println("-----------------------------------"); System.out.println(" 請選擇您要點的套餐 "); System.out.println("套餐A:田園雞腿堡+可樂 價格:¥25"); System.out.println("套餐B 香辣雞腿堡+咖啡 價格:¥45"); System.out.println("1.套餐A 2.套餐B"); System.out.println("-----------------------------------"); int choose2=scanner.nextInt(); switch (choose2) { case 1: ConcreteFactoryA fa=new ConcreteFactoryA(); hamburger ham=fa.hamburgerfactory(); drink cl=fa.drinksfactory(); System.out.println(ham); System.out.println(cl); write(ham.toString()+" "+cl.toString()); System.out.println("請問您有幾折卷"); int count1=scanner.nextInt(); System.out.println("您本次一共消費"+Money(25, count1)); write("您本次一共消費"+Money(25, count1)); System.out.println("請輸入您的付款金額"); int pay1=scanner.nextInt(); System.out.println("找零:"+(pay1-Money(25, count1))); write("找零:"+(pay1-Money(25, count1))); break; case 2: ConcreteFactoryB fa2=new ConcreteFactoryB(); hamburger ham2=fa2.hamburgerfactory(); drink cl2=fa2.drinksfactory(); System.out.println(ham2); System.out.println(cl2); write(ham2.toString()+" "+cl2.toString()); System.out.println("請問您有幾折卷"); int count2=scanner.nextInt(); System.out.println("您本次一共消費"+Money(35, count2)); write("您本次一共消費"+Money(35, count2)); System.out.println("請輸入您的付款金額"); int pay2=scanner.nextInt(); System.out.println("找零:"+(pay2-Money(25, count2))); write("找零:"+(pay2-Money(25, count2))); break; } } } }