BLOG-2

发布时间 2023-11-19 23:15:35作者: 会玩手机的小怪兽

一、前言

1.知识点运用:子类父类的继承关系,以及各种类之间的依赖、关联、聚合和组合等关系。

2.题量:菜单4,菜单5以及期中考试的三道题目。

3.难度:菜单4和菜单5较为困难,跟菜单3相比,菜单4增加了许多错误情况,而菜单5则是在特色菜和订多桌菜方面进行了迭代。

二、设计与分析

7-1 菜单计价程序-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)//根据序号查找一条记录

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

 

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

桌号信息错误。例如:

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

输出样例2:

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

wrong format

输入样例3:

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

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

输出样例3:

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

wrong format

输入样例4:

错误的菜谱记录。例如:

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

输出样例4:

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

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

输入样例5:

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

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

输出样例5:

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

wrong format

输入样例6:

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

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

输出样例6:

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

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

所画类图如下:

 

代码如下:

import java.time.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        boolean is_equals = false;
        List<List<Integer>> ordersDeletes = new ArrayList<>(); // 记录每个order的订单删除记录
        List<String> outPuts = new ArrayList<>();
        Menu menu = new Menu();
        List<Order> orders = new ArrayList<>();
        int n = -1; 
        int unit_price = 0;
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        boolean tableFlag = true; 
        boolean menuFlag = true;
        while (!input.equals("end")) {
            String[] strings = input.split("\\s+");
            if (menuFlag && strings.length == 2 && !strings[1].equals("delete") && n == -1) {
                String pattern = "\\S+ \\d+";
                Pattern p = Pattern.compile(pattern);
                Matcher m = p.matcher(input);
                if(!m.matches()) {
                    outPuts.add("wrong format");
                }
                else {
                    char[] chars = input.toCharArray();
                    int Blanknum = 0;
                    for (int i = 0; i < input.length(); i ++) {
                        if(chars[i] == ' ') Blanknum ++;
                    }
                    if(Blanknum > 1) {
                        outPuts.add("wrong format");
                    }
                    else {
                        char[] arr = strings[1].toCharArray();
                        if (!(isInteger(strings[1])) || arr[0] == '0' && strings[1].length() > 1 || isInteger(strings[0])) {
                            outPuts.add("wrong format");
                        }
                        else {
                            unit_price = Integer.parseInt(strings[1]);
                            if(unit_price > 300 || unit_price <= 0) {
                                outPuts.add(strings[0] + " price out of range " + unit_price);
                            }
                            else {
                                if(menu.searthDish(strings[0]) == null)
                                    menu.addDish(strings[0], unit_price, 'P');
                                else menu.modDishs(strings[0], unit_price, 'P');
                            }
                        }
                    }
                }
            }else if(strings.length == 3 && n == -1 && menuFlag) { // 判断为特色菜
                String pattern = "\\S+ \\d+ T";
                Matcher m = Pattern.compile(pattern).matcher(input);
                if(!m.matches()) {
                    outPuts.add("wrong format");
                }
                else {
                    char[] chars = input.toCharArray();
                    int Numblank = 0;
                    for (int i = 0; i < input.length(); i ++) {
                        if(chars[i] == ' ') Numblank ++;
                    }
                    if(Numblank > 2) {
                        outPuts.add("wrong format");
                    }
                    else {
                        char[] arr = strings[1].toCharArray();
                        boolean isWrongFormat = !isInteger(strings[1]) || (arr[0] == '0' && strings[1].length() > 1) || isInteger(strings[0]);
                        if (isWrongFormat) {
                            outPuts.add("wrong format");
                        }
                        else {
                            unit_price = Integer.parseInt(strings[1]);
                            if(unit_price >= 300 || unit_price <= 0) {
                                outPuts.add(strings[0] + " price out of range " + unit_price);
                            }
                            else {
                                if(menu.searthDish(strings[0]) == null) // 判断是否有重复的菜品信息
                                    menu.addDish(strings[0], unit_price, 'T');
                                else
                                    menu.modDishs(strings[0], unit_price, 'T');
                            }
                        }
                    }
                }
            }else if(strings[0].equals("table")) { // 判断为桌子信息
                is_equals = false;
                String pattern = "table \\d{1,2} \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}";
                Matcher m = Pattern.compile(pattern).matcher(input);
                if(!m.matches()) {
                    outPuts.add("wrong format");
                    tableFlag = false;
                } else {
                    menuFlag = false;
                    if(strings.length != 4) {
                        outPuts.add("wrong format");
                        tableFlag = false;
                    }
                    else {
                        LocalDate localDate = null;
                        LocalTime localTime = null;
                        int tableNum;
                        char[] arr = strings[1].toCharArray();
                        if(arr[0] == '0') {
                            outPuts.add("wrong format");
                            tableFlag = false;
                        }
                        else {
                            int dateN = 0;
                            int timeN = 0;
                            char[] charsDate = strings[2].toCharArray();
                            char[] charsTime = strings[3].toCharArray();
                            for (char c : charsDate) {
                                if (c == '/') {
                                    dateN++;
                                }
                            }
                            for (char c : charsTime) {
                                if (c == '/') {
                                    timeN++;
                                }
                            }
                            if(timeN != 2 || dateN != 2) {
                                outPuts.add("wrong format");
                                tableFlag = false;
                            } else {
                                String[] date = strings[2].split("/+");
                                String[] time = strings[3].split("/+");
                                boolean isValidFormat = isInteger(strings[1])
                                        && isInteger(date[0]) && isInteger(date[1]) && isInteger(date[2])
                                        && isInteger(time[0]) && isInteger(time[1]) && isInteger(time[2])
                                        && date[0].length() == 4
                                        && date[1].length() <= 2 && date[2].length() <= 2
                                        && time[0].length() <= 2 && time[1].length() <= 2 && time[2].length() <= 2;
                                if (!isValidFormat) {
                                    outPuts.add("wrong format");
                                    tableFlag = false;
                                }
                                else {
                                    tableNum = Integer.parseInt(strings[1]);
                                    if(tableNum > 55 || tableNum < 1) { // 判断桌号是否满足题意
                                        outPuts.add(tableNum + " table num out of range");
                                        tableFlag = false;
                                    }
                                    else {
                                        boolean flag = false;
                                        try {
                                            int year = Integer.parseInt(date[0]);
                                            int month = Integer.parseInt(date[1]);
                                            int day = Integer.parseInt(date[2]);
                                            int hour = Integer.parseInt(time[0]);
                                            int minute = Integer.parseInt(time[1]);
                                            int second = Integer.parseInt(time[2]);
                                            localDate = LocalDate.of(year, month, day);
                                            localTime = LocalTime.of(hour, minute, second);
                                            flag = true;
                                        } catch (DateTimeException e) {
                                            outPuts.add(strings[1] + " date error");
                                            tableFlag = false;
                                        }
                                        if(flag) {
                                            if(Order.is_LegalTime(localDate)) {
                                                if(Order.is_OpeningTime(localDate, localTime)) {
                                                    tableFlag = true;
                                                    for (Order order : orders) {
                                                        if (order.getTableNum() == tableNum) { // 判断两次相同桌号订单是否是同一个
                                                            int hourBefore = order.getLocalTime().getHour();
                                                            int minuteBefore = order.getLocalTime().getMinute();
                                                            int hourLater = localTime.getHour();
                                                            int minuteLater = localTime.getMinute();
                                                            LocalDateTime timeBefore = LocalDateTime.of(order.getLocalDate(), order.getLocalTime());
                                                            LocalDateTime timeLater = LocalDateTime.of(localDate, localTime);
                                                            LocalDateTime timeBeforePlus = timeBefore.plusMinutes(59).plusSeconds(59);
                                                            boolean isEquals = false;
                                                            boolean isWeekday = localDate.getDayOfWeek() != DayOfWeek.SATURDAY && localDate.getDayOfWeek() != DayOfWeek.SUNDAY;
                                                            boolean isWithinWeekdayTimeRange = (hourBefore >= 17 && hourBefore < 20 || (hourBefore == 20 && minuteBefore <= 30)) && (hourLater >= 17 && hourLater < 20 || (hourLater == 20 && minuteLater <= 30));
                                                            boolean isWithinWeekendTimeRange = (hourBefore >= 11 && hourBefore < 14 || (hourBefore == 14 && minuteBefore <= 30) || (hourBefore == 10 && minuteBefore >= 30)) && (hourLater >= 11 && hourLater < 14 || (hourLater == 14 && minuteLater <= 30) || (hourLater == 10 && minuteLater >= 30));
                                                            if ((order.getLocalDate().equals(localDate) && isWeekday && isWithinWeekdayTimeRange) || (order.getLocalDate().equals(localDate) && !isWeekday && timeLater.isAfter(timeBefore) && timeLater.isBefore(timeBeforePlus))) {
                                                                isEquals = true;
                                                                break;
                                                            }

                                                        }
                                                    }
                                                    if (!is_equals){
                                                        n ++;
                                                        Order order = new Order(menu, tableNum, localTime, localDate);
                                                        orders.add(order);
                                                        ordersDeletes.add(new ArrayList<>());
                                                        outPuts.add("table " + tableNum + ": ");
                                                    }
                                                } else {
                                                    outPuts.add("table " + strings[1] + " out of opening hours");
                                                    tableFlag = false;
                                                }
                                            } else {
                                                outPuts.add("not a valid time period");
                                                tableFlag = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }else if(strings.length == 4 && tableFlag) {//判断点菜
                String pattern = "\\d{1,2} \\S+ \\d \\d{1,2}";
                Matcher m = Pattern.compile(pattern).matcher(input);
                if(!m.matches()) {
                    outPuts.add("wrong format");
                }
                else {
                    menuFlag = false;
                    Record record = null;
                    Order order = null;
                    if(!isInteger(strings[2]) || !isInteger(strings[3]) || !isInteger(strings[0])) {
                        outPuts.add("wrong format");
                    }
                    else {
                        char[] arr = strings[3].toCharArray();
                        if (strings[2].length() > 1 || strings[0].charAt(0) == '0') {
                            outPuts.add("wrong format");
                        }
                        else {
                            int num = Integer.parseInt(strings[0]);
                            int portion = Integer.parseInt(strings[2]);
                            int copies = Integer.parseInt(strings[3]);
                            if (orders.size() > 0) {
                                order = orders.get(n);
                                int length = order.getRecords().size();
                                boolean flag1 = false;
                                boolean flag2 = true;
                                for (Dish dish : menu.getDishs()) {
                                    if(dish.getName().equals(strings[1])) {
                                        flag1 = true;
                                        break;
                                    }
                                }
                                for (Record record1 : order.getRecords()) {
                                    if(num <= record1.getOrderNum()) {
                                        flag2 = false;
                                        break;
                                    }
                                }
                                if(length == 0 || flag2 || is_equals) {
                                    if(flag1) {
                                        if(menu.searthDish(strings[1]).getType() == 'P' && portion >= 1 && portion <= 3) {
                                            if(copies <= 15 && copies >= 1) {
                                                record = order.addARecord(num, strings[1], portion, copies);
                                                outPuts.add(num + " " + strings[1] + " " + record.getPrice());
                                            }
                                            else {
                                                outPuts.add(num + " num out of range " + copies);
                                            }
                                        } else if(menu.searthDish(strings[1]).getType() == 'T' && (portion == 1 || portion == 2)) {
                                            if(copies <= 15 && copies >= 1) {
                                                record = order.addARecord(num, strings[1], portion, copies);
                                                outPuts.add(num + " " + strings[1] + " " + record.getPrice());
                                            }
                                            else {
                                                outPuts.add(num + " num out of range " + copies);
                                            }
                                        } else {
                                            outPuts.add(num + " portion out of range " + portion);
                                        }
                                    } else {
                                        outPuts.add(strings[1] + " does not exist");
                                    }
                                } else {
                                    outPuts.add("record serial number sequence error");
                                }
                            }
                        }
                    }
                }
            } else if(strings.length == 2 && strings[1].equals("delete") && tableFlag && n != -1) {//删除点菜记录
                menuFlag = false;
                Order order = orders.get(n);
                int num = Integer.parseInt(strings[0]);
                boolean flag = false;
                for (Record record : order.getRecords()) {
                    if(record.getOrderNum() == num) {
                        flag = true;
                        break;
                    }
                }
                if(!ordersDeletes.get(n).contains(num)) {
                    if(flag) {
                        order.delARecordByOrderNum(num);
                        ordersDeletes.get(n).add(num);
                    } else {
                        outPuts.add("delete error");
                    }
                } else {
                    outPuts.add("deduplication " + num);
                }
            } else if((strings.length == 2 && !strings[1].equals("delete") || strings.length == 3 && strings[2].equals("T")) && n != -1 && tableFlag) {
                outPuts.add("invalid dish");
            }
            else if(strings.length == 5 && isInteger(strings[3]) && isInteger(strings[1]) && tableFlag) {
                if(!isInteger(strings[0])) {
                    outPuts.add("wrong format");
                }
                else {
                    int num = Integer.parseInt(strings[1]);
                    int tableNumBefore = Integer.parseInt(strings[0]);
                    int portion = Integer.parseInt(strings[3]);
                    int copies = Integer.parseInt(strings[4]);
                    Record recordNew = new Record(num, menu.searthDish(strings[2]), portion, copies);
                    boolean flag = false;
                    for (int i = 0; i < orders.size() - 1; i++) {
                        if(orders.get(i).getTableNum() == tableNumBefore) {
                            flag = true;
                            break;
                        }
                    }
                    if(flag) {
                        outPuts.add(num + " table " + orders.get(n).getTableNum() + " pay for table " + tableNumBefore + " " + recordNew.getPrice());
                        OthersOrder othersOrder = new OthersOrder(orders.get(n), tableNumBefore, recordNew, menu);
                        orders.get(n).addAOthersOrder(othersOrder);
                    } else {
                        outPuts.add("Table number :" + strings[0] + " does not exist");
                    }
                }
            } else if(tableFlag) {
                outPuts.add("wrong format");
            }
            input = scanner.nextLine();
        }

        for (Order order : orders) {
            outPuts.add("table " + order.getTableNum() + ": " + (order.getOwnTotalPriceBefore() + order.getOthersTotalPriceBefore()) + " " + (order.getOthersTotalPriceAfter() + order.getOwnTotalPriceAfter()));
        }

        for (String i : outPuts) {
            System.out.println(i);
        }
    }


    public static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
class Dish {
    Dish(String name, int unit_price, char type) {
        this.type = type;
        this.name = name;
        this.unit_price = unit_price;
    }
    public char getType() {
        return type;
    }
    private char type;
    private String name; // 菜品名称
    private int unit_price; // 单价
    public String getName() {
        return name;
    }
    public int getUnit_price() {
        return unit_price;
    }
    public int getPrice(int portion) {
        if(portion == 1) return getUnit_price();
        else if(portion == 2) return (int)Math.round(getUnit_price() * 1.5);
        else return getUnit_price() * 2;
    }
}
class Menu {
    private List<Dish> dishs = new ArrayList<>();
    Menu() {
    }
    public void modDishs(String dishName, int unit_price, char type) {
        Dish dish = new Dish(dishName, unit_price, type);
        for (int i = 0; i < dishs.size(); i++) {
            if(dish.getName().equals(dishs.get(i).getName())) {
                dishs.set(i, dish);
                break;
            }
        }
    }
    public List<Dish> getDishs() {
        return dishs;
    }
    public void setDishs(List<Dish> dishs) {
        this.dishs = dishs;
    }
    public Dish searthDish(String dishName) {
        List<Dish> dishesNew = getDishs();
        for (Dish dish : dishesNew) {
            if(dish.getName().equals(dishName))
                return dish;
        }
        return null;
    } // 根据菜名在菜谱中查找菜品信息,返回Dish对象。
    public Dish addDish(String dishName, int unit_price, char type) {
        List<Dish> dishsNew = getDishs();
        Dish dish = new Dish(dishName, unit_price, type);
        dishsNew.add(dish);
        setDishs(dishsNew);
        return dish;
    } // 添加一道菜品信息
}
class Record {
    private int orderNum;
    private Dish d;
    private int portion;
    private int copies; 
    public Record() {
    }
    public Record(int orderNum, Dish d, int portion, int copies) {
        this.orderNum = orderNum;
        this.d = d;
        this.portion = portion;
        this.copies = copies;
    }
    public int getOrderNum() {
        return orderNum;
    }
    public Dish getD() {
        return d;
    }
    public int getPrice() {
        return d.getPrice(portion) * copies;
    }
}
class Order { // 订单类:保存用户点的所有菜的信息。
    private List<OthersOrder> othersOrders = new ArrayList<>();
    private int tableNum;
    private Menu menu;
    private List<Record> records = new ArrayList<>(); 
    private LocalDate localDate;
    private LocalTime localTime;
    public Order() {
    }
    public Order(Menu menu, int tableNum, LocalTime localTime, LocalDate localDate) {
        this.menu = menu;
        this.localDate = localDate;
        this.localTime = localTime;
        this.tableNum = tableNum;
    }
    public void addAOthersOrder(OthersOrder othersOrder) {
        this.othersOrders.add(othersOrder);
    }
    public int getTableNum() {
        return tableNum;
    }
    public LocalTime getLocalTime() {
        return localTime;
    }
    public LocalDate getLocalDate() {
        return localDate;
    }
    public void setRecords(List<Record> records) {
        this.records = records;
    }
    public List<Record> getRecords() {
        return records;
    }
    public static boolean is_LegalTime(LocalDate dateToCheck) {
        LocalDate startDate = LocalDate.of(2022, 1, 1); // 开始日期
        LocalDate endDate = LocalDate.of(2023, 12, 31); // 结束日期
        return ((dateToCheck.isEqual(startDate) || dateToCheck.isAfter(startDate))
                && (dateToCheck.isEqual(endDate) || dateToCheck.isBefore(endDate)));
    }
    public static boolean is_OpeningTime(LocalDate localDate, LocalTime localTime) {
        DayOfWeek dayOfWeek = localDate.getDayOfWeek();
        int hour = localTime.getHour();
        int minute = localTime.getMinute();
        return ((dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) && (hour >= 10 && hour < 21 || (hour == 9 && minute >= 30) || (hour == 21 && minute <= 30)))
                || ((!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)) && (hour >= 17 && hour < 20 || (hour == 20 && minute <= 30)))
                || ((!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)) && (hour >= 11 && hour < 14 || (hour == 14 && minute <= 30) || (hour == 10 && minute >= 30)));
    } // 判断时间是否营业
    public int getOwnTotalPriceAfter() {
        DayOfWeek dayOfWeek = getLocalDate().getDayOfWeek();
        int hour = getLocalTime().getHour();
        int minute = getLocalTime().getMinute();
        int sum = 0;
        List<Record> recordsNew = getRecords();
        for (Record record : recordsNew) {
            if(record.getD().getType() == 'P') {
                if((!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)) && (hour >= 17 && hour < 20 || (hour == 20 && minute <= 30)))
                    sum = sum + (int)Math.round(record.getPrice() * 0.8);
                else if((!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)) && (hour >= 11 && hour < 14 || (hour == 14 && minute <= 30) || (hour == 10 && minute >= 30)))
                    sum = sum + (int)Math.round(record.getPrice() * 0.6);
                else if((dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) && (hour >=10 && hour < 21 || (hour == 9 && minute >=30) || (hour == 21 && minute <=30)))
                    sum = sum + record.getPrice();

            }
            else if(record.getD().getType() == 'T') {
                if(!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY))
                    sum = sum + (int)Math.round(record.getPrice() * 0.7);
                else sum = sum + record.getPrice();
            }
        }
        return sum;
    } // 计算订单的总价
    public int getOwnTotalPriceBefore() {
        DayOfWeek dayOfWeek = getLocalDate().getDayOfWeek();
        int hour = getLocalTime().getHour();
        int minute = getLocalTime().getMinute();
        int sum = 0;
        List<Record> recordsNew = getRecords();
        for (Record record : recordsNew) {
            sum = sum + record.getPrice();
        }
        return sum;
    }
    public int getOthersTotalPriceBefore() {
        int sum = 0;
        for (OthersOrder othersOrder : othersOrders) {
            sum = sum + othersOrder.getPriceBefore();
        }
        return sum;
    }
    public int getOthersTotalPriceAfter() {
        int sum = 0;
        for (OthersOrder othersOrder : othersOrders) {
            sum = sum + othersOrder.getPriceAfter();
        }
        return sum;
    }

    public Record addARecord(int orderNum, String dishName, int portion, int copies) {
        Dish dish = menu.searthDish(dishName);
        Record recordNew = new Record(orderNum, dish, portion, copies);
        List<Record> recordsNew = getRecords();
        recordsNew.add(recordNew);
        setRecords(recordsNew);
        return recordNew;
    } // 添加一条菜品信息到订单中。
    public void delARecordByOrderNum(int orderNum) {
        List<Record> recordsNew = getRecords();
        for (Record record : recordsNew) {
            if(record.getOrderNum() == orderNum) {
                recordsNew.remove(record);
                break;
            }
        }
        setRecords(recordsNew);
    } // 根据序号删除一条记录
    public Record findRecordByNum(int orderNum) {
        List<Record> recordsNew = getRecords();
        for (Record record : recordsNew) {
            if(record.getOrderNum() == orderNum)
                return record;
        }
        return null;
    } // 根据序号查找一条记录
}
class OthersOrder {
    private Order order;
    private int OwnTableNum;
    private Record record;
    private Menu menu;
    OthersOrder(Order order, int OwnTableNum, Record record, Menu menu) {
        this.OwnTableNum = OwnTableNum;
        this.order = order;
        this.menu = menu;
        this.record = record;
    }
    public int getPriceBefore() {
        return record.getPrice();
    }
    public int getPriceAfter() {
        DayOfWeek dayOfWeek = order.getLocalDate().getDayOfWeek();
        int hour = order.getLocalTime().getHour();
        int minute = order.getLocalTime().getMinute();
        int sum = 0;
        if(record.getD().getType() == 'C') {
            if((!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)) && (hour >= 17 && hour < 20 || (hour == 20 && minute <= 30)))
                sum = sum + (int)Math.round(record.getPrice() * 0.8);
            else if((!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY)) && (hour >= 11 && hour < 14 || (hour == 14 && minute <= 30) || (hour == 10 && minute >= 30)))
                sum = sum + (int)Math.round(record.getPrice() * 0.6);
            else if((dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) && (hour >=10 && hour < 21 || (hour == 9 && minute >=30) || (hour == 21 && minute <=30)))
                sum = sum + record.getPrice();

        }
        else if(record.getD().getType() == 'T') {
            if(!(dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY))
                sum = sum + (int)Math.round(record.getPrice() * 0.7);
            else sum = sum + record.getPrice();
        }
        return sum;
    }
}

 

