首页 > 其他分享 >4-5次PTA题目总结blog

4-5次PTA题目总结blog

时间:2023-06-30 18:00:51浏览次数:37  
标签:题目 tem temp int System PTA blog time out

前言:

题目集 1~3 的知识点、题量、难度等情况如下:

  • 知识点:JAVA基础,基础算法,面向对象程序设计

  • 题量:共计 2 道题目

  • 难度:题目从易到难,逐层递进,分别为考察Java各个类的理解、掌握与运用。

设计与分析:

  1 import java.text.ParseException;
  2 import java.time.DateTimeException;
  3 import java.time.Duration;
  4 import java.time.LocalDateTime;
  5 import java.util.ArrayList;
  6 import java.util.Scanner;
  7 
  8 public class Main {
  9     public static boolean isNumeric(String string) {
 10         int intValue;
 11         try {
 12             intValue = Integer.parseInt(string);
 13             return true;
 14         } catch (NumberFormatException e) {
 15             return false;
 16         }
 17     }
 18 
 19     public static void main(String[] args) throws ParseException {
 20         Menu menu = new Menu();
 21         ArrayList<Table> tables = new ArrayList<Table>();
 22         Scanner input = new Scanner(System.in);
 23         String str = new String();
 24         int i = 0;
 25         int portion = 0, quota = 0;
 26         while (true) {// 输入菜单
 27             Dish tem = new Dish();
 28             int repeat = -1;
 29             str = input.nextLine();
 30             if (str.matches("[\\S]* [1-9][\\d]*")) {
 31                 String[] take = str.split(" ");
 32                 tem.name = take[0];
 33                 tem.unit_price = Integer.parseInt(take[1]);
 34                 if (tem.unit_price > 300) {
 35                     System.out.println(tem.name + " price out of range " + tem.unit_price);
 36                     continue;
 37                 }
 38                 tem.isT = false;
 39                 repeat = menu.searchDish(tem.name);
 40                 if (repeat != -1) {
 41                     menu.dishs.remove(repeat);
 42                 }
 43                 menu.dishs.add(tem);
 44             } else if (str.matches("[\\S]* [\\d]* T")) {
 45                 String[] take = str.split(" ");
 46                 tem.name = take[0];
 47                 tem.unit_price = Integer.parseInt(take[1]);
 48                 if (tem.unit_price > 300) {
 49                     System.out.println(tem.name + " price out of range " + tem.unit_price);
 50                     continue;
 51                 }
 52                 tem.isT = true;
 53                 if (repeat != -1) {
 54                     menu.dishs.remove(repeat);
 55                 }
 56                 menu.dishs.add(tem);
 57             } else if (str.equals("end")) {
 58                 break;
 59             } else if (str.matches("tab.*")) {
 60                 break;
 61 
 62             } else {
 63                 System.out.println("wrong format");
 64                 continue;
 65             }
 66         }
 67         while (!str.equals("end")) {
 68             Table tem = new Table();
 69             boolean repeat = false;
 70             int repeatNum = 0;
 71             if(str.matches(".*t.*a.*")) {
 72                 if (str.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {
 73                     String[] take = str.split(" ");
 74                     String[] Date = take[2].split("/");
 75                     String[] Time = take[3].split("/");
 76                     int[] intDate = new int[3];
 77                     int[] intTime = new int[3];
 78                     for (i = 0; i < 3; i++) {
 79                         intDate[i] = Integer.parseInt(Date[i]);
 80                         intTime[i] = Integer.parseInt(Time[i]);
 81                     }
 82                     tem.num = Integer.parseInt(take[1]);
 83                     if (tem.num > 55) {
 84                         System.out.println(tem.num + " table num out of range");
 85                         str = input.nextLine();
 86                         continue;
 87 
 88                     }
 89                     try {
 90                         tem.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1],
 91                                 intTime[2]);
 92                         tem.getWeekDay();
 93                     } catch (DateTimeException e) {
 94                         System.out.println(tem.num + " date error");
 95                         str = input.nextLine();
 96                         continue;
 97                     }
 98                     if (!tem.isOpen()) {
 99                         System.out.println("table " + tem.num + " out of opening hours");
100                         str = input.nextLine();
101                         continue;
102                     }
103                     if (!(tem.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))
104                             && tem.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {
105                         System.out.println("not a valid time period");
106                         str = input.nextLine();
107                         continue;
108                     }
109                     // 判断桌号是否重复
110                     if (tem.isOpen()) {
111                         for (i = 0; i < tables.size(); i++) {
112                             // 有重复的桌号
113                             if (tem.num == tables.get(i).num && tables.get(i).isOpen()) {
114                                 Duration duration = Duration.between(tem.time, tables.get(i).time);
115                                 // 同一天
116                                 if (duration.toDays() == 0) {
117                                     // 在周一到周五
118                                     if (tem.weekday > 0 && tem.weekday < 6) {
119                                         // 在同一时间段
120                                         if (tem.time.getHour() < 15 && tables.get(i).time.getHour() < 15) {
121                                             repeat = true;
122                                             repeatNum = i;
123                                             break;
124                                         }
125                                     }
126                                     // 在周末
127                                     else {
128                                         // 时间相差小于一小时
129                                         if (duration.toHours() < 3600) {
130                                             repeatNum = i;
131                                             repeat = true;
132                                             break;
133                                         }
134                                     }
135                                 }
136                             }
137                         }
138                     }
139                     System.out.println("table " + tem.num + ": ");
140 
141                 } else {
142                     System.out.println("wrong format");
143                     str = input.nextLine();
144                     continue;
145                 }
146                 // 本桌开始点菜
147                 while (true) {
148                     str = input.nextLine();
149                     if (str.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
150                         String[] take = str.split(" ");
151                         portion = Integer.parseInt(take[2]);
152                         quota = Integer.parseInt(take[3]);
153                         if (tem.order.records.size() > 0) {
154                             if (Integer
155                                     .parseInt(take[0]) <= tem.order.records.get(tem.order.records.size() - 1).orderNum) {
156                                 System.out.println("record serial number sequence error");
157                                 continue;
158                             }
159                         }
160                         if (menu.searchDish(take[1]) == -1) {
161                             System.out.println(take[1] + " does not exist");
162                             continue;
163                         }
164                         if (portion > 3 || portion < 1) {
165                             System.out.println(Integer.parseInt(take[0]) + " portion out of range " + portion);
166                             continue;
167                         }
168                         if (quota > 15) {
169                             System.out.println(Integer.parseInt(take[0]) + " num out of range " + quota);
170                             continue;
171                         }
172                         tem.od(menu, take[0], take[1], portion, quota);
173                     }
174                     // 判断是否为删除订单
175                     else if (str.matches("[1-9][\\d]* delete")) {
176                         String[] take = str.split(" ");
177                         tem.order.delARecordByOrderNum(Integer.parseInt(take[0]));
178                     }
179                     // 判断是否为夹杂菜单
180                     else if (str.matches("[\\S]* [\\d]*")) {
181                         System.out.println("invalid dish");
182                         continue;
183                     } else if (str.matches("[\\S]* [\\d]* T")) {
184                         System.out.println("invalid dish");
185                         continue;
186                     }
187                     // 判断是否为代点
188                     else if (str.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
189                         String[] take = str.split(" ");
190                         // 判断代点桌号是否存在
191                         boolean exist = false;
192                         for (int j = 0; j < tables.size(); j++) {
193                             if (tables.get(j).num == Integer.parseInt(take[0])) {
194                                 exist = true;
195                                 break;
196                             }
197                         }
198                         if (exist) {
199                             System.out.print(Integer.parseInt(take[1]) + " table " + tem.num + " pay for table "
200                                     + Integer.parseInt(take[0]) + " ");
201                             Record treat = new Record();
202                             treat.d = menu.dishs.get(menu.searchDish(take[2]));
203                             portion = Integer.parseInt(take[3]);
204                             quota = Integer.parseInt(take[4]);
205                             treat.portion = portion;
206                             treat.quota = quota;
207                             System.out.print(treat.getPrice() + "\n");
208                             tem.order.records.add( treat);
209                         }
210                         // 若不存在则输出内容
211                         else {
212                             System.out.println("Table number :" + Integer.parseInt(take[0]) + " does not exist");
213                         }
214 
215                     } else if (str.equals("end")) {
216                         break;
217                     } else if (str.matches("table.*")) {
218                         break;
219                     } else  {
220                         System.out.println("wrong format");
221                         continue;
222                     }
223                 }
224                 // 本桌点菜结束,进入下一桌
225                 if (repeat) {
226                     for (i = 0; i < tables.get(repeatNum).order.records.size(); i++) {
227                         for (int j = 0; j < tem.order.records.size(); j++) {
228                             if (tables.get(repeatNum).order.records.get(i).d.name == tem.order.records.get(j).d.name) {
229                                 if (tables.get(repeatNum).order.records.get(i).portion == tem.order.records
230                                         .get(j).portion) {
231                                     tables.get(repeatNum).order.records.get(i).quota += tem.order.records.get(j).quota;
232                                     tem.order.records.remove(j);
233                                 }
234                             }
235                         }
236 
237                     }
238                     tables.get(repeatNum).order.records.addAll(tem.order.records);
239                     continue;
240                 }
241                 tables.add(tem);
242             }
243             else {
244                 str = input.nextLine();
245             }
246         }
247 
248 
249 
250         // 最终输出桌号订单信息
251         for (i = 0; i < tables.size(); i++) {
252             if (tables.get(i).isOpen()) {
253                 tables.get(i).getSum();
254                 System.out
255                         .println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum);
256             }
257 
258         }
259     }
260 
261     static class Dish {
262         String name;
263         int unit_price;
264         boolean isT = false;
265     }
266 
267     static class Record {
268         int orderNum;
269         Dish d;
270         int portion;
271         int quota;
272         boolean isDeleted = false;
273 
274         int getPrice() {
275             if (portion == 2)
276                 return (int) Math.round(1.5 * d.unit_price) * quota;
277             else if (portion == 3)
278                 return 2 * d.unit_price * quota;
279             else
280                 return d.unit_price * quota;
281         }
282     }
283 
284     static class Menu {
285         ArrayList<Dish> dishs = new ArrayList<Dish>();
286 
287         int searchDish(String dishName) {
288             for (int i = 0; i < dishs.size(); i++) {
289                 if (dishName.equals(dishs.get(i).name)) {
290                     return i;
291                 }
292             }
293             return -1;
294         }
295 
296         Dish addDish(String dishName, int unit_price) {
297             Dish newDish = new Dish();
298             newDish.name = dishName;
299             newDish.unit_price = unit_price;
300             return newDish;
301         }
302     }
303 
304     static class Order {
305         //        Record[] records = new Record[20];
306         ArrayList<Record> records = new ArrayList<Record>();
307 
308         Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) {
309             Record newRecord = new Record();
310             newRecord.orderNum = orderNum;
311             newRecord.d = menu.dishs.get(menu.searchDish(dishName));
312             newRecord.portion = portion;
313             newRecord.quota = quota;
314             System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice());
315             return newRecord;
316         }
317 
318         int searchReocrd(String name) {
319             for (int i = 0; i < records.size(); i++) {
320                 if (records.get(i).d.name == name) {
321                     return i;
322                 }
323             }
324             return -1;
325         }
326 
327         boolean delARecordByOrderNum(int orderNum) {
328             int i = 0, flag = 0;
329             for (i = 0; i < records.size(); i++) {
330                 if (records.get(i).orderNum == orderNum) {
331                     if (records.get(i).isDeleted == false) {
332                         records.get(i).isDeleted = true;
333                     } else {
334                         System.out.println("deduplication " + orderNum);
335                     }
336                     flag++;
337                 }
338             }
339             if (flag == 0) {
340                 System.out.println("delete error;");
341                 return false;
342             }
343             return true;
344         }
345     }
346 
347     static class Table {
348         Order order = new Order();
349         int num;
350         LocalDateTime time;
351         int weekday;
352         long sum = 0;
353         long origSum = 0;
354 
355         void od(Menu menu, String str, String str2, int portion, int quota) {
356             {
357                 order.records.add(order.addARecord(Integer.parseInt(str), str2, portion, quota, menu));
358 
359             }
360         }
361 
362         void getWeekDay() {
363             weekday = time.getDayOfWeek().getValue();
364         }
365 
366         void getSum() {
367             for (int i = 0; i < order.records.size(); i++) {
368                 if (!order.records.get(i).isDeleted) {
369                     origSum += order.records.get(i).getPrice();
370                     if (order.records.get(i).d.isT) {
371                         if (weekday > 0 && weekday < 6) {
372                             sum += Math.round(order.records.get(i).getPrice() * 0.7);
373                         } else {
374                             sum += order.records.get(i).getPrice();
375                         }
376                     } else {
377                         if (weekday > 0 && weekday < 6) {
378                             if (time.getHour() >= 17 && time.getHour() < 20)
379                                 sum += Math.round(order.records.get(i).getPrice() * 0.8);
380                             if (time.getHour() == 20) {
381                                 if (time.getMinute() <= 30)
382                                     sum += Math.round(order.records.get(i).getPrice() * 0.8);
383                             }
384                             if (time.getHour() >= 10 && time.getHour() < 14)
385                                 sum += Math.round(order.records.get(i).getPrice() * 0.6);
386                             if (time.getHour() == 14) {
387                                 if (time.getMinute() <= 30)
388                                     sum += Math.round(order.records.get(i).getPrice() * 0.6);
389                             }
390                         } else
391                             sum += order.records.get(i).getPrice();
392                     }
393                 }
394             }
395 
396         }
397 
398         boolean isOpen() {
399             if (weekday > 0 && weekday < 6) {
400                 if (time.getHour() >= 17 && time.getHour() < 20)
401                     return true;
402                 if (time.getHour() == 20) {
403                     if (time.getMinute() <= 30)
404                         return true;
405                 }
406                 if (time.getHour() > 10 && time.getHour() < 14)
407                     return true;
408                 if (time.getHour() == 10) {
409                     if (time.getMinute() >= 30)
410                         return true;
411                 }
412                 if (time.getHour() == 14) {
413                     if (time.getMinute() <= 30)
414                         return true;
415                 }
416             } else {
417                 if (time.getHour() > 9 && time.getHour() < 21)
418                     return true;
419                 if (time.getHour() == 9) {
420                     if (time.getMinute() >= 30)
421                         return true;
422                 }
423                 if (time.getHour() == 21) {
424                     if (time.getMinute() <= 30)
425                         return true;
426                 }
427             }
428             return false;
429 
430         }
431     }
432 }

