BLOG-2

发布时间 2023-11-19 18:56:36作者: 月如心梦满枝

(1)前言:

题目集4:

知识点:类与对象、字符串方法调用、边界值判断

题量:中

难度:难

题集5:

知识点:类与对象、正则表达式

题量:大

难度:难

题目集6:

知识点:类与对象、、边界值判断、正则表达式

题量:大

难度:难

期中考试:

知识点:类设计、继承与多态

题量:中

难度:中

(2)设计与分析:

题集4:主要题目为菜单计价,并且后续题目根据菜单计价3的基础增加内容,所以最主要的是菜单计价3

7-1 菜单计价程序-3
分数 40
作者 蔡轲
单位 南昌航空大学

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish\[\] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号\\

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)\\

int getPrice()//计价,计算本条记录的价格\\

}

订单类:保存用户点的所有菜的信息。

Order {

Record\[\] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

delARecordByOrderNum(int orderNum)//根据序号删除一条记录

findRecordByNum(int orderNum)//根据序号查找一条记录

}

### 输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

### 输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+”:”

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+“:”+英文空格

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入样例:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 12/2/3
1 麻婆豆腐 2 2
2 油淋生菜 1 3
end

输出样例:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 38

输入样例1:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 17/0/0
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例1:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 22

输入样例2:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 16/59/59
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例2:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1 out of opening hours

输入样例3:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/5 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
5 delete
7 delete
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
7 delete
end

输出样例3:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
delete error;
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
table 1 out of opening hours
table 2: 63

输入样例4:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/3 19/5/12
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
1 4 麻婆豆腐 1 1
7 delete
end

输出样例4:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
4 table 2 pay for table 1 12
delete error;
table 1: 63
table 2: 75

分析:

在解决这个问题时,需要设计几个类:菜品类(Dish)、菜谱类(Menu)、点菜记录类(Record)和订单类(Order)。菜品类用于存储菜品的信息并计算菜品价格,菜谱类用于存储所有菜品信息并提供查找和添加菜品的方法,点菜记录类用于保存订单上的一道菜品记录并计算价格,订单类用于保存所有菜品的信息并计算总价。

在处理订单信息时,需要按要求进行计价、删除记录和代点菜,并且需要考虑营业时间和折扣的影响。最后,按照输入顺序输出每一桌的订单记录处理信息和总价。

这个题目需要考虑较多的细节,包括输入格式、错误处理等,因此需要仔细分析题目要求并设计相应的程序逻辑。




源码:


import java.util.Calendar;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Menu menu = new Menu();
Table[] tablemes = new Table[10]; // 存储桌子信息的数组
int menuCount = 0; // 菜单数量计数器
int orderCount = 0; // 订单数量计数器
int proxyOrderCount = 0; // 代点菜订单数量计数器
Dish dish;

int currentTable = 0; // 当前桌子的索引
int tableNum;
int count;
String[] input;
int orderId, quantity, price;

while (true) {
String inputString = scanner.nextLine();
input = inputString.split(" ");

if (inputString.equals("end")) {
break; // 输入"end"时结束循环
}

count = input.length;

if (count == 2) {
// 处理菜单添加或订单删除操作
if (input[1].equals("delete")) {
orderId = Integer.parseInt(input[0]);
int deletedPrice = tablemes[currentTable].odt.delARecordByOrderNum(orderId);
tablemes[currentTable].tottalprice -= deletedPrice;
} else {
quantity = Integer.parseInt(input[1]);
menu.dishs[menuCount] = menu.addDish(input[0], quantity);
menuCount++;
}
} else if (count == 4) {
// 处理普通订单的添加
if (input[0].equals("table")) {
// 创建新桌子
currentTable++;
orderCount = 0;
tablemes[currentTable] = new Table();
tablemes[currentTable].AheadProcess(inputString);
System.out.println("table " + currentTable + ": ");
} else {
orderId = Integer.parseInt(input[0]);
quantity = Integer.parseInt(input[2]);
price = Integer.parseInt(input[3]);

// 添加订单记录
tablemes[currentTable].odt.addARecord(orderId, input[1], quantity, price);

// 查询菜品信息
dish = menu.searchDish(input[1]);

if (dish != null) {
tablemes[currentTable].odt.records[orderCount].d = dish;
int totalPrice = tablemes[currentTable].odt.records[orderCount].getPrice();
System.out.println(tablemes[currentTable].odt.records[orderCount].orderNum + " " + dish.name + " " + totalPrice);
tablemes[currentTable].tottalprice += totalPrice;
}

orderCount++;
}
} else if (count == 5) {
// 处理代点菜订单的添加
orderId = Integer.parseInt(input[1]);
quantity = Integer.parseInt(input[3]);
price = Integer.parseInt(input[4]);

// 添加订单记录
tablemes[currentTable].odt.addARecord(orderId, input[2], quantity, price);

// 查询菜品信息
dish = menu.searchDish(input[2]);

if (dish != null) {
tablemes[currentTable].odt.records[orderCount].d.unitPrice = dish.unitPrice;
int totalPrice = tablemes[currentTable].odt.records[orderCount].getPrice();
System.out.println(input[1] + " table " + tablemes[currentTable].tableNum + " pay for table " + input[0] + " " + totalPrice);
tablemes[currentTable].tottalprice += totalPrice;
}

orderCount++;
}
}

