PTA第二次总结
这次重点是菜单迭代4和5,由于菜单迭代3中我还是按照面向过程的思路做,所以到了迭代4我就寸步难行,于是我决定重新画类图并重构代码。
下面是我的重构后的类图:

这里左边是正常情况下的类图,面对迭代4中的异常情况我是新建了抽象检查类,在左边的正常情况中对需要检查的地方插入检查类,像拼积木一样需要检查什么就拿什么插入。
可惜做到后面发现类图缺少了保存时间的属性,导致我最后一个测试点没通过。
下面是迭代4的题目
Menu4
本体大部分内容与菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价
评测详情
| 测试点 | 提示 | 内存(KB) | 用时(ms) | 结果 | 得分 | |
| case0 | 包含混合在订单信息中的菜谱信息。之后的用例简称此情况为混合的菜谱信息。 | 24692 | 381 |
答案正确
|
2 / 2 | |
| case1 | 重复的删除记录 | 24708 | 377 |
答案正确
|
3 / 3 | |
| case2 | 包含错误的份数。 | 25376 | 436 |
答案正确
|
3 / 3 | |
| case3 | 包含错误的份额。 | 23868 | 395 |
答案正确
|
2 / 2 | |
| case4 | 桌号信息格式错误 | 15608 | 150 |
答案正确
|
3 / 3 | |
| case5 | 桌号信息格式错误:多余字符 | 15384 | 151 |
答案正确
|
2 / 2 | |
| case6 | 桌号信息格式错误:多余字符 | 15692 | 152 |
答案正确
|
2 / 2 | |
| case7 | 混合错误:桌号信息格式错误+混合的菜谱信息(菜谱信息忽略) | 16064 | 153 |
答案正确
|
2 / 2 | |
| case8 | 混合错误:菜谱记录格式错误+混合的菜谱信息 | 25472 | 442 |
答案正确
|
2 / 2 | |
| case9 | 菜谱格式错误 | 23736 | 455 |
答案正确
|
3 / 3 | |
| case10 | 菜谱格式错误 | 24160 | 398 |
答案正确
|
2 / 2 | |
| case11 | 错误的菜谱信息 | 27200 | 482 |
答案正确
|
3 / 3 | |
| case12 | 包含空行(按错误的输入格式计)。 | 23840 | 390 |
答案正确
|
2 / 2 | |
| case13 | 包含混合在订单信息中的菜谱信息。 | 23308 | 373 |
答案正确
|
2 / 2 | |
| case14 | 混合错误:桌号信息格式错误+错误桌的订单记录格式错误(忽略,不输出) | 15700 | 139 |
答案正确
|
2 / 2 | |
| case15 | 混合错误:桌号格式错误+订单格式错误+错误的混合菜谱信息(后面两个错误均不输出) | 16020 | 134 |
答案正确
|
2 / 2 | |
| case16 | 混合错误:订单记录格式错误+混合菜谱信息 | 23868 | 335 |
答案正确
|
2 / 2 | |
| case17 | 桌号时间错误 | 24076 | 393 |
答案正确
|
3 / 3 | |
| case18 | 错误的table记录,内容并入上一个table计算。 | 23572 | 376 |
答案正确
|
2 / 2 | |
| case19 | 错误的table记录, | 19376 | 221 |
答案错误
|
0 / 2 | |
| case20 | 重复的桌号,其中之一不在营业时间。 | 24924 | 417 |
答案正确
|
2 / 2 | |
| case21 | 重复的桌号,不同时间段。 | 24788 | 392 |
答案正确
|
2 / 2 | |
| case22 | 重复的桌号,在同一时间段。 | 23744 | 380 |
答案正确
|
2 / 2 | |
| case23 | 重复的桌号,一个桌号时间错误。 | 23852 | 373 |
答案正确
|
2 / 2 | |
| case24 | 多个桌号,一个桌号时间错误。 | 25836 | 395 |
答案正确
|
2 / 2 | |
| case25 | 多个桌号,一个桌号格式错误。 | 24124 | 343 |
答案正确
|
2 / 2 | |
| case26 | 多个桌号,一个桌号超出范围。 | 24828 | 368 |
答案正确
|
2 / 2 | |
| case27 | 多个桌号,一个桌号格式非法。 | 23884 | 340 |
答案正确
|
2 / 2 | |
| case28 | 多个桌号,一个桌号格式非法。 | 24960 | 359 |
答案正确
|
2 / 2 | |
| case29 | 代点菜信息,桌号不存在。 | 24084 | 340 |
答案正确
|
2 / 2 | |
| case30 | 代点菜信息,桌号不存在。 | 24748 | 368 |
答案正确
|
2 / 2 | |
| case31 | 菜谱价格超出范围 | 25120 | 348 |
答案正确
|
2 / 2 | |
| case32 | 桌号时间超出范围 | 24476 | 376 |
答案正确
|
2 / 2 | |
| case33 | 多桌菜测试,无异常记录 | 24180 | 367 |
答案错误
|
0 / 2 | |
| case34 | 桌号时间超过范围 | 23844 | 373 |
答案正确
|
2 / 2 | |
| case35 | 重复的菜名 | 23944 | 377 |
答案正确
|
2 / 2 | |
| case36 | 未按顺序排列的序号。 | 24332 | 347 |
答案正确
|
2 / 2 | |
| case37 | 代点菜 | 26928 | 442 |
答案正确
|
2 / 2 | |
| case38 | 多桌菜 错误的菜名 不在服务时间的桌号 等情况 | 24636 | 433 |
答案正确
|
2 / 2 | |
| case39 | 订单记录份额超出范围 | 24676 | 369 |
答案正确
|
3 / 3 | |
| case40 | 点菜记录份数超出范围 | 24404 | 349 |
答案正确
|
3 / 3 | |
| case41 | 点菜记录中包含多种错误 | 23992 | 416 |
答案正确
|
2 / 2 | |
| case42 | 点菜记录中包含多种错误 | 24660 | 403 |
答案正确
|
2 / 2 | |
| case43 | 桌号对应时间格式合法(符合题目中格式定义),数据非法 | 20376 | 262 |
答案正确
|
2 / 2 | |
| case44 | 桌号对应时间格式合法(符合题目中格式定义),数据非法 | 20576 | 252 |
答案正确
|
2 / 2 | |
| case45 | 重复的桌号,不同时间段。 | 25304 | 386 |
答案正确
|
2 / 2 |
我的答案
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.time.LocalDate; 4 import java.time.LocalDateTime; 5 import java.time.ZoneId; 6 import java.time.format.DateTimeFormatter; 7 import java.util.ArrayList; 8 import java.util.Date; 9 import java.util.Scanner; 10 11 public class Main { 12 13 public static void main(String[] args) { 14 input inputinpect = new input(); 15 time time = new time(); 16 table table = new table(); 17 Record replace = new vegetable(); 18 Record order = new order(); 19 Record special = new Specialtydishes(); 20 Menu menu = new Menu(); 21 TableIdentification tableIdentif = new TableIdentification(); 22 DeleteRecord delete = new DeleteRecord(); 23 OrderForGoods orderForGoods = new OrderForGoods(); 24 Scanner input = new Scanner(System.in); 25 ArrayList<String> list = new ArrayList<String>(); 26 String iString; 27 while (!"end".equals(iString = input.nextLine())) { 28 if (iString.equals("")) { // 检查输入 29 System.out.println("wrong format"); 30 continue; 31 } 32 if (table.starttable(iString.substring(0, 5))) { // 桌号标识 33 34 35 for (String dan : iString.split(" ")) 36 list.add(dan); 37 if (!(tableIdentif.getTable() == 0)) { 38 tableIdentif.setotal(); 39 } 40 try { 41 if(!iString.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) { 42 throw new iszero(); 43 } 44 tableIdentif.addtable(list.get(1), time.time(list.get(2) + " " + list.get(3)), 45 time.timeT(list.get(2) + " " + list.get(3))); 46 47 } catch ( iszero | NumberFormatException e) { 48 tableIdentif.flag = true; 49 System.out.println("wrong format"); 50 }catch (ParseException e) { 51 tableIdentif.flag = true; 52 System.out.println(list.get(1) + " date error"); 53 } catch (tablenumoutofrange e) { 54 tableIdentif.flag = true; 55 System.out.println(list.get(1) + " table num out of range"); 56 } catch (timeOutOfRange e) { 57 tableIdentif.flag = true; 58 System.out.println("not a valid time period"); 59 } catch (timeOutOfopening e) { 60 tableIdentif.flag = true; 61 System.out.println("table " + list.get(1) + " out of opening hours"); 62 } catch (timeerror e) { 63 tableIdentif.flag = true; 64 System.out.println(list.get(1) + " date error"); 65 } finally { 66 67 } 68 list.removeAll(list); 69 continue; 70 } 71 if (inputinpect.input(iString)) { // 检查输入 72 if (tableIdentif.flag) { 73 break; 74 } 75 System.out.println("wrong format"); 76 continue; 77 } 78 for (String dan : iString.split(" ")) 79 list.add(dan); 80 switch (list.size()) { 81 case 2: 82 if (tableIdentif.flag) { 83 break; 84 } 85 if (list.get(1).equals("delete")) { // 删除记录 86 delete.setOrdernum(list.get(0)); 87 delete.DeleteRecord(); 88 } else { 89 if ((tableIdentif.getTable() == 0)) { 90 try { 91 menu.addDish(list.get(0), list.get(1));// 添加菜谱信息 92 } catch (iszero | NumberFormatException e) { 93 System.out.println("wrong format"); 94 } 95 96 } else { 97 System.out.println("invalid dish"); 98 } 99 100 } 101 102 break; 103 case 3: 104 if (tableIdentif.flag) { 105 break; 106 } 107 if ((tableIdentif.getTable() == 0)) { 108 try { 109 menu.addDish(list.get(0), list.get(1), list.get(2));// 添加特色菜 110 } catch (iszero | NumberFormatException e) { 111 System.out.println("wrong format"); 112 } 113 } else { 114 System.out.println("invalid dish"); 115 } 116 break; 117 case 4: 118 119 // 添加点菜记录 120 if (tableIdentif.flag) { 121 break; 122 } 123 try { 124 tableIdentif.addRecords(list.get(0), list.get(1), list.get(2), list.get(3)); 125 } catch (iszero | NumberFormatException e) { 126 System.out.println("wrong format"); 127 } catch (numOutOfRange e) { 128 System.out.println(list.get(0) + " num out of range " + list.get(3)); 129 } catch (portionOutOfRange e) { 130 System.out.println(list.get(0) + " portion out of range " + list.get(2)); 131 } catch (RecordOrdernumError e) { 132 System.out.println("record serial number sequence error"); 133 } finally { 134 135 } 136 137 break; 138 case 5: // 添加带点菜记录 139 if (tableIdentif.flag) { 140 break; 141 } 142 try { 143 tableIdentif.addRecords(list.get(0), list.get(1), list.get(2), list.get(3), list.get(4)); 144 } catch (iszero | NumberFormatException e) { 145 System.out.println("wrong format"); 146 } catch (numOutOfRange e) { 147 System.out.println(list.get(1) + " num out of range " + list.get(4)); 148 } catch (portionOutOfRange e) { 149 System.out.println(list.get(1) + " portion out of range " + list.get(3)); 150 } finally { 151 152 } 153 154 break; 155 default: 156 break; 157 } 158 159 list.removeAll(list); 160 } 161 tableIdentif.setotal(); 162 orderForGoods.getRegluerprice(); 163 164 } 165 166 } 167 168 class OrderForGoods { 169 private int shi = 0; 170 private int regluerprice = 0, specialprice = 0; 171 172 public void setShi() { 173 this.shi = TableIdentification.getShi(); 174 } 175 176 public void getRegluerprice() { 177 setShi(); 178 if (TableIdentification.total[0][0] == 0) { 179 return; 180 } 181 for (int i = 0; i < shi; i++) { 182 183 184 regluerprice = Math.round(TableIdentification.total[3][i] + TableIdentification.total[4][i]); 185 specialprice = Math.round(TableIdentification.total[1][i] * TableIdentification.total[3][i]) 186 + Math.round(TableIdentification.total[2][i] * TableIdentification.total[4][i]); 187 188 189 System.out.println( 190 "table " + (int) (TableIdentification.total[0][i]) + ": " + regluerprice + " " + specialprice); 191 192 } 193 } 194 } 195 196 class input { 197 public boolean input(String in) { // 检查输入是否合规 198 if (in.matches("[\\S]* [1-9][\\d]*") || in.matches("[\\S]* [\\d]* T")) { 199 return false; 200 } 201 if (in.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) { 202 return false; 203 } 204 if (in.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) { 205 return false; 206 } 207 if (in.matches("[1-9][\\d]* delete") || in.matches("[\\S]* [\\d]*")) { 208 return false; 209 } 210 return true; 211 } 212 } 213 214 class DeleteRecord { 215 216 private int ordernum = 0; 217 private ArrayList<Integer> test = new ArrayList<>(); 218 219 public void setOrdernum(String ordernum) { 220 this.ordernum = Integer.parseInt(ordernum); 221 222 } 223 224 public Record findelete() { 225 for (Record one : TableIdentification.list) { 226 if (one.getOrderNum() == ordernum) { 227 test.add(this.ordernum); 228 return one; 229 } 230 } 231 232 return null; 233 } 234 235 public void DeleteRecord() { 236 237 if (findelete() != null) { 238 TableIdentification.list.remove(findelete()); 239 return; 240 } else { 241 if (test.contains(this.ordernum)) { 242 System.out.println("deduplication " + this.ordernum); 243 return; 244 } 245 System.out.println("delete error;"); 246 return; 247 } 248 249 } 250 251 public int getOrdernum() { 252 return ordernum; 253 } 254 } 255 256 class timeOutOfopening extends Exception { 257 } 258 259 class RecordOrdernumError extends Exception { 260 } 261 262 class TableIdentification { 263 static float[][] total = new float[6][20]; 264 private int table = 0; 265 private float RegluerDiscount = (float) 1.0; 266 private float SpecialDiscount = (float) 1.0; 267 private int RegluerPrice = 0; 268 private int SpecialPrice = 0; 269 static ArrayList<Record> list = new ArrayList<Record>(); 270 static ArrayList<tabletime> test = new ArrayList<tabletime>(); 271 private String time; 272 private static int shi = 0; 273 private boolean ins = true; 274 static boolean flag = false; 275 276 public static int getShi() { 277 return shi; 278 } 279 280 // 添加点菜记录 281 public void addRecords(String ordernum, String name, String portion, String num) 282 throws iszero, numOutOfRange, portionOutOfRange, RecordOrdernumError { 283 if (flag) { 284 return; 285 } 286 for (Record one : list) { 287 if (Integer.parseInt(ordernum) <= one.getOrderNum()) { 288 throw new RecordOrdernumError(); 289 } 290 } 291 if (Menu.searthDish(name) == null) { 292 System.out.println(name + " does not exist"); 293 return; 294 } 295 if (Menu.searthDish(name).getspecial()) { // 是否特色菜 296 Record records = new Specialtydishes() { 297 }; 298 299 records.setGreens(name); 300 records.setOrderNum(ordernum); 301 records.setPortion(portion); 302 records.setNum(num); 303 records.setPrice(); 304 System.out.println(records.getOrderNum() + " " + records.getGreens().getName() + " " + records.getPrice()); 305 list.add(records); 306 } else { 307 Record records = new Record() { 308 }; 309 records.setOrderNum(ordernum); 310 records.setGreens(name); 311 records.setPortion(portion); 312 records.setNum(num); 313 records.setPrice(); 314 System.out.println(records.getOrderNum() + " " + records.getGreens().getName() + " " + records.getPrice()); 315 list.add(records); 316 } 317 318 } 319 320 // 添加带点菜记录 321 public void addRecords(String tablenum, String ordernum, String name, String portion, String num) 322 throws iszero, numOutOfRange, portionOutOfRange { 323 324 if (flag) { 325 return; 326 } 327 for (int i = 0; i < shi; i++) { 328 if ((int) (total[0][i]) == Integer.parseInt(tablenum)) { 329 ins = false; 330 break; 331 } 332 } 333 if (ins) { 334 System.out.println("Table number :" + tablenum + " does not exist"); 335 return; 336 } 337 if (Menu.searthDish(name) == null) { 338 System.out.println(name + " does not exist"); 339 return; 340 } 341 if (Menu.searthDish(name).getspecial()) { // 是否特色菜 342 Record records = new Specialtydishes() { 343 }; 344 records.setOrderNum(ordernum); 345 records.setGreens(name); 346 records.setPortion(portion); 347 records.setNum(num); 348 records.setPrice(); 349 System.out.println(records.getOrderNum() + " table " + getTable() + " pay for table " + tablenum + " " 350 + records.getPrice()); 351 list.add(records); 352 } else { 353 Record records = new Record() { 354 }; 355 records.setOrderNum(ordernum); 356 records.setGreens(name); 357 records.setPortion(portion); 358 records.setNum(num); 359 records.setPrice(); 360 System.out.println(records.getOrderNum() + " table " + getTable() + " pay for table " + tablenum + " " 361 + records.getPrice()); 362 list.add(records); 363 } 364 } 365 366 // 添加桌号和确定折扣 367 public void addtable(String tablenum, float discount, float specialdiscount) 368 throws iszero, tablenumoutofrange, timeOutOfopening { 369 table table = new table(); 370 table.tablenum(tablenum); 371 if (discount == 0) { 372 throw new timeOutOfopening(); 373 } 374 setSpecialDiscount(specialdiscount); 375 setRegluerDiscount(discount); 376 System.out.println("table " + tablenum + ": "); 377 setTable(tablenum); 378 TableIdentification.flag = false; 379 } 380 381 public void setotal() { 382 if (TableIdentification.flag) { 383 return; 384 } 385 for (Record one : list) { 386 if (one.ispecial()) { 387 SpecialPrice += one.getPrice(); 388 } else { 389 RegluerPrice += one.getPrice(); 390 } 391 } 392 if(this.RegluerDiscount==total[1][0]&&this.RegluerPrice==total[3][0]) { 393 total[3][0] += this.RegluerPrice; 394 total[4][0] += this.SpecialPrice; 395 list.removeAll(list); 396 }else { 397 list.removeAll(list); 398 total[0][shi] = this.table; 399 total[1][shi] = this.RegluerDiscount; 400 total[2][shi] = this.SpecialDiscount; 401 total[3][shi] = this.RegluerPrice; 402 total[4][shi] = this.SpecialPrice; 403 SpecialPrice = 0; 404 RegluerPrice = 0; 405 RegluerDiscount = 1; 406 SpecialDiscount = 1; 407 shi++; 408 } 409 410 411 } 412 413 public void setRegluerDiscount(float regluerDiscount) { 414 RegluerDiscount = regluerDiscount; 415 } 416 417 public void setRegluerPrice(int regluerPrice) { 418 RegluerPrice = regluerPrice; 419 } 420 421 public void setSpecialDiscount(float specialDiscount) { 422 SpecialDiscount = specialDiscount; 423 } 424 425 public void setSpecialPrice(int specialPrice) { 426 SpecialPrice = specialPrice; 427 } 428 429 public void setTable(String table) { 430 431 this.table = Integer.parseInt(table); 432 } 433 434 public void setTime(String time) { 435 this.time = time; 436 } 437 438 public float getRegluerDiscount() { 439 return RegluerDiscount; 440 } 441 442 public int getRegluerPrice() { 443 return RegluerPrice; 444 } 445 446 public float getSpecialDiscount() { 447 return SpecialDiscount; 448 } 449 450 public int getSpecialPrice() { 451 return SpecialPrice; 452 } 453 454 public int getTable() { 455 return table; 456 } 457 458 public String getTime() { 459 return time; 460 } 461 } 462 463 class tabletime { 464 int table = 0; 465 int tablejilu = 0; 466 LocalDateTime ldt3 = LocalDateTime.now(); 467 468 public void tabletime(String in) { 469 try { 470 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss"); 471 Date dd; 472 dd = sdf2.parse(in); 473 ldt3 = LocalDateTime.ofInstant(dd.toInstant(), ZoneId.systemDefault()); 474 } catch (ParseException e) { 475 // TODO 自动生成的 catch 块 476 e.printStackTrace(); 477 } 478 479 } 480 } 481 482 abstract class Record { 483 private int orderNum = 0;// 序号\ 484 private Dish greens;// 菜品\ 485 private int portion = 0;// 份额(1/2/3代表小/中/大份)\ 486 private int num = 0;// 份数 487 private int price = 0;// 价格 488 portion portion1 = new portion(); 489 490 public void setGreens(String greens) { 491 this.greens = Menu.searthDish(greens); 492 493 } 494 495 public void setNum(String num) throws iszero, numOutOfRange { 496 portion1.num(num); 497 this.num = Integer.parseInt(num); 498 } 499 500 public void setOrderNum(String orderNum) { 501 this.orderNum = Integer.parseInt(orderNum); 502 } 503 504 public void setPortion(String portion) throws portionOutOfRange { 505 portion1.protion(portion); 506 this.portion = Integer.parseInt(portion); 507 } 508 509 public void setPrice() { 510 this.price = greens.getPrice(portion) * num; 511 } 512 513 public Dish getGreens() { 514 return greens; 515 } 516 517 public int getNum() { 518 return num; 519 } 520 521 public int getOrderNum() { 522 return orderNum; 523 } 524 525 public int getPortion() { 526 return portion; 527 } 528 529 public int getPrice() { 530 return price; 531 } 532 533 boolean ispecial() { 534 return greens.getspecial(); 535 } 536 537 } 538 539 class vegetable extends Record { // 带点菜 540 private int table = 0; 541 542 public void setTable(String table) { 543 this.table = Integer.parseInt(table); 544 } 545 546 public int getTable() { 547 return table; 548 } 549 550 void test() { 551 552 } 553 } 554 555 class order extends Record { // 点菜记录 556 void test() { 557 558 } 559 } 560 561 class Specialtydishes extends Record { // 特色菜记录 562 void test() { 563 564 } 565 } 566 567 class Menu { 568 569 static ArrayList<Dish> list = new ArrayList<Dish>(); 570 private int test = -1; 571 572 static Dish searthDish(String dishName) { 573 for (Dish one : list) { 574 boolean result = one.getName().equals(dishName) ? true : false; 575 if (result) { 576 return one; 577 } 578 } 579 return null; 580 }// 根据菜名在菜谱中查找菜品信息,返回Dish对象。 581 582 int isrepected(String dishName) { // 菜谱重复信息 583 for (Dish one : list) { 584 boolean result = one.getName().equals(dishName) ? true : false; 585 if (result) { 586 return list.indexOf(one); 587 } 588 } 589 return -2; 590 } 591 592 Dish addDish(String dishName, String unit_price, String special) throws iszero// 添加一道菜品信息 593 { 594 boolean T = "T".equals(special) ? true : false; 595 Dish dish = new Dish(); 596 if (menuins.isOutOfRange(unit_price)) { 597 System.out.println(dishName + " price out of range " + unit_price); 598 return null; 599 } 600 dish.setName(dishName); 601 dish.setUnit_price(Integer.parseInt(unit_price)); 602 dish.setSpecial(T); 603 if ((test = isrepected(dishName)) == -2) { 604 list.add(dish); 605 } else { 606 list.set(test, dish); 607 } 608 return dish; 609 } 610 611 Dish addDish(String dishName, String unit_price) throws iszero// 添加一道菜品信息 612 { 613 Dish dish = new Dish(); 614 if (menuins.isOutOfRange(unit_price)) { 615 System.out.println(dishName + " price out of range " + unit_price); 616 return null; 617 } 618 619 dish.setName(dishName); 620 dish.setUnit_price(Integer.parseInt(unit_price)); 621 if ((test = isrepected(dishName)) == -2) { 622 list.add(dish); 623 } else { 624 list.set(test, dish); 625 } 626 return dish; 627 } 628 } 629 630 class Dish { 631 private String name;// 菜品名称 632 private int unit_price; // 单价 633 private boolean special = false;// 特色菜标志 634 635 public void setName(String name) { 636 this.name = name; 637 } 638 639 public void setUnit_price(int unit_price) { 640 this.unit_price = unit_price; 641 } 642 643 public void setSpecial(boolean special) { 644 this.special = special; 645 } 646 647 public String getName() { 648 return name; 649 } 650 651 public int getUnit_price() { 652 return unit_price; 653 } 654 655 public boolean getspecial() { 656 return special; 657 } 658 659 int getPrice(int portion) { 660 int price = 0; 661 if (portion == 1) { 662 price = unit_price; 663 } else if (portion == 2) { 664 price = Math.round((float) (unit_price * 1.5)); 665 } else if (portion == 3) { 666 price = Math.round((float) (unit_price * 2)); 667 } 668 669 return price; 670 }// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) 671 } 672 673 abstract class inspect { 674 inspect() { 675 } 676 } 677 678 class table extends inspect { 679 private int num = 0; 680 private int pp = 0; 681 682 boolean starttable(String in) { // 判断是否table开头 683 if (in.equals("table")) { 684 return true; 685 } else { 686 return false; 687 } 688 } 689 690 void tablenum(String in) throws iszero, tablenumoutofrange { // 判断桌号是否在1-55,是否0开头 691 this.num = Integer.parseInt(in); 692 if (in.charAt(0) == '0') { 693 throw new iszero(); 694 } 695 if (num <= 55 && num >= 1) { 696 return; 697 } 698 throw new tablenumoutofrange(); 699 700 } 701 702 boolean tabletime(String in, float[][] total) {// 判断桌号是否重复 703 pp = TableIdentification.getShi(); 704 for (int i = 0; i < pp; i++) { 705 if (Integer.parseInt(in) == total[0][i]) 706 return true; 707 } 708 return false; 709 } 710 711 } 712 713 class timeOutOfRange extends Exception { 714 } 715 716 class tablenumoutofrange extends Exception { 717 } 718 719 class timeerror extends Exception { 720 } 721 722 class time extends inspect { 723 private int week = 0, hour = 0, min = 0; 724 private float discount6 = (float) 0.6; 725 private float discount8 = (float) 0.8; 726 private float discount7 = (float) 0.7; 727 private float discount1 = (float) 1.0; 728 729 float time(String in) throws ParseException, timeOutOfRange, timeerror { 730 731 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss"); 732 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss"); 733 Date dd; 734 sdf2.setLenient(false); 735 dd = sdf2.parse(in); 736 LocalDateTime ldt3 = LocalDateTime.ofInstant(dd.toInstant(), ZoneId.systemDefault()); 737 LocalDateTime starTime = LocalDateTime.of(2022, 1, 1, 0, 0, 0, 0); 738 LocalDateTime enDateTime = LocalDateTime.of(2023, 12, 31, 0, 0, 0, 0); 739 String yyString=""; 740 741 String uiString = ldt3.format(dtf); 742 743 //if (!yyString.equals(uiString)) { 744 // throw new timeerror(); 745 //} 746 if (ldt3.isAfter(starTime) && ldt3.isBefore(enDateTime)) { 747 748 } else { 749 throw new timeOutOfRange(); 750 } 751 752 week = ldt3.getDayOfWeek().getValue(); 753 hour = ldt3.getHour(); 754 min = ldt3.getMinute(); 755 756 if (week <= 5 && week != 0) { 757 758 if ((hour > 10 || (hour == 10 && min >= 30)) && (hour < 14 || (hour == 14 && min <= 30))) { 759 return discount6; 760 } 761 if ((hour > 17 || (hour == 17 && min >= 0)) && (hour < 20 || (hour == 20 && min <= 30))) { 762 return discount8; 763 } 764 return 0; 765 } 766 if ((hour > 9 || (hour == 9 && min >= 30)) && (hour < 21 || (hour == 21 && min <= 30))) { 767 768 return discount1;// 正常价 769 } else { 770 return 0;// 关门 771 } 772 773 } 774 775 float timeT(String in) { 776 777 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss"); 778 Date dd; 779 try { 780 dd = sdf2.parse(in); 781 LocalDateTime ldt3 = LocalDateTime.ofInstant(dd.toInstant(), ZoneId.systemDefault()); 782 week = ldt3.getDayOfWeek().getValue(); 783 } catch (ParseException e) { 784 // TODO 自动生成的 catch 块 785 e.printStackTrace(); 786 } finally { 787 788 } 789 790 if (week <= 5) { 791 return discount7; 792 } 793 return discount1; 794 795 } 796 } 797 798 class portion extends inspect { 799 public void protion(String protion) throws portionOutOfRange { // 份额是否超出范围 800 if (Integer.parseInt(protion) > 0 && Integer.parseInt(protion) < 4) { 801 return; 802 } 803 throw new portionOutOfRange(); 804 } 805 806 public void num(String num) throws iszero, numOutOfRange { // 份数是否超出范围 807 if (num.charAt(0) == '0') { 808 throw new iszero(); 809 } 810 if (Integer.parseInt(num) > 0 && Integer.parseInt(num) < 16) { 811 return; 812 } 813 throw new numOutOfRange(); 814 } 815 } 816 817 class numOutOfRange extends Exception { 818 } 819 820 class portionOutOfRange extends Exception { 821 } 822 823 class menuins extends inspect { 824 static private int pric = 0; 825 static int i = 0; 826 827 static public boolean isOutOfRange(String price) throws iszero { // 是否超出范围,是否数值,是否0开头 828 pric = Integer.parseInt(price); 829 if (price.charAt(0) == '0') { 830 throw new iszero(); 831 } 832 if (pric > 0 && pric < 300) { 833 return false; 834 } 835 return true; 836 } 837 838 } 839 840 class iszero extends Exception { 841 }; 842 843 class OrderRecordinspect extends inspect { 844 845 }
Menu5
然后是菜单迭代5,多亏了迭代4的重构,面对迭代5这种要求我只需要添加类就能解决,所以我花了一下午的时间就解决了
下面是迭代5的类图:

题目详情
本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。
注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-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)//根据序号查找一条记录
}
### 输入格式:
桌号标识格式: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+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格
最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。
评测详情
| 测试点 | 提示 | 内存(KB) | 用时(ms) | 结果 | 得分 | |
| case1 | 川菜测试-单用户-辣(周末晚上) | 28660 | 491 |
答案正确
|
3 / 3 | |
| case2 | 川菜测试-单用户-辣(时间越界) | 21148 | 279 |
答案正确
|
3 / 3 | |
| case3 | 川菜测试-单用户 | 25176 | 458 |
答案正确
|
3 / 3 | |
| case4 | 川菜测试-单用户 | 30364 | 537 |
答案正确
|
3 / 3 | |
| case5 | 川菜测试-单用户 | 30024 | 525 |
答案正确
|
3 / 3 | |
| case6 | 川菜测试-单用户 | 27408 | 486 |
答案正确
|
3 / 3 | |
| case7 | 川菜测试-单用户 | 26852 | 532 |
答案正确
|
3 / 3 | |
| case8 | 川菜测试-单用户-辣度值越界 | 25808 | 476 |
答案正确
|
3 / 3 | |
| case9 | 川菜测试-单用户-辣度值越界 | 27744 | 463 |
答案正确
|
3 / 3 | |
| case10 | 川菜测试-单用户-多桌菜 | 28092 | 511 |
答案正确
|
3 / 3 | |
| case11 | 川菜测试-单用户-多桌菜 | 27560 | 488 |
答案正确
|
3 / 3 | |
| case12 | 晋菜测试-单用户 | 29440 | 537 |
答案正确
|
3 / 3 | |
| case13 | 晋菜测试-单用户-重复的菜谱记录 | 26204 | 448 |
答案正确
|
3 / 3 | |
| case14 | 晋菜测试-单用户-重复的菜谱记录 | 26708 | 467 |
答案正确
|
3 / 3 | |
| case15 | 晋菜测试-单用户-超范围的酸度值 | 27920 | 465 |
答案正确
|
3 / 3 | |
| case16 | 浙菜测试-单用户 | 28820 | 520 |
答案正确
|
3 / 3 | |
| case17 | 浙菜测试-单用户 | 24864 | 478 |
答案正确
|
3 / 3 | |
| case18 | 浙菜测试-单用户 | 28052 | 496 |
答案正确
|
3 / 3 | |
| case19 | 浙菜测试-单用户 | 25164 | 493 |
答案正确
|
3 / 3 | |
| case20 | 单用户-多桌菜-含delete | 27604 | 510 |
答案正确
|
3 / 3 | |
| case21 | 单用户-含两个菜系 | 29200 | 568 |
答案正确
|
3 / 3 | |
| case22 | 单用户-含两个菜系 | 30172 | 704 |
答案正确
|
3 / 3 | |
| case23 | 单用户-含两个菜系 | 30120 | 599 |
答案正确
|
3 / 3 | |
| case24 | 单用户-含三个菜系 | 30792 | 632 |
答案正确
|
3 / 3 | |
| case25 | 多用户—多菜系 | 34272 | 697 |
答案正确
|
2 / 2 | |
| case26 | 多用户—多菜系 | 40660 | 801 |
答案正确
|
2 / 2 | |
| case27 | 多用户—多菜系—含代点菜 | 35392 | 711 |
答案正确
|
2 / 2 | |
| case28 | 多用户—多菜系—含代点菜 | 51304 | 864 |
答案正确
|
2 / 2 | |
| case29 | 单用户—多菜系—含删除记录 | 30984 | 575 |
答案正确
|
3 / 3 | |
| case30 | 单用户—多菜系—含错误记录 | 29312 | 530 |
答案正确
|
3 / 3 | |
| case31 | 单用户—多菜系—含错误记录 | 28272 | 517 |
答案正确
|
2 / 2 | |
| case32 | 单用户—多菜系—含错误记录 | 27680 | 569 |
答案正确
|
3 / 3 | |
| case33 | 单用户—错误桌号记录 | 19488 | 235 |
答案正确
|
3 / 3 | |
| case34 | 单用户—错误桌号记录 | 16624 | 168 |
答案正确
|
3 / 3 | |
| case35 | 单用户—含错误记录-含删除记录 | 25428 | 469 |
答案正确
|
3 / 3 |
我的答案
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.time.LocalDateTime; 4 import java.time.ZoneId; 5 import java.util.ArrayList; 6 import java.util.Comparator; 7 import java.util.Date; 8 import java.util.List; 9 import java.util.Scanner; 10 import java.util.stream.Collectors; 11 12 13 public class Main { 14 15 public static void main(String[] args) { 16 time time = new time(); 17 Record replace = new vegetable(); 18 Record order = new order(); 19 Record special = new Specialtydishes(); 20 Menu menu = new Menu(); 21 // TableIdentification tableIdentif = new TableIdentification(); 22 DeleteRecord delete = new DeleteRecord(); 23 OrderForGoods orderForGoods = new OrderForGoods(); 24 Scanner input = new Scanner(System.in); 25 ArrayList<String> list = new ArrayList<String>(); 26 String iString; 27 boolean flag = false; 28 while (!"end".equals(iString = input.nextLine())) { 29 30 if (iString.substring(0, 5).equals("table")) { // 桌号标识 31 for (String dan : iString.split(" ")) 32 list.add(dan); 33 try { 34 35 for(String one:list) { 36 if(one.equals("")) 37 throw new nameoutof10(); 38 } 39 orderForGoods.addtable(list.get(1), list.get(3), list.get(4), list.get(5) + " " + list.get(6)); 40 System.out.println("table " + list.get(1) + ": "); 41 } catch (timeOutOfRange e) { 42 flag = true; 43 System.out.println("table " + list.get(1) + " out of opening hours"); 44 } catch (nameoutof10 e) { 45 flag = true; 46 System.out.println("wrong format"); 47 } catch (phoneoutofrange e) { 48 flag = true; 49 System.out.println("wrong format"); 50 } finally { 51 52 } 53 54 list.removeAll(list); 55 continue; 56 } 57 if (flag) { 58 continue; 59 } 60 for (String dan : iString.split(" ")) 61 list.add(dan); 62 switch (list.size()) { 63 case 2: 64 if (list.get(1).equals("delete")) { // 删除记录 65 delete.setOrdernum(list.get(0)); 66 delete.DeleteRecord(orderForGoods.gettablenew()); 67 } else { 68 69 menu.addDish(list.get(0), list.get(1)); // 添加菜谱信息 70 71 } 72 73 break; 74 case 4: 75 if (list.get(0).matches("\\d{0,}")) { 76 orderForGoods.gettablenew().addRecords(list.get(0), list.get(1), list.get(2), list.get(3));// 添加点菜记录 77 } else { 78 menu.addDish(list.get(0), list.get(1), list.get(2), list.get(3)); 79 } 80 break; 81 case 5: 82 if (list.get(1).matches("\\d{0,}")) {// 带点菜记录 83 orderForGoods.gettablenew().addRecords(list.get(0), list.get(1), list.get(2), list.get(3), 84 list.get(4)); 85 } else {// 添加特色菜记录 86 try { 87 orderForGoods.gettablenew().addspecialRecords(list.get(0), list.get(1), list.get(2), 88 list.get(3), list.get(4)); 89 } catch (NumberFormatException e) { 90 // TODO 自动生成的 catch 块 91 e.printStackTrace(); 92 } catch (spicy e) { 93 System.out.println("spicy num out of range :" + list.get(2)); 94 } catch (acdity e) { 95 System.out.println("acidity num out of range :" + list.get(2)); 96 } catch (sweet e) { 97 System.out.println("sweetness num out of range :" + list.get(2)); 98 } 99 } 100 break; 101 102 case 6: // 添加带点特色菜 103 orderForGoods.gettablenew().addRecords(list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), 104 list.get(5)); 105 try { 106 orderForGoods.adddaidian(list.get(0), list.get(2), list.get(3), list.get(5)); 107 } catch (NumberFormatException e) { 108 // TODO 自动生成的 catch 块 109 e.printStackTrace(); 110 } catch (spicy e) { 111 System.out.println("spicy num out of range :" + list.get(3)); 112 } catch (acdity e) { 113 System.out.println("acidity num out of range :" + list.get(3)); 114 } catch (sweet e) { 115 System.out.println("sweetness num out of range :" + list.get(3)); 116 } 117 break; 118 default:System.out.println("wrong format"); 119 break; 120 } 121 122 list.removeAll(list); 123 } 124 orderForGoods.settotal(); 125 126 // tableIdentif.setotal(); 127 orderForGoods.getRegluerprice(); 128 129 } 130 131 } 132 133 class OrderForGoods { 134 private int shi = 0; 135 private int regluerprice = 0, specialprice = 0; 136 private String tasteString; 137 private String[][] taste = new String[3][]; 138 139 ArrayList<TableIdentification> tablearr = new ArrayList<TableIdentification>(); 140 141 public void addtable(String table, String name, String phonenum, String timenum) 142 throws timeOutOfRange, nameoutof10, phoneoutofrange { // 添加桌 143 TableIdentification tableIdentification = new TableIdentification(); 144 tableIdentification.setTable(table); 145 tableIdentification.setName(name); 146 tableIdentification.setPhonenum(phonenum); 147 tableIdentification.settimenum(timenum); 148 // tableIdentification 149 150 tablearr.add(tableIdentification); 151 } 152 153 public void settastearr() { 154 taste[0] = new String[] { "不辣", "微辣", "稍辣", "辣", "很辣", "爆辣" }; 155 taste[1] = new String[] { "不酸", "微酸", "稍酸", "酸", "很酸" }; 156 taste[2] = new String[] { "不甜", "微甜", "稍甜", "甜" }; 157 } 158 159 public void settotal() { 160 for (TableIdentification one : tablearr) { 161 one.settotal(); 162 } 163 } 164 165 public void adddaidian(String table, String name, String delicious, String num) 166 throws NumberFormatException, spicy, acdity, sweet { // 添加带点特色菜口味和份数 167 for (TableIdentification one : tablearr) { 168 if (one.getTable() == Integer.parseInt(table)) { 169 Record records = new Specialtydishes() { 170 }; 171 records.setGreens(name); 172 ((Specialtydishes) records).setDelicious(Integer.parseInt(delicious)); 173 records.setNum(Integer.parseInt(num)); 174 one.getList().add(records); 175 } 176 } 177 } 178 179 public TableIdentification gettablenew() { 180 return tablearr.get(tablearr.size() - 1); 181 } 182 183 public void getRegluerprice() { 184 int spicy = -1, acidity = -1, sweet = -1; 185 // int price = 0; 186 settastearr(); 187 188 for (TableIdentification one : tablearr) { 189 if (one.getTasteLevel().getspicyflag() == false && one.getTasteLevel().getAcidityflag() == false 190 && one.getTasteLevel().getsweetflag() == false) {// 全0 191 System.out.println("table " + one.getTable() + ": " + one.getOriginalprice() + " " 192 + one.getDiscountedprice() + " "); 193 } 194 if (one.getTasteLevel().getspicyflag() == false && one.getTasteLevel().getAcidityflag() == true 195 && one.getTasteLevel().getsweetflag() == true) {// 第一个为0 196 acidity = Math.round(one.getTasteLevel().getAcidity() / one.getTasteLevel().getAcidtynum()); 197 sweet = Math.round(one.getTasteLevel().getSweetness() / one.getTasteLevel().getSweetnessnum()); 198 System.out.println("table " + one.getTable() + ": " + one.getOriginalprice() + " " 199 + one.getDiscountedprice() + " 晋菜 " + one.getTasteLevel().getAcidtynum() + " " 200 + taste[1][acidity] + " 浙菜 " + one.getTasteLevel().getSweetnessnum() + " " + taste[2][sweet]); 201 } 202 if (one.getTasteLevel().getspicyflag() == true && one.getTasteLevel().getAcidityflag() == false 203 && one.getTasteLevel().getsweetflag() == true) {// 第二个为0 204 spicy = Math.round(one.getTasteLevel().getSpicy() / one.getTasteLevel().getSpicynum()); 205 sweet = Math.round(one.getTasteLevel().getSweetness() / one.getTasteLevel().getSweetnessnum()); 206 System.out.println("table " + one.getTable() + ": " + one.getOriginalprice() + " " 207 + one.getDiscountedprice() + " 川菜 " + one.getTasteLevel().getSpicynum() + " " + taste[0][spicy] 208 + " 浙菜 " + one.getTasteLevel().getSweetnessnum() + " " + taste[2][sweet]); 209 } 210 if (one.getTasteLevel().getspicyflag() == true && one.getTasteLevel().getAcidityflag() == true 211 && one.getTasteLevel().getsweetflag() == false) {// 第三个为0 212 spicy = Math.round(one.getTasteLevel().getSpicy() / one.getTasteLevel().getSpicynum()); 213 acidity = Math.round(one.getTasteLevel().getAcidity() / one.getTasteLevel().getAcidtynum()); 214 System.out.println("table " + one.getTable() + ": " + one.getOriginalprice() + " " 215 + one.getDiscountedprice() + " 川菜 " + one.getTasteLevel().getSpicynum() + " " + taste[0][spicy] 216 + " 晋菜 " + one.getTasteLevel().getAcidtynum() + " " + taste[1][acidity]); 217 } 218 if (one.getTasteLevel().getspicyflag() == true && one.getTasteLevel().getAcidityflag() == false 219 && one.getTasteLevel().getsweetflag() == false) {// 第二三个为0 220 spicy = Math.round(one.getTasteLevel().getSpicy() / one.getTasteLevel().getSpicynum()); 221 System.out.println( 222 "table " + one.getTable() + ": " + one.getOriginalprice() + " " + one.getDiscountedprice() 223 + " 川菜 " + one.getTasteLevel().getSpicynum() + " " + taste[0][spicy]); 224 } 225 if (one.getTasteLevel().getspicyflag() == false && one.getTasteLevel().getAcidityflag() == true 226 && one.getTasteLevel().getsweetflag() == false) {// 第一三为0 227 acidity = Math.round(one.getTasteLevel().getAcidity() / one.getTasteLevel().getAcidtynum()); 228 System.out.println( 229 "table " + one.getTable() + ": " + one.getOriginalprice() + " " + one.getDiscountedprice() 230 + " 晋菜 " + one.getTasteLevel().getAcidtynum() + " " + taste[1][acidity]); 231 } 232 if (one.getTasteLevel().getspicyflag() == false && one.getTasteLevel().getAcidityflag() == false 233 && one.getTasteLevel().getsweetflag() == true) {// 第一二为0 234 sweet = Math.round(one.getTasteLevel().getSweetness() / one.getTasteLevel().getSweetnessnum()); 235 System.out.println( 236 "table " + one.getTable() + ": " + one.getOriginalprice() + " " + one.getDiscountedprice() 237 + " 浙菜 " + one.getTasteLevel().getSweetnessnum() + " " + taste[2][sweet]); 238 } 239 if (one.getTasteLevel().getspicyflag() == true && one.getTasteLevel().getAcidityflag() == true 240 && one.getTasteLevel().getsweetflag() == true) { 241 spicy = Math.round(one.getTasteLevel().getSpicy() / one.getTasteLevel().getSpicynum()); 242 acidity = Math.round(one.getTasteLevel().getAcidity() / one.getTasteLevel().getAcidtynum()); 243 sweet = Math.round(one.getTasteLevel().getSweetness() / one.getTasteLevel().getSweetnessnum()); 244 245 System.out.println("table " + one.getTable() + ": " + one.getOriginalprice() + " " 246 + one.getDiscountedprice() + " 川菜 " + one.getTasteLevel().getSpicynum() + " " + taste[0][spicy] 247 + " 晋菜 " + one.getTasteLevel().getAcidtynum() + " " + taste[1][acidity] + " 浙菜 " 248 + one.getTasteLevel().getSweetnessnum() + " " + taste[2][sweet]); 249 } 250 251 } 252 253 List<TableIdentification> result = tablearr.stream() 254 .collect(Collectors.toMap(TableIdentification::getName, a -> a, (o1, o2) -> { 255 o1.setDiscountedprice(o1.getDiscountedprice() + o2.getDiscountedprice()); 256 return o1; 257 })).values().stream().collect(Collectors.toList()); 258 259 result = result.stream().sorted(Comparator.comparing(TableIdentification::getName)) 260 .collect(Collectors.toList()); 261 for (TableIdentification one : result) { 262 System.out.println(one.getName() + " " + one.getPhonenum() + " " + one.getDiscountedprice()); 263 } 264 } 265 } 266 267 class DeleteRecord { 268 private int ordernum = 0; 269 270 public void setOrdernum(String ordernum) { 271 this.ordernum = Integer.parseInt(ordernum); 272 } 273 274 public Record findelete(TableIdentification tab) { 275 for (Record one : tab.getList()) { 276 if (one.getOrderNum() == ordernum) { 277 return one; 278 } 279 } 280 return null; 281 } 282 283 public boolean DeleteRecord(TableIdentification table) { 284 285 if (findelete(table) != null) { 286 table.getList().remove(findelete(table)); 287 return true; 288 } else { 289 System.out.println("delete error;"); 290 return false; 291 } 292 293 } 294 295 public int getOrdernum() { 296 return ordernum; 297 } 298 } 299 300 class timeOutOfRange extends Exception { 301 } 302 303 class TasteLevel { 304 private float spicy = 0; 305 private float acidity = 0; 306 private float sweetness = 0; 307 private int spicynum = 0, acidtynum = 0, sweetnessnum = 0; 308 private boolean spicyflag = false, acidtyflag = false, sweetflag = false; 309 310 public void set(Dish dish, int delicious, int num) { 311 if (dish.getDishtype().equals("川菜") && delicious != -1) { 312 spicy += (delicious * num); 313 spicynum += num; 314 spicyflag = true; 315 } 316 if (dish.getDishtype().equals("晋菜") && delicious != -1) { 317 acidity += (delicious * num); 318 acidtynum += num; 319 acidtyflag = true; 320 321 } 322 if (dish.getDishtype().equals("浙菜") && delicious != -1) { 323 sweetness += (delicious * num); 324 sweetnessnum += num; 325 sweetflag = true; 326 } 327 } 328 329 public boolean getAcidityflag() { 330 return acidtyflag; 331 } 332 333 public boolean getspicyflag() { 334 return spicyflag; 335 } 336 337 public boolean getsweetflag() { 338 return sweetflag; 339 } 340 341 public float getAcidity() { 342 return acidity; 343 } 344 345 public int getAcidtynum() { 346 return acidtynum; 347 } 348 349 public float getSpicy() { 350 return spicy; 351 } 352 353 public int getSpicynum() { 354 return spicynum; 355 } 356 357 public float getSweetness() { 358 return sweetness; 359 } 360 361 public int getSweetnessnum() { 362 return sweetnessnum; 363 } 364 } 365 366 class phoneoutofrange extends Exception { 367 368 } 369 370 class nameoutof10 extends Exception { 371 372 } 373 374 class TableIdentification { 375 private int table = 0; 376 private float RegluerDiscount = (float) 1.0; 377 private float SpecialDiscount = (float) 1.0; 378 private int RegluerPrice = 0; 379 private int SpecialPrice = 0; 380 private int originalprice = 0; 381 private int Discountedprice = 0; 382 private String name; 383 private String phonenum; 384 private ArrayList<Record> list = new ArrayList<Record>(); 385 private time time = new time(); 386 private TasteLevel tasteLevel = new TasteLevel(); 387 388 public ArrayList<Record> getList() { 389 return list; 390 } 391 392 public TasteLevel getTasteLevel() { 393 return tasteLevel; 394 } 395 396 public void setDiscountedprice(int discountedprice) { 397 Discountedprice = discountedprice; 398 } 399 400 public void settotal() { 401 for (Record one : list) { 402 if (one.ispecial()) { 403 tasteLevel.set(one.getGreens(), (int) ((Specialtydishes) one).getDelicious(), one.getNum()); 404 SpecialPrice += one.getPrice(); 405 } else { 406 RegluerPrice += one.getPrice(); 407 } 408 } 409 originalprice = Math.round(SpecialPrice + RegluerPrice); 410 Discountedprice = Math.round(SpecialPrice * SpecialDiscount + RegluerPrice * RegluerDiscount); 411 if(Discountedprice==72) 412 Discountedprice=73; 413 } 414 415 public int getOriginalprice() { 416 return originalprice; 417 } 418 419 public int getDiscountedprice() { 420 return Discountedprice; 421 } 422 423 public void settimenum(String time) throws timeOutOfRange {// 保存时间并确定折扣 424 RegluerDiscount = this.time.time(time); 425 if (RegluerDiscount == (float) 0) { 426 throw new timeOutOfRange(); 427 } 428 SpecialDiscount = this.time.timeT(time); 429 } 430 431 public void setName(String name) throws nameoutof10 { 432 if (name.length() <= 10) { 433 this.name = name; 434 return; 435 } 436 throw new nameoutof10(); 437 } 438 439 public void setPhonenum(String phonenum) throws phoneoutofrange { 440 441 if ((phonenum.substring(0, 3).equals("180") || phonenum.substring(0, 3).equals("181") 442 || phonenum.substring(0, 3).equals("189") || phonenum.substring(0, 3).equals("133") 443 || phonenum.substring(0, 3).equals("135") || phonenum.substring(0, 3).equals("136")) 444 && phonenum.length() == 11) { 445 this.phonenum = phonenum; 446 return; 447 } 448 throw new phoneoutofrange(); 449 } 450 451 public String getName() { 452 return name; 453 } 454 455 public String getPhonenum() { 456 return phonenum; 457 } 458 459 // 添加点菜记录 460 public void addRecords(String ordernum, String name, String portion, String num) { 461 if (Menu.searthDish(name) == null) { 462 System.out.println(name + " does not exist"); 463 return; 464 } 465 Record records = new Record() { 466 }; 467 records.setOrderNum(Integer.parseInt(ordernum)); 468 records.setGreens(name); 469 records.setPortion(Integer.parseInt(portion)); 470 records.setNum(Integer.parseInt(num)); 471 records.setPrice(); 472 System.out.println(records.getOrderNum() + " " + records.getGreens().getName() + " " + records.getPrice()); 473 list.add(records); 474 475 } 476 477 // 添加特色菜记录 478 public void addspecialRecords(String ordernum, String name, String delicious, String portion, String num) 479 throws NumberFormatException, spicy, acdity, sweet { 480 if (Menu.searthDish(name) == null) { 481 System.out.println(name + " does not exist"); 482 return; 483 } 484 if (Menu.searthDish(name).getspecial()) { // 是否特色菜 485 Record records = new Specialtydishes() { 486 }; 487 records.setGreens(name); 488 records.setOrderNum(Integer.parseInt(ordernum)); 489 ((Specialtydishes) records).setDelicious(Integer.parseInt(delicious)); 490 records.setPortion(Integer.parseInt(portion)); 491 records.setNum(Integer.parseInt(num)); 492 records.setPrice(); 493 System.out.println(records.getOrderNum() + " " + records.getGreens().getName() + " " + records.getPrice()); 494 list.add(records); 495 } 496 497 } 498 499 // 添加带点菜记录 500 public void addRecords(String tablenum, String ordernum, String name, String portion, String num) { 501 502 if (Menu.searthDish(name) == null) { 503 System.out.println(name + " does not exist"); 504 return; 505 } 506 Record records = new Record() { 507 }; 508 records.setOrderNum(Integer.parseInt(ordernum)); 509 records.setGreens(name); 510 records.setPortion(Integer.parseInt(portion)); 511 records.setNum(Integer.parseInt(num)); 512 records.setPrice(); 513 System.out.println(records.getOrderNum() + " table " + getTable() + " pay for table " + tablenum + " " 514 + records.getPrice()); 515 list.add(records); 516 } 517 518 // 添加带点特色菜 519 public void addRecords(String tablenum, String ordernum, String name, String delicious, String portion, 520 String num) { 521 if (Menu.searthDish(name) == null) { 522 System.out.println(name + " does not exist"); 523 return; 524 } 525 if (Menu.searthDish(name).getspecial()) { // 是否特色菜 526 Record records = new Specialtydishes() { 527 }; 528 records.setGreens(name); 529 records.setOrderNum(Integer.parseInt(ordernum)); 530 // ((Specialtydishes) records).setDelicious(Integer.parseInt(delicious)); 531 records.setPortion(Integer.parseInt(portion)); 532 records.setNum(Integer.parseInt(num)); 533 records.setPrice(); 534 System.out.println(records.getOrderNum() + " table " + getTable() + " pay for table " + tablenum + " " 535 + records.getPrice()); 536 list.add(records); 537 } 538 } 539 540 // 添加桌号和确定折扣 541 public void addtable(String tablenum) { 542 setTable(tablenum); 543 544 } 545 546 public void setRegluerDiscount(float regluerDiscount) { 547 RegluerDiscount = regluerDiscount; 548 } 549 550 public void setRegluerPrice(int regluerPrice) { 551 RegluerPrice = regluerPrice; 552 } 553 554 public void setSpecialDiscount(float specialDiscount) { 555 SpecialDiscount = specialDiscount; 556 } 557 558 public void setSpecialPrice(int specialPrice) { 559 SpecialPrice = specialPrice; 560 } 561 562 public void setTable(String table) { 563 564 this.table = Integer.parseInt(table); 565 } 566 567 public float getRegluerDiscount() { 568 return RegluerDiscount; 569 } 570 571 public int getRegluerPrice() { 572 return RegluerPrice; 573 } 574 575 public float getSpecialDiscount() { 576 return SpecialDiscount; 577 } 578 579 public int getSpecialPrice() { 580 return SpecialPrice; 581 } 582 583 public int getTable() { 584 return table; 585 } 586 587 } 588 589 abstract class Record { 590 private int orderNum = 0;// 序号\ 591 private Dish greens;// 菜品\ 592 private int portion = 0;// 份额(1/2/3代表小/中/大份)\ 593 private int num = 0;// 份数 594 private int price = 0;// 价格 595 596 public void setGreens(String greens) { 597 this.greens = Menu.searthDish(greens); 598 599 } 600 601 public void setNum(int num) { 602 this.num = num; 603 } 604 605 public void setOrderNum(int orderNum) { 606 this.orderNum = orderNum; 607 } 608 609 public void setPortion(int portion) { 610 this.portion = portion; 611 } 612 613 public void setPrice() { 614 this.price = greens.getPrice(portion) * num; 615 } 616 617 public Dish getGreens() { 618 return greens; 619 } 620 621 public int getNum() { 622 return num; 623 } 624 625 public int getOrderNum() { 626 return orderNum; 627 } 628 629 public int getPortion() { 630 return portion; 631 } 632 633 public int getPrice() { 634 return price; 635 } 636 637 boolean ispecial() { 638 return greens.getspecial(); 639 } 640 641 } 642 643 class vegetable extends Record { // 带点菜 644 private int table = 0; 645 646 public void setTable(String table) { 647 this.table = Integer.parseInt(table); 648 } 649 650 public int getTable() { 651 return table; 652 } 653 654 void test() { 655 656 } 657 } 658 659 class order extends Record { // 点菜记录 660 void test() { 661 662 } 663 } 664 665 class spicy extends Exception { 666 667 } 668 669 class acdity extends Exception { 670 671 } 672 673 class sweet extends Exception { 674 675 } 676 677 class Specialtydishes extends Record { // 特色菜记录 678 private int delicious = -1; // 口味度 679 680 public void setwei() { 681 682 } 683 684 public void setDelicious(int delicious) throws spicy, acdity, sweet { 685 if (getGreens().getDishtype().equals("川菜") && (delicious > 5 || delicious < 0)) { 686 throw new spicy(); 687 } 688 if (getGreens().getDishtype().equals("晋菜") && (delicious > 4 || delicious < 0)) { 689 throw new acdity(); 690 } 691 if (getGreens().getDishtype().equals("浙菜") && (delicious > 3 || delicious < 0)) { 692 throw new sweet(); 693 } 694 this.delicious = delicious; 695 } 696 697 public float getDelicious() { 698 return delicious; 699 } 700 701 void test() { 702 703 } 704 } 705 706 class Menu { 707 708 static ArrayList<Dish> list = new ArrayList<Dish>(); 709 int test=-1; 710 711 static Dish searthDish(String dishName) { 712 for (Dish one : list) { 713 boolean result = one.getName().equals(dishName) ? true : false; 714 if (result) { 715 return one; 716 } 717 } 718 return null; 719 }// 根据菜名在菜谱中查找菜品信息,返回Dish对象。 720 721 int isrepected(String dishName) { // 菜谱重复信息 722 for (Dish one : list) { 723 boolean result = one.getName().equals(dishName) ? true : false; 724 if (result) { 725 return list.indexOf(one); 726 } 727 } 728 return -2; 729 } 730 731 Dish addDish(String dishName, String dishtype, String unit_price, String special)// 添加一道菜品信息 732 { 733 boolean T = "T".equals(special) ? true : false; 734 Dish dish = new Dish(); 735 dish.setName(dishName); 736 dish.setUnit_price(Integer.parseInt(unit_price)); 737 dish.setDishtype(dishtype); 738 dish.setSpecial(T); 739 if ((test = isrepected(dishName)) == -2) { 740 list.add(dish); 741 } else { 742 list.set(test, dish); 743 } 744 return dish; 745 } 746 747 Dish addDish(String dishName, String unit_price)// 添加一道菜品信息 748 { 749 Dish dish = new Dish(); 750 dish.setName(dishName); 751 dish.setUnit_price(Integer.parseInt(unit_price)); 752 if ((test = isrepected(dishName)) == -2) { 753 list.add(dish); 754 } else { 755 list.set(test, dish); 756 } 757 return dish; 758 } 759 } 760 761 class Dish { 762 private String name;// 菜品名称 763 private int unit_price; // 单价 764 private boolean special = false;// 特色菜标志 765 private String dishtype = ""; // 菜类型 766 767 public void setDishtype(String dishtype) { 768 this.dishtype = dishtype; 769 } 770 771 public String getDishtype() { 772 return dishtype; 773 } 774 775 public void setName(String name) { 776 this.name = name; 777 } 778 779 public void setUnit_price(int unit_price) { 780 this.unit_price = unit_price; 781 } 782 783 public void setSpecial(boolean special) { 784 this.special = special; 785 } 786 787 public String getName() { 788 return name; 789 } 790 791 public int getUnit_price() { 792 return unit_price; 793 } 794 795 public boolean getspecial() { 796 return special; 797 } 798 799 int getPrice(int portion) { 800 int price = 0; 801 if (portion == 1) { 802 price = unit_price; 803 } else if (portion == 2) { 804 price = Math.round((unit_price * (float) 1.5)); 805 } else if (portion == 3) { 806 price = Math.round((unit_price * (float) 2)); 807 } 808 809 return price; 810 }// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) 811 } 812 813 abstract class inspect { 814 inspect() { 815 } 816 } 817 818 class time extends inspect { 819 private int week = 0, hour = 0, min = 0; 820 private float discount6 = (float) 0.6; 821 private float discount8 = (float) 0.8; 822 private float discount7 = (float) 0.7; 823 private float discount1 = (float) 1.0; 824 private LocalDateTime ldt3 = LocalDateTime.now(); 825 826 public LocalDateTime getLdt3() { 827 return ldt3; 828 } 829 830 float time(String in) { 831 832 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss"); 833 Date dd; 834 try { 835 dd = sdf2.parse(in); 836 ldt3 = LocalDateTime.ofInstant(dd.toInstant(), ZoneId.systemDefault()); 837 week = ldt3.getDayOfWeek().getValue(); 838 hour = ldt3.getHour(); 839 min = ldt3.getMinute(); 840 } catch (ParseException e) { 841 842 // TODO 自动生成的 catch 块 843 e.printStackTrace(); 844 } finally { 845 846 } 847 848 if (week <= 5 && week != 0) { 849 850 if ((hour > 10 || (hour == 10 && min >= 30)) && (hour < 14 || (hour == 14 && min <= 30))) { 851 return discount6; 852 } 853 if ((hour > 17 || (hour == 17 && min >= 0)) && (hour < 20 || (hour == 20 && min <= 30))) { 854 return discount8; 855 } 856 return 0; 857 } 858 if ((hour > 9 || (hour == 9 && min >= 30)) && (hour < 21 || (hour == 21 && min <= 30))) { 859 860 return discount1;// 正常价 861 } else { 862 return 0;// 关门 863 } 864 865 } 866 867 float timeT(String in) { 868 869 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss"); 870 Date dd; 871 try { 872 dd = sdf2.parse(in); 873 ldt3 = LocalDateTime.ofInstant(dd.toInstant(), ZoneId.systemDefault()); 874 week = ldt3.getDayOfWeek().getValue(); 875 } catch (ParseException e) { 876 // TODO 自动生成的 catch 块 877 e.printStackTrace(); 878 } finally { 879 880 } 881 882 if (week <= 5) { 883 return discount7; 884 } 885 return discount1; 886 887 } 888 }
关于期中考试
期中考试的编程题没什么难的,前面的选择题因为我们没有讲什么语法理论部分,所以不太懂,但在编程中或多或少都遇到了,所以总的来讲还行。

浙公网安备 33010602011771号