1. 程式人生 > >KFC簡易點餐(工廠方法+抽象工廠)

KFC簡易點餐(工廠方法+抽象工廠)

package com.product;

public interface product {
}

package com.product;

public interface drinks extends product {

}
package com.product;

public interface hamburger extends product {
	
}

package com.product;

public class Crabburger implements hamburger { //蟹黃堡
	private String flavor; //口味
	private String price;  //價格
	private int Type; //大中小
	
	public Crabburger() {
		super();
	}

	public Crabburger(String flavor, int type) {
		super();
		this.flavor = flavor;
		this.price = "25";
		Type = type;
	}
	
	public String toString() {
		return "Hamburger [flavor=" + flavor + ", price=" + price + ", Type="
				+ Type + "]";
	}

	public String getFlavor() {
		return flavor;
	}
	public void setFlavor(String flavor) {
		this.flavor = flavor;
	}
	
	public int getType() {
		return Type;
	}
	public void setType(int type) {
		Type = type;
	}
	
}

package com.product;

public class Cola implements drinks {
	
	private String style; //種類
	private String price;  //價格
	private int Type;  //大中小
	
	public Cola(String style, int type) {
		super();
		this.style = style;
		this.price = "8";
		Type = type;
	}

	public String toString() {
		return "Cola [style=" + style + ", price=" + price + ", Type=" + Type
				+ "]";
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}


	public int getType() {
		return Type;
	}

	public void setType(int type) {
		Type = type;
	}
	
package com.product;

public class Coffee implements drinks {
	private String style; //種類
	private String price;  //價格
	private int Type;  //大中小
	
	public Coffee(String style, int type) {
		super();
		this.style = style;
		this.price ="10";
		Type = type;
	}

	public String toString() {
		return "Cola [style=" + style + ", price=" + price + ", Type=" + Type
				+ "]";
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	

	public int getType() {
		return Type;
	}

	public void setType(int type) {
		Type = type;
	}
}	

package com.product;

public class Cheeseburger implements hamburger {  //芝士漢堡
	private String flavor; //口味
	private String price;  //價格
	private int Type; //大中小
	
	public Cheeseburger() {
		super();
	}
	
	public Cheeseburger(String flavor, int type) {
		super();
		this.flavor = flavor;
		this.price = "22.5";
		Type = type;
	}
	
	public String toString() {
		return "Hamburger [flavor=" + flavor + ", price=" + price + ", Type="
				+ Type + "]";
	}

	

	public String getFlavor() {
		return flavor;
	}
	public void setFlavor(String flavor) {
		this.flavor = flavor;
	}

	public int getType() {
		return Type;
	}
	public void setType(int type) {
		Type = type;
	}
	
	
}

//套餐

package com.abstractfactory;

import com.product.drinks;
import com.product.hamburger;

public interface AbstractFactory {
	public hamburger hamburgerfactory();
	public drinks drinksfactory();
}

package com.abstractfactory;

import com.product.Cheeseburger;
import com.product.Cola;
import com.product.drinks;
import com.product.hamburger;




//芝士漢堡+可樂
public class ConcreteFactory1 implements AbstractFactory {

	//
	public hamburger hamburgerfactory() {
		// TODO Auto-generated method stub
		return new Cheeseburger("多芝士",1);
	}

	public drinks drinksfactory() {
		// TODO Auto-generated method stub
		return new Cola("不加冰可樂",2);
	}

}

package com.abstractfactory;

import com.product.drinks;
import com.product.hamburger;
import com.product.Coffee;
import com.product.Crabburger;

//蟹黃堡+咖啡
public class ConcreteFactory2 implements AbstractFactory {

	public hamburger hamburgerfactory() {
		// TODO Auto-generated method stub
		return new Crabburger("蟹黃堡",2);
	}
	
	public drinks drinksfactory() {
		// TODO Auto-generated method stub
		return new Coffee("不加糖",1);
	}

}

//單點

package com.factorymethod;

import com.product.hamburger;

public interface hamburgerfactory {
	public hamburger factoryMethod(String flavor,int type);
	
}	


package com.factorymethod;

import com.product.drinks;

public interface drinksfactory {
	public drinks factoryMethod(String style,int type);
}
package com.factorymethod;

import com.product.Cheeseburger;
import com.product.Crabburger;
import com.product.hamburger;

public class CrabburgerFactory implements hamburgerfactory {

	public hamburger factoryMethod(String flavor, int type) {
		// TODO Auto-generated method stub
		return new Crabburger(flavor,type);
			
	}

}

package com.factorymethod;

import com.product.Cola;
import com.product.drinks;

public class ColaFactory implements drinksfactory {

	public drinks factoryMethod(String style, int type) {
		// TODO Auto-generated method stub
		return new Cola(style,type);
	}

}

package com.factorymethod;

import com.product.Coffee;
import com.product.drinks;

public class CoffeeFactory implements drinksfactory {

	public drinks factoryMethod(String style, int type) {
		// TODO Auto-generated method stub
		return new Coffee(style,type);
	}

}

package com.factorymethod;

import com.product.Cheeseburger;
import com.product.hamburger;

public class CheeseburgerFactory implements hamburgerfactory {