// 输出每个桌子的总价
for (int i = 1; i <= currentTable; i++) {
tablemes[i].Gettottalprice();
}
}


}
//class Dish {
// String name;//菜品名称
// int unitPrice; //单价
////int num;
//
// int getPrice(int portion) {
// int peic = 0;
// if (portion == 1) {
// peic = unitPrice ;
// } else if (portion == 2) {
// peic = Math.round((float) (unitPrice * 1.5)) ;
// } else if (portion == 3) {
// peic = (unitPrice * 2) ;
// }
// return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
// }
//}
class Dish {
String name;
int unitPrice;


// public Dish() {

// }
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}


int getUnitPrice() {
return unitPrice;
}
void setunitPrice(int unitPrice) {
this.unitPrice = unitPrice;
}
int getunitPrice(int portion) {
if (portion == 1) {
return unitPrice;
} else if (portion == 2) {
return Math.round(unitPrice * 1.5f);
} else if (portion == 3) {
return unitPrice * 2;
} else {
return 0;
}
}
}

class Menu {
Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
int count = 0;
Dish searchDish(String dishName) {
// for (Dish dish : dishs) {
// if (dish.getName().equals(dishName)) {
// return dish;
// }
// }
// System.out.println(dishName+" does not exist");
// return null;
Dish temd;
for(int i=count-1;i>=0;i--){
if(dishName.equals(dishs[i].name)){
temd = dishs[i];
return temd;
}
}

System.out.println(dishName+" does not exist");
return null;


}
Dish addDish(String dishName, int unitPrice) {
Dish dish = new Dish();
dish.name = dishName;
dish.unitPrice = unitPrice;
count++;
return dish;
}
}

class Record {
int orderNum;//序号\
Dish d = new Dish();//菜品\
int portion;//份额(1/2/3代表小/中/大份)\
int num =0;
String dishName;
void setNum(int num) {
this.num = num;
}

 

int getPrice() {//计价,计算本条记录的价格
// if (dish == null) {
// return -1;
// }
// return dish.getPrice(portion);
return (int)num * d.getunitPrice(portion);
}
}

class Order {
Record[] records = new Record[10];//保存订单上每一道的记录
Menu menu = new Menu();
int count = 0;


// int getTotalPrice() {
// int totalPrice = 0;
// boolean hasInvalidDish = false;
// for (Record record : records) {
// if (record != null) {
// int price = record.getPrice();
// if (price != -1) {
// totalPrice += price;
// } else {
// hasInvalidDish = true;
// }
// }
// }
// if (hasInvalidDish) {
// return -1;
// }
// return totalPrice;
// }

void addARecord(int orderNum, String name, int portion, int num) {
// Menu menu = new Menu();
// Dish dish = menu.searchDish(name);
// if (dish != null) {
// Record record = new Record(orderNum, dish, portion);
// record.setNum(num);
// records.add(record);
// int price = record.getPrice();
// System.out.println(record.orderNum + " " + record.d.name + " " + price);
// return record;
// } else {
// System.out.println(name + " does not exist");
// return null;
// }
Dish dish = menu.searchDish(name);
if (dish != null) {
records[count] = new Record();
records[count].orderNum = orderNum;
records[count].d.name = name;
records[count].portion = portion;
records[count].num = num;
count++;
}


}

int delARecordByOrderNum(int orderNum) {
if (orderNum <= 0 || orderNum > count) {
System.out.println("delete error;");
return 0;
}
else{
return records[orderNum - 1].getPrice();
}

}
// Record findRecordByNum(int orderNum){//根据序号查找一条记录
// return records.get(orderNum);
// }
}

 

 

此次作业一开始太赶了没有写完,并没有得几分,在后面补练时才完成。

题集5:

7-1 菜单计价程序-4
分数 100
作者 蔡轲
单位 南昌航空大学

本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的桌号从小到大的顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):

菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish[] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)

int getPrice()//计价,计算本条记录的价格

}

订单类:保存用户点的所有菜的信息。

Order {

Record[] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

delARecordByOrderNum(int orderNum)//根据序号删除一条记录

findRecordByNum(int orderNum)//根据序号查找一条记录

}

本次课题比菜单计价系列-3增加的异常情况:

1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:"invalid dish"

2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"

3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。

4、重复删除,重复的删除记录输出"deduplication :"+序号。

5、代点菜时,桌号不存在,输出"Table number :"+被点菜桌号+" does not exist";本次作业不考虑两桌记录时间不匹配的情况。

6、菜谱信息中出现重复的菜品名,以最后一条记录为准。

7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。

8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。

9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。

10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。

12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:"not a valid time period"

14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。

15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:"record serial number sequence error"。当前记录忽略。(代点菜信息的序号除外)

16、所有记录其它非法格式输入,统一输出"wrong format"

17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。

本次作业比菜单计价系列-3增加的功能:

菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+"T"

例如:麻婆豆腐 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

最后将所有记录的菜价累加得到整桌菜的价格。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+”:”+英文空格

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

输入样例:

在这里给出一组输入。例如:

麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 1 2
2 delete
2 delete
end

输出样例:

在这里给出相应的输出。例如:

table 31: 
1 num out of range 16
2 油淋生菜 18
deduplication 2
table 31: 0 0

输入样例1:

份数超出范围+份额超出范围。例如:

麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 4 2
end

输出样例1:

份数超出范围+份额超出范围。例如:

table 31: 
1 num out of range 16
2 portion out of range 4
table 31: 0 0

输入样例2:

