第二次博客作业

PTA菜单计价4,5以及期中考试总结

7-1菜单计价程序-4

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

输入内容按先后顺序包括两部分:菜单、订单,最后以"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

img

img代码如下:

import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static boolean isNumeric(String string) {
        int intValue;
        try {
            intValue = Integer.parseInt(string);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static void main(String[] args) throws ParseException {
        Menu menu = new Menu();
        ArrayList<Table> tables = new ArrayList<Table>();
        Scanner input = new Scanner(System.in);
        String str1 = new String();
        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.dishs.remove(isRepeat);
                }
                menu.dishs.add(temp);
            } else if (str1.matches("[\\S]* [\\d]* T")) {
                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 = true;
                if (isRepeat != -1) {
                    menu.dishs.remove(isRepeat);
                }
                menu.dishs.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")) {
            for (int j = 0; j < 2; j++) {

            }
            Table temp = new Table();
            boolean isRepeat = false;
            int repeatNum = 0;
            if (str1.matches("table.*")) {
                if (str1.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {
                    String[] token = str1.split(" ");
                    String[] Date = token[2].split("/");
                    String[] Time = token[3].split("/");
                    int[] intDate = new int[3];
                    int[] intTime = new int[3];
                    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.get_WeekDay();
                    } catch (DateTimeException e) {
                        System.out.println(temp.num + " date error");
                        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) {
                                            temp = tables.get(i);
                                            isRepeat = true;
                                            repeatNum = i;
                                            break;
                                        }
                                    }
                                    // 在周末
                                    else {
                                        // 时间相差小于一小时
                                        if (duration.toHours() < 3600) {
                                            temp = tables.get(i);
                                            repeatNum = i;
                                            isRepeat = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(!isRepeat) {
                        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 (temp.order.records.size() > 0) {
                            if (Integer.parseInt(
                                    token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {
                                System.out.println("record serial number sequence error");
                                continue;
                            }
                        }
                        if (menu.searchDish(token[1]) == -1) {
                            System.out.println(token[1] + " does not exist");
                            continue;
                        }
                        if (portion > 3 || portion < 1) {
                            System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);
                            continue;
                        }
                        if (quota > 15) {
                            System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota);
                            continue;
                        }
                        temp.od(menu, token[0], token[1], portion, quota);
                    }
                    // 判断是否为删除订单
                    else if (str1.matches("[1-9][\\d]* delete")) {
                        String[] token = str1.split(" ");
                        temp.order.delRecordByOrderNum(Integer.parseInt(token[0]));
                    }
                    // 判断是否为夹杂菜单
                    else if (str1.matches("[\\S]* [\\d]*")) {
                        for (int j = 0; j < 1; j++) {
                            System.out.println("invalid dish");
                        }
                        continue;
                    } else if (str1.matches("[\\S]* [\\d]* T")) {
                        for (int j = 0; j < 1; j++) {
                            System.out.println("invalid dish");
                        }
                        continue;
                    }
                    // 判断是否为代点
                    else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
                        String[] token = str1.split(" ");
                        // 判断代点桌号是否存在
                        boolean exist = false;
                        for (int 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.dishs.get(menu.searchDish(token[2]));
                            portion = Integer.parseInt(token[3]);
                            quota = Integer.parseInt(token[4]);
                            treat.portion = portion;
                            treat.quota = quota;
                            System.out.print(treat.get_Price() + "\n");
                            temp.sum += treat.get_Price();
                        }
                        // 若不存在则输出内容
                        else {
                            for (int j = 0; j < 1; j++) {
                                System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
                            }
                        }

                    } else if (str1.equals("end")) {
                        break;
                    } else if(str1.matches("ta.*")){
                        break;

                    }
                    else {
                        System.out.println("wrong format");
                        continue;
                    }
                }
            } else if (str1.matches("t.*")) {
                isRepeat = true;
                temp = tables.get(tables.size());
                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 (temp.order.records.size() > 0) {
                            if (Integer.parseInt(
                                    token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {
                                System.out.println("record serial number sequence error");
                                continue;
                            }
                        }
                        if (menu.searchDish(token[1]) == -1) {
                            System.out.println(token[1] + " does not exist");
                            continue;
                        }
                        if (portion > 3 || portion < 1) {
                            System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);
                            continue;
                        }
                        if (quota > 15) {
                            System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota);
                            continue;
                        }
                        temp.od(menu, token[0], token[1], portion, quota);
                    }
                    // 判断是否为删除订单
                    else if (str1.matches("[1-9][\\d]* delete")) {
                        String[] token = str1.split(" ");
                        temp.order.delRecordByOrderNum(Integer.parseInt(token[0]));
                    }
                    // 判断是否为夹杂菜单
                    else if (str1.matches("[\\S]* [\\d]*")) {
                        System.out.println("invalid dish");
                        continue;
                    } else if (str1.matches("[\\S]* [\\d]* T")) {
                        System.out.println("invalid dish");
                        continue;
                    }
                    // 判断是否为代点
                    else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
                        String[] token = str1.split(" ");
                        // 判断代点桌号是否存在
                        boolean exist = false;
                        for (int 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.dishs.get(menu.searchDish(token[2]));
                            portion = Integer.parseInt(token[3]);
                            quota = Integer.parseInt(token[4]);
                            treat.portion = portion;
                            treat.quota = quota;
                            System.out.print(treat.get_Price() + "\n");
                            temp.sum += treat.get_Price();
                        }
                        // 若不存在则输出内容
                        else {
                            for (int j = 0; j < 1; j++) {
                                System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
                            }
                        }

                    } else if (str1.equals("end")) {
                        break;
                    } else {
                        System.out.println("wrong format");
                        continue;
                    }
                }
                for (int j = 0; j < 1; j++) {

                }
                if (tables.size() != 0) {
                    tables.get(tables.size() - 1).order.records.addAll(temp.order.records);
                }
            } else {
                str1 = input.nextLine();
                continue;
            }

            // 本桌点菜结束,进入下一桌
            if (isRepeat) {
                tables.remove(repeatNum);
            }
            temp.get_Sum();
            tables.add(temp);
        }
        // 最终输出桌号订单信息
        for (i = 0; i < tables.size(); i++) {
            if (tables.get(i).isOpen()) {
                for (int j = 0; j < 1; j++) {
                    System.out.println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum);
                }
            } else
                for (int j = 0; j < 1; j++) {
                    System.out.println("table " + tables.get(i).num + " out of opening hours");
                }

        }
    }

    static class Dish {
        String name;
        int unit_price;
        boolean isT = false;
    }

    static class Record {
        int orderNum;
        Dish d;
        int portion;
        int quota;
        boolean is_Deleted = false;

        int get_Price() {
            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> dishs = new ArrayList<Dish>();

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

        Dish addDish(String dishName, int unit_price) {
            Dish newDish = new Dish();
            newDish.name = dishName;
            newDish.unit_price = unit_price;
            return newDish;
        }
    }

    static class Order {
        //		Record[] records = new Record[20];
        ArrayList<Record> records = new ArrayList<Record>();

        Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) {
            Record newRecord = new Record();
            newRecord.orderNum = orderNum;
            newRecord.d = menu.dishs.get(menu.searchDish(dishName));
            newRecord.portion = portion;
            newRecord.quota = quota;
            System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.get_Price());
            return newRecord;
        }

        int searchReocrd(String name) {
            for (int i = 0; i < records.size(); i++) {
                if (records.get(i).d.name == name) {
                    return i;
                }
            }
            return -1;
        }

        boolean delRecordByOrderNum(int orderNum) {
            int i = 0, flag = 0;
            for (i = 0; i < records.size(); i++) {
                if (records.get(i).orderNum == orderNum) {
                    if (records.get(i).is_Deleted == false) {
                        records.get(i).is_Deleted = true;
                    } else {
                        System.out.println("deduplication " + orderNum);
                    }
                    flag++;
                }
            }
            if (flag == 0) {
                System.out.println("delete error;");
                return false;
            }
            return true;
        }
    }

    static class Table {
        Order order = new Order();
        int num;
        LocalDateTime time;
        int weekday;
        long sum = 0;
        long origSum = 0;
        void od(Menu menu, String str1, String str2, int portion, int quota) {
            {
                order.records.add(order.addARecord(Integer.parseInt(str1), str2, portion, quota, menu));

            }
        }

        void get_WeekDay() {
            weekday = time.getDayOfWeek().getValue();
        }

        void get_Sum() {
            for (int i = 0; i < order.records.size(); i++) {
                if (!order.records.get(i).is_Deleted) {
                    origSum += order.records.get(i).get_Price();
                    if (order.records.get(i).d.isT) {
                        if (weekday > 0 && weekday < 6) {
                            sum += Math.round(order.records.get(i).get_Price() * 0.7);
                        }
                        else {
                            sum += order.records.get(i).get_Price();
                        }
                    }
                    else
                    {
                        if (weekday > 0 && weekday < 6) {
                            if (time.getHour() >= 17 && time.getHour() < 20)
                                sum += Math.round(order.records.get(i).get_Price() * 0.8);
                            if (time.getHour() == 20) {
                                if (time.getMinute() <= 30)
                                    sum += Math.round(order.records.get(i).get_Price() * 0.8);
                            }
                            if (time.getHour() >= 10 && time.getHour() < 14)
                                sum += Math.round(order.records.get(i).get_Price() * 0.6);
                            if (time.getHour() == 14) {
                                if (time.getMinute() <= 30)
                                    sum += Math.round(order.records.get(i).get_Price() * 0.6);
                            }
                        }
                        else sum+=order.records.get(i).get_Price();
                    }
                }
            }

        }

        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) {
                    if (time.getMinute() <= 30)
                        return true;
                }
            } else {
                if (time.getHour() > 9 && time.getHour() < 21)
                    return true;
                if (time.getHour() == 9) {
                    if (time.getMinute() >= 30)
                        return true;
                }
                if (time.getHour() == 21) {
                    if (time.getMinute() <= 30)
                        return true;
                }
            }
            return false;

        }
    }
}