该部分是对菜单题目的升华

import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Menu menu = new Menu();
        ArrayList<Table> tables = new ArrayList<Table>();
        ArrayList<User> users = new ArrayList<User>();
        Scanner input = new Scanner(System.in);
        String str1 = "";
        int i = 0;
        int portion = 0, quota = 0;
        while (true) {// 输入菜单
            Dish temp = new Dish();
            int isRepeat = -1;
            str1 = input.nextLine();
            if (str1.matches("[\\S]* [1-9][\\d]*")) {
                String[] token = str1.split(" ");
                temp.name = token[0];
                temp.unit_price = Integer.parseInt(token[1]);
                if (temp.unit_price > 300) {
                    System.out.println(temp.name + " price out of range " + temp.unit_price);
                    continue;
                }
                temp.isT = false;
                isRepeat = menu.searchDish(temp.name);
                if (isRepeat != -1) {
                    menu.dishes.remove(isRepeat);
                }
                menu.dishes.add(temp);
            } else if (str1.matches("[\\S]* [\\S]* [\\d]* T")) {
                String[] token = str1.split(" ");
                temp.name = token[0];
                temp.unit_price = Integer.parseInt(token[2]);
                if (temp.unit_price > 300) {
                    System.out.println(temp.name + " price out of range " + temp.unit_price);
                    continue;
                }
                temp.isT = true;
                switch (token[1]) {
                    case "川菜":
                        temp.FlavorType = "spicy";
                        temp.MaxFlavor = 5;
                        break;
                    case "晋菜":
                        temp.FlavorType = "acidity";
                        temp.MaxFlavor = 4;
                        break;
                    case "浙菜":
                        temp.FlavorType = "sweetness";
                        temp.MaxFlavor = 3;
                        break;
                }
                isRepeat = menu.searchDish(temp.name);
                if (isRepeat != -1) {
                    menu.dishes.remove(isRepeat);
                }
                menu.dishes.add(temp);
            } else if (str1.equals("end")) {
                break;
            } else if (str1.matches("tab.*")) {
                break;

            } else {
                System.out.println("wrong format");
                continue;
            }
        }
        while (!str1.equals("end")) {
            Table temp = new Table();
            boolean isRepeat = false;
            int repeatNum = 0;
            if (str1.matches(".*t.*a.*")) {
                if (str1.matches(
                        "table [1-9][\\d]* : [\\S]* [\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {
                    String[] token = str1.split(" ");
                    temp.userName = token[3];
                    if(token[3].length()>10) {
                        str1 = input.nextLine();
                        System.out.println("wrong format");
                        continue;
                    }
                    temp.callNum = token[4];
                    if(token[4].length()!=11) {
                        str1 = input.nextLine();
                        System.out.println("wrong format");
                        continue;
                    }
                    String headNum = token[4].substring(0, 3);
                    if(!headNum.equals("180")&&!headNum.equals("181")&&!headNum.equals("189")&&!headNum.equals("133")&&!headNum.equals("135")&&!headNum.equals("136")) {
                        str1 = input.nextLine();
                        System.out.println("wrong format");
                        continue;
                    }
                    String[] Date = token[5].split("/");
                    String[] Time = token[6].split("/");
                    int[] intDate = new int[5];
                    int[] intTime = new int[6];
                    for (i = 0; i < 3; i++) {
                        intDate[i] = Integer.parseInt(Date[i]);
                        intTime[i] = Integer.parseInt(Time[i]);
                    }
                    temp.num = Integer.parseInt(token[1]);
                    if (temp.num > 55) {
                        System.out.println(temp.num + " table num out of range");
                        str1 = input.nextLine();
                        continue;

                    }
                    try {
                        temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1],
                                intTime[2]);
                        temp.getWeekDay();
                    } catch (DateTimeException e) {
                        System.out.println(temp.num + " date error");
                        str1 = input.nextLine();
                        continue;
                    }
                    if (!temp.isOpen()) {
                        System.out.println("table " + temp.num + " out of opening hours");
                        str1 = input.nextLine();
                        continue;
                    }
                    if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))
                            && temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {
                        System.out.println("not a valid time period");
                        str1 = input.nextLine();
                        continue;
                    }
                    // 判断桌号是否重复
                    if (temp.isOpen()) {
                        for (i = 0; i < tables.size(); i++) {
                            // 有重复的桌号
                            if (temp.num == tables.get(i).num && tables.get(i).isOpen()) {
                                Duration duration = Duration.between(temp.time, tables.get(i).time);
                                // 同一天
                                if (duration.toDays() == 0) {
                                    // 在周一到周五
                                    if (temp.weekday > 0 && temp.weekday < 6) {
                                        // 在同一时间段
                                        if (temp.time.getHour() < 15 && tables.get(i).time.getHour() < 15) {
                                            isRepeat = true;
                                            repeatNum = i;
                                            break;
                                        }
                                    }
                                    // 在周末
                                    else {
                                        // 时间相差小于一小时
                                        if (duration.toHours() < 3600) {
                                            repeatNum = i;
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    System.out.println("table " + temp.num + ": ");

                } else {
                    System.out.println("wrong format");
                    str1 = input.nextLine();
                    continue;
                }
                // 本桌开始点菜
                while (true) {
                    str1 = input.nextLine();
                    //判断是否为非特色菜
                    if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
                        String[] token = str1.split(" ");
                        portion = Integer.parseInt(token[2]);
                        quota = Integer.parseInt(token[3]);
                        if (menu.searchDish(token[1]) == -1) {
                            System.out.println(token[1] + " does not exist");
                            continue;
                        }
                        Record tempRecord = new Record();
                        tempRecord.d = menu.dishes.get(menu.searchDish(token[1]));
                        tempRecord.orderNum = Integer.parseInt(token[0]);
                        tempRecord.portion = portion;
                        tempRecord.quota = quota;
                        temp.records.add(tempRecord);
                        System.out.println(tempRecord.orderNum +" "+token[1]+ " " +tempRecord.getPrice());
                    }
                    //判断是否为特色菜
                    else if(str1.matches("[1-9][\\d]* [\\S]* [\\d] [\\d] [1-9][\\d]*")) {
                        String[] token = str1.split(" ");
                        int FlavorNum = Integer.parseInt(token[2]);
                        if(FlavorNum<0||FlavorNum>menu.dishes.get(menu.searchDish(token[1])).MaxFlavor) {
                            System.out.println(menu.dishes.get(menu.searchDish(token[1])).FlavorType+" num out of range :"+FlavorNum);
                            continue;
                        }
                        portion = Integer.parseInt(token[3]);
                        quota = Integer.parseInt(token[4]);
                        if (menu.searchDish(token[1]) == -1) {
                            System.out.println(token[1] + " does not exist");
                            continue;
                        }
                        Record tempRecord = new Record();
                        tempRecord.d = menu.dishes.get(menu.searchDish(token[1]));
                        tempRecord.FlavorNum = FlavorNum;
                        tempRecord.orderNum = Integer.parseInt(token[0]);
                        tempRecord.portion = portion;
                        tempRecord.quota = quota;
                        temp.records.add(tempRecord);
                        System.out.println(tempRecord.orderNum +" "+token[1]+" "+ tempRecord.getPrice());
                    }
                    // 判断是否为删除订单
                    else if (str1.matches("[1-9][\\d]* delete")) {
                        String[] token = str1.split(" ");
                        int delNum = Integer.parseInt(token[0]);
                        for(i=0;i<temp.records.size();i++) {
                            if(temp.records.get(i).orderNum==delNum) {
                                if(!temp.records.get(delNum).isDeleted) {
                                    temp.records.get(delNum).isDeleted = true;
                                }
                                else System.out.println("deduplication :"+delNum);
                                break;
                            }
                            if(i==temp.records.size()-1)System.out.println("delete error");
                        }

                    }
                    // 判断是否为夹杂菜单
                    else if (str1.matches("[\\S]* [\\d]*")) {
                        System.out.println("invalid dish");
                        continue;
                    } else if (str1.matches("[\\S]* [\\S]* [\\d]* T")) {
                        System.out.println("invalid dish");
                        continue;
                    }
                    // 判断是否为代点
                    else if (str1.matches("[\\d]* [\\d]* [\\S].*")) {
                        String[] token = str1.split(" ");
                        // 判断代点桌号是否存在
                        boolean exist = false;
                        int j = 0;
                        for (j = 0; j < tables.size(); j++) {
                            if (tables.get(j).num == Integer.parseInt(token[0])) {
                                exist = true;
                                break;
                            }
                        }
                        if (exist) {
                            System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table "
                                    + Integer.parseInt(token[0]) + " ");
                            Record treat = new Record();
                            treat.d = menu.dishes.get(menu.searchDish(token[2]));
                            if(treat.d.isT) {
                                treat.FlavorNum = Integer.parseInt(token[3]);
                                treat.portion = Integer.parseInt(token[4]);
                                treat.quota = Integer.parseInt(token[5]);
                                if(treat.d.FlavorType.equals("spicy")) {
                                    tables.get(j).ChuanNum+=treat.quota;
                                    tables.get(j).spicyType +=treat.FlavorNum*treat.quota;
                                }
                                if(treat.d.FlavorType.equals("acidity")) {
                                    tables.get(j).JinNum+=treat.quota;
                                    tables.get(j).spicyType +=treat.FlavorNum*treat.quota;
                                }
                                if(treat.d.FlavorType.equals("sweetness")) {
                                    tables.get(j).ZheNum+=treat.quota;
                                    tables.get(j).spicyType +=treat.FlavorNum*treat.quota;
                                }
                            }
                            else {
                                treat.portion = Integer.parseInt(token[3]);
                                treat.quota = Integer.parseInt(token[4]);
                            }
                            System.out.print(treat.getPrice() + "\n");
                            treat.isTreat = true;
                            temp.records.add(treat);
                        }
                        // 若不存在则输出内容
                        else {
                            System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
                        }

                    } else if (str1.equals("end")) {
                        break;
                    } else if (str1.matches("table.*")) {
                        break;
                    } else {
                        System.out.println("wrong format");
                        continue;
                    }
                }
                // 本桌点菜结束,进入下一桌
                if (isRepeat) {
                    for (i = 0; i < tables.get(repeatNum).records.size(); i++) {
                        for (int j = 0; j < temp.records.size(); j++) {
                            if (Objects.equals(tables.get(repeatNum).records.get(i).d.name, temp.records.get(j).d.name)) {
                                if (tables.get(repeatNum).records.get(i).portion == temp.records
                                        .get(j).portion) {
                                    tables.get(repeatNum).records.get(i).quota += temp.records.get(j).quota;
                                    temp.records.remove(j);
                                }
                            }
                        }

                    }
                    tables.get(repeatNum).records.addAll(temp.records);
                    continue;
                }
                tables.add(temp);
            } else {
                str1 = input.nextLine();
            }
        }

        // 最终输出桌号订单信息
        for (i = 0; i < tables.size(); i++) {
            if (tables.get(i).isOpen()) {
                tables.get(i).getSum();
                tables.get(i).getFlavorNum();
                if(i!=0)System.out.print("\n");
                tables.get(i).showEnd();
                boolean isRepeat = false;
                for (User user : users) {
                    if (tables.get(i).userName.equals(user.userName)) {
                        user.paidMoney += tables.get(i).sum;
                        isRepeat = true;
                        break;
                    }
                }
                if(!isRepeat) {
                    User tempUser = new User();
                    tempUser.userName = tables.get(i).userName;
                    tempUser.callNum = tables.get(i).callNum;
                    tempUser.paidMoney = tables.get(i).sum;
                    users.add(tempUser);
                }
            }

        }
        users.sort(new Comparator<User>() {
            public int compare(User a,User b) {
                return a.userName.compareTo(b.userName);
            }
        });
        for(i=0;i<users.size();i++) {
            System.out.print("\n"+users.get(i).userName +" "+ users.get(i).callNum +" "+ users.get(i).paidMoney);
        }
    }
    static class Dish {
        String name;
        String FlavorType;
        int MaxFlavor;
        int unit_price;
        boolean isT = false;
    }

    static class Record {
        int orderNum;
        Dish d;
        int FlavorNum;
        int portion;
        int quota;
        boolean isDeleted = false;
        boolean isTreat  = false;

        int getPrice() {
            if (portion == 2)
                return (int) Math.round(1.5 * d.unit_price) * quota;
            else if (portion == 3)
                return 2 * d.unit_price * quota;
            else
                return d.unit_price * quota;
        }
    }

    static class Menu {
        ArrayList<Dish> dishes = new ArrayList<Dish>();

        int searchDish(String dishName) {
            for (int i = 0; i < dishes.size(); i++) {
                if (dishName.equals(dishes.get(i).name)) {
                    return i;
                }
            }
            return -1;
        }

    }

    static class User{
        String userName;
        String callNum;
        long paidMoney;
    }
    static class Table {
        ArrayList<Record> records = new ArrayList<Record>();
        int num;
        String userName;
        String callNum;
        LocalDateTime time;
        int weekday;
        int ChuanNum = 0;
        float spicyType = 0;
        int JinNum = 0;
        float acidType = 0;
        int ZheNum = 0;
        float sweetType = 0;
        long sum = 0;
        long origSum = 0;

        void getWeekDay() {
            weekday = time.getDayOfWeek().getValue();
        }
        void getFlavorNum() {
            for (Record record : records) {
                if (record.d.isT && !record.isDeleted && !record.isTreat) {
                    if (record.d.FlavorType.equals("spicy")) {
                        ChuanNum += record.quota;
                        spicyType += record.FlavorNum * record.quota;
                    }
                    if (record.d.FlavorType.equals("acidity")) {
                        JinNum += record.quota;
                        acidType += record.FlavorNum * record.quota;
                    }
                    if (record.d.FlavorType.equals("sweetness")) {
                        ZheNum += record.quota;
                        sweetType += record.FlavorNum * record.quota;
                    }
                }
            }
            if(ChuanNum!=0)spicyType = Math.round(spicyType/ChuanNum*1.0);
            if(JinNum!=0)acidType = Math.round(acidType/JinNum*1.0);
            if(ZheNum!=0)sweetType = Math.round(sweetType/ZheNum*1.0);
        }
        void showEnd() {
            System.out.print("table " + num + ": " + origSum + " " + sum);
            //输出川菜
            if(ChuanNum!=0) {
                System.out.print(" 川菜 "+ChuanNum);
                switch((int)spicyType) {
                    case 0 :System.out.print(" 不辣");break;
                    case 1 :System.out.print(" 微辣");break;
                    case 2 :System.out.print(" 稍辣");break;
                    case 3 :System.out.print(" 辣");break;
                    case 4 :System.out.print(" 很辣");break;
                    case 5 :System.out.print(" 爆辣");break;
                }
            }
            //输出晋菜
            if(JinNum!=0) {
                System.out.print(" 晋菜 "+JinNum);
                switch((int)acidType) {
                    case 0 :System.out.print(" 不酸");break;
                    case 1 :System.out.print(" 微酸");break;
                    case 2 :System.out.print(" 稍酸");break;
                    case 3 :System.out.print(" 酸");break;
                    case 4 :System.out.print(" 很酸");break;
                }
            }
            //输出浙菜
            if(ZheNum!=0) {
                System.out.print(" 浙菜 "+ZheNum);
                switch((int)sweetType) {
                    case 0 :System.out.print(" 不甜");break;
                    case 1 :System.out.print(" 微甜");break;
                    case 2 :System.out.print(" 稍甜");break;
                    case 3 :System.out.print(" 甜");break;
                }
            }

        }
        void getSum() {
            for (Record record : records) {
                if (!record.isDeleted) {
                    origSum += record.getPrice();
                    if (record.d.isT) {
                        if (weekday > 0 && weekday < 6) {
                            sum += Math.round(record.getPrice() * 0.7);
                        } else {
                            sum += record.getPrice();
                        }
                    } else {
                        if (weekday > 0 && weekday < 6) {
                            if (time.getHour() >= 17 && time.getHour() < 20)
                                sum += Math.round(record.getPrice() * 0.8);
                            if (time.getHour() == 20) {
                                if (time.getMinute() <= 30)
                                    sum += Math.round(record.getPrice() * 0.8);
                            }
                            if (time.getHour() >= 10 && time.getHour() < 14)
                                sum += Math.round(record.getPrice() * 0.6);
                            if (time.getHour() == 14) {
                                if (time.getMinute() <= 30)
                                    sum += Math.round(record.getPrice() * 0.6);
                            }
                        } else
                            sum += record.getPrice();
                    }
                }
            }

        }

        boolean isOpen() {
            if (weekday > 0 && weekday < 6) {
                if (time.getHour() >= 17 && time.getHour() < 20)
                    return true;
                if (time.getHour() == 20) {
                    if (time.getMinute() <= 30)
                        return true;
                }
                if (time.getHour() > 10 && time.getHour() < 14)
                    return true;
                if (time.getHour() == 10) {
                    if (time.getMinute() >= 30)
                        return true;
                }
                if (time.getHour() == 14) {
                    return time.getMinute() <= 30;
                }
            } else {
                if (time.getHour() > 9 && time.getHour() < 21)
                    return true;
                if (time.getHour() == 9) {
                    if (time.getMinute() >= 30)
                        return true;
                }
                if (time.getHour() == 21) {
                    return time.getMinute() <= 30;
                }
            }
            return false;

        }
    }
}
  • 仍未解决的问题:

    • 即使优化了代码的结构,代码的运行效率依旧达不到预期的水准,个人猜测是由于判断输入数据格式的方法太过笨拙。
    • 使用不可拓展容器来储存变量,有超出容器容量的风险,应当换用ArrayList等容器。这样不仅解决了容器大小的问题,又可以让数据的增删改查更加方便。
    • 由于胡乱的增加类,导致类与类的关系变得混乱,应当将Table类与Order类结合起来,可以增加代码的可读性。

    总结:

      个人感觉本次的题目集对与刚上手的学生来说还有很大难度,此次对于刚开始接触Java的同学来说还是有很大难度的,需要兼顾语法的同时对题目的算法有不错的理解。不然很容易出现欠缺考虑的情况。但通过这次的题目也可以很好人让我们深入了解面向对象设计语言该如何编写,让我们更快的了解Java运行方式。。总体而言,这套菜单系统让我学到了很多新知识,并且使我对代码结构的理解更加深刻,意识到了代码可拓展性的重要。

