罗HP第二次博客作业

发布时间 2023-06-29 16:45:16作者: 唱航纯爱战神

这里是对PTA4,5题目集及对期中考试的总结

前言:

在这个学期第一次接触java语言,到现在看来,在面向对象之前所学的C语言和java只有部分的语法差别,不同的语言肯定有不同的语法规则。而在接触面向对象之后,java的特点才会展现出来;继承,多态以及封装。私以为只要有关面对对象的的编程思维转变过来理解过来,java就不会太难。

现在来看看PTA4题目集的菜单4

7-1 菜单计价程序-4
分数 100
作者 蔡轲
单位 南昌航空大学

本体大部分内容与菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

以下是我的代码:

import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Objects;
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;
        int i;
        int portion, DishNumber;
        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.Dishprice = Integer.parseInt(token[1]);
                if (temp.Dishprice > 300) {
                    System.out.println(temp.name + " price out of range " + temp.Dishprice);
                    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.Dishprice = Integer.parseInt(token[1]);
                if (temp.Dishprice > 300) {
                    System.out.println(temp.name + " price out of range " + temp.Dishprice);
                    continue;
                }
                temp.isT = true;
                menu.dishs.add(temp);
            } else if (str1.equals("end")) {
                break;
            } else if (str1.matches("tab.*")) {
                break;

            } else {
                System.out.println("wrong format");
            }
        }
        while (!str1.equals("end")) {
            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.getWeekDay();
                    } 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]);
                        DishNumber = 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 (DishNumber > 15) {
                            System.out.println(Integer.parseInt(token[0]) + " num out of range " + DishNumber);
                            continue;
                        }
                        temp.od(menu, token[0], token[1], portion, DishNumber);
                    }
                    // 判断是否为删除订单
                    else if (str1.matches("[1-9][\\d]* delete")) {
                        String[] token = str1.split(" ");
                        temp.order.delARecordByOrderNum(Integer.parseInt(token[0]));
                    }
                    // 判断是否为夹杂菜单
                    else if (str1.matches("[\\S]* [\\d]*")) {
                        System.out.println("invalid dish");
                    } 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 (Table table : tables) {
                            if (table.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]);
                            DishNumber = Integer.parseInt(token[4]);
                            treat.portion = portion;
                            treat.DishNumber = DishNumber;
                            System.out.print(treat.getPrice() + "\n");
                            temp.sum += treat.getPrice();
                        }
                        // 若不存在则输出内容
                        else {
                            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");
                    }
                }
            } 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]);
                        DishNumber = 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 (DishNumber > 15) {
                            System.out.println(Integer.parseInt(token[0]) + " num out of range " + DishNumber);
                            continue;
                        }
                        temp.od(menu, token[0], token[1], portion, DishNumber);
                    }
                    // 判断是否为删除订单
                    else if (str1.matches("[1-9][\\d]* delete")) {
                        String[] token = str1.split(" ");
                        temp.order.delARecordByOrderNum(Integer.parseInt(token[0]));
                    }
                    // 判断是否为夹杂菜单
                    else if (str1.matches("[\\S]* [\\d]*")) {
                        System.out.println("invalid dish");
                    } else if (str1.matches("[\\S]* [\\d]* T")) {
                        System.out.println("invalid dish");
                    }
                    // 判断是否为代点
                    else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {
                        String[] token = str1.split(" ");
                        // 判断代点桌号是否存在
                        boolean exist = false;
                        for (Table table : tables) {
                            if (table.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]);
                            DishNumber = Integer.parseInt(token[4]);
                            treat.portion = portion;
                            treat.DishNumber = DishNumber;
                            System.out.print(treat.getPrice() + "\n");
                            temp.sum += treat.getPrice();
                        }
                        // 若不存在则输出内容
                        else {
                            System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");
                        }

                    } else if (str1.equals("end")) {
                        break;
                    } else {
                        System.out.println("wrong format");
                    }
                }
                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.getSum();
            tables.add(temp);
        }
        // 最终输出桌号订单信息
        for (i = 0; i < tables.size(); i++) {
            if (tables.get(i).isOpen()) {
                System.out
                        .println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum);
            } else
                System.out.println("table " + tables.get(i).num + " out of opening hours");
        }
    }

    static class Record {
        int orderNum;   //序号
        Dish d;  //菜品
        int portion;    //份额(1,2,3 小,中,大)
        int DishNumber;    //配额(菜品c)
        boolean isDeleted = false;      //判断删除

        int getPrice() {       //计价,计算本条菜品的价格
            if (portion == 2)
                return (int) Math.round(1.5 * d.Dishprice) * DishNumber;
            else if (portion == 3)
                return 2 * d.Dishprice * DishNumber;
            else
                return d.Dishprice * DishNumber;
        }
    }

    class Object{
        boolean crossRiver=false;
        boolean isAlive=true;
        String name;
        void showStatus() {
            System.out.println(name+"has Cross:  " + this.crossRiver);
        }
    }
    static class Dish {   //菜的类
        String name;     //菜品名称
        int Dishprice;   //菜品单价
        boolean isT = false;   //判断特色菜
    }

    class Father{
        boolean crossRiver=false;
        boolean isAlive=true;
        String name;
        void showStatus() {
            System.out.println(name+"has Cross:  " + this.crossRiver);
        }
    }
    static class Order {
        //        Record[] records = new Record[20];
        Record addARecord(int orderNum, String dishName, int portion, int DishNumber, Menu menu) {    //添加一条菜单信息到订单中
            Record newRecord = new Record();
            newRecord.orderNum = orderNum;
            newRecord.d = menu.dishs.get(menu.searchDish(dishName));
            newRecord.portion = portion;
            newRecord.DishNumber = DishNumber;
            System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice());
            return newRecord;
        }
        //保存订单上每一道的记录,存放在数组里
        ArrayList<Record> records = new ArrayList<>();

        //根据序号删除一条记录
        boolean delARecordByOrderNum(int orderNum) {
            int i = 0, flag = 0;
            for (i = 0; i < records.size(); i++) {
                if (records.get(i).orderNum == orderNum) {
                    if (records.get(i).isDeleted == false) {
                        records.get(i).isDeleted = true;
                    } else {
                        System.out.println("deduplication " + orderNum);
                    }
                    flag++;
                }
            }
            if (flag == 0) {
                System.out.println("delete error;");
                return false;
            }
            return true;
        }

        //跟据序号查找一条记录
        int searchRecord(String name) {
            for (int i = 0; i < records.size(); i++) {
                if (Objects.equals(records.get(i).d.name, name)) {
                    return i;       //返回序号
                }
            }
            return -1;
        }

    }

    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.Dishprice = unit_price;
            return newDish;
        }
    }

    static class Table {
        Order order = new Order();
        LocalDateTime time;
        int num;
        int weekday;
        long sum = 0;
        long origSum = 0;


        void od(Menu menu, String str1, String str2, int portion, int DishNumber) {
            {
                order.records.add(order.addARecord(Integer.parseInt(str1), str2, portion, DishNumber, menu));

            }
        }
        boolean isOpen() {
            if (weekday > 0 && weekday < 6) {
                if (time.getHour() >= 17 && time.getHour() < 20)
                    return true;
                if (time.getHour() == 20) {
                    if (time.getMinute() <= 30)
                        return true;
                }
                if (time.getHour() > 10 && time.getHour() < 14)
                    return true;
                if (time.getHour() == 10) {
                    if (time.getMinute() >= 30)
                        return true;
                }
                if (time.getHour() == 14) {
                    return time.getMinute() <= 30;
                }
            } else {
                if (time.getHour() > 9 && time.getHour() < 21)
                    return true;
                if (time.getHour() == 9) {
                    if (time.getMinute() >= 30)
                        return true;
                }
                if (time.getHour() == 21) {
                    return time.getMinute() <= 30;
                }
            }
            return false;
        }
        void getSum() {
            for (int i = 0; i < order.records.size(); i++) {
                if (!order.records.get(i).isDeleted) {
                    origSum += order.records.get(i).getPrice();
                    if (order.records.get(i).d.isT) {
                        if (weekday > 0 && weekday < 6) {
                            sum += Math.round(order.records.get(i).getPrice() * 0.7);
                        }
                        else {
                            sum += order.records.get(i).getPrice();
                        }
                    }
                    else
                    {
                        if (weekday > 0 && weekday < 6) {
                            if (time.getHour() >= 17 && time.getHour() < 20)
                                sum += Math.round(order.records.get(i).getPrice() * 0.8);
                            if (time.getHour() == 20) {
                                if (time.getMinute() <= 30)
                                    sum += Math.round(order.records.get(i).getPrice() * 0.8);
                            }
                            if (time.getHour() >= 10 && time.getHour() < 14)
                                sum += Math.round(order.records.get(i).getPrice() * 0.6);
                            if (time.getHour() == 14) {
                                if (time.getMinute() <= 30)
                                    sum += Math.round(order.records.get(i).getPrice() * 0.6);
                            }
                        }
                        else sum+=order.records.get(i).getPrice();
                    }
                }
            }

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

    }
}