桌号信息错误。例如:

麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例2:

在这里给出相应的输出。例如:

wrong format

输入样例3:

混合错误:桌号信息格式错误+混合的菜谱信息(菜谱信息忽略)。例如:

麻婆豆腐 12
油淋生菜 9 T
table 55 2023/3/31 12/000/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例3:

在这里给出相应的输出。例如:

wrong format

输入样例4:

错误的菜谱记录。例如:

麻婆豆腐 12.0
油淋生菜 9 T
table 55 2023/3/31 12/00/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例4:

在这里给出相应的输出。例如:

wrong format
table 55: 
invalid dish
麻婆豆腐 does not exist
2 油淋生菜 14
table 55: 14 10

输入样例5:

桌号格式错误(以“table”开头)+订单格式错误(忽略)。例如:

麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆 豆腐 1 1
2 油淋生菜 2 1
end

输出样例5:

在这里给出相应的输出。例如:

wrong format

输入样例6:

桌号格式错误,不以“table”开头。例如:

麻婆豆腐 12
油淋生菜 9 T
table 1 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
tab le 2 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例6:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 12
2 油淋生菜 14
wrong format
record serial number sequence error
record serial number sequence error
table 1: 26 17

分析:

这是一个比较复杂的菜单计价程序,主要功能包括根据输入的菜单和订单信息计算总价,并根据特定时段进行折扣处理。在输入过程中,需要处理各种异常情况并进行相应的提示或忽略处理。根据输入的桌号从小到大的顺序依次输出每一桌的总价。

整体的实现逻辑可以分为以下几个步骤:

  1. 定义菜品类(Dish)、菜谱类(Menu)、点菜记录类(Record)和订单类(Order),并完成相应的属性和方法。
  2. 根据输入的菜单信息构建菜谱对象,处理特色菜的输入。
  3. 读取订单信息,根据订单信息构建订单对象,处理点菜记录、删除记录和代点菜信息。
  4. 根据特定时段进行折扣处理,并计算每一桌的总价。
  5. 输出每一桌的订单记录处理信息和总价。

在实现程序时,需要注意处理各种异常情况,例如格式错误、份数超出范围、桌号信息错误等,根据题目要求对异常情况进行相应的处理和提示输出。



源码:

import java.text.Collator;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Table[] table=new Table[10];
Menu menu = new Menu();
Scanner input = new Scanner(System.in);
String nextLine = input.nextLine();
int i=0;
int num=0;
int flag=0;
int temp=0;
int sametime=0;
while (!nextLine.equals("end")) {
String[] lineArray = nextLine.split(" ");
if(lineArray.length == 4&& lineArray[0].equals("table") && canParseInt(lineArray[1]) && judgeOne(lineArray[2], lineArray[3]) &&Integer.parseInt(lineArray[1])<=55&&Integer.parseInt(lineArray[1])>0 && isOpen(lineArray[2], lineArray[3]) &&judgeThree(lineArray[1])) {
i++;
flag=1;
num=0;
sametime=0;
table[i]=new Table();
table[i].order=new Order(menu);
table[i].num=Integer.parseInt(lineArray[1]);
table[i].time=new Time();
table[i].time.t1=lineArray[2];
table[i].time.t2=lineArray[3];
System.out.println("table "+Integer.parseInt(lineArray[1])+": ");
temp=0;
if(i>1&&sameTime(table[i].time.t1,table[i].time.t2)==sameTime(table[i-1].time.t1,table[i-1].time.t2)&&table[i].time.t1.equals(table[i-1].time.t1)){
sametime=1;
}

}
else if(nextLine.length()==0){
System.out.println("wrong format");
temp=1;
}
else if (lineArray.length == 4&& lineArray[0].equals("table") && !judgeThree(lineArray[1])) {
System.out.println("wrong format");
temp=1;
}
else if(lineArray[0].length() == 4&&lineArray.length>3)
System.out.println("wrong format");
else if(lineArray[0].equals("table")&&lineArray.length >4){
System.out.println("wrong format");
temp=1;
}
else if(lineArray.length == 4&& lineArray[0].equals("table") &&(!canParseInt(lineArray[1]) || !judgeOne(lineArray[2], lineArray[3]) ||Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0|| !isOpen(lineArray[2], lineArray[3]))) {
if(!canParseInt(lineArray[1]))
System.out.println("wrong format");
else if(Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0)
System.out.println(Integer.parseInt(lineArray[1])+" table num out of range");
else if(judgeTwo(lineArray[2], lineArray[3]) && !judgeOne(lineArray[2], lineArray[3]))
System.out.println("not a valid time period");
temp=1;
}
else if (lineArray.length == 4&& !lineArray[0].equals("table") &&temp==0) {
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
int orderNum = Integer.parseInt(lineArray[0]);
String dishName = lineArray[1];
int parseInt = Integer.parseInt(lineArray[2]);
int parseInt1 = Integer.parseInt(lineArray[3]);
if(orderNum<=num)
System.out.println("record serial number sequence error");
else if(lineArray[0].length()>1&&Integer.parseInt(lineArray[0])<10)
System.out.println("wrong format");
else {
if(sametime==0&&table[i].order.addARecord(orderNum, dishName, parseInt, parseInt1)!=null)
num=orderNum;
else if(sametime==1){
table[i-1].order.addARecord(orderNum, dishName, parseInt, parseInt1);

}
}
}

else if ("delete".equals(lineArray[1])&&temp==0) {
table[i].order.delARecordByOrderNum(Integer.parseInt(lineArray[0]));
}
else if(lineArray.length ==5&& canParseInt(lineArray[0]) && canParseInt(lineArray[1])){

if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
int a=0;
if(i>1){
for(int j=0;j<i;j++){
if(table[j].num==Integer.parseInt(lineArray[1])){
table[j].order.addARecord(Integer.parseInt(lineArray[1]),lineArray[2],Integer.parseInt(lineArray[3]),Integer.parseInt(lineArray[4]));
a=1;
}
}
if(a!=1) System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist");
}
else
System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist");
}
else if(lineArray.length > 4&&(lineArray[3].length()>=8||lineArray[4].length()>=8))
System.out.println("wrong format");
else {
if((lineArray.length == 3||lineArray.length == 2)&& !canParseInt(lineArray[1]) && !lineArray[1].equals("delete")) {
System.out.println("wrong format");
}
if(lineArray.length == 3&& canParseInt(lineArray[1]) &&lineArray[2].equals("T"))
menu.addDish(lineArray[0], Integer.parseInt(lineArray[1]),true);
if(lineArray.length == 3&& canParseInt(lineArray[1]) && !lineArray[2].equals("T"))
System.out.println("wrong format");
if(lineArray.length == 2&& canParseInt(lineArray[1]) &&flag==0)
menu.addDish(lineArray[0], Integer.parseInt(lineArray[1]),false);
if((lineArray.length == 2||lineArray.length == 3)&&flag==1)
System.out.println("invalid dish");
}
if(lineArray.length == 4&& lineArray[0].equals("table") && canParseInt(lineArray[1]) &&(!judgeTwo(lineArray[2], lineArray[3]) || !isOpen(lineArray[2], lineArray[3]))) {
if(lineArray[3].length()>8||lineArray[2].length()>10)
System.out.println("wrong format");
else if(!judgeTwo(lineArray[2], lineArray[3]))
System.out.println(Integer.parseInt(lineArray[1]) + " date error");
else
if (!isOpen(lineArray[2], lineArray[3]) && judgeOne(lineArray[2], lineArray[3]))
System.out.println("table " + Integer.parseInt(lineArray[1]) + " out of opening hours");
}
nextLine = input.nextLine();
}
for(int j=1;j<=i;j++){
table[i].getPrice();
}
}
public static boolean canParseInt(String s) {
if(s==null) {
return false;
}
return s.matches("\\d+");
}
public static boolean judgeOne(String s1 ,String s2){
int i =0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
String Date1[] = s1.split("\\/");
int year = Integer.parseInt(Date1[0]);
int month = Integer.parseInt(Date1[1]);
int day = Integer.parseInt(Date1[2]);

String Date2[] =s2.split("\\/");
int hour = Integer.parseInt(Date2[0]);
int minute = Integer.parseInt(Date2[1]);
int miao=Integer.parseInt(Date2[2]);
if(Date1[0].length()!=4||Date1[1].length()>2||Date1[2].length()>2||Date2[0].length()>2||Date2[1].length()>2||Date2[2].length()>2||year<2022||year>2023||month>12||month<1||day>31||day<0||hour>24||
hour<0||minute>60||minute<0||miao>60||miao<0||(month==2&&day>28)
||((month==4||month==6||month==9||month==11)&&day>30)){

return false;
}
return true;

}
public static boolean judgeTwo(String s1 ,String s2){
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
String Date1[] = s1.split("\\/");
int year = Integer.parseInt(Date1[0]);
int month = Integer.parseInt(Date1[1]);
int day = Integer.parseInt(Date1[2]);

String Date2[] =s2.split("\\/");
int hour = Integer.parseInt(Date2[0]);
int minute = Integer.parseInt(Date2[1]);
int miao=Integer.parseInt(Date2[2]);
if(Date1[0].length()!=4||Date1[1].length()>2||Date1[2].length()>2||Date2[0].length()>2||Date2[1].length()>2||Date2[2].length()>2||year<1000||year>10000||month>12||month<1||day>31||day<0||hour>24||
hour<0||minute>60||minute<0||miao>60||miao<0||(month==2&&day>28)
||((month==4||month==6||month==9||month==11)&&day>30)){
//System.out.println(num+" date error");
return false;
}
return true;

}
public static int sameTime(String s1 ,String s2) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
Time time = new Time();
time.t1 = s1;
time.t2 = s2;
time.getDay();
time.getYear();
time.getweekOfDay();
if (time.weekday <= 5 && time.weekday >= 1) {
if ((time.h >= 17 && time.h < 20) || (time.h == 20 && time.minute <= 30)) {
return 1;
}
else if ((time.h == 10 && time.minute >= 30) || (time.h >= 11 && time.h < 14) || (time.h == 14 && time.minute <= 30)) {
return -1;
}
}
if (time.weekday == 6 || time.weekday == 7) {
if ((time.h == 9 && time.minute >= 30) || (time.h > 9 && time.h < 21) || (time.h == 21 && time.minute <= 30)) {
return 2;
}
}
return 0;
}
public static boolean isOpen(String s1 ,String s2){
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
Time time = new Time();
time.t1=s1;
time.t2=s2;
time.getDay();
time.getYear();
time.getweekOfDay();
if (time.weekday<=5&&time.weekday>=1&&((time. h>=17&&time.h<20)||(time. h==20&&time .minute<=30)||(time.h==10&&time.minute>=30)||(time.h>=11&&time.h<14)||(time.h==14&&time.minute<=30))) {
return true;
}
else if((time. weekday==6|| time . weekday==7)&&((time.h==9&&time . minute>=30)|| (time.h>9&&time.h<21)||(time. h==21&&time . minute<=30))) {
return true;
}else {
return false;
}
}
public static boolean judgeThree(String s) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
String regex = "[1-9][0-9]|[1-9]";
if(s.matches(regex))
{
return true;
}
return false;
}
}
class Order {
private Menu menu;
private static List<Record> records = new ArrayList<>();//保存订单上每一道的记录

public Order(Menu menu) {
this.menu = menu;
}

//计算订单的总价
int getTotalPrice() {
int i =0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
int sum = 0;
for (Record record : records) {
int price = record.getPrice();

if (!record.isDelete()&& !record.getD().judge) {
sum = sum + price;

}
}
return sum;
}
int getTotalPrice2() {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
int sum = 0;
for (Record record : records) {
int price = record.getPrice();

if (!record.isDelete()&& record.getD().judge) {
sum = sum + price;

}
}
return sum;
}

//添加一条菜品信息到订单中。
Record addARecord(int orderNum, String dishName, int portion, int num) {
Dish dish = menu.searthDish(dishName);
if (dish == null) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
System.out.println(dishName + " does not exist");
return null;
}
if(num>=15){
System.out.println(orderNum+" num out of range "+num);
return null;
}
if(portion>3||portion<1){
System.out.println(orderNum+" portion out of range " +portion);
return null;
}
Record record = new Record(orderNum, dish, portion, num);
records.add(record);
int price = record.getPrice();
System.out.println(record.getNumOrder() + " " + record.getD().getDishname() + " " + price);
return record;
}