本题心得:

添加菜品信息:根据输入的菜品名称和价格,向菜单中添加菜品信息。点菜:根据桌号、菜品名称、份数等信息,向订单中添加点菜记录。订单管理:对订单进行管理,如删除点菜记录、计算单总价等操作。时间判断:根据日期和时间判断营业时间,以及特定时间段的折扣优惠。

本题的难点在于各种各样的输入错误,如何分别进行识别,以及时间段的判断尤为困难。本题我就卡在最后的“重复的桌号,在同一时间段”这一个测试点上,尝试多种方法,写出如下判断是否同一桌的函数,结果仍是报错,因为如果换一种方式记录点菜时间,应该可以成功通过,但是别的方面也要大改,因时间不够,所以最后仍没有通过该测试点。

for (Order order : orders) {
                                                        if (order.getTableNum() == tableNum) { // 判断两次相同桌号订单是否是同一个
                                                            int hourBefore = order.getLocalTime().getHour();
                                                            int minuteBefore = order.getLocalTime().getMinute();
                                                            int hourLater = localTime.getHour();
                                                            int minuteLater = localTime.getMinute();
                                                            LocalDateTime timeBefore = LocalDateTime.of(order.getLocalDate(), order.getLocalTime());
                                                            LocalDateTime timeLater = LocalDateTime.of(localDate, localTime);
                                                            LocalDateTime timeBeforePlus = timeBefore.plusMinutes(59).plusSeconds(59);
                                                            boolean isEquals = false;
                                                            boolean isWeekday = localDate.getDayOfWeek() != DayOfWeek.SATURDAY && localDate.getDayOfWeek() != DayOfWeek.SUNDAY;
                                                            boolean isWithinWeekdayTimeRange = (hourBefore >= 17 && hourBefore < 20 || (hourBefore == 20 && minuteBefore <= 30)) && (hourLater >= 17 && hourLater < 20 || (hourLater == 20 && minuteLater <= 30));
                                                            boolean isWithinWeekendTimeRange = (hourBefore >= 11 && hourBefore < 14 || (hourBefore == 14 && minuteBefore <= 30) || (hourBefore == 10 && minuteBefore >= 30)) && (hourLater >= 11 && hourLater < 14 || (hourLater == 14 && minuteLater <= 30) || (hourLater == 10 && minuteLater >= 30));
                                                            if ((order.getLocalDate().equals(localDate) && isWeekday && isWithinWeekdayTimeRange) || (order.getLocalDate().equals(localDate) && !isWeekday && timeLater.isAfter(timeBefore) && timeLater.isBefore(timeBeforePlus))) {
                                                                isEquals = true;
                                                                break;
                                                            }

                                                        }
                                                    }