代码运行结果:

分析:

在处理输入信息时,必须精准地分隔不同类型的输入格式,如菜单、表格、点菜和带点菜等,然后进行详细的分析和相应的信息处理。这包括对相应类别的信息更新,同时需要巧妙地区分和提醒错误输入信息,确保系统能够有效地应对各种情况。

处理时间错误尤为复杂,首先涉及到工作日的准确判断,其次是对优惠日的辨识,最后需要考虑到开店时间的验证。在进行这些判断的同时,还要处理不存在日期的情况,例如处理2月30号以及时间输入错误的情况(如25:00分)。在这个过程中,使用try和catch机制是必要的,以确保对错误信息的准确判断和处理。

对输入的信息进行全面的判断,将相应的信息妥善存储在对应的对象数据结构中。在输出信息时,需要进行有效的提取,确保系统能够准确而高效地呈现用户所需的信息。这涉及到对输入数据的合理分类和对数据结构的良好设计,以便系统在不同情境下都能够正确地处理和展示信息。

难点:

在进行数据处理时,对数据格式的判断至关重要。我采用了双向关联的关系模型,然而在实际应用中,这种选择给我的代码编写带来了巨大的负担。在选择框架时,需要考虑到对数据处理的效率和便捷性,以提高代码的可维护性和可读性。

