首页 > 其他分享 >题目集4-5及期中期末考试

题目集4-5及期中期末考试

时间:2023-06-30 15:57:04浏览次数:40  
标签:题目 string int 期中 record 期末考试 time dish table

一、前言:总结三次题目集的知识点、题量、难度等情况

对于我来说第4、5次题目集难度较大,题量适中。

期中考试难度一般,题量适中。

知识点考察的是接口,对编程中时间信息的处理,类,继承等等。

二、设计与分析

没怎么写出来菜单题,分析不了源码。

分析一下就是自己的态度有问题,没有迎难而上,也不愿意花时间去学习,畏难退却了。导致后面想重新捡起的时候非常困难,只能拿非常低的分。

下面是自己的代码,但因为能力加态度问题,得分很低,并且当时做的时候注释不够详尽,不值得做参考。

  1 import java.io.BufferedReader;
  2 import java.io.IOException;
  3 import java.io.InputStreamReader;
  4 import java.time.*;
  5 import java.util.*;
  6 
  7 public class Main {
  8     public static void main(String[] args) throws IOException {
  9         Menu menu = new Menu();
 10         ArrayList<Table> tables = new ArrayList<>();
 11         ArrayList<Customer> customers = new ArrayList<>();
 12         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 13 
 14         String string;
 15         int m;
 16         int portion;        //分量
 17         int n;          //份数
 18         while (true) {          // 输入菜单
 19             Dish dish = new Dish();
 20             int isRepeat;
 21             string = br.readLine();
 22             if (string.matches("\\S* [1-9]" + "\\d*")) {
 23                 String[] token = string.split(" ");
 24                 dish.name = token[0];
 25                 dish.unit_price = Integer.parseInt(token[1]);
 26                 if (dish.unit_price > 300) {
 27                     System.out.println(dish.name + " price out of range " + dish.unit_price);
 28                     continue;
 29                 }
 30                 dish.isEffective = false;
 31                 isRepeat = menu.searchDish(dish.name);
 32                 if (isRepeat != -1) {
 33                     menu.dishes.remove(isRepeat);
 34                 }
 35                 menu.dishes.add(dish);
 36             } else if (string.matches("\\S* \\S* \\d* T")) {
 37                 String[] array = string.split(" ");
 38                 dish.name = array[0];
 39                 dish.unit_price = Integer.parseInt(array[2]);
 40                 if (dish.unit_price > 300) {
 41                     System.out.println(dish.name + " price out of range " + dish.unit_price);
 42                     continue;
 43                 }
 44                 dish.isEffective = true;
 45                 switch (array[1]) {
 46                     case "川菜":
 47                         dish.type = "spicy";
 48                         dish.maxFlavor = 5;
 49                         break;
 50                     case "晋菜":
 51                         dish.type = "acidity";
 52                         dish.maxFlavor = 4;
 53                         break;
 54                     case "浙菜":
 55                         dish.type = "sweetness";
 56                         dish.maxFlavor = 3;
 57                         break;
 58                 }
 59                 isRepeat = menu.searchDish(dish.name);
 60                 if (isRepeat != -1) {
 61                     menu.dishes.remove(isRepeat);
 62                 }
 63                 menu.dishes.add(dish);
 64             } else if (string.equals("end")) {
 65                 break;
 66             } else if (string.matches("tab.*")) {
 67                 break;
 68 
 69             } else {
 70                 printWrongFormat();
 71             }
 72         }
 73         while (!string.equals("end")) {
 74             Table table = new Table();
 75             boolean isRepeat = false;
 76             int nRepeat = 0;
 77             if (string.matches(".*t.*a.*")) {
 78                 if (string.matches(
 79                         "table [1-9][\\d]* : [\\S]* [\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {
 80                     String[] token = string.split(" ");
 81                     table.customerName = token[3];
 82                     if(token[3].length()>10) {
 83                         string = br.readLine();
 84                         printWrongFormat();
 85                         continue;
 86                     }
 87                     table.telephone = token[4];
 88                     if(token[4].length()!=11) {
 89                         string = br.readLine();
 90                         printWrongFormat();
 91                         continue;
 92                     }
 93                     String headNum = token[4].substring(0, 3);
 94                     if(!headNum.equals("180")&&!headNum.equals("181")&&!headNum.equals("189")&&!headNum.equals("133")&&!headNum.equals("135")&&!headNum.equals("136")) {
 95                         string = br.readLine();
 96                         printWrongFormat();
 97                         continue;
 98                     }
 99                     String[] Date = token[5].split("/");
100                     String[] Time = token[6].split("/");
101                     int[] date = new int[5];
102                     int[] time = new int[6];
103                     for (m = 0; m < 3; m++) {
104                         date[m] = Integer.parseInt(Date[m]);
105                         time[m] = Integer.parseInt(Time[m]);
106                     }
107                     table.num = Integer.parseInt(token[1]);
108                     if (table.num > 55) {
109                         System.out.println(table.num + " table num out of range");
110                         string = br.readLine();
111                         continue;
112 
113                     }
114                     try {
115                         table.time = LocalDateTime.of(date[0], date[1], date[2], time[0], time[1],
116                                 time[2]);
117                         table.getWeekDay();
118                     } catch (DateTimeException e) {
119                         System.out.println(table.num + " date error");
120                         string = br.readLine();
121                         continue;
122                     }
123                     if (!table.isBusinessHour()) {
124                         System.out.println("table " + table.num + " out of opening hours");
125                         string = br.readLine();
126                         continue;
127                     }
128                     if (!(table.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))))
129                         if(table.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))
130                         {
131                             System.out.println("not a valid time period");
132                             string = br.readLine();
133                             continue;
134                         }
135                     // 判断桌号是否重复
136                     if (table.isBusinessHour()) {
137                         for (m = 0; m < tables.size(); m++) {
138                             if (table.num == tables.get(m).num && tables.get(m).isBusinessHour()) {
139                                 Duration duration = Duration.between(table.time, tables.get(m).time);
140                                 if (duration.toDays() == 0) {
141                                     if (table.weekday > 0 && table.weekday < 6) {
142                                         if (table.time.getHour() < 15 && tables.get(m).time.getHour() < 15) {
143                                             isRepeat = true;
144                                             nRepeat = m;
145                                             break;
146                                         }
147                                     }
148                                     else {
149                                         if (duration.toHours() < 3600) {
150                                             nRepeat = m;
151                                             isRepeat = true;
152                                             break;
153                                         }
154                                     }
155                                 }
156                             }
157                         }
158                     }
159                     System.out.println("table " + table.num + ": ");
160 
161                 } else {
162                     printWrongFormat();
163                     string = br.readLine();
164                     continue;
165                 }
166                 // 本桌开始点菜
167                 while (true) {
168                     string = br.readLine();
169                     //判断是否为非特色菜
170                     if (string.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
171                         String[] array = string.split(" ");
172                         portion = Integer.parseInt(array[2]);
173                         n = Integer.parseInt(array[3]);
174                         if (menu.searchDish(array[1]) == -1) {
175                             System.out.println(array[1] + " does not exist");
176                             continue;
177                         }
178                         Record record = new Record();
179                         record.dish = menu.dishes.get(menu.searchDish(array[1]));
180                         record.number = Integer.parseInt(array[0]);
181                         record.portion = portion;
182                         record.n = n;
183                         table.records.add(record);
184                         System.out.println(record.number +" "+array[1]+ " " +record.getPrice());
185                     }
186                     //判断是否为特色菜
187                     else if(string.matches("[1-9][\\d]* [\\S]* [\\d] [\\d] [1-9][\\d]*")) {
188                         String[] array = string.split(" ");
189                         int flavorNum = Integer.parseInt(array[2]);
190                         if(flavorNum<0||flavorNum>menu.dishes.get(menu.searchDish(array[1])).maxFlavor) {
191                             System.out.println(menu.dishes.get(menu.searchDish(array[1])).type +" num out of range :"+flavorNum);
192                             continue;
193                         }
194                         portion = Integer.parseInt(array[3]);
195                         n = Integer.parseInt(array[4]);
196                         if (menu.searchDish(array[1]) == -1) {
197                             System.out.println(array[1] + " does not exist");
198                             continue;
199                         }
200                         Record tempRecord = new Record();
201                         tempRecord.dish = menu.dishes.get(menu.searchDish(array[1]));
202                         tempRecord.flavorNum = flavorNum;
203                         tempRecord.number = Integer.parseInt(array[0]);
204                         tempRecord.portion = portion;
205                         tempRecord.n = n;
206                         table.records.add(tempRecord);
207                         System.out.println(tempRecord.number +" "+array[1]+" "+ tempRecord.getPrice());
208                     }
209                     // 判断是否为删除订单
210                     else if (string.matches("[1-9][\\d]* delete")) {
211                         String[] array = string.split(" ");
212                         int delNum = Integer.parseInt(array[0]);
213                         for(m=0;m<table.records.size();m++) {
214                             if(table.records.get(m).number ==delNum) {
215                                 if(!table.records.get(delNum).hasDeleted) {
216                                     table.records.get(delNum).hasDeleted = true;
217                                 }
218                                 else System.out.println("deduplication :"+delNum);
219                                 break;
220                             }
221                             if(m==table.records.size()-1)System.out.println("delete error");
222                         }
223 
224                     }
225                     // 判断是否为夹杂菜单
226                     else if (string.matches("[\\S]* [\\d]*")) {
227                         System.out.println("invalid dish");
228                     } else if (string.matches("\\S* \\S* [\\d]* T")) {
229                         System.out.println("invalid dish");
230                     }
231                     // 判断是否为代点
232                     else if (string.matches("\\d* \\d* [\\S].*")) {
233                         String[] token = string.split(" ");
234                         // 判断代点桌号是否存在
235                         boolean exist = false;
236                         int j;
237                         for (j = 0; j < tables.size(); j++) {
238                             if (tables.get(j).num == Integer.parseInt(token[0])) {
239                                 exist = true;
240                                 break;
241                             }
242                         }
243                         if (exist) {
244                             System.out.print(Integer.parseInt(token[1]) + " table " + table.num + " pay for table "
245                                     + Integer.parseInt(token[0]) + " ");
246                             Record treat = new Record();
247                             treat.dish = menu.dishes.get(menu.searchDish(token[2]));
248                             if(treat.dish.isEffective) {
249                                 treat.flavorNum = Integer.parseInt(token[3]);
250                                 treat.portion = Integer.parseInt(token[4]);
251                                 treat.n = Integer.parseInt(token[5]);
252                                 if(treat.dish.type.equals("spicy")) {
253                                     tables.get(j).aSpicy +=treat.n;
254                                     tables.get(j).spicy +=treat.flavorNum *treat.n;
255                                 }
256                                 if(treat.dish.type.equals("acidity")) {
257                                     tables.get(j).aAcidity +=treat.n;
258                                     tables.get(j).spicy +=treat.flavorNum *treat.n;
259                                 }
260                                 if(treat.dish.type.equals("sweetness")) {
261                                     tables.get(j).aSweetness +=treat.n;
262                                     tables.get(j).spicy +=treat.flavorNum *treat.n;
263                                 }
264                             }
265                             else {
266                                 treat.portion = Integer.parseInt(token[3]);
267                                 treat.n = Integer.parseInt(token[4]);
268                             }
269                             System.out.print(treat.getPrice() + "\n");
270                             treat.hasTreat = true;
271                             table.records.add(treat);
272                         }
273                         // 若不存在则输出内容
274                         else {
275                             System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
276                         }
277 
278                     } else if (string.equals("end")) {
279                         break;
280                     } else if (string.matches("table.*")) {
281                         break;
282                     } else {
283                         printWrongFormat();
284                     }
285                 }
286                 // 本桌点菜结束,进入下一桌
287                 if (isRepeat) {
288                     for (m = 0; m < tables.get(nRepeat).records.size(); m++) {
289                         for (int n = 0; n < table.records.size(); n++) {
290                             if (Objects.equals(tables.get(nRepeat).records.get(m).dish.name, table.records.get(n).dish.name)) {
291                                 if (tables.get(nRepeat).records.get(m).portion == table.records
292                                         .get(n).portion) {
293                                     tables.get(nRepeat).records.get(m).n += table.records.get(n).n;
294                                     table.records.remove(n);
295                                 }
296                             }
297                         }
298 
299                     }
300                     tables.get(nRepeat).records.addAll(table.records);
301                     continue;
302                 }
303                 tables.add(table);
304             } else {
305                 string = br.readLine();
306             }
307         }
308 
309         // 点餐桌信息
310         for (m = 0; m < tables.size(); m++) {
311             if (tables.get(m).isBusinessHour()) {
312                 tables.get(m).sum();
313                 tables.get(m).getFlavorNum();
314                 if(m!=0)System.out.print("\n");
315                 tables.get(m).showEnd();
316                 boolean isRepeat = false;
317                 for (Customer customer : customers) {
318                     if (tables.get(m).customerName.equals(customer.name)) {
319                         customer.needToPay += tables.get(m).sum;
320                         isRepeat = true;
321                         break;
322                     }
323                 }
324                 if(!isRepeat) {
325                     Customer customer = new Customer();
326                     customer.name = tables.get(m).customerName;
327                     customer.telephone = tables.get(m).telephone;
328                     customer.needToPay = tables.get(m).sum;
329                     customers.add(customer);
330                 }
331             }
332 
333         }
334         customers.sort(Comparator.comparing(a -> a.name));
335         for(m=0; m< customers.size(); m++) {
336             System.out.print("\n"+ customers.get(m).name +" "+ customers.get(m).telephone +" "+ customers.get(m).needToPay);
337         }
338     }
339 
340     static class Dish {
341         String name;
342         String type;
343         int maxFlavor;
344         int unit_price;
345         boolean isEffective = false;
346     }
347 
348     static class Record {
349         int number;
350         Dish dish;
351         int flavorNum;
352         int portion;
353         int n;
354         boolean hasDeleted = false;
355         boolean hasTreat  = false;
356 
357         int getPrice() {
358             double[] m = {1, 1.5, 2};
359             return (int) Math.round(dish.unit_price*m[portion-1]*n);
360         }
361     }
362 
363     static class Menu {
364         ArrayList<Dish> dishes = new ArrayList<>();
365 
366         int searchDish(String dishName) {
367             for (int i = 0; i < dishes.size(); i++) {
368                 if (dishName.equals(dishes.get(i).name)) {
369                     return i;
370                 }
371             }
372             return -1;
373         }
374 
375     }
376 
377     static class Customer {
378         String name;
379         String telephone;
380         long needToPay;
381     }
382     static class Table {
383         ArrayList<Record> records = new ArrayList<>();
384         int num;
385         String customerName;
386         String telephone;
387         LocalDateTime time;
388         int weekday;
389         int aSpicy = 0;
390         float spicy = 0;
391         int aAcidity = 0;
392         float acidity = 0;
393         int aSweetness = 0;
394         float sweetness = 0;
395         long sum = 0;
396         long origSum = 0;
397 
398         void getWeekDay() {
399             weekday = time.getDayOfWeek().getValue();
400         }
401         void getFlavorNum() {
402             for (Record record : records) {
403                 if (record.dish.isEffective && !record.hasDeleted && !record.hasTreat) {
404                     if (record.dish.type.equals("spicy")) {
405                         aSpicy += record.n;
406                         spicy += record.flavorNum * record.n;
407                     }
408                     if (record.dish.type.equals("acidity")) {
409                         aAcidity += record.n;
410                         acidity += record.flavorNum * record.n;
411                     }
412                     if (record.dish.type.equals("sweetness")) {
413                         aSweetness += record.n;
414                         sweetness += record.flavorNum * record.n;
415                     }
416                 }
417             }
418             if(aSpicy !=0) spicy = Math.round(spicy / aSpicy *1.0);
419             if(aAcidity !=0)acidity = Math.round(acidity/ aAcidity *1.0);
420             if(aSweetness !=0) sweetness = Math.round(sweetness / aSweetness *1.0);
421         }
422         void showEnd() {
423             System.out.print("table " + num + ": " + origSum + " " + sum);
424             //川菜
425             if(aSpicy !=0) {
426                 String[] spicy = {"不辣","微辣","稍辣","辣","很辣","爆辣"};
427                 System.out.print(" "+ aSpicy +" "+spicy[aSpicy]);
428             }
429             //晋菜
430             if(aAcidity !=0) {
431                 String[] acidity = {"不酸","微酸","稍酸","酸","很酸"};
432                 System.out.print(" "+ aAcidity +" "+acidity[aAcidity]);
433             }
434             //浙菜
435             if(aSweetness !=0) {
436                 String[] sweetness = {"不甜","微甜","稍甜","甜"};
437                 System.out.print(" "+ aSweetness +" "+sweetness[aSweetness]);
438             }
439 
440         }
441         void sum() {
442             for (Record record : records) {
443                 if (!record.hasDeleted) {
444                     origSum += record.getPrice();
445                     if (record.dish.isEffective) {
446                         if (weekday > 0 && weekday < 6) {
447                             sum += Math.round(record.getPrice() * 0.7);
448                         } else {
449                             sum += record.getPrice();
450                         }
451                     } else {
452                         if (weekday > 0 && weekday < 6) {
453                             if (time.getHour() >= 17 && time.getHour() < 20)
454                                 sum += Math.round(record.getPrice() * 0.8);
455                             if (time.getHour() == 20) {
456                                 if (time.getMinute() <= 30)
457                                     sum += Math.round(record.getPrice() * 0.8);
458                             }
459                             if (time.getHour() >= 10 && time.getHour() < 14)
460                                 sum += Math.round(record.getPrice() * 0.6);
461                             if (time.getHour() == 14) {
462                                 if (time.getMinute() <= 30)
463                                     sum += Math.round(record.getPrice() * 0.6);
464                             }
465                         } else
466                             sum += record.getPrice();
467                     }
468                 }
469             }
470 
471         }
472 
473         boolean isBusinessHour() {
474             if (weekday > 0 && weekday < 6) {
475                 if (time.getHour() >= 17 && time.getHour() < 20)
476                     return true;
477                 if (time.getHour() == 20) {
478                     if (time.getMinute() <= 30)
479                         return true;
480                 }
481                 if (time.getHour() > 10 && time.getHour() < 14)
482                     return true;
483                 if (time.getHour() == 10) {
484                     if (time.getMinute() >= 30)
485                         return true;
486                 }
487                 if (time.getHour() == 14) {
488                     return time.getMinute() <= 30;
489                 }
490             } else {
491                 if (time.getHour() > 9 && time.getHour() < 21)
492                     return true;
493                 if (time.getHour() == 9) {
494                     if (time.getMinute() >= 30)
495                         return true;
496                 }
497                 if (time.getHour() == 21) {
498                     return time.getMinute() <= 30;
499                 }
500             }
501             return false;
502 
503         }
504     }
505     public static void printWrongFormat(){
506         System.out.println("wrong format");
507     }
508 }