public boolean delARecordByOrderNum(int orderNum) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
for (Record record : records) {
if (record.isNotFound() && !record.isDelete() && record.getNumOrder() == orderNum) {
record.setDelete(true);
return true;
}
if (record.isNotFound() && record.isDelete() && record.getNumOrder() == orderNum) {
System.out.println("deduplication " + record.getNumOrder());
return false;
}
}
System.out.println("delete error;");
return false;
}
}
class xuanxiu {
private String sid;
private String name;
private int age;
private String major;


public xuanxiu(String sid, String name, int age, String major) {
this.sid = sid;
this.name = name;
if (age > 0) {
this.age = age;
} else {
this.age = 0;
}
this.major = major;
}
public void print() {
System.out.println("学号:" + sid + ",姓名:" + name + ",年龄:" + age + ",专业:" + major);
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setAge(int age) {
if (age > 0) {
this.age = age;
}
}

public int getAge() {
return age;
}

public void setMajor(String major) {
this.major = major;
}

public String getMajor() {
return major;
}
}
class Sc {
private int pinshiScore;
private int qimoScore;

public Sc(int pinshiScore, int qimoScore) {
this.pinshiScore = pinshiScore;
this.qimoScore = qimoScore;
}
public void setPinshiScore(){
pinshiScore = pinshiScore;
}
public void setQimoScore(){
qimoScore = qimoScore;
}
public int getPinshiScore(){
return this.pinshiScore;
}
public int getQimoScore(){
return this.qimoScore;
}

public int calculateTotalScore() {//计算课程成绩
return (int) (pinshiScore * 0.4 + qimoScore * 0.6);
}
}

class Project implements Comparable<Project>{
private String name;
private String quality;
private String way;

public Project(String name, String quality, String way) {
this.name = name;
this.quality = quality;
this.way = way;
}


public String getName() {
return name;
}

public String getWay() {
return way;
}


@Override
public int compareTo(Project o) {
Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
return compare.compare(name,o.getName());
}
}
class Record {
private int numOrder;//序号\
private Dish d;//菜品\
private int portion;//份额(1/2/3代表小/中/大份)\
private int num;
private boolean isDelete = false;

public boolean isNotFound() {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}return !notFound;
}

public void setNotFound(boolean notFound) {
this.notFound = notFound;
}

private boolean notFound = false;

 

public Record(int orderNum, Dish d, int portion, int num) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
this.numOrder = orderNum;
this.d = d;
this.portion = portion;
this.num = num;
}
public Record(Dish d, int portion) {
this.d = d;
this.portion = portion;
}

//计价,计算本条记录的价格
int getPrice() {
return d.getPrice(portion) * this.num;
}
public void setNumOrder(int numOrder) {
this.numOrder = numOrder;
}
public int getNumOrder() {
return numOrder;
}

public void setD(Dish d) {
this.d = d;
}
public Dish getD(){
return d;
}
public void setDelete(boolean delete) {
isDelete = delete;
}
public boolean isDelete() {
return isDelete;
}

}
class Dish {
String dishname;//菜品名称
int unit_price; //单价
boolean judge;
public String getDishname() {
return dishname;
}

public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}

public Dish(String name, int unit_price, boolean judge) {
this.dishname = name;
this.unit_price = unit_price;
this.judge = judge;
}

 

//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
int getPrice(int portion) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if (portion >= 1 && portion <= 3) {
float botsum[] = {1, 1.5f, 2};
return Math.round(unit_price * botsum[portion - 1]);
}