在处理数据时,准确判断数据的类型是至关重要的。缺乏对数据类型的准确判断将导致无法对输入的信息进行精准的分析和判断。确保对数据类型的明确认知,有助于建立更为健壮和高效的数据处理系统。这包括使用适当的工具或技术,以确保系统能够在各种情况下正确地解读和处理不同类型的数据,提高代码的鲁棒性。

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.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String scan = null;
        String[] weeks = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
        Scanner input = new Scanner(System.in);
        Menu menu = new Menu();
        String[] regex = new String[6];
        regex[0] = "[\\u4e00-\\u9fa5]+ ([\\u4e00-\\u9fa5]+ )?\\d+(\\.\\d+)?( T)?";  //菜品信息正则表达式
        regex[1] = "\\d+ delete";  //删除信息正则表达式
        regex[2] = "table[\\s\\S]+";  //桌号信息正则表达式
        regex[3] = "\\d+ [\\u4e00-\\u9fa5a-zA-Z]+ \\d+ \\d+( \\d+)?";  //普通记录信息正则表达式
        regex[4] = "\\d+ \\d+ [\\u4e00-\\u9fa5a-zA-Z]+( \\d+) \\d+ \\d+";  //代点菜记录信息正则表达式
        regex[5] = "end";  //退出信息正则表达式
        ArrayList<Table> tables = new ArrayList<Table>();
        ArrayList<Order> orders = new ArrayList<Order>();
        ArrayList<Client> clients = new ArrayList<Client>();
        int count = 0;
        while (true) {
            scan = input.nextLine();
            String[] splitScan = scan.split(" ");
            for (int i = 0; i < 6; i++) {
                if (scan.matches(regex[i])) {
                    break;
                } else if (i == 5 && count == 0) {
                    System.out.println("wrong format");
                }
            }
            if (scan.matches(regex[0])) {  //如果格式为第一中菜品名+菜品单价
                Dish newDish = new Dish();
                if (scan.matches("[\\u4e00-\\u9fa5]+ (川菜|晋菜|浙菜) \\d+ T")) {  //如果是特色菜的情况
                    newDish = menu.add_Dish(splitScan[0], Integer.parseInt(splitScan[2]));
                    newDish.special = true;
                    newDish.type = splitScan[1];
                } else if (scan.matches("[\\u4e00-\\u9fa5]+ \\d+")) {
                    newDish = menu.add_Dish(splitScan[0], Integer.parseInt(splitScan[1]));
                } else {
                    System.out.println("wrong format");
                    continue;
                }
                int index = menu.searthDish(newDish.name);
                if (index == -1) {  //查询新增的菜品是否已经在菜单中
                    menu.dishes.add(newDish);
                } else {
                    menu.dishes.set(index, newDish);
                }
            }
            while (scan.matches(regex[2])) { //如果格式为桌号识别格式,则录入对应的桌号信息和后续的点菜记录
                count++;
                if (scan.matches("table \\d+ : [a-zA-Z]{1,10} (180|181|189|133|135|136)\\d{8} \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}")) {
                    Table table = new Table();
                    Date date1 = null;
                    try {//需要处理异常语句,即格式出错的情况
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
                        date1 = simpleDateFormat.parse(splitScan[5] + " " + splitScan[6]);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    String[] splitDate1 = date1.toString().split(" |:");
                    for (int i = 0; i < weeks.length; i++) { //获得对应的星期
                        if (splitDate1[0].equals(weeks[i])) {
                            table.week = i + 1;
                            break;
                        }
                    }
                    table.tableNumber = Integer.parseInt(splitScan[1]);  //获得对应桌号
                    table.client = splitScan[3];  //获得对应客户姓名
                    table.telephone = splitScan[4];  //获得对应客户电话
                    table.hour = Integer.parseInt(splitDate1[3]); //获得对应的小时
                    table.min = Integer.parseInt(splitDate1[4]);  //获得对应的分钟
                    table.sec = Integer.parseInt(splitDate1[5]);  //获得对应的秒钟
                    if (time(table.week, table.hour, table.min) == -1) {
                        System.out.println("table" + " " + table.tableNumber + " out of opening hours");
                        break;
                    }
                    System.out.println("table " + table.tableNumber + ": ");
                    tables.add(table);
                    scan = input.nextLine();
                    splitScan = scan.split(" ");
                    Order order = new Order();
                    orders.add(order);
                    while (!scan.matches(regex[2]) && !scan.matches(regex[5])) {
                        if (scan.matches(regex[1])) {  //删除记录
                            int index = orders.get(orders.size() - 1).find_RecordByNum(Integer.parseInt(splitScan[0]));
                            if (index != -1){
                                all_Price(orders.get(orders.size() - 1).records.get(index), table, "-");
                                all_Taste(orders.get(orders.size() - 1).records.get(index), table ,"-");
                                orders.get(orders.size() - 1).records.remove(index);
                            }else {
                                System.out.println("delete error;");
                            }
                        } else if (scan.matches(regex[3])) {  //普通记录
                            int flag = 0;
                            Record newRecord = new Record();
                            Dish dish = new Dish();
                            dish.name = splitScan[1];
                            if (menu.searthDish(dish.name) == -1) {
                                System.out.println(dish.name + " does not exist");
                                scan = input.nextLine();
                                splitScan = scan.split(" ");
                                continue;
                            }
                            dish = menu.dishes.get(menu.searthDish(dish.name));
                            if (scan.matches("\\d+ [\\u4e00-\\u9fa5a-zA-Z]+ \\d+ \\d+") && !dish.special) {
                                newRecord = order.add_ARecord(Integer.parseInt(splitScan[0]), splitScan[1], Integer.parseInt(splitScan[2]), Integer.parseInt(splitScan[3]));
                                flag = 1;
                            } else if (scan.matches("\\d+ [\\u4e00-\\u9fa5a-zA-Z]+ \\d+ \\d+ \\d+") && dish.special) {
                                newRecord = order.add_ARecord(Integer.parseInt(splitScan[0]), splitScan[1], Integer.parseInt(splitScan[3]), Integer.parseInt(splitScan[4]));
                                newRecord.level = Integer.parseInt(splitScan[2]);
                                flag = 1;
                            }
                            if (flag == 1) {
                                newRecord.d = menu.dishes.get(menu.searthDish(newRecord.d.name));
                                if (checkLevel(newRecord)) {  //检查口味程度是否超出范围
                                    orders.get(orders.size() - 1).records.add(newRecord);//将对于记录加到末尾
                                    all_Price(newRecord, table , "+");
                                    all_Taste(newRecord, table, "+");
                                    System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.get_Price());
                                }
                            } else {
                                System.out.println("wrong format");
                            }
                        } else if (scan.matches(regex[4])) {  //代点菜记录
                            int flag = 0;
                            Record newRecord = new Record();
                            Dish dish = new Dish();
                            dish.name = splitScan[2];
                            if (menu.searthDish(dish.name) == -1) {
                                System.out.println(dish.name + " does not exist");
                                scan = input.nextLine();
                                splitScan = scan.split(" ");
                                continue;
                            }
                            dish = menu.dishes.get(menu.searthDish(dish.name));
                            if (scan.matches("\\d+ \\d+ [\\u4e00-\\u9fa5a-zA-Z]+ \\d+ \\d+") && !dish.special) {
                                newRecord = order.add_ARecord(Integer.parseInt(splitScan[0]), splitScan[2], Integer.parseInt(splitScan[3]), Integer.parseInt(splitScan[4]));
                                flag = 1;
                            } else if (scan.matches("\\d+ \\d+ [\\u4e00-\\u9fa5a-zA-Z]+ \\d+ \\d+ \\d+") && dish.special) {
                                newRecord = order.add_ARecord(Integer.parseInt(splitScan[0]), splitScan[2], Integer.parseInt(splitScan[4]), Integer.parseInt(splitScan[5]));
                                newRecord.level = Integer.parseInt(splitScan[3]);
                                flag = 1;
                            }
                            if (flag == 1) {
                                if (checkLevel(newRecord)) {  //检查口味程度是否超出范围
                                    newRecord.d = menu.dishes.get(menu.searthDish(newRecord.d.name));
                                    int index = 0;
                                    for (int i = 0; i < tables.size(); i++) {
                                        if (tables.get(i).tableNumber == Integer.parseInt(splitScan[0])) {
                                            index = i;
                                            break;
                                        }
                                    }
                                    all_Price(newRecord, table, "+");
                                    all_Taste(newRecord, tables.get(index) ,"+");
                                    orders.get(index).records.add(newRecord);//将对于记录加到末尾
                                    System.out.println(splitScan[1] + " table " + tables.get(tables.size() - 1).tableNumber + " pay for table " + splitScan[0] + " " + newRecord.get_Price());
                                }
                            } else {
                                System.out.println("wrong format");
                            }
                        } else {
                            System.out.println("wrong format");
                        }
                        scan = input.nextLine();
                        splitScan = scan.split(" ");
                    }
                }else{
                    System.out.println("wrong format");
                    break;
                }
            }
            if (scan.matches(regex[5])) {
                break;
            }
        }
        for (int i = 0; i < tables.size(); i++) {  //桌价信息输出
            int count1 = 0;
            System.out.print("table" + " " + tables.get(i).tableNumber + ": " + tables.get(i).tablePrice + " " + tables.get(i).discountPrice);
            if (tables.get(i).spicyNum != 0) {
                System.out.print(" 川菜" + " " + tables.get(i).spicyNum + " ");
                printLevel(tables.get(i).spicyNum,tables.get(i).spicySum,0);
                count1++;
            }
            if (tables.get(i).acidityNum != 0) {
                System.out.print(" 晋菜" + " " + tables.get(i).acidityNum + " ");
                printLevel(tables.get(i).acidityNum,tables.get(i).aciditySum,1);
                count1++;
            }
            if (tables.get(i).sweetNum != 0) {
                System.out.print(" 浙菜" + " " + tables.get(i).sweetNum + " ");
                printLevel(tables.get(i).sweetNum,tables.get(i).sweetSum,2);
                count1++;
            }
            if (count1 == 0){
                System.out.print(" ");
            }
            System.out.println();
        }
        if (!tables.isEmpty()){
            clients.add(new Client(tables.get(0).client,tables.get(0).telephone,tables.get(0).discountPrice));
        }
        for (int i = 1; i < tables.size(); i++) {
            for (int j = 0; j < clients.size(); j++) {
                if (tables.get(i).client.equals(clients.get(j).name) && tables.get(i).telephone.equals(clients.get(j).telephone)){
                    clients.get(j).pay += tables.get(i).discountPrice;
                    break;
                } else if (j == clients.size() - 1) {
                    clients.add(new Client(tables.get(i).client,tables.get(i).telephone,tables.get(i).discountPrice));
                    break;
                }
            }
        }
        sort(clients);
        for (int i = 0; i < clients.size(); i++) {
            System.out.print(clients.get(i).name + " " + clients.get(i).telephone + " " + clients.get(i).pay);
            if(i != clients.size() - 1){
                System.out.println();
            }

        }
    }
    public static void printLevel(int Num,int Sum,int type) {
        String[] spicy = new String[]{"不辣","微辣","稍辣","辣","很辣","爆辣"};
        String[] acidity = new String[]{"不酸","微酸","稍酸","酸","很酸"};
        String[] sweetness = new String[]{"不甜","微甜","稍甜","甜"};
        int average = (int)(Sum * 1.0 / Num + 0.5);
        if (type == 0){
            System.out.print(spicy[average]);
        } else if (type == 1) {
            System.out.print(acidity[average]);
        } else if (type == 2) {
            System.out.print(sweetness[average]);
        }
    }
    public static int time(int week, int hour, int min){
        if (week >= 1 && week <= 5){
            if(hour >= 17 && hour < 20 || (hour == 20 && min <= 30)){
                return 8;
            }
            if(hour >= 11 && hour < 14 || (hour == 14 && min <= 30) || (hour == 10 && min >= 30)){
                return 6;
            }
        } else {
            if(hour >= 10 && hour < 21 || (hour == 9 && min >= 30) || (hour == 21 && min <= 30)){
                return 10;
            }
        }
        return -1;
    }


    public static boolean checkLevel(Record record){
        if ("川菜".equals(record.d.type)){
            if (record.level < 0 || record.level > 5) {
                System.out.println("spicy num out of range :" + record.level);
                return false;
            }
        } else if ("晋菜".equals(record.d.type)) {
            if (record.level < 0 || record.level > 4) {
                System.out.println("acidity num out of range :" + record.level);
                return false;
            }
        } else if ("浙菜".equals(record.d.type)) {
            if (record.level < 0 || record.level > 3) {
                System.out.println("sweetness num out of range :" + record.level);
                return false;
            }
        }
        return true;
    }
    public static int get_DiscountPrice(Record record,int week,int hour,int min){
        if ( record.d.special ){ //特色菜的情况
            if (time(week, hour, min) == 10){
                return record.get_Price();
            }else {
                return (int) (record.get_Price() * 0.7 + 0.5);
            }
        }else {
            if (time(week, hour, min) == 10){
                return record.get_Price();
            }else if (time(week, hour, min) == 8){
                return (int)(record.get_Price() * 0.8 + 0.5);
            }else if (time(week, hour, min) == 6){
                return (int)(record.get_Price() * 0.6 + 0.5);
            }else{
                return 0;
            }
        }
    }
    public static void all_Taste(Record record,Table table ,String operate) {
        if ("+".equals(operate)) {
            if ("川菜".equals(record.d.type)) {
                table.spicyNum += record.number;
                table.spicySum += record.level * record.number;
            } else if ("晋菜".equals(record.d.type)) {
                table.acidityNum += record.number;
                table.aciditySum += record.level * record.number;
            } else if ("浙菜".equals(record.d.type)) {
                table.sweetNum += record.number;
                table.sweetSum += record.level * record.number;
            }
        } else if ("-".equals(operate)) {
            if ("川菜".equals(record.d.type)) {
                table.spicyNum -= record.number;
                table.spicySum -= record.level * record.number;
            } else if ("晋菜".equals(record.d.type)) {
                table.acidityNum -= record.number;
                table.aciditySum -= record.level * record.number;
            } else if ("浙菜".equals(record.d.type)) {
                table.sweetNum -= record.number;
                table.sweetSum -= record.level * record.number;
            }
        }
    }
    public static void all_Price(Record record,Table table,String operate) {
        if ("+".equals(operate)) {
            table.tablePrice += record.get_Price();
            table.discountPrice += get_DiscountPrice(record, table.week, table.hour, table.min);
        }else if ("-".equals(operate)){
            table.tablePrice -= record.get_Price();
            table.discountPrice -= get_DiscountPrice(record, table.week, table.hour, table.min);
        }
    }
    public static void sort(ArrayList<Client> clients){
        int min = 0;
        for (int i = 0; i < clients.size(); i++) {
            min = i;
            for (int j = i + 1; j < clients.size(); j++) {
                if (clients.get(min).name.compareToIgnoreCase(clients.get(j).name) > 0){
                    min = j;
                }
            }
            Client swap = clients.get(i);
            clients.set(i,clients.get(min));
            clients.set(min,swap);
        }
    }
}
class Dish{
    String name;//菜品名称
    int unit_price; //单价
    boolean special;  //特色菜
    String type;  //菜品类型

    public Dish() {
        special = false;
        type = null;
    }
    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
        special = false;
        type = null;
    }
    public Dish(String name, int unit_price , boolean special) {
        this.name = name;
        this.unit_price = unit_price;
        this.special = special;
    }

    public int get_Price(int portion){ //portion点菜份额1小/2中/3大
        int price = 0;
        if(portion == 1){
            price = this.unit_price;
        } else if (portion == 2) {
            price = (int) (this.unit_price*1.5+0.5);
        } else if (portion == 3) {
            price = this.unit_price*2;
        }
        return price;
    }
}
class Menu{
    ArrayList<Dish> dishes;//菜品数组,保存所有菜品信息

    public Menu() {
        dishes = new ArrayList<Dish>();
    }

    public int searthDish(String dishName) {  //根据菜名在菜谱中查找菜品信息,返回对应下标。
        for(int i = 0;i <dishes.size();i++ ){
            if(dishes.get(i).name.equals(dishName)){
                return i;
            }
        }
        return -1;
    }
    public Dish add_Dish(String dishName,int unit_price){//添加一道菜品信息
        return new Dish(dishName,unit_price);
    }
}
class Record{
    int orderNum;//序号
    Dish d;//菜品
    int portion;//份额(1/2/3代表小/中/大份)
    int number;//份数
    int level;//酸甜辣程度