emmmmmmmmmmmmmmmmmmmmmmm,在这次菜单中,总体来说难度与之前比较差不多,但是对应的要求变动了,但是总体差别不大。现在来看看我在菜单中遇到的问题以及相应的知识点。

在这里面,涉及最多的就是各种类之间的信息传递,因此我总结了以下知识点:

  1. 类之间的关系:在Java程序设计中,类之间可以有不同的关系。要想让类A返回数值到类B,两个类之间必须要建立正确的关联,例如继承、实现接口或者依赖关系等。

  2. 访问权限:Java类中的方法和属性具有访问权限控制。对于要返回的数据,需要保证其访问权限得到了限定,并根据需求选择合适的访问修饰符,如private、protected、public等。

  3. 返回值类型和参数传递:Java中方法可以定义返回值类型和参数列表。对于要返回的数值,需要在方法的定义中正确指定返回值类型,并将需要用到的参数正确传递给方法。

  4. 实例化对象:将数值返回给另一个类时,还需要了解如何实例化另一个类的对象,从而能够直接使用该类的方法或属性。

不仅如此,在输入异常案例时的错误抛出也很重要:

在 Java 中,try 是异常处理的关键字之一,用于捕获可能抛出异常的代码块并采取相应的处理措施。以下是 try 关键字的一些知识点:

  1. 异常处理机制:try 结构用于创建一个异常处理块,它包含可能引发异常的代码。当异常发生时,程序会跳转到 try 块之后的 catch 块来处理异常。

  2. try-catch 块:catch 用于捕获和处理特定类型的异常。在 try 块中,可以放置需要监视的可能引发异常的代码,然后在 catch 块中指定对应的异常类型,并提供处理异常的代码逻辑。

  1. finally 块:finally 块是可选的,它用来执行无论是否发生异常都必须执行的代码。不管异常是否被 catch 捕获或处理,finally 块中的代码总是会被执行。
  1. 多重 catch 块:可以在一个 try 块中使用多个 catch 块来捕获不同类型的异常。异常类从上到下逐个匹配,一旦找到匹配的异常类型,相应的 catch 块就会执行,其他 catch 块将被忽略。

  2. try-catch-finally 高级用法:try-catch 块也可以与 finally 块一起使用,保证无论是否发生异常,finally 中的代码总是得以执行。

  3. 嵌套 try-catch 语句:在 trycatch 块内部,可以嵌套使用另一个 try-catch 块来处理更具体的异常情况。

