📄 sell.java
字号:
return "鸡排";
case Corn:
return "玉米";
default:
return "";
}
}
}
enum Eum_SeaFood
{
Conch("201", 50), // 扇贝
Lobster("202", 120), // 龙虾
Crab("203", 150); // 螃蟹
String id;
double price;
Eum_SeaFood(String id, double price)
{
this.id = id;
this.price = price;
}
public String toString()
{
switch (this)
{
case Conch:
return "扇贝 ";
case Lobster:
return "龙虾";
case Crab:
return "螃蟹";
default:
return "";
}
}
}
enum Eum_WineFood
{
Aquavit("301", 30), // 白兰地
Champagne("302", 35), // 香槟酒
Beer("303", 10); // 啤酒
String id;
double price;
Eum_WineFood(String id, double price)
{
this.id = id;
this.price = price;
}
public String toString()
{
switch (this)
{
case Aquavit:
return "白兰地 ";
case Champagne:
return "香槟酒";
case Beer:
return "啤酒";
default:
return "";
}
}
}
// 食品对象工厂
class Factory
{
public static FoodStuff createNormalFood(String id, String name,double price)
{
return new NormalFood(id, name, price);
}
public static FoodStuff createSeaFood(String id, String name, double price)
{
return new SeaFood(id, name, price);
}
public static FoodStuff createWineFood(String id, String name, double price)
{
return new WineFood(id, name, price);
}
public static FoodStuff createFood(String id)
{
char flag = id.charAt(0);
switch (flag)
{
case '1':
return createNormalFoodById(id);
case '2':
return createSeaFoodById(id);
case '3':
return createWineFoodById(id);
default:
return null;
}
}
private static FoodStuff createNormalFoodById(String id)
{
for (Eum_NormalFood normal : Eum_NormalFood.values())
{
if (normal.id.equals(id))
{
return createNormalFood(normal.id, normal.toString(),
normal.price);
}
}
return null;
}
private static FoodStuff createSeaFoodById(String id)
{
for (Eum_SeaFood sea : Eum_SeaFood.values())
{
if (sea.id.equals(id))
{
return createSeaFood(sea.id, sea.toString(), sea.price);
}
}
return null;
}
private static FoodStuff createWineFoodById(String id)
{
for (Eum_WineFood wine : Eum_WineFood.values())
{
if (wine.id.equals(id))
{
return createWineFood(wine.id, wine.toString(), wine.price);
}
}
return null;
}
}
interface Input
{
public Map<String, Integer> getConsumeINF(Menu menu);
public String getDiscountINF();
public boolean getContinueState();
}
interface Output
{
public void prtBill(List<FoodStuff> consumeList, double account,
Map<String, Double> discountMap, double payment);
public void prtMenu(Map<String, FoodStuff> menuMap) ;
}
class BillIO implements Input, Output
{
public void prtBill(List<FoodStuff> consumeList, double account,
Map<String, Double> discountMap, double payment)
{
prtBillTitle();
prtCousumeContent(consumeList);
prtAccount(account);
prtBillDiscount(discountMap);
prtPayment(payment);
}
public void prtMenu(Map<String, FoodStuff> menuMap) {
final String MSG="菜 单:";
prtInformation(MSG);
for (String str : menuMap.keySet()) {
FoodStuff food = menuMap.get(str);
prtInformation(food.getId(),food.getName(),food.getPrice());
}
}
public Map<String, Integer> getConsumeINF(Menu menu)
{
Map<String, Integer> inputMap;
final String errMsg = "消费食品的代码不正确,请重新输入!";
while (true)
{
inputMap = getInputMap(menu);
if (inputMap != null)
return inputMap;
prtInputErrMsg(errMsg);
continue;
}
}
public String getDiscountINF()
{
final String MSG1 = "您享受进餐等待优惠吗?(Y/N)";
final String MSG2 = "您享受VIP优惠吗?(Y/N)";
StringBuffer sbf = new StringBuffer();
sbf.append(getState(MSG1));
sbf.append(" ");
sbf.append(getState(MSG2));
sbf.append(" ");
sbf.append(getToken());
return sbf.toString();
}
public boolean getContinueState()
{
final String MSG = "继续计算食品消费吗?(Y/N)";
prtWhiteRow();
return (getState(MSG) == 1) ? true : false;
}
private void prtBillTitle()
{
final String MSG = "您消费了以下的食品:";
prtInformation(MSG);
}
private void prtCousumeContent(List<FoodStuff> consumeList)
{
for (FoodStuff food : consumeList)
{
prtInformation(food.getId(), food.getName(), food.getPrice());
}
}
private void prtAccount(double account)
{
final String MSG = "您消费的总金额";
prtInformation(MSG, account);
}
private void prtBillDiscount(Map<String, Double> discountMap)
{
final String MSG = "您享受的折扣如下:";
prtInformation(MSG);
for (String str : discountMap.keySet())
{
prtInformation(str, discountMap.get(str));
}
}
private void prtPayment(double payment)
{
final String MSG = "你需要支付的金额";
prtInformation(MSG, payment);
}
private Map<String, Integer> getInputMap(Menu menu)
{
Map<String, Integer> consumeMap = new HashMap<String, Integer>();
Map<String,FoodStuff> menuMap=menu.getMenuMap();
Scanner sc = new Scanner(getInputStr());
while (sc.hasNext())
{
String str = sc.next();
String id = str.substring(0, 3);
if (!menuMap.containsKey(id))
{
return null;
}
Integer num = new Integer(str.substring(3));
if (consumeMap.get(id) == null)
{
consumeMap.put(id, num);
}
else
{
consumeMap.put(id, consumeMap.get(id) + num);
}
}
return consumeMap;
}
private String getInputStr()
{
final String pattern = "\\d{5}|\\d{5}(\\s+\\d{5})+";
final String errMsg = "消费食品的输入格式不正确,请重新输入!";
String inputStr;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
prtInputMsg();
try
{
inputStr = br.readLine();
if (inputStr.trim().matches(pattern))
return inputStr;
else
{
prtInputErrMsg(errMsg);
continue;
}
}
catch (IOException e)
{
e.getStackTrace();
}
}
}
private int getState(String msg)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
System.out.println(msg);
try
{
String state = br.readLine();
if (state.equals("Y") || state.equals("y"))
{
return 1;
}
if (state.equals("N") || state.equals("n"))
{
return 0;
}
}
catch (IOException e)
{
e.getStackTrace();
}
continue;
}
}
private double getToken()
{
final String MSG = "您使用代金券的金额:(整数)";
while (true)
{
prtInformation(MSG);
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String str = br.readLine();
if (str.matches("[0-9]*"))
{
if (str.equals(""))
return 0;
return new Double(str).doubleValue();
}
continue;
}
catch (IOException e)
{
e.getStackTrace();
}
}
}
private void prtInputMsg()
{
final String InputNotice1 = "消费信息输入格式:xxxxx(前三位是消费品编号,后两位是消费品数量);不同种类的消费品间用一个空格分隔;";
final String InputNotice2 = "消费的信息输入:";
prtInformation(InputNotice1);
prtInformation(InputNotice2);
}
private void prtInformation(String id, String name, double price)
{
System.out.println(id + " " + name + " " + price);
}
private void prtInformation(String msg, double discount)
{
System.out.println(msg + ":" + discount);
}
private void prtInformation(String msg)
{
System.out.println(msg);
}
private void prtInputErrMsg(String errMsg)
{
prtInformation(errMsg);
prtWhiteRow();
}
private void prtWhiteRow()
{
System.out.println(" ");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -