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