	public hamburger factoryMethod(String flavor,int type) {
		// TODO Auto-generated method stub
		return new Cheeseburger(flavor,type);
	}

}

package com.Test;



import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

import org.omg.CosNaming.NamingContextPackage.NotEmpty;

import com.abstractfactory.ConcreteFactory1;
import com.abstractfactory.ConcreteFactory2;
import com.factorymethod.CheeseburgerFactory;
import com.factorymethod.CoffeeFactory;
import com.factorymethod.ColaFactory;
import com.factorymethod.CrabburgerFactory;
import com.factorymethod.drinksfactory;
import com.factorymethod.hamburgerfactory;
import com.product.Cheeseburger;
import com.product.Coffee;
import com.product.Cola;
import com.product.Crabburger;
import com.product.drinks;
import com.product.hamburger;


public class Main {

	/**
	 * @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("H:\\MyEclipseWorkSpace\\KFCmodel\\note.txt"),true);   
			      fw.write(str);   
			      fw.flush();   
			      fw.close();    
			  } catch (IOException e) {   
			      e.printStackTrace();   
			 }   
			   
	}
	
	public static void main(String[] args) {
		
		// TODO Auto-generated method stub、
		//單點漢堡
		/*hamburgerfactory fac=new CheeseburgerFactory();
		Cheeseburger cheese=(Cheeseburger)fac.factoryMethod("辣",2);
		System.out.println(cheese);
		//單點咖啡
		drinksfactory df=new CoffeeFactory();
		Coffee  cs=(Coffee) df.factoryMethod("MilkCoffee",3);
		System.out.println(cs);
	
		
		//用抽象工廠 (套餐)
		ConcreteFactory1 fa=new ConcreteFactory1();
		hamburger ham=fa.hamburgerfactory();
		Cola cl=(Cola)fa.drinksfactory();
		System.out.println(ham);
		System.out.println(cl);*/
		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.蟹黃堡  ¥25  2.芝士漢堡 ¥22.5");
			System.out.println("-----------------------------------");
			
			int foodchoose=scanner.nextInt();
			
			System.out.println("-----------------------------------");
			System.out.println("         請選擇您需要點的飲品          ");
			System.out.println("飲品系列:1.可樂         ¥8   2.咖啡    ¥10");
			System.out.println("-----------------------------------");
			
			int drinkchoose=scanner.nextInt();
		//foodchoose drinkchoose分別代表選擇的漢堡和飲品
		//您點的餐品:
		switch(foodchoose){
		case 1:
				hamburgerfactory fac=new CheeseburgerFactory();
				Cheeseburger cheese=(Cheeseburger)fac.factoryMethod("蟹黃堡",2);
				System.out.println(cheese);
				write(cheese.toString());
				money=money+22.5;
				break;
		case 2:
				hamburgerfactory fac2=new CrabburgerFactory();
				Crabburger crab=(Crabburger)fac2.factoryMethod("芝士漢堡",2);
				System.out.println(crab);
				write(crab.toString());
				money=money+25;
				break;	
		default:
				System.out.println("沒有此類餐品");
		}
		
		switch(drinkchoose){
		case 1:
				drinksfactory fac=new ColaFactory();
				Cola cola=(Cola)fac.factoryMethod("可樂",1);
				System.out.println(cola);
				write(cola.toString());
				money=money+8;
				break;
		case 2:
				drinksfactory fac2=new CoffeeFactory();
				Coffee coffee=(Coffee)fac2.factoryMethod("咖啡",2);
				System.out.println(coffee);
				write(coffee.toString());
				money=money+10;
				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:芝士漢堡+可樂      價格:¥30");
				System.out.println("套餐B:蟹黃堡+咖啡         價格:¥32");
				System.out.println("1.套餐A 2.套餐B");
				System.out.println("-----------------------------------");
				int choose2=scanner.nextInt();
				switch (choose2) {
				case 1:
					ConcreteFactory1 fa=new ConcreteFactory1();
					hamburger ham=fa.hamburgerfactory();
					drinks cl=fa.drinksfactory();
					System.out.println(ham);
					System.out.println(cl);
					write(ham.toString()+"        "+cl.toString());
					System.out.println("請問您有幾折卷");
					int count=scanner.nextInt();
					System.out.println("您本次一共消費"+Money(30, count));
					write("您本次一共消費"+Money(30, count));
					break;

				case 2:
					ConcreteFactory2 fa2=new ConcreteFactory2();
					hamburger ham2=fa2.hamburgerfactory();
					drinks cl2=fa2.drinksfactory();
					System.out.println(ham2);
					System.out.println(cl2);
					write(ham2.toString()+"        "+cl2.toString());
					System.out.println("請問您有幾折卷");
					int count1=scanner.nextInt();
					System.out.println("您本次一共消費"+Money(32, count1));
					write("您本次一共消費"+Money(32, count1));
					break;
				}
				
				
		}
		
		
		
	}

}

結構如下:

在這裡插入圖片描述