    public Record() {
        level = -1;
    }

    public Record(int orderNum, String dishName, int portion, int number) {
        this.d = new Dish();
        this.orderNum = orderNum;
        this.d.name = dishName;
        this.portion = portion;
        this.number = number;
        level = -1;
    }
    public int get_Price() {//计价,计算本条记录的价格
        return d.get_Price(portion)*number;
    }
}
class Order{
    ArrayList <Record> records;//保存订单上每一道的记录
    public int getTotal_Price() {//计算订单的总价
        int allPrice = 0;
        for (int i = 0;i < records.size();i++ ){
            allPrice += records.get(i).get_Price();
        }
        return allPrice;
    }
    public Order() {
        records = new ArrayList<Record>();
    }

    public Record add_ARecord(int orderNum,String dishName,int portion,int num) {//添加一条菜品信息到订单中。
        Record newrecord = new Record();
        newrecord.d = new Dish();
        newrecord.orderNum = orderNum;
        newrecord.d.name = dishName;
        newrecord.portion = portion;
        newrecord.number = num;
        return newrecord;
    }
    public int find_RecordByNum(int orderNum) {//根据序号查找一条记录
        for (int i = 0;i < records.size();i++){
            if (records.get(i).orderNum == orderNum){
                return i;
            }
        }
        return -1;
    }
}
class Table{
    public Table() {
        tablePrice = 0;
        discountPrice = 0;
        spicyNum = 0;
        spicySum = 0;
        acidityNum = 0;
        aciditySum = 0;
        sweetNum = 0;
        sweetSum = 0;
    }
    int tableNumber; //桌号
    int hour; //小时
    int week;  //星期
    int min;  //分钟
    int sec;  //秒钟
    int tablePrice;  //折扣前总价
    int discountPrice;  //折扣后总价
    String client;  //客户姓名
    String telephone;  //客户电话
    int spicySum;  //辣度和
    int spicyNum;  //辣菜份数
    int aciditySum;  //酸度和
    int acidityNum;  //酸菜份数
    int sweetSum;  //甜度和
    int sweetNum;  //甜菜份数
}
class Client{
    public Client() {
        pay = 0;
    }
    public Client(String name, String telephone, int pay) {
        this.name = name;
        this.telephone = telephone;
        this.pay = pay;
    }
    String name;
    String telephone;
    int pay;
}