7-1 菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格

 

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

输入样例1:

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

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

输出样例1:

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

table 1 out of opening hours

输入样例2:

一种口味的菜品。例如:

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

输出样例2:

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

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

输入样例3:

辣度值超出范围。例如:

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

输出样例3:

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

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

输入样例4:

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

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

输出样例4:

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

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

输入样例5:

多用户多桌菜。例如:

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

输出样例5:

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

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

输入样例6:

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

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

输出样例6:

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

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

输入样例7:

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

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

输出样例7:

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

wrong format
wrong format

所画类图如下:

 代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Dish[] dishes = new Dish[20];
        int x = 0;
        int y = 0 ;
        int z = 0 ;
        int z1 = 0 ;
        int p = 0;
        SpecialDish[] specialDishes = new SpecialDish[20];
        ArrayList<Table> tables = new ArrayList<>();
        Table table = new Table();
        int tableNum;
        int dishNum ;
        int portion ;
        int num ;
        int unit_price ;
        int deleteNum ;
        int tasteNum;
        Record[] records = new Record[20];
        PersonPay[] personPays = new PersonPay[10];
        Order order = new Order();
        Menu menu = new Menu();
        lastPrint print = new lastPrint();
        String menuName ;
        String dayTime ;
        int Output=0;
        String hourTime ;
        String name ;
        String tableName ;
        String telephoneNum ;
        String taste;
        int totalprice1;
        boolean isHaveTable = false ;
        boolean forOther = false;
        while(true){
            String input = in.readLine();

            if(input.equals("end")){
                if(x!=0){
                    table.order = order;
                    tables.add(x-1 , table);
                    Output ++;
                }
                print.tables = tables;
                print.personPays = personPays;
                print.print();
                print.payPrint();
                break;
            }
            String[] getInput = input.split(" ");
            if(input.matches("^(table)( )([1-9][0-9]*)( )(:)( )(\\S+)( )((136|133|135|180|181|189)[0-9]{8})( )([0-9]{4})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})( )([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})$")) {
                if(x != 0) {
                    table.order = order;
                    tables.add(x-1 , table);
                    Output ++;
                    table = new Table();
                    y = 0;
                }
                isHaveTable = true;
                tableNum = Integer.parseInt(getInput[1]);
                tableName = getInput[3];
                telephoneNum = getInput[4];
                dayTime = getInput[5];
                hourTime = getInput[6];
                Output ++;
                table.tableNum = tableNum;
                table.tableName = tableName;
                table.telephoneNum = telephoneNum;
                table.mainTime = dayTime;
                table.remainTime = hourTime;
                if (table.timeJudgement1()) {
                    Output ++;
                } else {
                    isHaveTable = false;
                    System.out.println("table " + table.tableNum + " out of opening hours");
                    continue;
                }
                order = new Order();
                records = new Record[10];
                if(x==0){
                    personPays[p] = new PersonPay();
                    personPays[p].name = tableName;
                    personPays[p].telephoneNum = telephoneNum;
                    p++;
                    Output++;
                    print.p = p;
                    for(int v=0;v<10;v++){
                        Output=Output-1;
                    }
                }
                else{
                    boolean found = false;
                    int i;
                    for (i = 0; i < p; i++) {
                        if (personPays[i].name.equals(tableName) && personPays[i].telephoneNum.equals(telephoneNum)) {
                            found = true;
                            for(int v=0;v<10;v++){
                                Output=Output-1;
                            }
                            break;
                        }
                    }
                    if (i == p && !found) {
                        personPays[p] = new PersonPay();
                        personPays[p].name = tableName;
                        personPays[p].telephoneNum = telephoneNum;
                        for(int v=0;v<10;v++){
                            Output=Output-1;

                        }
                        p++;
                        print.p = p;
                    }
                }
                System.out.println("table "+tableNum+": ");
                x++;
            }
            else if(input.matches("^(\\S+)( )([1-9][0-9]*)$")){
                int i = 0;
                menuName = getInput[0];
                unit_price = Integer.parseInt(getInput[1]);
                for(int v=0;v<10;v++){
                    Output=Output-1;
                }
                if(z == 0) {
                    dishes[0] = menu.addDish(menuName , unit_price);
                    z++;
                }
                else{
                    boolean found = false;
                    for (; i < z; i++) {
                        if (menuName.equalsIgnoreCase(dishes[i].name)) {
                            dishes[i].unit_price = unit_price;
                            found = true;
                            break;
                        }
                    }

                }
                if(i == z){
                    dishes[z] = menu.addDish(menuName , unit_price);
                    z++;
                }
                menu.dishes = dishes;
                menu.dishNum = z;
            }
            else if(input.matches("^(\\S+)( )(\\S+)( )([1-9][0-9]*)( )(T)$")){
                int i = 0;
                menuName = getInput[0];
                taste = getInput[1];
                unit_price = Integer.parseInt(getInput[2]);
                if(z1 == 0){
                    specialDishes[0] = menu.addSpecialDish(menuName , unit_price , taste);
                    z1++;
                    for(int v=0;v<10;v++){
                        Output=Output-1;
                    }
                }
                else{
                    for(;i < z1 ; i++) {
                        if(menuName.equalsIgnoreCase(specialDishes[i].name)) {
                            specialDishes[i].unit_price = unit_price;
                            break;
                        }
                    }
                }
                if(i == z1){
                    specialDishes[z1] = menu.addSpecialDish(menuName , unit_price , taste);
                    z1++;
                }
                menu.specialDishes = specialDishes;
                menu.dishNum1 = z1;
            }
            else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){
                if(!isHaveTable)
                    continue;
                dishNum = Integer.parseInt(getInput[0]);
                name = getInput[1];
                portion = Integer.parseInt(getInput[2]);
                num = Integer.parseInt(getInput[3]);
                Dish searchedDish = menu.searthDish(name);
                if (searchedDish != null) {
                    records[y] = order.addARecord(dishNum, name, portion, num, menu);
                    records[y].isSpecialDish = false;
                    System.out.println(records[y].orderNum + " " + records[y].d.name + " " + records[y].getPrice());
                    y++;
                    for(int v=0;v<10;v++){
                        Output=Output-1;
                    }
                    order.records = records;
                    order.dishNum = y;
                } else {
                    System.out.println(name + " does not exist");
                }
            }
            else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([0-9]+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){
                if(!isHaveTable)
                    continue;
                dishNum = Integer.parseInt(getInput[0]);
                name = getInput[1];
                tasteNum = Integer.parseInt(getInput[2]);
                portion = Integer.parseInt(getInput[3]);
                num = Integer.parseInt(getInput[4]);
                if(menu.searthSpecialDish(name) != null) {
                    if(((menu.searthSpecialDish(name).taste.equals("川菜") && tasteNum >= 0 && tasteNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晋菜") && tasteNum>=0 && tasteNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tasteNum>=0 && tasteNum <= 3))){
                        records[y] = new Record();
                        records[y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tasteNum , forOther);
                        records[y].isSpecialDish = true;
                        System.out.println(records[y].orderNum+" "+records[y].d.name+" "+records[y].getPrice());
                        y++;
                        order.records = records;
                        order.dishNum = y;
                    }
                }
                else{
                    System.out.println(name+" does not exist");
                    continue;
                }
                SpecialDish specialDish = menu.searthSpecialDish(name);
                if (specialDish != null) {
                    switch (specialDish.taste) {
                        case "川菜":
                            if (tasteNum < 0 || tasteNum > 5) {
                                System.out.println("spicy num out of range: " + tasteNum);
                            }
                            break;
                        case "晋菜":
                            if (tasteNum < 0 || tasteNum > 4) {
                                System.out.println("acidity num out of range: " + tasteNum);
                            }
                            break;
                        case "浙菜":
                            if (tasteNum < 0 || tasteNum > 3) {
                                System.out.println("sweetness num out of range: " + tasteNum);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            else if(input.matches("([1-9][0-9]*)( )(delete)")) {
                if(!isHaveTable)
                    continue;
                deleteNum = Integer.parseInt(getInput[0]);
                if(order.findRecordByNum(deleteNum) == 1){
                    order.delARecordByOrderNum(deleteNum);
                }
                else
                    System.out.println("delete error;");
            }
            else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) {
                if(!isHaveTable)
                    continue;
                int t = Integer.parseInt(getInput[0]);
                dishNum = Integer.parseInt(getInput[1]);
                name = getInput[2];
                portion = Integer.parseInt(getInput[3]);
                num = Integer.parseInt(getInput[4]);
                for(int i = 0;i<x-1;i++){
                    if(tables.get(i).tableNum == t){
                        if(menu.searthDish(name) != null) {
                            records[y] = new Record();
                            records[y] = order.addARecord(dishNum , name , portion , num , menu);
                            records[y].isSpecialDish = false;
                            System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[y].getPrice());
                            y++;
                            order.records = records;
                            order.dishNum = y;
                        }
                        break;
                    }
                }
            }
            else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([0-9]*)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) {
                if(!isHaveTable)
                    continue;
                int t = Integer.parseInt(getInput[0]);
                dishNum = Integer.parseInt(getInput[1]);
                name = getInput[2];
                tasteNum = Integer.parseInt(getInput[3]);
                portion = Integer.parseInt(getInput[4]);
                num = Integer.parseInt(getInput[5]);
                for(int i = 0;i<x-1;i++){
                    if(tables.get(i).tableNum == t){
                        if(menu.searthSpecialDish(name) != null) {
                            if(((menu.searthSpecialDish(name).taste.equals("川菜") && tasteNum >= 1 && tasteNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晋菜") && tasteNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tasteNum <= 3))){
                                records[y] = new Record();
                                records[y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tasteNum , !forOther);
                                records[y].isSpecialDish = true;
                                System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[y].getPrice());
                                tables.get(i).giveTaste(name,num,tasteNum,menu);
                                y++;
                                order.records = records;
                                order.dishNum = y;
                            }
                        }

                        break;
                    }
                }
            }
            else {
                System.out.println("wrong format");
            }
        }
    }
    public String judgeTriangleType(double side1, double side2, double side3) {
        if (side1 <= 0 || side2 <= 0 || side3 <= 0) {
            return "does not exist";
        }
        if (side1 + side2 <= side3 || side1 + side3 <= side2 || side2 + side3 <= side1) {
            return "spicy num out of range:";
        }
        if (side1 == side2 && side2 == side3) {
            return "acidity num out of range:";
        } else if (side1 == side2 || side1 == side3 || side2 == side3) {
            return "pay for table";
        } else {
            return "sweetness num out of range";
        }
    }
}


class Dish{
    String name;
    int unit_price;
    public int getPrice(int portion){
        double getPrice=0;
        if(portion==1)
            getPrice=unit_price;
        else if(portion==2)
            getPrice=unit_price*1.5;
        else if(portion==3)
            getPrice=unit_price*2;
        return (int) Math.round(getPrice);
    }
}
class SpecialDish extends Dish{
    String taste;
}
class Menu{
    Dish[] dishes;
    SpecialDish[] specialDishes;
    int dishNum;
    int dishNum1;
    public Dish searthDish(String dishName){
        for(int k=0;k<dishNum;k++) {
            if(dishName.equals(dishes[k].name)){
                return dishes[k];
            }
        }
        return null;
    }
    public SpecialDish searthSpecialDish(String dishName){
        for(int k=0;k<dishNum1;k++) {
            if(dishName.equals(specialDishes[k].name)){
                return specialDishes[k];
            }
        }
        return null;
    }
    public Dish addDish(String dishName,int unit_price){
        Dish addDish=new Dish();
        addDish.name=dishName;
        addDish.unit_price=unit_price;
        return addDish;
    }
    public SpecialDish addSpecialDish(String dishName,int unit_price,String taste){
        SpecialDish addDish=new SpecialDish();
        addDish.name=dishName;
        addDish.unit_price=unit_price;
        addDish.taste=taste;
        return addDish;
    }
}
class Record{
    int orderNum;
    Dish d;
    int portion;
    int num;
    int spicy;
    int acidity;
    int sweetness;
    boolean isSpecialDish;
    public int getPrice()
    {
        return d.getPrice(portion)*num;
    }
    public void setD(Dish dish) {
    }
    public void setOrderNum(int orderNum) {
    }
    public void setPortion(int portion) {
    }
    public void setNum(int num) {
    }


}
class Order {
    int dishNum;
    int allCommonPrice;
    int allSpecialPrice;
    Record[] records;
    public void addPortion() {
        Map<String, Integer> dishMap = new HashMap<>();
        for (int i = 0; i < dishNum; i++) {
            String key = records[i].d.name + "_" + records[i].portion;
            if (dishMap.containsKey(key)) {
                int index = dishMap.get(key);
                records[index].num += records[i].num;
                records[i].num = 0;
            } else {
                dishMap.put(key, i);
            }
        }
    }
    public void getTotalPrice(){
        for(int k=0;k<dishNum;k++) {
            if(!records[k].isSpecialDish)
                allCommonPrice+=records[k].getPrice();
            else
                allSpecialPrice+=records[k].getPrice();
        }
    }




    public int getTotalPrice(double commonDiscount, double specialDiscount){
        int totalPrice = 0;
        for (int k = 0; k < dishNum; k++) {
            if (!records[k].isSpecialDish) {
                totalPrice += Math.round(records[k].getPrice() * commonDiscount);
            } else {
                totalPrice += Math.round(records[k].getPrice() * specialDiscount);
            }
        }
        return totalPrice;
    }
    public Record addARecord(int orderNum,String dishName,int portion,int num , Menu menu){
        Record x=new Record();
        x.d=menu.searthDish(dishName);
        x.orderNum=orderNum;
        x.portion=portion;
        x.num=num;
        return x;
    }

    public Record addASpecialRecord(int orderNum,String dishName,int portion,int num ,Menu menu,int tasteNum , boolean forOther){
        Record x=new Record();
        x.d=menu.searthSpecialDish(dishName);
        x.orderNum=orderNum;
        x.portion=portion;
        x.num=num;
        if(!forOther){
            if(menu.searthSpecialDish(dishName).taste.equals("川菜")){
                x.spicy = tasteNum;
                x.acidity = -1;
                x.sweetness = -1;
            }
            if(menu.searthSpecialDish(dishName).taste.equals("晋菜")){
                x.spicy = -1;
                x.acidity = tasteNum;
                x.sweetness = -1;
            }
            if(menu.searthSpecialDish(dishName).taste.equals("浙菜")){
                x.spicy = -1;
                x.acidity = -1;
                x.sweetness = tasteNum;
            }
        }
        else{
            x.spicy = -1;
            x.acidity = -1;
            x.sweetness = -1;
        }
        return x;
    }
    public void delARecordByOrderNum(int orderNum){
        for(int k=0;k<dishNum;k++) {
            if(orderNum==records[k].orderNum) {
                records[k].num=0;
            }
        }
    }
    public int findRecordByNum(int orderNum){
        for(int k = 0; k<dishNum;k++) {
            if(records[k].orderNum==orderNum) {
                return 1;
            }
        }
        return 0;
    }
}
class Table{
    int tableNum;
    String tableName;
    String telephoneNum;
    String mainTime;
    String remainTime;
    Order order;
    double commonDiscount;
    double specialDiscount;
    int averageSpicy;
    int averageAcidity;
    int averageSweetness;
    double spicyNum;
    double acidityNum;
    double sweetnessNum;
    public boolean timeJudgement1() {
        String[] remainTimeParts = remainTime.split("/");
        String[] mainTimeParts = mainTime.split("/");

        int year = Integer.parseInt(mainTimeParts[0]);
        int month = Integer.parseInt(mainTimeParts[1]);
        int day = Integer.parseInt(mainTimeParts[2]);
        double hour = Double.parseDouble(remainTimeParts[0]);
        double minute = Double.parseDouble(remainTimeParts[1]);
        double second = Double.parseDouble(remainTimeParts[2]);

        LocalDate date = LocalDate.of(year, month, day);
        DayOfWeek dayOfWeek = date.getDayOfWeek();

        if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) {
            if ((hour >= 10 && hour < 21) || (hour == 9 && minute >= 30) || (hour == 21 && minute < 30) || (hour == 21 && minute == 30 && second == 0)) {
                specialDiscount = commonDiscount = 1;
                return true;
            } else {
                return false;
            }
        } else if (((DayOfWeek) dayOfWeek).getValue() >= 1 && dayOfWeek.getValue() <= 5) {
            if ((hour > 17 && hour < 20) || (hour == 17 && minute >= 0) || (hour == 20 && minute < 30) || (hour == 20 && minute == 30 && second == 0)) {
                commonDiscount = 0.8;
                specialDiscount = 0.7;
                return true;
            } else if ((hour > 10 && hour < 14) || (hour == 10 && minute >= 30) || (hour == 14 && minute < 30) || (hour == 14 && minute == 30 && second == 0)) {
                commonDiscount = 0.6;
                specialDiscount = 0.7;
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
    public int tablePrice(){
        return this.order.getTotalPrice(commonDiscount,specialDiscount);
    }
    public void getTaste(){
        for(int k = 0;k<this.order.dishNum;k++){
            if(order.records[k].isSpecialDish){
                if(order.records[k].spicy!=-1){
                    averageSpicy+=order.records[k].spicy*order.records[k].num;
                    spicyNum=order.records[k].num+spicyNum;
                }
                if(order.records[k].acidity!=-1){
                    averageAcidity+=order.records[k].acidity*order.records[k].num;
                    acidityNum=order.records[k].num+acidityNum;
                }
                if(order.records[k].sweetness!=-1){
                    averageSweetness+=order.records[k].sweetness*order.records[k].num;
                    sweetnessNum=order.records[k].num+sweetnessNum;
                }
            }
        }
        if(spicyNum!=0){
            averageSpicy=(int)Math.round(averageSpicy/spicyNum);
        }
        if(acidityNum!=0){
            averageAcidity=(int)Math.round(averageAcidity/acidityNum);
        }
        if(sweetnessNum!=0){
            averageSweetness=(int)Math.round(averageSweetness/sweetnessNum);
        }
    }
    public String spicyLevel(){
        switch (averageSpicy) {
            case 0:
                return "不辣";
            case 1:
                return "微辣";
            case 2:
                return "稍辣";
            case 3:
                return "辣";
            case 4:
                return "很辣";
            case 5:
                return "爆辣";
            default:
                return null;
        }
    }
    public String acidityLevel(){
        switch (averageAcidity) {
            case 0:
                return "不酸";
            case 1:
                return "微酸";
            case 2:
                return "稍酸";
            case 3:
                return "酸";
            case 4:
                return "很酸";
            default:
                return null;
        }
    }
    public String sweetnessLevel(){
        switch (averageSweetness) {
            case 0:
                return "不甜";
            case 1:
                return "微甜";
            case 2:
                return "稍甜";
            case 3:
                return "甜";
            default:
                return null;
        }
    }
    public void setOrder(Order order) {
    }
    public void giveTaste(String name, int num, int tasteLevel, Menu menu) {
        SpecialDish specialDish = menu.searthSpecialDish(name);
        if (specialDish != null) {
            switch (specialDish.taste) {
                case "川菜":
                    averageSpicy += tasteLevel * num;
                    spicyNum += num;
                    break;
                case "晋菜":
                    averageAcidity += tasteLevel * num;
                    acidityNum += num;
                    break;
                case "浙菜":
                    averageSweetness += tasteLevel * num;
                    sweetnessNum += num;
                    break;
            }
        }
    }
    private void updateTasteValues(int spicy, int numSpicy, int acidity, int numAcidity, int sweetness, int numSweetness) {
        if (spicy != -1) {
            averageSpicy += spicy * numSpicy;
            spicyNum += numSpicy;
        }
        if (acidity != -1) {
            averageAcidity += acidity * numAcidity;
            acidityNum += numAcidity;
        }
        if (sweetness != -1) {
            averageSweetness += sweetness * numSweetness;
            sweetnessNum += numSweetness;
        }
    }
    private void updateAverageTaste(String tasteType, int num, int averageTaste) {
        if (num != 0) {
            switch (tasteType) {
                case "spicy":
                    averageSpicy = (int) Math.round(averageSpicy / spicyNum);
                    break;
                case "acidity":
                    averageAcidity = (int) Math.round(averageAcidity / acidityNum);
                    break;
                case "sweetness":
                    averageSweetness = (int) Math.round(averageSweetness / sweetnessNum);
                    break;
            }
        }
    }
}
class CourseSelection {
    private String studentName;
    private int studentID;
    private ArrayList<String> selectedCourses;
    public CourseSelection(String studentName, int studentID) {
        this.studentName = studentName;
        this.studentID = studentID;
        this.selectedCourses = new ArrayList<>();
    }
    public void selectCourse(String courseName) {
        selectedCourses.add(courseName);
    }
    public void dropCourse(String courseName) {
        selectedCourses.remove(courseName);
    }
    public void displaySelectedCourses() {
        System.out.println("Student Name: " + studentName);
        System.out.println("Student ID: " + studentID);
        System.out.println("Selected Courses:");
        for (String course : selectedCourses) {
            System.out.println("- " + course);
        }
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getStudentID() {
        return studentID;
    }
    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }
    public ArrayList<String> getSelectedCourses() {
        return selectedCourses;
    }
    public void setSelectedCourses(ArrayList<String> selectedCourses) {
        this.selectedCourses = selectedCourses;
    }
}
class PersonPay{
    String name;
    String telephoneNum;
    int payPrice;
}
class lastPrint{
    ArrayList<Table> tables = new ArrayList<>();
    PersonPay[] personPays;
    int p;
    public void print(){
        for (Table table : tables) {
            table.order.getTotalPrice();
            table.getTaste();
            if(table.spicyNum==0&&table.acidityNum==0&&table.sweetnessNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice()+" ");
            if(table.spicyNum!=0&&table.acidityNum==0&&table.sweetnessNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel());
            if(table.spicyNum==0&&table.acidityNum!=0&&table.sweetnessNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 晋菜 " +(int)table.acidityNum+" "+table.acidityLevel());
            if(table.spicyNum==0&&table.acidityNum==0&&table.sweetnessNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 浙菜 " +(int)table.sweetnessNum+" "+table.sweetnessLevel());
            if(table.spicyNum!=0&&table.acidityNum!=0&&table.sweetnessNum==0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()+" 晋菜 "+(int)table.acidityNum+" "+table.acidityLevel());
            if(table.spicyNum!=0&&table.acidityNum==0&&table.sweetnessNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()+" 浙菜 "+(int)table.sweetnessNum+" "+table.sweetnessLevel());
            if(table.spicyNum==0&&table.acidityNum!=0&&table.sweetnessNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 晋菜 " +(int)table.acidityNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.sweetnessNum+" "+table.sweetnessLevel());
            if(table.spicyNum!=0&&table.acidityNum!=0&&table.sweetnessNum!=0)
                System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()+" 晋菜 "+(int)table.acidityNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.sweetnessNum+" "+table.sweetnessLevel());
        }
    }
    public void payPrint() {
        // 计算每个人的支付金额
        for (int i = 0; i < p; i++) {
            for (Table table : tables) {
                if (table.tableName.equals(personPays[i].name)) {
                    personPays[i].payPrice += table.tablePrice();
                }
            }
        }
        Arrays.sort(personPays, 0, p, (p1, p2) -> p1.name.compareToIgnoreCase(p2.name));
        Arrays.stream(personPays, 0, p)
                .forEach(pay -> System.out.println(pay.name + " " + pay.telephoneNum + " " + pay.payPrice));
    }
}

 

本题心得:

本题我觉得难度主要在于多用户多操作这方面,相比较菜单4而言,难度有所提高。我增加了许多类,进行了比较复杂的关联与继承。做题时,发觉只有菜单3的那几个类是远远不够的,我想把各种计算和记录分清楚,所以我采用了许多类,帮助我分清楚各个方案各个函数的意义及应该在哪里用。在这道题中,我觉得收获最多的是学着把许多类理清楚,彼此之间的联系不弄混,在主函数中调用时,如何才能更好的使用这些类。

7-1 测验1-圆类设计

 

创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积

输入格式:

输入圆的半径,取值范围为(0,+∞),输入数据非法,则程序输出Wrong Format,注意:只考虑从控制台输入数值的情况

输出格式:

输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)

输入样例:

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

2.35

输出样例:

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

17.35

类图如下:

 代码如下:

import java.util.Scanner;

class Circle {
    double r;
    public Circle(double r) {
        this.r = r;
    }
    public double getArea() {
        return Math.PI * Math.pow(r, 2);
    }
}
public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle(0);
        Scanner scanner = new Scanner(System.in);
        double r = scanner.nextDouble();
        if (r <= 0) {
            System.out.println("Wrong Format");
        } else {
            circle.r = r;
            double area = circle.getArea();
            System.out.printf("%.2f%n", area);
        }
    }
}

本题心得:

这道题相比于菜单系列容易太多了,一个类就完成了,甚至不用类都能完成。

7-2 测验2-类结构设计

设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:

 

 

输入格式:

分别输入两个坐标点的坐标值x1,y1,x2,y2。

输出格式:

输出该矩形的面积值(保留两位小数)。

输入样例:

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

6 5.8 -7 8.9

输出样例:

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

40.30

代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        double width = Math.abs(x2 - x1);
        double height = Math.abs(y2 - y1);
        double area = width * height;
        System.out.printf("%.2f%n", area);
    }
}

本题心得:

这道题也很简单,当时考试时我甚至都没有想着用类做,直接就定义几个变量,用math计算一下,直接打印出来结果了。结果我提交完这道题后,老师说必须用类做,唉,又因为我直接提交就是满分,也不能重新修改,真的是难受。

7-3 测验3-继承与多态

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

 

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

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

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

输入格式:

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

输出格式:

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

输入样例1:

1
5.6

输出样例1:

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

98.52

输入样例2:

2
5.6
-32.5
9.4
-5.6

输出样例2:

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

102.22

类图如下:

 代码如下:

import java.util.Scanner;
import java.text.DecimalFormat;
abstract class Shape {
    abstract double getArea();
    void printArea(Shape shape) {
        double a = shape.getArea();
        DecimalFormat df = new DecimalFormat("#.00");
        System.out.println(df.format(a));
    }
}

class Circle extends Shape {
    private double r;
    public Circle(double r) {
        this.r = r;
    }
    public double getArea() {
        return Math.PI * r * r;
    }
}

class Rectangle extends Shape {
    Point leftTopPoint;
    Point lowerRightPoint;
    public Rectangle(Point leftTopPoint, Point lowerRightPoint) {
        this.leftTopPoint = leftTopPoint;
        this.lowerRightPoint = lowerRightPoint;
    }
    public double getArea() {
        double width = Math.abs(leftTopPoint.x - lowerRightPoint.x);
        double height = Math.abs(leftTopPoint.y - lowerRightPoint.y);
        return width * height;
    }
}
class Point {
    double x;
    double y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        switch(choice) {
            case 1:
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                circle.printArea(circle);
                break;
            case 2: // Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();
                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);
                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                rectangle.printArea(rectangle);
                break;
        }
    }
}

本题心得:

真的服了,给的代码少了个},考试时写好了类,把给的代码放上去后直接报错,查哪里报错用了十几分钟(类代码我都没写这么久),这道题不难,主要是将上两题的类稍微整理一下,抽象出一个父类即可。

7-4 测验4-抽象类与接口

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

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

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

        int choice = input.nextInt();

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

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

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

输入格式:

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

输入图形所需参数

输出格式:

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

输入样例:

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

1
2.3
2
3.2
3
6
5
1
2.3
0

输出样例:

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

5.60 16.62 16.62 

类图如下:

 代码如下:

import java.util.ArrayList;
import java.util.Scanner;

class Point {
    double x;
    double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

abstract class Shape implements Comparable<Shape> {
    public abstract double getArea();

    @Override
    public int compareTo(Shape other) {
        return Double.compare(this.getArea(), other.getArea());
    }
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle extends Shape {
    private Point leftTopPoint;
    private Point lowerRightPoint;

    public Rectangle(Point leftTopPoint, Point lowerRightPoint) {
        this.leftTopPoint = leftTopPoint;
        this.lowerRightPoint = lowerRightPoint;
    }

    @Override
    public double getArea() {
        double width = Math.abs(lowerRightPoint.x - leftTopPoint.x);
        double height = Math.abs(lowerRightPoint.y - leftTopPoint.y);
        return width * height;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();

        int choice = input.nextInt();

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

        list.sort(Comparable::compareTo); // 使用默认的compareTo方法进行排序

        for (Shape shape : list) {
            System.out.print(String.format("%.2f", shape.getArea()) + " ");
        }
    }
}

本题心得:

相比于第三题,这道题用了个list.sort(Comparable::compareTo)函数,方便排序,在shape中加了个Comparable接口,也不算很麻烦,相对于第三题写代码和找报错花费的时间少了许多。通过point类,能较方便地串联各个类,比较舒服。

三、踩坑心得

1.对于菜单5,我增加了许多类,虽然方便我理清题目,但是在实际操作写代码时,反而会比较麻烦,主要在于容易混淆各个变量和类中的函数。

2.对于菜单4,题目增加了许多异常情况,可能会需要修改大量的现有代码,主函数逻辑等。这样会导致代码的可维护性变差。可以考虑使用工厂模式或者策略模式,将不同类型的图形创建逻辑封装到各自的工厂类或策略类中,从而减少对现有代码的修改,提高代码的可扩展性。

四、主要困难及改进意见
1.对于多种异常情况,像以前处理起来容易造成代码冗余,很累赘。
改进意见:这两周上课时老师在将工厂模式,利用工厂模式能很好地解决这类问题,同时能使代码更为简洁易懂。
2.如果增加许多类,会造成结构混乱,不容易理清楚。
改进意见:增加类有好处也有坏处,需要根据自身需求考虑。
五、总结
我知道如何运用类的多态与继承,以及继承类的super用法,还有正则表达式的简单使用。明白如何使用容器类和容器类的作用,还有电脑文件的调用和一些对文件的处理方法。对正则表达式方面的知识还要多加深入学习;每天还是要多练,多学,多动手写代码,以达到熟练运用的效果。但是对那个构造一个对象数组的构造上还是有点迷糊,还有抽象父类中方法是否需要用抽象方法或者空方法体还没去细致了解这方面的差别。