标签:题目,tem,temp,int,System,PTA,blog,time,out
From: https://www.cnblogs.com/shinehui/p/17512542.html

相关文章

  • BLOG-2
    BLOG-2前言这次blog是关于pta题目集4-6和期中考试的总结,菜单计价系统还是挺难的,而期中考试就较为简单,题量也不大,但就是很耗时间。7-1菜单计价程序-4分数 100全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字......
  • BLOG-2
    一、前言(要写3000个字还是很累的,希望互评能给高点分,我给所有人都是95以上的!)对于pta4和pta5不想多说,点菜系列巅峰之作两题难度都非常大,而且很复杂,写很多次也没啥分,题量也大关于期中考试,难度到不大,主要考察继承多态接口什么的,题量还可以,就是要在规定时间内写完有点紧张二......
  • iptables是如何影响数据包的传输的?
    iptables的表和链iptables可以设置不同的规则,并且它把规则按用途进行分类,分成了4个表,分别是表用途raw表用于去除数据包上的连接追踪机制(ConnectionTracking)。mangle表用于修改数据包的报文头信息,比如服务类型(TypeOfService,ToS)、生存周期(TimetoLive,TTL)。n......
  • Java PTA第4~5次题目集总结以及期中考试总结
    一.前言1.第四次题目集的知识点涉及Time类以及前面学的各种知识点;题量很少只有一题;难度比较大。2.第五次题目集的知识点主要是Time类、异常处理等等;题量很少只有一题;难度比较大。3.期中考试的知识点涉及类、继承与多态、接口等等;题量不多,一共4题;整体难度不高。二.设计与分析7......
  • 题目集4-5及期中期末考试
    一、前言:总结三次题目集的知识点、题量、难度等情况对于我来说第4、5次题目集难度较大,题量适中。期中考试难度一般,题量适中。知识点考察的是接口,对编程中时间信息的处理,类,继承等等。二、设计与分析没怎么写出来菜单题,分析不了源码。分析一下就是自己的态度有问题,没有迎难而......
  • BLOG-2
    考试分析:个人感觉本次考试难度偏简单,也无需注意些什么,只要仔细读题问题应该都不大; 1.创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积输入格式:输入圆的半径,取值范围为(0,+∞),输入数据非法,则程序输出WrongFormat,注意:只考虑从控制台输入数值的情况......
  • BLOG2-PTA题目集4、5以及期中考试
    (1)前言本次博客主要涵盖了Java题目的几个主要知识点,包括:1.面向对象的基础知识:这部分主要包括了类和对象的基本概念,构造方法,访问权限和成员变量的相关内容。在面向对象编程中,对这些基础知识的理解至关重要。2.面向对象的设计原则:这个题目强调了两个重要的设计原则,即继承和组......
  • 余同取余的一道变式题目
    二刷,本题感悟:善于从全局出发,也要学会从个例中找特点。根据题目信息可得:÷5...2÷8...7÷7...6如果三个一起看,未能找出规律,似乎不符合余同取余。但是如果孤立出后两项:÷8...7÷7...6可以发现除数与余数差为1......
  • BLOG-2
    前言:这两次PTA4与5都是菜单计价系统的续集,并且都是PTA3的不同迭代的代码,所以需要对PTA3进行不同的修改来完成题目要求。PTA4:由于时间等诸多原因,我并没有完成PTA4上的任务,并且没有在PTA4上及时的提交代码,所以我无法附上我PTA4的源码,但是我将对菜单计价系统4的题目进行分析设计,......
  • Blog - 2
    前言题目量和难度我觉得觉得作业题量适中,不会太多也不会太少,能让我练习。这次大作业,一共三道题。第一题和第三题难度不大,但是第二题是点菜系列的题目,这道题占了70分,而我只拿到了60分。在期中考中,虽然有三道题,但难度不算太大,每一道题都是在前一道题的基础上稍加修改即可。实验一......