三、采坑心得

对我来说,出现的问题很大,有基本上处处都是坑,因为前面的菜单题没能好好完成,后面的菜单题也陷入了困难,因为后面的题目是基于前面的题目进行修改的,导致后来去弥补的这个过程难上加难。

因为上述原因,很难拿结果和源码、测试结果说话。

四、主要困难以及改进建议

主要困难是自己编程知识的不足,常常要去网上搜索相关知识,进度缓慢。这导致我期中考试的时候拿了很低的分,因为期中考试禁止百度和翻课本。

对于菜单题目的编码,我觉得如果可以的话,尽量做到题目的简洁化,题目太过繁琐,很容易让初学者陷入窘境。

五、总结:对本阶段三次题目集的综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。

虽然我在这三次题目集中并没有得到很高的份数,但我确实学到了很多,接口,类等方面的知识慢慢地从无到有,现在已经能使用得较为流畅了;也大大提高了我的自学能力。

我的建议是希望老师课堂上能够结合我们做过的刚结束不久的PTA上的题目跟我们讲课,这样才能让我们学生对课堂上的理论知识有更好的理解。

标签:题目,string,int,期中,record,期末考试,time,dish,table
From: https://www.cnblogs.com/230523-abc/p/17516912.html

相关文章

  • 4-5-期中
    前言:关于类:1、类似C++中的struct,构造函数、内置方法(函数)都比较相似2、但是无法重载运算符,这是比较难受的一点。3、尽量避免代码的重复,把private和public的方法搞清晰。4、把我们需要解决的问题进行拆分,拆得好就写得快关于java语法:1、小心使用运算符,第一次作业用==去比较两个Big......
  • BLOG2-PTA题目集4、5以及期中考试
    (1)前言本次博客主要涵盖了Java题目的几个主要知识点,包括:1.面向对象的基础知识:这部分主要包括了类和对象的基本概念,构造方法,访问权限和成员变量的相关内容。在面向对象编程中,对这些基础知识的理解至关重要。2.面向对象的设计原则:这个题目强调了两个重要的设计原则,即继承和组......
  • 4、5及期中考试总结
    一.前言Java编程语言是当今最流行的编程语言之一,由于其跨平台性、面向对象性和安全性等特点,受到广泛的应用。作为一名计算机专业的学生,在学习Java编程语言时,我们需要完成多个作业来巩固所学知识。在前三次Java作业中,我们已经学习了Java的基础知识和常用技术,通过完成这些作业,我们......
  • 余同取余的一道变式题目
    二刷,本题感悟:善于从全局出发,也要学会从个例中找特点。根据题目信息可得:÷5...2÷8...7÷7...6如果三个一起看,未能找出规律,似乎不符合余同取余。但是如果孤立出后两项:÷8...7÷7...6可以发现除数与余数差为1......
  • 菜单4、5以及期中考试总结-BLOG-PTA-4、5
    22201612-刘健涛目录(1)前言(2)设计与分析(3)踩坑心得(4)改进建议(5)总结正文(1)前言  (2)设计与分析(3)踩坑心得(4)改进建议(5)总结......
  • 4-5次及期中考试PTA题目总结
    前言 第四次和第五次pta每次都只有一道题目,分别是菜单计价4和菜单计价5,没有了其他小题pta拿起分来变得困难了许多。不过好在题目要求上菜单计价4和菜单计价5只是菜单计价3的两个分支,没有太多需要叠加的功能,避免的题目过于的复杂。 第四次的菜单计价在菜单计价三的基础上没有加......
  • PTA4-5及期中总结
    (1)前言期中考试的题目集总体来说还是很简单的,题目量比较少而且难度偏易,考察的知识点可以说是很基础的面向对象编程知识点,基本上就是在考验我们的基本功扎不扎实,对于知识点很熟悉的同学可以很快的完成大部分题目,但是还有个接口题目,需要使用java自带类,这题先前没有遇到过就会做......
  • BLOG_OOP_期中考试
    前言涉及知识点1.对于创建对象和类的初步实践;如构建圆类和矩形类;1.对于抽象类和继承与多态的认识;如构建shape类;题量不多,可以完成。难度不大,可以完成。设计与分析题目源码如下importjava.util.*;publicclassMain{publicstaticvoidmain(String[]......
  • pta题目集4~5及期中考试总结性blog
    一、前言总结三次题目集的知识点、题量、难度等情况第四次题目集主要更新了各种异常情况,是对代码正确度的深入考察,涉及时间的格式问题,信息的格式问题各种格式问题等等,涉及到hashset、面向对象编程的封装性、BigDecimal类关于精确小数的运算以及了解Scanner类中nextLine()等方法......
  • 面向对象程序编程PTA题目集4、5以及期中考试的总结性Blog
    1.对之前发布的PTA题目集4、5以及期中考试的总结性Blog,内容要求如下:(1)前言:总结之前所涉及到的知识点、题量、难度等情况期中考试作业:知识点:主要就是考了对类的使用,和不同类间的相互配合使用,还有对于一个程序的不断循环使用(相比之前更加灵活,可以自定义输入的个数),正则表达(可用可不......