现在来看看期中考试中的题目:

7-1 测验1-圆类设计

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

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

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


image.png

这两道题,应该没什么难度,就是要注意非零返回。

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

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


image.png

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

这里只需要把两个题目合并到一起,然后第四题就是在第三题的基础上加一个接口。

这里因为PTA打不开,源码锁了,就只能这样一笔带过了。

现在来简绍一下第5次PTA:

这个是第五次的菜单,吐槽一下我是真没想到菜单能够出到系列五。

来看看题目把:

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"

 这里附上我的代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

public class Main {


    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");   //时间模板
        Menu menu = new Menu();
        HashMap<String ,String>hash=new HashMap<>();
        String []customer=new String[30];
        int cnt=0;
        int nm=0;
        ArrayList<Table> tables = new ArrayList<Table>();
        Scanner input = new Scanner(System.in);
        String []zhe=new String[]{"不甜","微甜","稍甜","甜"};
        String []jin=new String[]{"不酸","微酸","稍酸","酸","很酸"};
        String []chuan=new String[]{"不辣","微辣","稍辣","辣","很辣","爆辣"};
        int []tastedGrade=new int [100];
        int []tastedNumber=new int [100];
        String cra1 = new String();
        String cra2 = new String();
        String cra3=new String();
        String time = new String();
        String []dish=new String[]{};
        String tb = null;
        DishNum dishNum = new DishNum();
        int [][]taste=new int[20][7];
        int table_count = 0;
        int i = 0, flag = 0;
        int portion = 0, number = 0;
        //份额,份数
        //cra2="0";cra3=" ";cra1=" ";
        menu.dishes.add(menu.addDish("", 0," "));
        while (true) {// 输入菜单
            dish=input.nextLine().split(" ");
            if(dish.length>4&&!dish[0].equals("table")||dish.length==3) {
                System.out.println("wrong format");
                continue;
            }
            if(dish.length>4&&dish[0].equals("table")){cra1=dish[0];
                tb=" "+dish[1]+" "+dish[2]+" "+dish[3]+" "+dish[4]+" "+dish[5]+" "+dish[6];
                break;}
            if(dish.length==4){cra1=dish[0];cra2=dish[2];cra3=dish[1];}
            if(dish.length==2){cra1=dish[0];cra2=dish[1];}
            if(dish.length==1&&dish[0].equals("end")){cra1=dish[0];break;}
            if(dish.length==1&&!dish[0].equals("end")) {
                System.out.println("wrong format");
                continue;
            }
            if (dish.length==4) { // 判断这道菜是否为特价菜
                if(dish[3].equals("T")&&isNumeric(cra2)&&!isNumeric(cra3)) {
                    menu.dishes.get(menu.dishes.size() - 1).isT = true;
                    menu.dishes.get(menu.dishes.size() - 1).taste = cra3;
                    menu.dishes.get(menu.dishes.size() - 1).name = cra1;
                    menu.dishes.get(menu.dishes.size() - 1).price = Integer.parseInt(cra2);
                }
                else {
                    System.out.println("wrong format");
                    continue;
                }
            }
            else cra3=" ";
            if(isNumeric(cra1)||!isNumeric(cra2)) {
                System.out.println("wrong format");
                continue;
            }
            // 判断菜谱是否重复输入
            for (i = 0; i < menu.dishes.size(); i++) {
                if (menu.dishes.get(i).equals(cra1)) {
                    menu.dishes.get(i).price = Integer.parseInt(cra2);
                    flag++;
                    break;
                }
            }
            if (flag == 0) {
                menu.dishes.add(menu.addDish(cra1, Integer.parseInt(cra2),cra3));
            }
            flag = 0;
        }
        boolean first = false;
        if(cra1.equals("end"))return ;
        while(!cra1.equals("end")){
            if(cra1.equals("end"))break;
            int chuanGrade=0,chuanNumber=0,zheGrade=0,zheNumber=0,jinGrade=0,jinNumber=0;
            String []a=new String[]{};
            String OrderNum,call,useName;
            Table table= new Table();
            if(first)
                OrderNum = input.nextLine();
            else {
                OrderNum = tb;
                first=true;
            }
            a=OrderNum.split(" ");
            cra2=a[1];
            useName=a[3];
            call=a[4];
            String threeCall=call.substring(0,3);
            if(useName.length()>10||call.length()!=11||searchCall(threeCall)){
                System.out.println("wrong format");
                break;
            }
            String[] Date = a[5].split("/");
            String[] Time =a[6].split("/");
            table.num = Integer.parseInt(cra2);
            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]);
            }
            try {
                table.time = LocalDateTime.of(intDate[0],intDate[1],intDate[2],intTime[0],intTime[1],intTime[2]);
                //时间判断
                tables.add(table);
            }catch(DateTimeException e){
                System.out.println( table.num + " date error");
                break;
            }
            System.out.println("table "+cra2+": ");
            while (true) {
                cra1 = input.next();
                if (cra1.equals("end"))
                    break;
                if (cra1.equals("table"))
                    break;
                cra2 = input.next();
                // 判断是否为代点
                if (isNumeric(cra2)) {
                    //判断代点桌号是否存在
                    boolean exist = false;
                    for(int j=0;j<tables.size();j++) {
                        if(tables.get(j).num==Integer.parseInt(cra1)) {
                            exist = true;
                            break;
                        }
                    }
                    //若存在则完成代点
                    if(exist) {
                        System.out.print(Integer.parseInt(cra2) + " table " +tables.get(table_count).num + " pay for table "
                                + Integer.parseInt(cra1) + " ");
                        String str=cra1;
                        Record treat = new Record();
                        cra1 = input.next();
                        treat.ds = menu.dishes.get(menu.searchDish(cra1));
                        if(menu.searchDish(cra1)!=-1){
                            if (menu.dishes.get(menu.searchDish(cra1)).isT) {
                                cra3 = input.next();
                                tastedGrade[Integer.parseInt(str)] = Integer.parseInt(cra3);
                            } else cra3 = " ";
                        }else {
                            System.out.println(cra1 + " does not exist");
                            cra1=input.nextLine();continue;
                        }
                        portion = input.nextInt();
                        number = input.nextInt();
                        tastedNumber[Integer.parseInt(str)]=number;
                        treat.portion = portion;
                        treat.number = number;
                        System.out.print(treat.getPrice() + "\n");
                        if(menu.dishes.get(menu.searchDish(cra1)).taste.equals("川菜")) {
                            if(Integer.parseInt(cra3)<0||Integer.parseInt(cra3)>5){
                                System.out.println("spicy num out of range :"+cra3);
                                cra1=input.nextLine();
                                continue;
                            }
                            taste[Integer.parseInt(str)-1][0] += Integer.parseInt(cra3)*number;
                            taste[Integer.parseInt(str)-1][3]+=number;
                        }
                        if(menu.dishes.get(menu.searchDish(cra1)).taste.equals("浙菜")) {
                            if(Integer.parseInt(cra3)<0||Integer.parseInt(cra3)>3){
                                System.out.println("sweetness num out of range :"+cra3);
                                cra1=input.nextLine();
                                continue;
                            }
                            taste[Integer.parseInt(str)-1][2] += Integer.parseInt(cra3)*number;
                            taste[Integer.parseInt(str)-1][5]+=number;
                        }
                        if(menu.dishes.get(menu.searchDish(cra1)).taste.equals("晋菜")) {
                            if(Integer.parseInt(cra3)<0||Integer.parseInt(cra3)>4){
                                System.out.println("acidity num out of range :"+jinGrade);
                                cra1=input.nextLine();
                                continue;
                            }
                            taste[Integer.parseInt(str)-1][1] += Integer.parseInt(cra3)*number;
                            taste[Integer.parseInt(str)-1][4]+=number;
                        }
                        tables.get(table_count).add(menu, "代点",cra2, cra1, portion, number);
                    }
                    //若不存在则输出内容
                    else {
                        System.out.println("Table number :"+Integer.parseInt(cra1)+" does not exist");
                    }
                }


                // 若不是代点
                else {
                    // 若不为删除订单,则读入份数和大小
                    if (!cra2.equals("delete")) {
                        boolean T=false;
                        if(menu.searchDish(cra2)!=-1) {
                            if (menu.dishes.get(menu.searchDish(cra2)).isT) {
                                hash.put(cra1, menu.dishes.get(menu.searchDish(cra2)).taste);
                                cra3 = input.next();
                                portion = input.nextInt();
                                number = input.nextInt();
                                tastedGrade[Integer.parseInt(cra1)] = Integer.parseInt(cra3);
                                tastedNumber[Integer.parseInt(cra1)] = number;
                                T = true;
                                if (menu.dishes.get(menu.searchDish(cra2)).taste.equals("浙菜")) {
                                    if (Integer.parseInt(cra3) < 0 || Integer.parseInt(cra3) > 3) {
                                        System.out.println("sweetness num out of range :" + cra3);
                                        cra1 = input.nextLine();
                                        continue;
                                    }
                                    zheGrade += Integer.parseInt(cra3) * number;
                                    zheNumber += number;
                                }
                                if (menu.dishes.get(menu.searchDish(cra2)).taste.equals("川菜")) {
                                    if (Integer.parseInt(cra3) < 0 || Integer.parseInt(cra3) > 5) {
                                        System.out.println("spicy num out of range :" + cra3);
                                        cra1 = input.nextLine();
                                        continue;
                                    }
                                    chuanGrade += Integer.parseInt(cra3) * number;
                                    chuanNumber += number;
                                }
                                if (menu.dishes.get(menu.searchDish(cra2)).taste.equals("晋菜")) {
                                    if (Integer.parseInt(cra3) < 0 || Integer.parseInt(cra3) > 4) {
                                        System.out.println("acidity num out of range :" + cra3);
                                        cra1 = input.nextLine();
                                        continue;
                                    }
                                    jinGrade += Integer.parseInt(cra3) * number;
                                    jinNumber += number;
                                }
                            } else cra3 = " ";
                        }else  {
                            System.out.println(cra2 + " does not exist");
                            cra1=input.nextLine();continue;
                        }
                        if(T==false) {
                            portion = input.nextInt();
                            number = input.nextInt();
                        }
                    }
                    tables.get(table_count).add(menu, cra3,cra1, cra2, portion, number);
                    if(cra2.equals("delete")){
                        if(hash.get(cra1).equals("浙菜")) {
                            zheGrade -= tastedGrade[Integer.parseInt(cra1)];
                            zheNumber -= tastedNumber[Integer.parseInt(cra1)];
                        }
                        if(hash.get(cra1).equals("川菜")) {
                            chuanGrade -= tastedGrade[Integer.parseInt(cra1)];
                            chuanNumber -= tastedNumber[Integer.parseInt(cra1)];
                        }
                        if(hash.get(cra1).equals("晋菜")) {
                            jinGrade -= tastedGrade[Integer.parseInt(cra1)];
                            jinNumber -= tastedNumber[Integer.parseInt(cra1)];
                        }
                        hash.put(cra1,"");
                    }
                }

            }
            taste[table_count][1]=chuanGrade;
            taste[table_count][0]=jinGrade;
            taste[table_count][3]=zheGrade;
            taste[table_count][2]=chuanNumber;
            taste[table_count][5]=jinNumber;
            taste[table_count][4]=zheNumber;
            /*
            if (tables.get(i).isOpen()) {
                System.out.print("table " + tables.get(i).num + ": " + tables.get(i).origSum+" "+tables.get(i).sum);
                for(int j=0;j<3;j++){
                    if(taste[i][j+3]!=0){
                        if(j==0){
                            System.out.print(" 川菜 "+taste[i][j+3]+" "+chuan[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                        if(j==1){
                            System.out.print(" 晋菜 "+taste[i][j+3]+" "+jin[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                        if(j==2){
                            System.out.print(" 浙菜 "+taste[i][j+3]+" "+zhe[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                    }
                }

            * */
            System.out.println("");
            OrderDs orderDs = new OrderDs();
            taste[table_count][0]=chuanGrade;
            taste[table_count][1]=jinGrade;
            taste[table_count][2]=zheGrade;
            taste[table_count][3]=chuanNumber;
            taste[table_count][4]=jinNumber;
            taste[table_count][5]=zheNumber;
            // 本桌点菜结束,进入下一桌
            tables.get(table_count).getSum();
            int sum=0;
            String str=useName+" "+call+" "+tables.get(table_count).sum;
            String []b=new String[]{};
            if(table_count==0)customer[cnt++]=str;
            else {
                b = str.split(" ");
                boolean r=true;
                for (int l = 0; l < cnt; l++) {
                    String[] c = customer[l].split(" ");
                    if (b[0].equals(c[0])) {
                        sum = Integer.parseInt(c[2]) + Integer.parseInt(b[2]);
                        customer[l] = useName + " " + call + " " + sum;
                        r=false;
                    }
                }
                if(r)customer[cnt++]=str;
            }
            table_count++;
        }
        // 最终输出桌号订单信息
        for (i = 0; i < table_count; i++) {
            if (tables.get(i).isOpen()) {
                System.out.print("table " + tables.get(i).num + ": " + tables.get(i).origSum+" "+tables.get(i).sum);
                for(int j=0;j<3;j++){
                    if(taste[i][j+3]!=0){
                        if(j==0){
                            System.out.print(" 川菜 "+taste[i][j+3]+" "+chuan[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                        if(j==1){
                            System.out.print(" 晋菜 "+taste[i][j+3]+" "+jin[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                        if(j==2){
                            System.out.print(" 浙菜 "+taste[i][j+3]+" "+zhe[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                    }
                }
                System.out.println();
            } else
                System.out.println("table " + tables.get(i).num + " out of opening hours");
        }
        Arrays.sort(customer,0,cnt);
        for(int j=0;j<cnt;j++) {
            if(j<cnt-1)
                System.out.println(customer[j]);
            else System.out.print(customer[j]);
        }
    }
    public static boolean searchCall(String threeCall) {
        String []CallNumber=new String[]{"180","181","189","133","135","136"};
        for(int i=0;i<CallNumber.length;i++)
            if(CallNumber[i].equals(threeCall))return false;
        return true;
    }
    public static boolean isNumeric(String string) {  //判断是否为数字
        int intValue;
        try {
            intValue = Integer.parseInt(string);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
class Table {
    Order order = new Order();
    int num;
    LocalDateTime time;
    long sum = 0;
    long origSum = 0;
    void add(Menu menu, String str3,String str1, String str2, int portion, int number) {     //判断点菜的模式(是否存在,删除,添加)
        if (str2.equals("delete")) {
            order.delARecordByOrderNum(Integer.parseInt(str1));
        } else {
            if (menu.searchDish(str2) != -1) {
                order.records.add(order.addARecord(str3,Integer.parseInt(str1), str2, portion, number, menu));
            } else
                System.out.println(str2 + " does not exist");
        }
    }

    void getSum() {
        double ts = time.getHour() + time.getMinute() / 60;
        int wek = time.getDayOfWeek().getValue();
        for (int i = 0; i < order.records.size(); i++) {
            if (!order.records.get(i).isDeleted) {
                origSum += order.records.get(i).getPrice();
                if ((wek == 7 || wek == 6) && (ts >= 9.5) && (ts <= 21))
                    sum += order.records.get(i).getPrice();
                if ((wek >= 1 && wek <= 5) && (ts >= 17) && (ts <= 20.5)) {
                    if(!order.records.get(i).ds.isT)
                        sum += Math.round(order.records.get(i).getPrice() * 0.8);
                    else sum += Math.round(order.records.get(i).getPrice() * 0.7);
                }
                if ((wek >= 1 && wek <= 5) && (ts >= 10.5) && (ts <= 14.5)) {
                    if(!order.records.get(i).ds.isT)
                        sum += Math.round(order.records.get(i).getPrice() * 0.6);
                    else sum += Math.round(order.records.get(i).getPrice() * 0.7);
                }

            }
        }
    }

    boolean isOpen() {          //判断是否开门
        int weekday = time.getDayOfWeek().getValue();
        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;
    }
}
class Dish {   //菜单格式
    String name;
    int price;
    String taste;

    boolean isT = false;
}
class Order {       //点菜
    ArrayList<Record> records = new ArrayList<Record>();

    Record addARecord(String tasteGrade,int orderNum, String dishName, int portion, int number, Menu menu) {
        Record newRecord = new Record();
        newRecord.orderNum = orderNum;
        newRecord.ds = menu.dishes.get(menu.searchDish(dishName));
        newRecord.portion = portion;
        newRecord.number = number;
        if(!tasteGrade.equals("代点"))
            System.out.println(newRecord.orderNum + " " + newRecord.ds.name + " " + newRecord.getPrice());
        return newRecord;
    }

    boolean delARecordByOrderNum(int orderNum) {
        int i = 0, flag = 0;
        for (i = 0; i < records.size(); i++) {
            if (records.get(i).orderNum == orderNum) {
                if (records.get(i).isDeleted == false) {
                    records.get(i).isDeleted = true;
                }
                else {
                    System.out.println("deduplication " + orderNum);
                }
                flag++;
            }
        }
        if (flag == 0) {
            System.out.println("delete error;");
            return false;
        }
        return true;
    }
}
class DishNum {
    String name;//菜品名称
    int unit_price;    //单价
    //int num;
    int price;
    String taste;

    boolean isT = false;
    int getPrice(int portion) {//计价,挑选份额
        int peic = 0;
        if (portion == 1) {
            peic = unit_price ;
        } else if (portion == 2) {
            peic = Math.round((float) (unit_price * 1.5)) ;
        } else if (portion == 3) {
            peic = (unit_price * 2) ;
        }
        return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    }
}
class OrderDs{
    int orderNum;//序号\
    //int AntherOrderNum;
    Dish d = new Dish();//菜品\
    int num = 0;
    void getprice(){
        System.out.println("");
        for(int i =0 ; i< 10; i++){
            System.out.println("");
        }
    }
}
class Menu {   //菜单
    ArrayList<Dish> dishes = new ArrayList<Dish>();

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

    Dish addDish(String dishName, int price,String taste) {
        Dish newDish = new Dish();
        newDish.name = dishName;
        newDish.price = price;
        newDish.taste=taste;
        return newDish;
    }
}
class Record {      //点菜
    int orderNum;  //序号
    Dish ds;   //菜品
    int portion;  //份额
    int number;  //份数
    boolean isDeleted = false;

    int getPrice() {
        if (portion == 2)
            return (int) Math.round(1.5 * ds.price) * number;
        else if (portion == 3)
            return 2 * ds.price * number;
        else
            return ds.price * number;
    }
}

来看看我从中学到的知识点:

这里涉及了arraylist用法:

以下是它的知识点(这里因为题目比较简单,所以省去几题目):

ArrayList 是 Java 中的一个常用类,提供了动态数组的实现。以下是一些关键知识点:

1. **动态数组:** ArrayList 是一个可调整大小的数组,它可以根据需要自动增长或缩小。与传统的数组相比,ArrayList 不需要手动管理容量和索引,并且能够在运行时自由地添加、删除和修改元素。

2. **泛型支持:** ArrayList 支持使用泛型,即在声明 ArrayList 时指定元素类型。这样可以在编译时进行类型检查,并且避免了在获取元素时进行类型转换的麻烦。例如,`ArrayList<Integer>` 表示只能存储整数类型的元素。

3. **常用方法:** ArrayList 提供了一系列常用的方法来操作数据,包括添加(`add()`)、获取(`get()`)、移除(`remove()`)、修改(`set()`)等。此外,还有一些其他方法用于操作 ArrayList 的大小、查找元素、判断是否包含某元素等。

4. **迭代器:** ArrayList 可以通过迭代器来遍历其中的元素。迭代器提供了 `hasNext()` 和 `next()` 方法,可用于逐个访问 ArrayList 中的元素。

5. **性能考虑:** 在较大的数据集合上频繁执行插入和删除操作可能导致性能下降,因为这涉及到重新分配和复制数组元素。如果需要大量的插入和删除操作,可以考虑使用 LinkedList 或其他数据结构。

6. **与数组的相互转换:** ArrayList 可以通过 `toArray()` 方法将其转换为常规数组,也可以使用 `Arrays.asList()` 方法将常规数组转换为 ArrayList。

7. **线程安全性:** ArrayList 不是线程安全的。在并发环境下,多个线程同时修改 ArrayList 可能导致不确定的结果。如果需要在多线程环境中使用,应该考虑使用线程安全的类,如 Vector 或 CopyOnWriteArrayList。

以上是关于 ArrayList 的一些重要知识点。熟悉这些知识将有助于充分利用 ArrayList 提供的功能,并且能够选择适合实际需求的数据结构。

除此之外还涉及了接口以及覆盖的用法:

接口(Interface)和方法的覆盖(Overriding)是 Java 中面向对象编程的重要概念。以下是它们的关键知识点:

**接口(Interface):**

1. **定义和作用:** 接口是一种抽象类型,通过关键字 `interface` 定义,可以看作是一种约定或契约。它只声明了方法的签名而不提供实现细节。接口允许定义常量和抽象方法,并可被类实现。

2. **多继承:** 类只能单继承一个父类,但可以实现多个接口。通过实现接口,类可以继承多个接口定义的方法。这种机制支持Java的多继承特性,使得类可以从多个源接收功能。

3. **强制实现:** 当类实现接口时,需要提供接口中所有声明的方法的具体实现。否则,类必须声明为抽象类,交由其子类进行实现。实现接口的类必须使用关键字 `implements`。

4. **默认方法:** 从Java 8开始,接口还支持默认方法(default method),通过关键字 `default` 提供默认的方法实现。默认方法在接口中有具体的实现,但仍然可以在实现类中被覆盖。

5. **静态方法:** 从Java 8开始,接口还支持静态方法(static method),通过关键字 `static` 定义。静态方法是与接口关联的工具方法,可以直接通过接口调用。

**方法覆盖(Overriding):**

1. **定义和作用:** 方法覆盖是指子类重写父类中已有的方法的过程。当子类定义一个与父类中声明相同的方法时,子类会覆盖(重写)父类的方法实现,使得子类在运行时使用自己的方法而不是父类的方法。

2. **规则:** 方法覆盖必须满足一些规则:方法名、参数列表以及返回类型必须与父类中的方法相同;子类中的方法不能比父类中的方法具有更严格的访问修饰符;子类方法中抛出的异常不能比父类中的方法所声明的异常更多或者更通用。

3. **动态绑定:** 方法覆盖是面向对象编程中的一个重要概念,它允许程序根据实际对象类型来决定调用哪个方法。方法的调用是在运行时动态确定的,这称为动态绑定。

4. **@Override 注解:** 为了避免意外错误和提高代码可读性,可以在子类方法上使用 @Override 注解来明确表示该方法是对父类方法的覆盖。

学习和理解接口和方法覆盖的概念是进行Java面向对象编程的关键。它们提供了灵活性、可扩展性和代码组织性,使得程序更易于维护、重用和理解。

菜单五分析:

1.主类过于冗余,判断在输出的信息,应该提取在多个方法中。
2.定义的gettotalprice();注释掉了,使用了一个单独变量作为价格总和。面向过程的代码,复用率低。
3.日期的判断采用的提取字符串。可用Java自带的日期类减少代码量和复杂度。
4.程序时间复杂度高。过测试点过的很极限。
5.桌类的定义没有很好的与前几个类组合一起,方法的定义很长

菜单四,五的总结:

在菜单四中,我运用了面向对象的语言去编写程序,通过书写类,对象,类中的方法,我逐步克服菜单中的难点:

1.返回值出错,由其他类返回到其他类的返回值可能因为private的缘故出错

2.书写boolean类,巧妙的运用布尔类型可以返回很多需要判断的问题以及数据

3.类方法的调用,在调用类方法的时候,或者写对象的属性的时候,经常会出错,因为写代码中总是因为自己原因写的不正规

在克服这些困难中,我不断去查代码,学习新知识,查算法,学习arraylist,huahset,hashmap,boolean或者询问别人,通过学习他人的代码,结合自己的理解,将代码不断修改完全,从不会书写类,到学习修改报错,对于idea的使用,比如调试,插件的安装与查找等技能,得到了很好的锻炼。现在可以做到以idea调试独自解决代码出现的问题,很多问题通过百度可以解决。自身解决问题能力大大提升。