代码运行结果如下:

img

该题测试点较多,难度较大,相较于上一题,难度继续加大了

分析:

与之前的菜单计价程序-4相比,这次的程序引入了特色菜,同时需要处理更多的信息,包括菜品名称和身份证。此外,还加入了对table的错误判断因素,需要对相应的输出进行更细致的数据处理。这使得整个程序更为复杂,要求系统在处理不同输入情况时能够高效而准确地应对。

相对于上一版本,这次的程序涉及了更多的菜品属性,如甜度、酸度和辣度,从而增加了代码对信息读入和处理的工作量。这进一步要求程序在计算菜单总价的同时,还要考虑各种属性的影响,使得系统更为智能化和全面化。

输入方面,这次的数据掺杂了上一次的输入格式和新的输入格式,因此需要在两种相似的输入格式中进行细致的区分。这意味着程序必须具备辨别不同输入模式的能力,确保对各种输入情况的正确响应。在处理这样混合输入的情况下,系统需要更高的灵活性和智能性,以确保用户友好且无误的操作体验

难点:

针对输入信息的多样性和更为复杂的格式,系统需要具备更高的适应性,以实现对输入数据的细致区分并进行相应的信息处理。这包括对不同输入格式的准确识别和区分,确保系统能够有效地理解和处理用户提供的各种信息。在面对更为多变的输入形式时,系统的数据处理机制必须足够灵活,以保证整个程序能够应对不同情况下的信息输入要求。