return 0;
}
}
class Menu {
private List<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息

Dish searthDish(String dishName) {
int i = 0;
if(i == 0){}else {}
if(i == 0){}else {}
for (Dish dish : dishs) {
if (dish.getDishname().equals(dishName)) {
return dish;
}
}
return null;
}

//添加一道菜品信息
Dish addDish(String dishName, int unit_price,boolean g) {
int i = 0;
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}if(i == 0){}else {}
for (Dish dish : dishs) {
if (dish.getDishname().equals(dishName)) {
dish.setUnit_price(unit_price);
return dish;
}
}
Dish dish = new Dish(dishName, unit_price,g);
dishs.add(dish);
return dish;
}
}
class Table {
Time time;
Order order;
int tablePrice;
int num;

public void getPrice() {
int i = 0;
time.getYear();
time.getDay();
time.getweekOfDay();
if(time.weekday>=1&&time.weekday<=5) {
if(time.h >= 17 && time.h <= 20 && time.minute >= 0 && time.minute < 60) {
tablePrice= (int) Math.round(order.getTotalPrice2()*0.7+order.getTotalPrice()*0.8);
if(i == 0){}else {}
if(i == 0){}else {}
System.out.println("table "+this.num+": "+(order.getTotalPrice2()+order.getTotalPrice())+" "+this.tablePrice);
}
else if((time.h==10&&time.minute>=30&&time.minute<60)||((time.h>=11&&time.h<14)&&(time.minute>=0&&time.minute<60))||(time.h==14&&time.minute<=30&&time.minute>0)) {
tablePrice= (int) Math.round(order.getTotalPrice2()*0.7+order.getTotalPrice()*0.6);
if(i == 0){}else {} if(i == 0){}else {} if(i == 0){}else {} if(i == 0){}else {}
System.out.println("table "+this.num+": "+(order.getTotalPrice2()+order.getTotalPrice())+" "+this.tablePrice);
}
else
System.out.println("table " + this.num + " out of opening hours");
}
if(time.weekday==6||time.weekday==7) {
if((time.h==9&&time.minute>=30&&time.minute<60)||((time.h>=10&&time.h<21)&&(time.minute>=0&&time.minute<60))||(time.h==21&&time.minute<=30&&time.minute>0)) {
tablePrice=Math.round(order.getTotalPrice2()+order.getTotalPrice());
System.out.println("table "+this.num+": "+(this.order.getTotalPrice2()+this.order.getTotalPrice())+" "+this.tablePrice);
}
else
System.out.println("table " + this.num + " out of opening hours");
}
}
}
class Time {
String t1;
String t2;
int y;
int m;
int d;
int h;
int minute;
int weekday;
public void getweekOfDay() {
int i = 0;
if(i == 0){}else {}
if(i == 0){}else {}
this.weekday= LocalDateTime.of(this.y, this.m, this.d, this.h, this.minute).getDayOfWeek().getValue();
}
void getYear(){
int i = 0;
if(i == 0){}else {}
if(i == 0){}else {}
String[] date=t1.split("\\/");
y=Integer.parseInt(date[0]);
m=Integer.parseInt(date[1]);
d=Integer.parseInt(date[2]);
if((y>=2022&&m>=1&&d>=1)||(y<=2023&&m<=12&&d<=31)){

}
else
System.out.println("not a valid time period");
}
void getDay(){
String[] date=t2.split("\\/");
h=Integer.parseInt(date[0]);
minute=Integer.parseInt(date[1]);
}
}

 

题集六:

 

 

}

 

点菜记录类:保存订单上的一道菜品记录

 

Record {

 

   int orderNum;//序号\\

 

   Dish d;//菜品\\

 

   int portion;//份额(1/2/3代表小/中/大份)\\

 

   int getPrice()//计价,计算本条记录的价格\\

 

}

 

订单类:保存用户点的所有菜的信息。

 

Order {

 

   Record[] records;//保存订单上每一道的记录

 

   int getTotalPrice()//计算订单的总价

 

   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

 

   delARecordByOrderNum(int orderNum)//根据序号删除一条记录

 

   findRecordByNum(int orderNum)//根据序号查找一条记录

 

}

 

### 输入格式:

 

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

 

菜品记录格式:

 

菜名+英文空格+基础价格

 

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

 

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

 

删除记录格式:序号 +英文空格+delete

 

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

 

最后一条记录以“end”结束。

 

### 输出格式:

 

按输入顺序输出每一桌的订单记录处理信息,包括:

 

1、桌号,格式:table+英文空格+桌号+”:”

 

2、按顺序输出当前这一桌每条订单记录的处理信息,

 

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

 

如果删除记录的序号不存在,则输出“delete error”

 

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

 

以上为菜单计价系列-3的题目要求,加粗的部分是有调整的内容。本次课题相比菜单计价系列-3新增要求如下:

 

1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

 

 

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

输出用户支付金额格式:

用户姓名+英文空格+手机号+英文空格+支付金额

 

 

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

 

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

将所有记录的菜价累加得到整桌菜的价格。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

 

菜品记录格式:

 

菜名+口味类型+英文空格+基础价格

 

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

 

点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范围见题目中说明。

 

删除记录格式:序号 +英文空格+delete

 

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称**+英文空格+辣/酸/甜度值+**英文空格+份额+英文空格+分数

 

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

 

1、桌号,格式:table+英文空格+桌号+“:”+英文空格

 

2、按顺序输出当前这一桌每条订单记录的处理信息,

 

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

 

如果删除记录的序号不存在,则输出“delete error”

 

