1. 程式人生 > >KFC簡單工廠模式實現

KFC簡單工廠模式實現

在這裡插入圖片描述 在這裡插入圖片描述

在這裡插入程式碼片
```package domain;
1.KFC
package domain;

public class KFC {
	private String name;
    private int price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}
2.可樂
package domain;

public class Beverage extends KFC {
	private String name = "可樂";
    private int price=10;

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }
}
3.雞翅\漢堡包\薯條\套餐一\套餐二   類同上面

4.收銀系統
package domain;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Collect {
	private ArrayList<KFC> aa;
	private int money;
	private int yhq = 0;

	public Collect() {

	}

	public void setKfc(ArrayList<KFC> aa) {
		this.aa = aa;
	}

	public void setYhq(int yhq) {
		this.yhq = yhq;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public float judge() throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = sdf.parse("2018-10-27 00:00:00");
		Date end = sdf.parse("2018-10-28 15:00:00");
		Date day=new Date(); 
		
		float i = 0;
		float sum = 0;
		for (KFC kfc : aa) {
			if(day.equals(start)|| day.after(start) && day.equals(end) || day.before(end)){
				sum =(float) ((sum+ kfc.getPrice())*0.8);
			}else{
				sum += kfc.getPrice();
			}
		}
		if (sum >= 100) {
			if (yhq == 50) {
				i = money - sum + 20;
			} else if (yhq == 100) {
				i = money - sum + 30;
			} else {
				i = money - sum;
			}
		} else if (sum >= 50) {
			if (yhq == 50) {
				i = money - sum + 20;
			} else {
				i = money - sum;
			}
		}else {
			i = money - sum;
		}
		System.out.println("找零" + i + "元");
		return i;
	}

	public void print() {
		System.out.println("本次消費如下:");
		int sum = 0;
		StringBuffer s = new StringBuffer();
		for (KFC kfc : aa) {
			sum += kfc.getPrice();
			if (aa.indexOf(kfc) != aa.size() - 1) {
				s.append(kfc.getName() + "---------" + kfc.getPrice() + "\r\n");
			} else {
				s.append(kfc.getName() + "---------" + kfc.getPrice());
			}
		}

		System.out.println(s);
		System.out.println("共消費" + sum + "元");
		if (sum >= 50) {
			System.out.println("***您使用了優惠券滿50減20***" + "\r\n");
		} else if (sum >= 100) {
			System.out.println("***您使用了優惠券滿100減30***" + "\r\n");
		} else {
			System.out.println("***    沒有使用優惠券       ***" + "\r\n");
		}
	}

	public void printXiaopiao() throws IOException, ParseException {
		Date day=new Date();    
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		
		BufferedWriter br = new BufferedWriter(new FileWriter("xiaopiao.txt"));
		br.write("=========================" + "\r\n");
		br.write("-------------------------" + "\r\n");
		br.write("*******歡迎光臨KFC*******" + "\r\n");
		br.write("**"+df.format(day)+"**"+"\r\n"); 
		br.write("*******本次消費如下******" + "\r\n");
		int sum = 0;
		for (KFC kfc : aa) {
			sum += kfc.getPrice();
			if (aa.indexOf(kfc) != aa.size() - 1) {
				br.write("***" + kfc.getName() + "-----------" + kfc.getPrice() + "***" + "\r\n");
			} else {
				br.write("***" + kfc.getName() + "-----------" + kfc.getPrice() + "***" + "\r\n");
			}
		}
		br.write("***消費" + "-----------" + sum + "元" + "***" + "\r\n");
		br.write("***支付" + "-----------" + money + "元" + "***" + "\r\n");
		if (sum >= 50) {
			br.write("***您使用了優惠券滿50減20***" + "\r\n");
		} else if (sum >= 100) {
			br.write("***您使用了優惠券滿100減30***" + "\r\n");
		} else {
			br.write("***    沒有使用優惠券       ***" + "\r\n");
		}
		br.write("***找零" + "-----------" + judge() + "元" + "***" + "\r\n");
		br.write("=========================" + "\r\n");
		br.newLine();
		br.flush();
		br.close();
	}
}
5.方法呼叫
package domain;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Scanner;

public class DOS {
	private ArrayList<KFC> aa = new ArrayList();
    private int yhq = 0;
    public DOS() throws IOException, ParseException{
        zhuJieMain();
    }
    public void zhuJieMain() throws IOException, ParseException{
    	System.out.println("==============================");
    	System.out.println("------------------------------");
        System.out.println("*****    歡迎來到肯德基      *****");
        System.out.println("*****     1.點餐                  *****");
        System.out.println("*****     2.領取優惠券       *****");
        System.out.println("*****     3.結賬                  *****");
        System.out.println("------------------------------");
        System.out.println("==============================");
        System.out.println("請選擇:");
        show();
    }
    
    public void show() throws IOException, ParseException{
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        if(x==1){
            diancan();
        }
        else if(x==2){
            youhuiquan();
        }else if(x==3){
            jieZhang();
        }
        else{
            System.out.println("輸入錯誤");
        }
    }
    public void diancan() throws IOException, ParseException{
		System.out.println("本店退出以下產品:");
		System.out.println("1.漢堡:15/個       2.雞翅:15/個");
		System.out.println("3.薯條:10/個       4.可樂:10/個");
		System.out.println("5.套餐1:漢堡+可樂+雞翅:35/份");
		System.out.println("6.套餐2:漢堡+可樂+薯條:30/份");
		System.out.println("請選擇:");
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		xuanze(x);
    }

    private void xuanze(int x) throws IOException, ParseException {
        KFC a = null;
        Scanner sc = new Scanner(System.in);
        while (true) {
            if (x == 1) {
                a = Factory.creat("漢堡包");
            } else if(x==2){
            	 a = Factory.creat("雞翅");
            }else if (x == 3) {
                a = Factory.creat("薯條");
            }else if (x == 4) {
                a = Factory.creat("可樂");
            }else if (x == 5) {
                a = Factory.creat("套餐1");
            } else if (x == 6) {
                a = Factory.creat("套餐2");
            } else {
                System.out.println("沒有該套餐");
            }
            aa.add(a);
            System.out.println("繼續y,結賬x,主介面n");
            String s = sc.next();
            if (s.equals("N")||s.equals("n")){
                zhuJieMain();
                break;
            }else if(s.equals("Y")||s.equals("y")){
                diancan();
                break;
            }else if (s.equals("X")||s.equals("x")){
                jieZhang();
                break;
            }
        }
    }
    public void youhuiquan() throws IOException, ParseException{
        System.out.println("============================");
        System.out.println("----------------------------");
        System.out.println("*********1.滿50減20*********");
        System.out.println("*********2.滿100減30********");
        System.out.println("============================");
        Scanner sc = new Scanner(System.in);
        System.out.println("請選擇:");
        int x = sc.nextInt();
        if(x==1){
            yhq=50;
        }else if(x==2){
            yhq=100;
        }else{
            System.out.println("沒有該操作");
        }
        System.out.println("領取優惠券"+yhq+"元成功");
        System.out.println("返回Y");
        String s = sc.next();
        if (s.equals("Y")||s.equals("y")){
            zhuJieMain();
        }else{
            System.out.println("你還不想結賬?");
        }
    }
    public void jieZhang() throws IOException, ParseException{
        Scanner sc = new Scanner(System.in);
        Collect c = new Collect();
        c.setKfc(aa);
        c.setYhq(yhq);
        c.print();
        System.out.println("請付款:");
        int x1 = sc.nextInt();
        c.setMoney(x1);
        c.printXiaopiao();
    }
}
6.測試類
package Demo;

import java.io.IOException;
import java.text.ParseException;

import domain.DOS;

public class demo {
	public static void main(String[]args) throws IOException, ParseException{
	       DOS a= new DOS();
	   }
}
歡迎指點.....