对于菜品信息的处理需要更多的分类信息读取和读入。这意味着系统必须能够识别和提取不同层次的菜品信息,包括但不限于各类分类、属性、和标签。扩展对菜品信息的处理不仅使系统更为全面,还增加了用户在点餐过程中的选择和个性化定制的可能性。系统在读取和处理这些更为详细的菜品信息时,需要具备更为智能和精准的数据处理能力。

总结:

在动手之前充分思考,我以前的习惯是在理解题目的同时边写边思考,觉得慢慢改进也无妨。然而,这次的菜单计价程序遇到了一些困难。类之间的结构呈现多样化,各自具有一些缺点,因此需要根据实际需求不断调整类的结构。这次经验使我深刻认识到,在开发程序时,在设计类的结构时要更加慎重,注重灵活性和可维护性。

学到了正则表达式的应用,发现在数据检查的过程中非常有用。正则表达式提供了一种强大而灵活的工具,能够更精准地匹配和处理各种复杂的数据格式。这项技能为我的编程能力增色不少,也让我在数据验证方面更为得心应手。在未来的项目中,我将更加注重在程序设计初期就充分考虑类之间的结构,并充分利用正则表达式等工具,以提高程序的效率和可靠性。

期中考试

在Java中,下列说法正确的是?

A.

JAVA程序的main方法必须写在类里面

B.

一个JAVA类中可以有多个main方法

C.

JAVA程序中类名必须与文件名一样

D.

JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来

答案:A

B中,每个JAVA程序中只能有一个main方法。C中,大家想一下这个问题,一个JAVA程序中只能有一个类吗?当然不是还有内部类呢,所以这句话应该换为JAVA程序中必须存在一个权限为public 的类与文件名一样。D中,JAVA程序的main方法中如果只有一条语句,必须用{}(大括号)括起来,怎么说呢,只要不是放在抽象类中有没有语句都要有大括号。

期中考试总的来说是难度适中的,其难度相较于菜单计价程序4和5来说都更简单一些。选择题出现了这样一个错误,对此我也进行了解析。编程题相较于其他来说也是较为简单的。

posted @ 2023-11-19 12:18  Camellia00D8  阅读(90)  评论(0)    收藏  举报