之后按输入顺序一次输出每一桌所有菜品的价格(整数数值),

格式:table+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格

 

最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。

输入样例1:

桌号时间超出营业范围。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 21/30/00
1 麻婆豆腐 3 1 2
2 油淋生菜 2 1
3 麻婆豆腐 2 3 2
end

输出样例1:

在这里给出相应的输出。例如:

table 1 out of opening hours

输入样例2:

一种口味的菜品。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 20/30/00
1 麻婆豆腐 2 1 2
2 油淋生菜 2 1
3 麻婆豆腐 2 3 2
end

输出样例2:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 24
2 油淋生菜 14
3 麻婆豆腐 48
table 1: 86 62 川菜 4 稍辣
tom 13605054400 62

 

输入样例3:

辣度值超出范围。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 18/30/00
1 麻婆豆腐 6 1 2
2 油淋生菜 1 1
3 麻婆豆腐 5 3 2
end

输出样例3:

在这里给出相应的输出。例如:

table 1: 
spicy num out of range :6
2 油淋生菜 9
3 麻婆豆腐 48
table 1: 57 41 川菜 2 爆辣
tom 13605054400 41

输入样例4:

同一用户对应多桌菜。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 18/30/00
1 麻婆豆腐 1 1 2
2 油淋生菜 1 1
3 麻婆豆腐 2 2 2
table 2 : tom 13605054400 2023/5/6 18/30/00
1 麻婆豆腐 2 1 2
2 麻辣鸡丝 2 2
3 麻婆豆腐 2 1 1
end

输出样例4:

在这里给出相应的输出。例如:

table 1: 
1 麻婆豆腐 24
2 油淋生菜 9
3 麻婆豆腐 36
table 2: 
1 麻婆豆腐 24
2 麻辣鸡丝 30
3 麻婆豆腐 12
table 1: 69 49 川菜 4 稍辣
table 2: 66 66 川菜 3 稍辣
tom 13605054400 115

输入样例5:

多用户多桌菜。例如:

东坡肉 浙菜 25 T
油淋生菜 9
蜜汁灌藕 浙菜 10 T
刀削面 晋菜 10 T
醋浇羊肉 晋菜 30 T
麻婆豆腐 川菜 12 T
麻辣鸡丝 川菜 15 T
table 1 : tom 13605054400 2023/5/6 12/30/00
1 醋浇羊肉 4 1 1
3 刀削面 1 1 3
2 东坡肉 3 2 1
4 麻辣鸡丝 2 1 1
table 2 : jerry 18100334566 2023/5/1 12/30/00
1 醋浇羊肉 1 1 2
3 麻婆豆腐 2 2 1
4 麻辣鸡丝 2 3 3
table 3 : jerry 18100334566 2023/5/1 12/30/00
1 醋浇羊肉 2 1 1
3 蜜汁灌藕 1 1 2
2 东坡肉 2 2 1
4 麻辣鸡丝 5 1 1
end

输出样例5:

在这里给出相应的输出。例如:

table 1: 
1 醋浇羊肉 30
3 刀削面 30
2 东坡肉 38
4 麻辣鸡丝 15
table 2: 
1 醋浇羊肉 60
3 麻婆豆腐 18
4 麻辣鸡丝 90
table 3: 
1 醋浇羊肉 30
3 蜜汁灌藕 20
2 东坡肉 38
4 麻辣鸡丝 15
table 1: 113 113 川菜 1 稍辣 晋菜 4 稍酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣 晋菜 2 微酸
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
jerry 18100334566 191
tom 13605054400 113

输入样例6:

多用户多桌菜含代点菜。例如:

东坡肉 浙菜 25 T
油淋生菜 9
蜜汁灌藕 浙菜 10 T
刀削面 晋菜 10 T
醋浇羊肉 晋菜 30 T
麻婆豆腐 川菜 12 T
麻辣鸡丝 川菜 15 T
table 1 : tom 13605054400 2023/5/6 12/30/00
1 醋浇羊肉 4 1 1
3 刀削面 1 1 3
2 东坡肉 3 2 1
4 麻辣鸡丝 2 1 1
table 2 : jerry 18100334566 2023/5/1 12/30/00
1 1 醋浇羊肉 0 1 2
3 麻婆豆腐 2 2 1
4 麻辣鸡丝 2 3 3
table 3 : lucy 18957348763 2023/5/1 12/30/00
1 醋浇羊肉 2 1 1
3 蜜汁灌藕 1 1 2
2 东坡肉 2 2 1
4 麻辣鸡丝 5 1 1
end

输出样例6:

在这里给出相应的输出。例如:

table 1: 
1 醋浇羊肉 30
3 刀削面 30
2 东坡肉 38
4 麻辣鸡丝 15
table 2: 
1 table 2 pay for table 1 60
3 麻婆豆腐 18
4 麻辣鸡丝 90
table 3: 
1 醋浇羊肉 30
3 蜜汁灌藕 20
2 东坡肉 38
4 麻辣鸡丝 15
table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
jerry 18100334566 118
lucy 18957348763 73
tom 13605054400 113

输入样例7:

错误的菜品记录和桌号记录,用户丢弃。例如:

东坡肉 25 T
油淋生菜 9
table 1 : tom 136050540 2023/5/1 12/30/00
2 东坡肉 3 2 1
end

输出样例7:

在这里给出相应的输出。例如:

wrong format
wrong format


 

 

分析:

这是一个相当复杂的问题,需要考虑菜品的口味类型、折扣、客户信息等多个方面。

首先,创建一个类来表示菜品记录,其中包括菜名、基础价格以及口味类型和是否为特色菜。然后,创建另一个类来表示订单,其中包括订单号、菜品记录、客户信息等。

实现一个方法来解析输入,并根据输入执行相应的操作,比如添加菜品记录、删除菜品记录、计算总价等。在处理口味类型时,需要根据具体的口味范围进行判断,并输出相应的提示信息。

最后,按要求输出每一桌的订单记录处理信息,以及每位客户需要支付的金额。对于支付金额的累加,需要根据客户姓名和手机号进行识别和计算。

 

 

 期中考试:

期中考试的难度偏低前面两题很基础,第三题结合前面两个题目,要求采用继承类解决问题

7-3 测验3-继承与多态
分数 20
作者 段喜龙
单位 南昌航空大学

将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类,类图如下所示:


image.png

试编程完成如上类图设计,主方法源码如下(可直接拷贝使用):

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        int choice = input.nextInt();
        
        switch(choice) {
        case 1://Circle
            double radiums = input.nextDouble();
            Shape circle = new Circle(radiums);
            printArea(circle);
            break;
        case 2://Rectangle
            double x1 = input.nextDouble();
            double y1 = input.nextDouble();
            double x2 = input.nextDouble();
            double y2 = input.nextDouble();
            
            Point leftTopPoint = new Point(x1,y1);
            Point lowerRightPoint = new Point(x2,y2);
            
            Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
            
            printArea(rectangle);
            break;
        }
        
    }

其中,printArea(Shape shape)方法为定义在Main类中的静态方法,体现程序设计的多态性。

输入格式:

输入类型选择(1或2,不考虑无效输入)
对应图形的参数(圆或矩形)

输出格式:

图形的面积(保留两位小数)

输入样例1:

1
5.6

输出样例1:

在这里给出相应的输出。例如:

98.52

输入样例2:

2
5.6
-32.5
9.4
-5.6

输出样例2:

在这里给出相应的输出。例如:

102.22

分析:把前面两题代码贴过来,在加个继承就完成了,总体上没什么难度。

7-4 测验4-抽象类与接口
分数 20
作者 段喜龙
单位 南昌航空大学

在测验3的题目基础上,重构类设计,实现列表内图形的排序功能(按照图形的面积进行排序)。
提示:题目中Shape类要实现Comparable接口。

其中,Main类源码如下(可直接拷贝使用):

public class Main {
    public static void main(String\[\] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();    

        int choice = input.nextInt();

        while(choice != 0) {
            switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                list.add(circle);
                break;
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();            
                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);
                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                list.add(rectangle);
                break;
            }
            choice = input.nextInt();
        }    

        list.sort(Comparator.naturalOrder());//正向排序

        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }    
    }    
}

输入格式:

输入图形类型(1:圆形;2:矩形;0:结束输入)

输入图形所需参数

输出格式:

按升序排序输出列表中各图形的面积(保留两位小数),各图形面积之间用空格分隔。

输入样例:

在这里给出一组输入。例如:

1
2.3
2
3.2
3
6
5
1
2.3
0

输出样例:

在这里给出相应的输出。例如:

5.60 16.62 16.62 


分析:这题主要考察list.sort的使用只需要改变一行代码就行了

list.sort(Comparator.comparingDouble(Shape::getArea))


(3)采坑心得:

  1. 数据结构设计:

    • 在设计菜单计价系统时,需要合理设计数据结构来表示菜品、订单和客户信息。使用合适的数据结构能够简化问题的建模和实现。
    • 可以考虑使用类和对象来表示菜品、订单和客户,这样能够更清晰地表达它们之间的关系。
  2. 计价逻辑:

    • 在进行菜品计价时,需要考虑到折扣、口味类型和特色菜等因素。要确保计价逻辑准确无误,并且考虑到各种边界情况。
    • 可以将计价逻辑封装成一个独立的函数或方法,便于复用和维护。
  3. 输入处理:

    • 对用户输入进行合理的处理和验证是非常重要的,可以通过正则表达式、条件判断等方式来确保输入的合法性。
    • 针对口味类型和特色菜的处理,需要充分考虑用户可能的输入,以及如何进行提示和纠错。
  4. 输出结果:

    • 输出订单记录和每位客户需支付的金额时,要求按照规定的格式进行输出,并确保结果的准确性。
    • 可以编写专门的输出函数来处理订单记录和支付金额的输出,提高代码的可读性和可维护性。

(4)改进建议:总体上都很好,但是菜单计价的连续性太强导致在前面题目没有全对完成的情况下,完成后续题目的难度大大加大。

(5)总结:在解决菜单计价题目时,我深刻体会到了对问题进行合理的抽象和建模是至关重要的。首先,需要清晰地定义菜品、订单和客户等概念,并设计合适的数据结构来表示它们的关系。其次,计价逻辑需要考虑到各种可能的情况,比如折扣、口味类型和特色菜等因素,这要求对业务需求有深入的理解和分析。处理用户输入时,需要考虑到输入的合法性和用户友好的交互,以及如何进行提示和纠错。最后,输出结果时要求严格按照规定的格式,并确保结果的准确性。总而言之,解决菜单计价问题不仅仅是编写代码,更需要深入理解业务需求、合理抽象问题、设计合适的算法和数据结构,并将代码编写规范、可读性高。