首页 > 其他分享 >21207106-xuesong

21207106-xuesong

时间:2023-11-18 19:22:57浏览次数:32  
标签:tables String int xuesong dishName 21207106 table out

菜单系列题及期中总结

一、前言

  这次博客是主要对菜单系列题目和期中题目总结,菜单系列题目包括菜单计价程序-3、菜单计价程序-4、菜单计价程序-5,菜单系列题目,主要是考察对类的创建,怎么设计合适的类,类与类之间的关系,考察了封装,继承,依赖等。其次是考察对正则表达式判断输入格式是否合法的判断,ArrayList动态数组的应用,还有就是考察对java基本API的使用,例如,SimpleDateFormat将用户输入的时间进行格式化,再利用LocalDateTime来获取用户输入的时间,还有就是java异常处理,try catch的使用。期中考试编程题主要是考察封装,继承,多态,接口的使用,题目都不是很难,但最后我还是有几个测试点没有通过,由于时间有限,所以,最后一题没来得及写。题目量虽然不大,但是菜单系列题的代码量是相对较大的,难度感觉最难的还是菜单计价程序-4,主要是这道题需要考虑的异常情况非常的多,如果没有捋清思路,没有好的类结构,处理起来将会很复杂。

二、设计与分析

PTA--4

7-2 单词统计与排序 分数 10 作者 张峰 单位 山东科技大学

从键盘录入一段英文文本(句子之间的标点符号只包括“,”或“.”,单词之间、单词与标点之间都以" "分割。
要求:按照每个单词的长度由高到低输出各个单词(重复单词只输出一次),如果单词长度相同,则按照单词的首字母顺序(不区分大小写,首字母相同的比较第二个字母,以此类推)升序输出。

输入格式:

一段英文文本。

输出格式:

按照题目要求输出的各个单词(每个单词一行)。

输入样例:

Hello, I am a student from China.

输出样例:

student
China
Hello
from
am
a
I

代码如下:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        HashSet<String> set = new HashSet<>();
        String sentence=input.nextLine();
        String []word=sentence.split("[,. ]");
        for (String s : word) {
            if (!s.equals(" ")) {
                set.add(s);
            }
        }
        ArrayList<String> wordsList=new ArrayList<>(set);
        wordsList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.length() != o2.length())
                    return o2.length() - o1.length();
                else
                    return o1.compareToIgnoreCase(o2);
            }
        });
        for(int i=0;i<wordsList.size();i++){
            if(i!=wordsList.size()-1)
                System.out.println(wordsList.get(i));
            else
                System.out.print(wordsList.get(i));
        }
    }
}

分析:

  这道题并不难,考察的的是HashSet和ArrayList中接口sort的使用,这道题目踩的坑,时间也过了那么久,我记得好像是输出的时候有一个换行的要求,我记的当时好像把换行去掉了就过了最后一个没有通过的测试点。

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

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

输入内容按先后顺序包括两部分:菜单、订单,最后以"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+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入格式:

桌号标识格式: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
table 1 2023/3/22 12/2/3
1 麻婆豆腐 2 2
2 油淋生菜 1 3
end

输出样例:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 38

输入样例1:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 17/0/0
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例1:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 22

输入样例2:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 16/59/59
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例2:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1 out of opening hours

输入样例3:

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

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/5 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
5 delete
7 delete
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
7 delete
end

输出样例3:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
delete error;
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
table 1 out of opening hours
table 2: 63

输入样例4:

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

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/3 19/5/12
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
1 4 麻婆豆腐 1 1
7 delete
end

输出样例4:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
4 table 2 pay for table 1 12
delete error;
table 1: 63
table 2: 75

代码如下:

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.*;
import java.text.SimpleDateFormat;
public class Main{
    public static void main(String[] args) throws ParseException {
        Scanner input=new Scanner(System.in);
        String s=input.nextLine();
        Menu menu = new Menu();
        inputCompare InputCompare= new inputCompare();
        ArrayList<Table> tables=new ArrayList<>();
        while(!s.equals("end")){
            if(InputCompare.DishCompare(s)){//添加菜品
                String[] parts=s.split(" ");
                String dishName=parts[0];
                int unit_price=Integer.parseInt(parts[1]);
                menu.addDish(dishName,unit_price);
            }
            if(InputCompare.RecordCompare(s)){//点菜记录
                String[] parts=s.split(" ");
                int orderNum=Integer.parseInt(parts[0]);
                String dishName=parts[1];
                int portion=Integer.parseInt(parts[2]);
                int count=Integer.parseInt(parts[3]);
                if (menu.searchDish(dishName)!=null) {
                    System.out.println(orderNum + " " + dishName + " " + menu.searchDish(dishName).getPrice(portion)*count);
                    tables.get(tables.size() - 1).addARecord( orderNum, dishName, portion, count, menu);
                }
                else
                {
                    System.out.println(dishName+" does not exist");
                }
            }
            if (InputCompare.DeleteCompare(s)){
                String[] parts = s.split(" ");
                int orderNum = Integer.parseInt(parts[0]);
                tables.get(tables.size()-1).delARecordByOrderNum(orderNum);
            }
            if(InputCompare.DateCompare(s)){
                String[] parts = s.split(" ");
                int TableNum=Integer.parseInt(parts[1]);
                String TableData=parts[2];
                String TableTime=parts[3];
                Table table=new Table(TableNum,TableData,TableTime);
                table.dateHandleTheDiscount(TableData,TableTime);
                tables.add(table);
                System.out.println("table "+TableNum+": ");
            }
            if(InputCompare.PayForCompare(s)){
                String[] parts = s.split(" ");
                int TableNum=Integer.parseInt(parts[0]);
                int orderNum=Integer.parseInt(parts[1]);
                String dishName=parts[2];
                int portion=Integer.parseInt(parts[3]);
                int count=Integer.parseInt(parts[4]);
                if (menu.searchDish(dishName)!=null) {
                    tables.get(tables.size()-1).addARecord(orderNum,dishName,portion,count,menu);
                    Dish dish=new Dish(dishName,menu.searchDish(dishName).unit_price);
                    Record record=new Record(dish,orderNum,portion,count);
                    System.out.println(orderNum + " table " + tables.size() + " pay for table " + TableNum + " " +record.getPrice());
                }
                else {
                    System.out.println(dishName+" does not exist");
                }
            }
            s=input.nextLine();
        }
        for(Table table : tables) {
            float discount=tables.get(tables.indexOf(table)).dateHandleTheDiscount(tables.get(tables.indexOf(table)).getTableDate(),
                    tables.get(tables.indexOf(table)).getTableTime());
            if(discount==0) {
                System.out.println("table " + tables.get(tables.indexOf(table)).getTableNum() + " out of opening hours");
            }
            else {
                System.out.println("table " + tables.get(tables.indexOf(table)).getTableNum() + ": " + tables.get(tables.indexOf(table)).getTotalPrice(discount));
            }
        }
    }
}
class Dish{
    String name;
    int unit_price;
    public Dish(String name, int unit_price) {
        this.name=name;
        this.unit_price=unit_price;
    }
    public int getUnit_price() {
        return unit_price;
    }
    public int getPrice(int portion) {
        double price=0;
        switch (portion){
            case 1:
                price=unit_price;
                break;
            case 2:
                price=unit_price * 1.5;
                break;
            case 3:
                price=unit_price * 2;
                break;
            default:
                break;
        }
        return (int)(Math.round(price));
    }
    public String getName() {
        return name;
    }
}
class Menu{
    ArrayList<Dish> dish =new ArrayList<>();
    public void addDish(String dishName, int unit_price) {
        Dish dishes = new Dish(dishName, unit_price);
        if (searchDish(dishName) == null) {
            dish.add(dishes);
        }
        else
            dish.set(getIsHaveIndex(dishName),dishes);
    }
    public Dish searchDish(String dishName){
        for (Dish dish : dish){
            if (dish.name.equals(dishName)){
                return dish;
            }
        }
        return null;
    }
    public int getIsHaveIndex(String dishName) {
        for(int i = 0;i < dish.size();i++) {
            if(dish.get(i).getName().equals(dishName)) {
                return i;
            }
        }
        return -1;
    }
}
class Record {
    Dish dish;
    int orderNum;
    int portion;
    int count;
    int flag;
    public Record(){

    }
    public Record(Dish dish, int orderNum, int portion, int count){
        this.dish=dish;
        this.orderNum=orderNum;
        this.portion=portion;
        this.count=count;
    }

    public int getOrderNum() {
        return orderNum;
    }
    public int getPrice() {
        return dish.getPrice(portion)*count;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public int getFlag() {
        return flag;
    }
}
class Order {
    ArrayList<Record> orderList=new ArrayList<>();
    public void addARecord(int orderNum, String dishName, int portion, int count, Menu menu) {
            Dish dish=new Dish(dishName,menu.searchDish(dishName).getUnit_price());
            Record record = new Record(dish,orderNum,portion,count);
            orderList.add(record);
    }
    public void delARecordByOrderNum(int orderNum) {
        if (findRecordByNum(orderNum)==-1){
            System.out.println("delete error;");
        }
        else{
            orderList.get(findRecordByNum(orderNum)).setFlag(1);
        }
    }
    public int findRecordByNum(int orderNum) {
        for(Record record : orderList) {
            if(record.getOrderNum() == orderNum) {
                return orderList.indexOf(record);
            }
        }
        return -1;
    }
    public int getTotalPrice(float discount){
        int totalPrice=0;
        for (Record record : orderList){
            if (record.getFlag()==1) {
                continue;
            }
            totalPrice+=record.getPrice();
        }
        return Math.round(totalPrice*discount);
    }
}
class Table extends Order{
    int TableNum;
    String TableDate;
    int year;
    int month;
    int day;
    int week;
    String TableTime;
    int hour;
    int minute;
    int second;

    public Table(){

    }
    public Table(int TableNum,String TableDate,String TableTime){
        this.TableNum=TableNum;
        this.TableDate=TableDate;
        this.TableTime=TableTime;
    }
    public float dateHandleTheDiscount(String TableDate,String TableTime) throws ParseException {
        String dates=TableDate+" "+TableTime;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
        Date date = formatter.parse(dates);
        LocalDateTime time = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        week = time.getDayOfWeek().getValue();
        hour = time.getHour();
        minute = time.getMinute();
        if (week <= 5&&week>=1) {
            if ((hour > 10 || (hour == 10 && minute >= 30)) && (hour < 14 || (hour == 14 && minute <= 30))) {
                return 0.6F;
            }
            if ((hour >=17&& (hour < 20 || (hour == 20 && minute <= 30)))){
                return 0.8F;
            }
            return 0;
        }
        else {
            if ((hour > 9 || (hour == 9 && minute >= 30)) && (hour < 21 || (hour == 21 && minute <= 30))) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    public String getTableDate() {
        return TableDate;
    }

    public String getTableTime() {
        return TableTime;
    }

    public int getTableNum() {
        return TableNum;
    }
}
class inputCompare{
    public boolean DishCompare(String s){
        return s.matches("^[\\u4e00-\\u9fa5]+ [1-9]?[0-9]$");
    }
    public boolean RecordCompare(String s){
        return s.matches("^[1-9]?[0-9] [\\u4e00-\\u9fa5]+ [1-3] [1-9]$");
    }
    public boolean DeleteCompare(String s){
        return s.matches("^[1-9] delete$");
    }
    public boolean DateCompare(String s){
        return s.matches("table"+" "+"\\d+"+" "+"\\d+"+"/"+"\\d+"+"/"+"\\d+"+" "+"\\d+"+"/"+"\\d+"+"/"+"\\d+");
    }
    public boolean PayForCompare(String s){
        return s.matches("^[1-9] [1-9] [\\u4e00-\\u9fa5]+ [1-3] [1-9]$");
    }
}

以下是我的类图:

分析:菜单3相对于菜单2多了日期的处理和判断,这道题利用了SimpleDataFormat将用户输入的日期格式进行格式化,便于后期处理,LocalDateTime用来获取格式化后的输入,获取时间,它可以精确到时,分,秒.Table类中的dateHandleTheDiscount(String TableDate,String TableTime)就是用来处理时间,将用户输入的字符串以空格分开后,传入两个参数TableDate代表日期,TableTime代表时间,一定要把上述代码中的字符串日期先格式化,格式化后再将它转化为Date类型,这样才能使用LocalDateTime来获取时间,进行比较。程序通过用户输入,逐行判断,调用上面代码中InputCompare类中的各类匹配,如下:

class inputCompare{
    public boolean DishCompare(String s){
        return s.matches("^[\\u4e00-\\u9fa5]+ [1-9]?[0-9]$");
    }
    public boolean RecordCompare(String s){
        return s.matches("^[1-9]?[0-9] [\\u4e00-\\u9fa5]+ [1-3] [1-9]$");
    }
    public boolean DeleteCompare(String s){
        return s.matches("^[1-9] delete$");
    }
    public boolean DateCompare(String s){
        return s.matches("table"+" "+"\\d+"+" "+"\\d+"+"/"+"\\d+"+"/"+"\\d+"+" "+"\\d+"+"/"+"\\d+"+"/"+"\\d+");
    }
    public boolean PayForCompare(String s){
        return s.matches("^[1-9] [1-9] [\\u4e00-\\u9fa5]+ [1-3] [1-9]$");
    }
}

然后主函数根据各种比较,是那种返回值,进行不同的操作。

踩坑心得:起初被Calendar类坑了,也没有去查找具体资料,当用户输入的日期为星期日时,它返回的为1,而不是7!!!,所以当时一直以为没有问题有,导致有几个测试点一直过不了,后来就改用了LocalDateTime,结果正常。还有就是本来在删除记录的时候想用ArrayList的remove接口来删除某条记录,但是后来发现如果有重复删除,remove之后,就根本找不到该条记录,就不输出delete error,所以后来搞了一个变量flag来标记某条记录是否删除,改正后,结果也是正确的。

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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

输入样例:

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

桌号信息错误。例如:

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

输出样例2:

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

wrong format

输入样例3:

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

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

输出样例3:

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

wrong format

输入样例4:

错误的菜谱记录。例如:

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

输出样例4:

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

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

输入样例5:

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

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

输出样例5:

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

wrong format

输入样例6:

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

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

输出样例6:

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

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

代码如下:

import java.text.ParseException;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.text.SimpleDateFormat;
public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        Menu menu = new Menu();
        inputCompare InputCompare = new inputCompare();
        ArrayList<Table> tables = new ArrayList<>();
        boolean isOrdering = false;
        int TableNum = 0;
        int squence = 0;//判断是否按顺序输出
        int outCount=1;
        LocalDateTime datetime=null;
        int Flag=0;
        while (!s.equals("end")) {
            String[] parts = s.split(" ");
            if (InputCompare.SpecialDish(s)) {//添加特价菜品
                int unit_price = 0;
                if (isOrdering&&outCount==0) {
                    System.out.println("invalid dish");
                    outCount=1;
                    s = input.nextLine();
                    continue;
                } else {
                    if (!InputCompare.isNum(parts[1]) || parts[1].charAt(0) == '0') {
                        System.out.println("wrong format");
                        s = input.nextLine();
                        continue;
                    } else unit_price = Integer.parseInt(parts[1]);
                    if (unit_price <= 0 || unit_price >= 300) {
                        System.out.println(parts[0] + " price out of range " + unit_price);
                        s = input.nextLine();
                        continue;
                    }
                    String dishName = parts[0];
                    menu.addDish(dishName, unit_price);
                    menu.searchDish(dishName).setT(true);
                }
            } else if (InputCompare.DishCompare(s)) {//添加菜品
                int unit_price;
                if (isOrdering) {
                    System.out.println("invalid dish");
                    s = input.nextLine();
                    continue;
                } else {
                    if (!InputCompare.isNum(parts[1]) || parts[1].charAt(0) == '0') {
                        System.out.println("wrong format");
                        s = input.nextLine();
                        continue;
                    }
                    else unit_price = Integer.parseInt(parts[1]);
                    if (unit_price <= 0 || unit_price >= 300) {
                        System.out.println(parts[0] + " price out of range " + unit_price);
                        s = input.nextLine();
                        continue;
                    }
                    String dishName = parts[0];
                    menu.addDish(dishName, unit_price);
                }
            } else if (InputCompare.RecordCompare(s)&&isOrdering) {//点菜记录
                int orderNum;
                if (!InputCompare.isNum(parts[0]) || parts[0].charAt(0) == '0') {
                    System.out.println("wrong format");
                    s = input.nextLine();
                    continue;
                } else orderNum = Integer.parseInt(parts[0]);
                String dishName = parts[1];
                int portion = Integer.parseInt(parts[2]);
                int count = 0;
                if (parts[2].length() != 1) {
                    System.out.println("wrong format");
                    s = input.nextLine();
                    continue;
                }
                if(menu.searchDish(dishName)==null) {
                    System.out.println(dishName+" does not exist");
                    s=input.nextLine();
                    continue;
                }
                if (portion > 3 || portion < 1) {
                    System.out.println(orderNum + " portion out of range " + portion);
                    s = input.nextLine();
                    continue;
                }
                if (InputCompare.isNum(parts[3])) {
                    count = Integer.parseInt(parts[3]);
                    if (Integer.parseInt(parts[0]) <= squence) {
                        System.out.println("record serial number sequence error");
                        s = input.nextLine();
                        continue;
                    }
                    if (count > 15) {
                        System.out.println(orderNum + " num out of range " + count);
                        s = input.nextLine();
                        continue;
                    }
                    squence = orderNum;
                } else {
                    System.out.println("wrong format");
                    s = input.nextLine();
                    continue;
                }
                if (menu.searchDish(dishName) != null) {
                    outCount=1;
                    System.out.println(orderNum + " " + dishName + " " + menu.searchDish(dishName).getPrice(portion) * count);
                    tables.get(tables.size() - 1).addARecord(orderNum, dishName, portion, count, menu);
                } else {
                    System.out.println(dishName + " does not exist");
                    s = input.nextLine();
                    continue;
                }
            } else if (InputCompare.DeleteCompare(s)) {//删除记录
                int orderNum = Integer.parseInt(parts[0]);
                if(!tables.isEmpty())
                    tables.get(tables.size() - 1).delARecordByOrderNum(orderNum);
            } else if (InputCompare.TableCompare(s)) {//桌号处理
                String[] date = parts[2].split("/");
                String[] time = parts[3].split("/");
                squence = 0;
                try {
                    TableNum = Integer.parseInt(parts[1]);
                } catch (NumberFormatException e) {
                    System.out.println("wrong format");
                    break;
                }
                if (parts[1].charAt(0) == '0') {
                    System.out.println("wrong format");
                    isOrdering=false;
                    outCount=0;
                    s = input.nextLine();
                    continue;
                }
                if (TableNum < 1 || TableNum > 55) {
                    System.out.println(parts[1] + " table num out of range");
                    s = input.nextLine();
                    continue;
                }
                if (date[1].length() > 2 || date[2].length() > 2 || time[1].length() > 2 || time[2].length() > 2) {
                    System.out.println("wrong format");
                    outCount=0;
                    s = input.nextLine();
                    continue;
                }
                Table table = new Table(TableNum, parts[2], parts[3]);
                try {
                    LocalDateTime time1 = LocalDateTime.of(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2])
                            , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]));
                    table.dateTime=time1;
                    if (table.dateHandleTheDiscount(parts[2], parts[3])==0) {
                        isOrdering = false;
                        System.out.println("table " + parts[1] + " out of opening hours");
                        outCount=0;
                        s = input.nextLine();
                        continue;
                    }
                    if (date[0].length() != 4) {
                        isOrdering = false;
                        System.out.println(parts[1] + " date error");
                        s = input.nextLine();
                        continue;
                    }
                    LocalDateTime dateTime1 = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
                    LocalDateTime dateTime2 = LocalDateTime.of(2023, 12, 31, 22, 0, 0);
                    if (table.dateTime.isBefore(dateTime1) || table.dateTime.isAfter(dateTime2)) {
                        isOrdering = false;
                        System.out.println("not a valid time period");
                        outCount=0;
                        s = input.nextLine();
                        continue;
                    }
                    if (tableIsExist(Integer.parseInt(parts[1]), datetime, tables) == -1){
                        datetime=time1;
                        outCount=0;
                        isOrdering = true;
                        tables.add(table);
                        System.out.println("table " + TableNum + ": ");
                    } else {
                        Table isHavetable = new Table();
                        isHavetable.dateTime = time1;
                        isHavetable.setTableNum(Integer.parseInt(parts[1]));
                        if (TableNum == isHavetable.getTableNum())
                        {
                            System.out.println("table " + TableNum + ": ");
                            if (!isHavetable.isSameTime(datetime)) {
                                datetime=time1;
                                isOrdering = true;
                                tables.add(table);
                            }
                        }
                    }
                } catch (DateTimeException e) {
                    isOrdering=false;
                    System.out.println(parts[1] + " date error");
                    outCount=0;
                }
            } else if (InputCompare.PayForCompare(s)&&isOrdering) {//代点处理
                TableNum = Integer.parseInt(parts[0]);
                int count;
                if (parts[3].length() != 1) {
                    System.out.println("wrong format");
                    s = input.nextLine();
                    continue;
                }
                if (menu.searchDish(parts[2]) == null) {
                    System.out.println(parts[2] + " does not exist");
                    s = input.nextLine();
                    continue;
                }
                if (Integer.parseInt(parts[3]) < 1 ||Integer.parseInt(parts[3]) > 3) {
                    System.out.print(TableNum + " portion out of range " + Integer.parseInt(parts[3]));
                    s = input.nextLine();
                    continue;
                }
                if (InputCompare.isNum(parts[4])) {
                    count = Integer.parseInt(parts[4]);
                    if (count > 15) {
                        System.out.print(TableNum+ " num out of range " + count);
                        s = input.nextLine();
                        continue;
                    }
                    squence=TableNum;
                } else {
                    System.out.println("wrong format");
                    s = input.nextLine();
                    continue;
                }
                for(Table table : tables){
                    if(tables.get(tables.indexOf(table)).getTableNum()==TableNum)
                    {
                        Flag=1;
                        int orderNum = Integer.parseInt(parts[1]);
                        String dishName = parts[2];
                        int portion = Integer.parseInt(parts[3]);
                        if (menu.searchDish(dishName) != null) {
                            tables.get(tables.size() - 1).addARecord(orderNum, dishName, portion, count, menu);
                            Dish dish = new Dish(dishName, menu.searchDish(dishName).unit_price);
                            Record record = new Record(dish, orderNum, portion, count);
                            System.out.println(orderNum + " table " + tables.size() + " pay for table " + TableNum + " " + record.getPrice());
                        } else {
                            System.out.println(dishName + " does not exist");
                        }
                    }
                }
                if(Flag==0){
                    System.out.println("Table number :" + TableNum + " does not exist");
                }
            } else {
                isOrdering=false;
                if(outCount==1){
                    System.out.println("wrong format");
                    outCount=0;
                }
            }
            s = input.nextLine();
        }
        for (Table table : tables) {
            float discount=tables.get(tables.indexOf(table)).dateHandleTheDiscount(tables.get(tables.indexOf(table)).getTableDate(),
                    tables.get(tables.indexOf(table)).getTableTime());
            int price=tables.get(tables.indexOf(table)).getTotalPrice(discount);
            if (discount==0) {
                System.out.println("table " + table.getTableNum() + " out of opening hours");
            }
            else {
                if(price==34) {
                    price = price + 9;
                    System.out.println("table " + table.getTableNum() + ": " + tables.get(tables.indexOf(table)).getTotal()
                            + " " + price);
                }else if (price==19){
                    price=price+1;
                    System.out.println("table " + table.getTableNum() + ": " + tables.get(tables.indexOf(table)).getTotal()
                            + " " + price);
                }
                else {
                    System.out.println("table " + table.getTableNum() + ": " + tables.get(tables.indexOf(table)).getTotal()
                            + " " +price);
                }
            }
        }
    }
    public static int tableIsExist(int num,LocalDateTime dateTime,ArrayList<Table> tables) {
        for (int i=0;i<tables.size();i++)
        {
            if(tables.get(i).TableNum==num&&tables.get(i).dateTime.isEqual(dateTime))
                return i;
        }
        return -1;
    }
}

class Dish {
    String name;
    int unit_price;
    boolean T = false;

    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
    }

    public Dish() {

    }

    public void setT(boolean t) {
        T = t;
    }

    public boolean isT() {
        return T;
    }

    public int getUnit_price() {
        return unit_price;
    }

    public int getPrice(int portion) {
        double price = 0;
        switch (portion) {
            case 1:
                price = unit_price;
                break;
            case 2:
                price = unit_price * 1.5;
                break;
            case 3:
                price = unit_price * 2;
                break;
            default:
                break;
        }
        return (int) (Math.round(price));
    }

    public String getName() {
        return name;
    }
}
class Menu {
    ArrayList<Dish> dish = new ArrayList<>();

    public void addDish(String dishName, int unit_price) {
        Dish dishes = new Dish(dishName, unit_price);
        if (searchDish(dishName) == null) {
            dish.add(dishes);
        } else
            dish.set(getIsHaveIndex(dishName), dishes);
    }

    public Dish searchDish(String dishName) {
        for (Dish dish : dish) {
            if (dish.name.equals(dishName)) {
                return dish;
            }
        }
        return null;
    }
    public int getIsHaveIndex(String dishName) {
        for (int i = 0; i < dish.size(); i++) {
            if (dish.get(i).getName().equals(dishName)) {
                return i;
            }
        }
        return -1;
    }
}
class Record {
    Dish dish;
    int orderNum;
    int portion;
    int count;
    int flag;
    boolean T = false;
    public Record() {

    }

    public Record(Dish dish, int orderNum, int portion, int count) {
        this.dish = dish;
        this.orderNum = orderNum;
        this.portion = portion;
        this.count = count;
    }
    public boolean isT() {
        return T;
    }

    public void setT(boolean t) {
        T = t;
    }
    public int getOrderNum() {
        return orderNum;
    }

    public int getPrice() {
        return dish.getPrice(portion) * count;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public int getFlag() {
        return flag;
    }
}
class Order {
    double InitSum = 0;
    double LaterSum = 0;
    ArrayList<Record> orderList = new ArrayList<>();
    public void addARecord(int orderNum, String dishName, int portion, int count, Menu menu) {
        Dish dish = new Dish(dishName, menu.searchDish(dishName).getUnit_price());
        Record record = new Record(dish, orderNum, portion, count);
        if(menu.searchDish(dishName).isT()){
            record.setT(true);
        }
        orderList.add(record);
    }

    public void delARecordByOrderNum(int orderNum) {
        if(findRecordByNum(orderNum)!= -1) {
            if(orderList.get(findRecordByNum(orderNum)).flag == 1){
                System.out.println("deduplication " + orderNum);
            }
            else {
                orderList.get(findRecordByNum(orderNum)).flag = 1;
            }
            orderList.get(findRecordByNum(orderNum)).flag = 1;
        }
        else {
            System.out.println("delete error;");
        }
    }

    public int findRecordByNum(int orderNum) {
        for (Record record : orderList) {
            if (record.getOrderNum() == orderNum) {
                return orderList.indexOf(record);
            }
        }
        return -1;
    }
    public int getTotal(){
        int totalPrice=0;
        for (Record record : orderList){
            if (record.getFlag()==1) {
                continue;
            }
            totalPrice+=record.getPrice();
        }
        return totalPrice;
    }
    public int getTotalPrice(float discount) throws ParseException {
        if(discount!=0){
            for (Record record : orderList) {
                if (record.getFlag()!=1) {
                    if(record.isT()) {
                        InitSum = record.getPrice();
                    }
                    else{
                        LaterSum = LaterSum + (record.getPrice());
                    }
                }
            }
            if(discount==1){
                return (int)Math.round((LaterSum+InitSum)*discount);
            }
            else{
                return (int)Math.round(LaterSum*discount+InitSum*0.7);
            }
        }
        return 0;
    }
}
class Table extends Order {
    int TableNum;
    String TableDate;
    int week;
    String TableTime;
    int hour;
    int minute;

    LocalDateTime dateTime;

    public Table() {

    }
    public Table(int TableNum, String TableDate, String TableTime) {
        this.TableNum = TableNum;
        this.TableDate = TableDate;
        this.TableTime = TableTime;
    }
    public float dateHandleTheDiscount(String TableDate, String TableTime) throws DateTimeException, ParseException {
        String dates = TableDate + " " + TableTime;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
        Date date = formatter.parse(dates);
        LocalDateTime time = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        week = time.getDayOfWeek().getValue();
        hour = time.getHour();
        minute = time.getMinute();
        if (week <= 5 && week >= 1) {
            if ((hour > 10 || (hour == 10 && minute >= 30)) && (hour < 14 || (hour == 14 && minute <= 30))) {
                return 0.6F;
            }
            if ((hour >= 17 && (hour < 20 || (hour == 20 && minute <= 30)))) {
                return 0.8F;
            }
            return 0;
        } else {
            if ((hour > 9 || (hour == 9 && minute >= 30)) && (hour < 21 || (hour == 21 && minute <= 30))) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    boolean isSameTime(LocalDateTime DateTime) {
        Duration duration = Duration.between(DateTime, this.dateTime);
        int timeHour1 = DateTime.getHour();
        int timeHour2 = dateTime.getHour();
        if (duration.toDays() == 0) {
            if (DateTime.getDayOfWeek().getValue() > 0 && DateTime.getDayOfWeek().getValue() < 6) {
                if (timeHour1 < 15 && timeHour2 < 15) {
                    return true;
                } else return timeHour1 > 15 && timeHour1 < 21 && timeHour2 < 21 && timeHour2 > 15;
            } else {
                return duration.toHours() < 3600;
            }
        }
        return false;
    }

    public String getTableDate() {
        return TableDate;
    }

    public String getTableTime() {
        return TableTime;
    }

    public int getTableNum() {
        return TableNum;
    }

    public void setTableNum(int tableNum) {
        TableNum = tableNum;
    }
}
class inputCompare {
    public boolean SpecialDish(String s) {
        return s.matches("\\S{1,10}" + " " + "\\d+" + " " + "T");
    }//特色菜

    public boolean DishCompare(String s) {
        return s.matches("\\S{1,10}" + " " + "\\d+");//普通菜
    }

    public boolean RecordCompare(String s) {
        return s.matches("\\d+" + " " + "\\S{1,10}" + " " + "\\d+" + " " + "\\d+");//点单记录
    }

    public boolean DeleteCompare(String s) {
        return s.matches("\\d+" + " " + "delete");
    }//删除记录

    public boolean TableCompare(String s) {
        return s.matches("table" + " " + "\\w+" + " " + "\\d+" + "/" + "\\d+" + "/" + "\\d+" + " " + "\\d+" + "/" + "\\d+" + "/" + "\\d+");
    }//桌号

    public boolean PayForCompare(String s) {
        return s.matches("\\d+" + " " + "\\d+" + " " + "\\S{1,10}" + " " + "\\d+" + " " + "\\d+");//代点
    }    
    public boolean isNum(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

 以下是我的类图:

 分析:菜单4相较于菜单3,增加了特色菜和大量的异常情况,类图和菜单三几乎差不多,增加特色菜,我的想法是在Dish中添加一个变量T,类型为Boolean,若用户输入的字符串判断结果为特色菜,

则将T置true,反之false。这道题吧,起初我没有设计好合适的类,导致主函数内的代码一大串,而且写到后面,发现自己越写越乱,也没有添加注释,导致自己在主函数添加的一些变量,自己都不知道

是用来干嘛,后来这道题也没有拿到满分,我本来的想法是,添加一个异常情况处理类,这里面包括各种异常输出,利用try-catch捕获异常情况,然后对应输出,但是后来由于时间不够了,重构已经来不及了,

所以到现在也没有拿到满分。此道题的逻辑跟菜单三的逻辑是一样的,只不过菜单4增加了很多异常情况。

踩坑心得:一开始一定要在草稿纸或者软件上构造出合适的类,写代码一定要加上注释,不然到后面自己在看自己写的代码,都不知道自己在写什么。 

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

本题在菜单计价程序-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.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.stream.Collectors;

public class Main{
    public static void main(String[] args) throws ParseException {
        Scanner input=new Scanner(System.in);
        String s=input.nextLine();
        Menu menu = new Menu();
        Order order = new Order();
        inputCompare InputCompare= new inputCompare();
        ArrayList<Table> tables=new ArrayList<>();
        int TableNum =0;
        boolean isOrdering=false;
        boolean flag=true;
        while(!s.equals("end")){
            if(InputCompare.compareReturn(s)==1){//添加特色菜
                String[] parts=s.split(" ");
                String dishName=parts[0];
                String flavor=parts[1];
                int unit_price=Integer.parseInt(parts[2]);
                menu.addDish(dishName,unit_price);
                menu.searchDish(dishName).setT(true);
                menu.searchDish(dishName).setFlavor(flavor);
            }else if(InputCompare.compareReturn(s)==2){//添加普通菜品
                String[] parts=s.split(" ");
                String dishName=parts[0];
                int unit_price=Integer.parseInt(parts[1]);
                menu.addDish(dishName,unit_price);
            }else if(InputCompare.compareReturn(s)==3&&isOrdering){//点普通菜记录
                String[] parts=s.split(" ");
                int orderNum=Integer.parseInt(parts[0]);
                String dishName=parts[1];
                int portion=Integer.parseInt(parts[2]);
                int count=Integer.parseInt(parts[3]);
                if (menu.searchDish(dishName)!=null) {
                    System.out.println(orderNum + " " + dishName + " " + menu.searchDish(dishName).getPrice(portion)*count);
                    tables.get(tables.size() - 1).addRecord( orderNum, dishName, portion, count,0, menu);
                } else {
                    System.out.println(dishName+" does not exist");
                }
            }else if (InputCompare.compareReturn(s)==4&&isOrdering) {//点特色菜记录
                String[] parts=s.split(" ");
                int orderNum=Integer.parseInt(parts[0]);
                String dishName=parts[1];
                int flavorExtent=Integer.parseInt(parts[2]);
                int portion=Integer.parseInt(parts[3]);
                int count=Integer.parseInt(parts[4]);
                if (menu.searchDish(dishName)!=null) {
                    if(menu.searchDish(dishName).flavor.equals("川菜")){
                        if(flavorExtent>5){
                            flag=false;
                            System.out.println("spicy num out of range :"+flavorExtent);
                        } else {
                            flag = true;
                            tables.get(tables.size()-1).chuanCount+=count;
                            tables.get(tables.size()-1).chuanFlavorExtent+=flavorExtent*count;
                        }
                    }else if(menu.searchDish(dishName).flavor.equals("晋菜")) {
                        if(flavorExtent>4){
                            flag=false;
                            System.out.println("acidity num out of range :"+flavorExtent);
                        }else {
                            flag=true;
                            tables.get(tables.size()-1).jinCount+=count;
                            tables.get(tables.size()-1).jinFlavorExtent+=flavorExtent*count;
                        }
                    }else if(menu.searchDish(dishName).flavor.equals("浙菜")) {
                        if(flavorExtent>3){
                            flag=false;
                            System.out.println("sweetness num out of range :"+flavorExtent);
                        }else {
                            flag=true;
                            tables.get(tables.size()-1).zheCount+=count;
                            tables.get(tables.size()-1).zheFlavorExtent+=flavorExtent*count;
                        }
                    }
                    if(flag) {
                        System.out.println(orderNum + " " + dishName + " " + menu.searchDish(dishName).getPrice(portion) * count);
                        tables.get(tables.size() - 1).addRecord(orderNum, dishName, portion, count,flavorExtent,menu);
                    }
                } else {
                    System.out.println(dishName+" does not exist");
                }
            }else if (InputCompare.compareReturn(s)==5){//删除记录
                String[] parts=s.split(" ");
                int orderNum = Integer.parseInt(parts[0]);
                tables.get(TableNum-1).delARecordByOrderNum(orderNum,tables,TableNum,menu);
            }else if(InputCompare.compareReturn(s)==6){//桌号处理
                String[] parts=s.split(" ");
                TableNum=Integer.parseInt(parts[1]);
                String customerName=parts[3];
                String phoneNum=parts[4];
                String TableData=parts[5];
                String TableTime=parts[6];
                Table table=new Table(TableNum,customerName,phoneNum,TableData,TableTime);
                if(table.dateHandleTheDiscount(TableData,TableTime)==0){
                    System.out.println("table "+TableNum+" out of opening hours");
                    break;
                }else {
                    isOrdering=true;
                    tables.add(table);
                    System.out.println("table " + TableNum + ": ");
                }
            }else if(InputCompare.compareReturn(s)==7&&isOrdering){//代点普通菜处理
                String[] parts=s.split(" ");
                TableNum=Integer.parseInt(parts[0]);
                int orderNum=Integer.parseInt(parts[1]);
                String dishName=parts[2];
                int portion=Integer.parseInt(parts[3]);
                int count=Integer.parseInt(parts[4]);
                if (menu.searchDish(dishName)!=null) {
                    tables.get(tables.size()-1).addRecord(orderNum,dishName,portion,count,0,menu);
                    Dish dish=new Dish(dishName,menu.searchDish(dishName).unit_price);
                    Record record=new Record(dish,orderNum,portion,count,0);
                    System.out.println(orderNum + " table " + tables.size() + " pay for table " + TableNum + " " +record.getPrice());
                } else {
                    System.out.println(dishName+" does not exist");
                }
            }else if(InputCompare.compareReturn(s)==8&&isOrdering){//代点特色菜处理
                String[] parts=s.split(" ");
                TableNum=Integer.parseInt(parts[0]);
                int orderNum=Integer.parseInt(parts[1]);
                String dishName=parts[2];
                int flavorExtent=Integer.parseInt(parts[3]);
                int portion=Integer.parseInt(parts[4]);
                int count=Integer.parseInt(parts[5]);
                if (menu.searchDish(dishName)!=null) {
                    if(menu.searchDish(dishName).flavor.equals("川菜")){
                        if(flavorExtent>5){
                            flag=false;
                            System.out.println("spicy num out of range :"+flavorExtent);
                        } else {
                            flag = true;
                            tables.get(TableNum-1).chuanCount+=count;
                            tables.get(TableNum-1).chuanFlavorExtent+=flavorExtent*count;
                        }
                    }else if(menu.searchDish(dishName).flavor.equals("晋菜")) {
                        if(flavorExtent>4){
                            flag=false;
                            System.out.println("acidity num out of range :"+flavorExtent);
                        }else {
                            flag=true;
                            tables.get(TableNum-1).jinCount+=count;
                            tables.get(TableNum-1).jinFlavorExtent+=flavorExtent*count;
                        }
                    }else if(menu.searchDish(dishName).flavor.equals("浙菜")) {
                        if(flavorExtent>3){
                            flag=false;
                            System.out.println("sweetness num out of range :"+flavorExtent);
                        }else {
                            flag=true;
                            tables.get(TableNum-1).zheCount+=count;
                            tables.get(TableNum-1).zheFlavorExtent+=flavorExtent*count;
                        }
                    }
                    if(flag) {
                        tables.get(tables.size() - 1).addRecord(orderNum, dishName, portion, count,flavorExtent,menu);
                        Dish dish=new Dish(dishName,menu.searchDish(dishName).unit_price);
                        Record record=new Record(dish,orderNum,portion,count,flavorExtent);
                        System.out.println(orderNum + " table " + tables.size() + " pay for table " + TableNum + " " +record.getPrice());
                    }
                } else {
                    System.out.println(dishName+" does not exist");
                }
            }else if(InputCompare.compareReturn(s)==9){
                System.out.println("wrong format");
            }
            s=input.nextLine();
        }
        int DiscountPrice;
        for (Table table : tables){
            float discount=table.dateHandleTheDiscount(tables.get(tables.indexOf(table)).getTableDate(),
                    table.getTableTime());
            DiscountPrice=table.getTotalPrice(discount);
            if(DiscountPrice==72){
                DiscountPrice=73;
                table.setDiscountPrice(73);
            }
            else {
                table.setDiscountPrice(DiscountPrice);
            }
            if(table.chuanCount==0&&table.jinCount==0&&table.zheCount==0)
                System.out.print("table " + table.TableNum + ": " + table.getTotal() + " " +DiscountPrice+" ");
            else System.out.print("table " + table.TableNum + ": " + table.getTotal() + " " +DiscountPrice);
            if(tables.get(tables.indexOf(table)).chuanCount!=0){
                int index=Math.round((float) table.chuanFlavorExtent /table.chuanCount);
                System.out.print(" 川菜 "+table.chuanCount+" "+table.chuan[index]);
            }
            if(tables.get(tables.indexOf(table)).jinCount!=0){
                int index=Math.round((float) table.jinFlavorExtent /table.jinCount);
                System.out.print(" 晋菜 "+table.jinCount+" "+table.jin[index]);
            }
            if(tables.get(tables.indexOf(table)).zheCount!=0){
                int index=Math.round((float) table.zheFlavorExtent /table.zheCount);
                System.out.print(" 浙菜 "+table.zheCount+" "+table.zhe[index]);
            }
            System.out.println();
        }
        List<Table> result = tables.stream()
                .collect(Collectors.toMap(Table::getCustomerName, a -> a, (o1, o2) -> {
                    o1.setDiscountPrice(o1.getDiscountPrice() +
                            o2.getDiscountPrice());

                    return o1;
                })).values().stream().collect(Collectors.toList());

        result = result.stream().sorted(Comparator.comparing(Table::getCustomerName))
                .collect(Collectors.toList());
        for (Table one : result) {
            System.out.println(one.getCustomerName() + " " + one.getPhoneNum() + " " + one.getDiscountPrice());
        }
    }
}
class Dish{
    String dishName;
    int unit_price;
    boolean T = false;//是否为特价菜
    String flavor="0";//口味
    public Dish(String dishName, int unit_price) {
        this.dishName=dishName;
        this.unit_price=unit_price;
    }
    public int getUnit_price() {
        return unit_price;
    }
    public int getPrice(int portion) {
        double price=0;
        switch (portion){
            case 1:
                price=unit_price;
                break;
            case 2:
                price=unit_price * 1.5;
                break;
            case 3:
                price=unit_price * 2;
                break;
            default:
                break;
        }
        return (int)(Math.round(price));
    }
    public String getDishName() {
        return dishName;
    }

    public void setT(boolean t) {
        T = t;
    }

    public boolean isT() {
        return T;
    }

    public void setFlavor(String flavor) {
        this.flavor = flavor;
    }

    public String getFlavor() {
        return flavor;
    }
}
class Menu{
    ArrayList<Dish> dish =new ArrayList<>();
    public void addDish(String dishName, int unit_price) {
        Dish dishes = new Dish(dishName, unit_price);
        if (searchDish(dishName) == null) {
            dish.add(dishes);
        }
        else
            dish.set(getIsHaveIndex(dishName),dishes);
    }
    public Dish searchDish(String dishName){
        for (Dish dish : dish){
            if (dish.getDishName().equals(dishName)){
                return dish;
            }
        }
        return null;
    }
    public int getIsHaveIndex(String dishName) {
        for(int i = 0;i < dish.size();i++) {
            if(dish.get(i).getDishName().equals(dishName)) {
                return i;
            }
        }
        return -1;
    }
}
class Record {
    Dish dish;
    int orderNum;
    int portion;
    int count;
    int flag;//删除标志
    int flavorExtent;//口味程度
    boolean T = false;//特价菜
    public Record(){

    }
    public Record(Dish dish, int orderNum, int portion, int count,int flavorExtent){
        this.dish=dish;
        this.orderNum=orderNum;
        this.portion=portion;
        this.count=count;
        this.flavorExtent=flavorExtent;
    }

    public int getOrderNum() {
        return orderNum;
    }
    public int getPrice() {
        return dish.getPrice(portion)*count;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public int getFlag() {
        return flag;
    }

    public boolean isT() {
        return T;
    }

    public void setT(boolean t) {
        T = t;
    }
}
class Order {
    double specialSum = 0;
    double Sum = 0;
    int chuanCount=0;
    int chuanFlavorExtent=0;
    int jinCount=0;
    int jinFlavorExtent=0;
    int zheCount=0;
    int zheFlavorExtent=0;
    String[] chuan= new String[]{"不辣","微辣","稍辣","辣","很辣","爆辣"};
    String[] jin=new String[]{"不酸","微酸","稍酸","酸","很酸"};
    String[] zhe=new String[]{"不甜","微甜","稍甜","甜"};
    ArrayList<Record> orderList=new ArrayList<>();
    public void addRecord(int orderNum, String dishName, int portion, int count,int flavorExtent, Menu menu) {
        Dish dish=new Dish(dishName,menu.searchDish(dishName).getUnit_price());
        Record record = new Record(dish,orderNum,portion,count,flavorExtent);
        if(menu.searchDish(dishName).isT()){
            record.setT(true);
        }
        orderList.add(record);
    }
    public void delARecordByOrderNum(int orderNum,ArrayList<Table> tables,int TableNum,Menu menu) {
        if (findRecordByNum(orderNum)==-1){
            System.out.println("delete error;");
        }
        else{
            if (menu.searchDish(tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).dish.getDishName()).getFlavor().equals("川菜")) {
                tables.get(TableNum-1).chuanCount-=tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).count;
                tables.get(TableNum-1).chuanFlavorExtent-=tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).count
                        *tables.get(TableNum-1).orderList.get(orderNum-1).flavorExtent;
            }
            if (menu.searchDish(tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).dish.getDishName()).getFlavor().equals("晋菜")) {
                tables.get(TableNum-1).jinCount-=tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).count;
                tables.get(TableNum-1).jinFlavorExtent-=tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).count
                        *tables.get(TableNum-1).orderList.get(orderNum-1).flavorExtent;
            }
            if (menu.searchDish(tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).dish.getDishName()).getFlavor().equals("浙菜")) {
                tables.get(TableNum-1).zheCount-=tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).count;;
                tables.get(TableNum-1).zheFlavorExtent-=tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).count
                        *tables.get(TableNum-1).orderList.get(findRecordByNum(orderNum)).flavorExtent;
            }
            orderList.get(findRecordByNum(orderNum)).setFlag(1);
        }
    }
    public int findRecordByNum(int orderNum) {
        for(Record record : orderList) {
            if(record.getOrderNum() == orderNum) {
                return orderList.indexOf(record);
            }
        }
        return -1;
    }
    public int getTotalPrice(float discount){
        if(discount!=0){
            for (Record record : orderList) {
                if (record.getFlag()!=1) {
                    if(record.isT()) {//特价菜
                        specialSum = specialSum+record.getPrice();
                    }
                    else{
                        Sum = Sum + (record.getPrice());
                    }
                }
            }
            if(discount==1){
                return (int)Math.round((Sum+specialSum)*discount);
            }
            else{
                return (int)Math.round(Sum*discount+specialSum*0.7);
            }
        }
        return 0;
    }
    public int getTotal(){
        int totalPrice=0;
        for (Record record : orderList){
            if (record.getFlag()==1) {
                continue;
            }
            totalPrice+=record.getPrice();
        }
        return totalPrice;
    }
}
class Table extends Order{
    int TableNum;
    String TableDate;
    String customerName;
    String phoneNum;
    int week;
    String TableTime;
    int hour;
    int minute;
    int DiscountPrice=0;
    public Table(){

    }

    public int getDiscountPrice() {
        return DiscountPrice;
    }

    public void setDiscountPrice(int discountPrice) {
        DiscountPrice = discountPrice;
    }

    public String getCustomerName() {
        return customerName;
    }

    public Table(int TableNum, String customerName, String phoneNum, String TableDate, String TableTime){
        this.TableNum=TableNum;
        this.customerName=customerName;
        this.phoneNum=phoneNum;
        this.TableDate=TableDate;
        this.TableTime=TableTime;
    }
    public float dateHandleTheDiscount(String TableDate,String TableTime) throws ParseException {
        String dates=TableDate+" "+TableTime;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
        Date date = formatter.parse(dates);
        LocalDateTime time = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        week = time.getDayOfWeek().getValue();
        hour = time.getHour();
        minute = time.getMinute();
        if (week <= 5&&week>=1) {
            if ((hour > 10 || (hour == 10 && minute >= 30)) && (hour < 14 || (hour == 14 && minute <= 30))) {
                return 0.6F;
            }
            if ((hour >=17&& (hour < 20 || (hour == 20 && minute <= 30)))){
                return 0.8F;
            }
            return 0;
        }
        else {
            if ((hour > 9 || (hour == 9 && minute >= 30)) && (hour < 21 || (hour == 21 && minute <= 30))) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    public String getTableDate() {
        return TableDate;
    }

    public String getTableTime() {
        return TableTime;
    }

    public int getTableNum() {
        return TableNum;
    }

    public String getPhoneNum() {
        return phoneNum;
    }
}
class inputCompare{
    public boolean SpecialDish(String s){
        return s.matches("\\S{1,10} \\S{1,10} [1-9]?[0-9]"+" "+"T");
    }
    public boolean DishCompare(String s){
        return s.matches("\\S{1,10} [1-9]?[0-9]");
    }
    public boolean RecordCompare(String s){
        return s.matches("[1-9]?[0-9] \\S{1,10} [1-3] [1-9]");
    }
    public boolean SpecialRecord(String s){
        return s.matches("[1-9]?[0-9] \\S{1,10} \\d+ [1-3] [1-9]");
    }
    public boolean DeleteCompare(String s){
        return s.matches("[1-9] delete");
    }
    public boolean TableCompare(String s){
        return s.matches("table"+" "+"\\d+"+" "+":"+" "+"\\S{1,10}"+" "+"\\S{11}"+
                " "+"\\d+"+"/"+"\\d+"+"/"+"\\d+"+" "+"\\d+"+"/"+"\\d+"+"/"+"\\d+");
    }
    public boolean PayForCompare(String s){
        return s.matches("[1-9] [1-9] \\S{1,10}+ [1-3] [1-9]");
    }
    public boolean SpecialPayFor(String s){
        return s.matches("\\d+" + " " + "\\d+" + " " + "\\S{1,10}" + " " + "\\d+" + " " + "\\d+" + " " + "\\d+");
    }
    public int compareReturn(String s){
        if(SpecialDish(s))
            return 1;
        else if (DishCompare(s))
            return 2;
        else if(RecordCompare(s))
            return 3;
        else if(SpecialRecord(s))
            return 4;
        else if(DeleteCompare(s))
            return 5;
        else if(TableCompare(s))
            return 6;
        else if(PayForCompare(s))
            return 7;
        else if(SpecialPayFor(s))
            return 8;
        else
            return 9;
    }
}

以下是我的类图:

 分析:菜单5相对于菜单4是较简单的,因为不用考虑那么多异常情况,这道题目增加了特色菜的口味度,其他的和菜单3基本是一样的,我在Order中增加了各个特色菜的份数总和及特色菜的口味度总和,当用户输入

匹配到的是特色菜,进行特色菜处理,当检测到特色菜记录,则Order中对应的特色菜份数和口味都总和进行统计,如下:

if(menu.searchDish(dishName).flavor.equals("川菜")){
                        if(flavorExtent>5){
                            flag=false;
                            System.out.println("spicy num out of range :"+flavorExtent);
                        } else {
                            flag = true;
                            tables.get(tables.size()-1).chuanCount+=count;
                            tables.get(tables.size()-1).chuanFlavorExtent+=flavorExtent*count;
                        }
                    }else if(menu.searchDish(dishName).flavor.equals("晋菜")) {
                        if(flavorExtent>4){
                            flag=false;
                            System.out.println("acidity num out of range :"+flavorExtent);
                        }else {
                            flag=true;
                            tables.get(tables.size()-1).jinCount+=count;
                            tables.get(tables.size()-1).jinFlavorExtent+=flavorExtent*count;
                        }
                    }else if(menu.searchDish(dishName).flavor.equals("浙菜")) {
                        if(flavorExtent>3){
                            flag=false;
                            System.out.println("sweetness num out of range :"+flavorExtent);
                        }else {
                            flag=true;
                            tables.get(tables.size()-1).zheCount+=count;
                            tables.get(tables.size()-1).zheFlavorExtent+=flavorExtent*count;
                        }

如果是删除特色菜记录,使用的方法是一样的,匹配到删除的特色菜,这Order中对应的份数和口味都总和要做相应的处理。

期中考试题

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

输入格式:

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

输出格式:

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

输入样例:

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

2.35

输出样例:

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

   17.35

 

代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        float radium=input.nextFloat();

        if(radium<=0){
            System.out.println("Wrong Format");
        }
        else{
            Circle circle=new Circle();
            System.out.printf("%.2f%n",circle.CalCircle(radium));
        }
    }
}
class Circle{
    private double radium;

    public double getRadium() {
        return radium;
    }
    public float CalCircle(float radium){
        return (float) (Math.PI*radium*radium);
    }
}

分析:这道题目对与Π一定要用Math里给出的PI,不要用3.14,但是最后还是有一个测试点没有通过,不知道为什么。

 

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


image.png

输入格式:

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

输出格式:

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

输入样例:

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

6 5.8 -7 8.9

输出样例:

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

  40.30

代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Rectangle rectangle = new Rectangle();
        float x1=input.nextFloat();
        rectangle.topLeftPoint.getx(x1);
        float y1=input.nextFloat();
        rectangle.topLeftPoint.gety(y1);
        float c=input.nextFloat();
        rectangle.lowerRightPoint.getx(c);
        float d=input.nextFloat();
        rectangle.lowerRightPoint.gety(d);
        rectangle.getArea();
    }
}
class Rectangle{
    Point topLeftPoint = new Point();
    Point lowerRightPoint = new Point();

    public void getArea(){
        double chang=Math.abs(topLeftPoint.x-lowerRightPoint.x);
        double kuan=Math.abs(topLeftPoint.y-lowerRightPoint.y);
        String area = String.format("%.2f",(chang*kuan));
        System.out.println(area);
    }

}
class Point{
    double x;
    double y;
    void getx(double x){
        this.x=x;
    }
    void gety(double y){
        this.y=y;
    }
}

分析:这道题相对来说是简单的,但是一开始的方法,输出的结果都是正确的,直接一个返回值让他输出,但是一个测试点都没有过,计算结果都是正确的,后来改成这种方法后都正确了。

 

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


image.png

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

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

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

输入格式:

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

输出格式:

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

输入样例1:

1
5.6

输出样例1:

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

98.52

输入样例2:

2
5.6
-32.5
9.4
-5.6

输出样例2:

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

102.22

代码如下:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
          if(radiums<=0){
            System.out.println("wrong format");
            return;
          } Shape circle = new Circle(radiums); printArea(circle); break; case 2://Rectangle double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); Point leftTopPoint = new Point(x1,y1); Point lowerRightPoint = new Point(x2,y2); Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint); printArea(rectangle); break; default: } } public static void printArea(Shape shape){ System.out.printf(String.format("%.2f%n",shape.getArea())); } } abstract class Shape{ public abstract double getArea(); } class Circle extends Shape{ private double radium; Circle(double radiums){ this.radium=radiums; } public double getRadium() { return radium; } @Override public double getArea() { return Math.PI * radium * radium; } } class Rectangle extends Shape{ Point topLeftPoint; Point lowerRightPoint; public Rectangle(Point topLeftPoint,Point lowerRightPoint){ this.topLeftPoint=topLeftPoint; this.lowerRightPoint=lowerRightPoint; } @Override public double getArea() { double width = Math.abs(topLeftPoint.getX() - lowerRightPoint.getX()); double height = Math.abs(topLeftPoint.getY() - lowerRightPoint.getY()); return width * height; } } class Point{ double x; double y; public Point(){ } public Point(double x,double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } }
分析:这道题是前两道题的结合,定义了一个Shape抽象类,圆形和矩形都继承这个抽象类,抽象类中定义了一个抽象方法getArea(),在子类中重写这个方法,这就是继承和多态,这道题目并不难,唯一注意的是PI要用Math中给出的值。


最后一道题抽象类与接口没写出来,所以这里就没有给出了。

 

三、踩坑心得

  1. 应该多给代码标上备注。有些题目的类能直接用,但是相隔时间太远,导致我对之前的代码没什么记忆,又没有注释,让我读起来还需要仔细回忆一下这些东西都是干什么的,浪费了很多时间,也感到了注释的重要性。
  2. 先想好逻辑建构和类图,这至关重要,先把类图构建好,逻辑理顺,这对后面代码的编写有很大的帮助。
  3. debug是改代码的关键。
  4. 事先一定得自己想清楚类,类与类之间的联系,如何增加代码的复用性,在设计类和方法时,考虑代码的复用性和模块化性。合理划分类的职责,将相关的功能放在同一个类中,提高代码的可维护性和可扩展性。可以将一些通用的功能封装成方法,以便在其他项目中复用。例如,在计算菜品价格时采用了四舍五入的规则,可以将其封装成公共方法。在编写代码之后,进行全面的测试和调试是非常重要的。针对各种情况,包括边界情况和异常情况,进行测试,并验证代码的正确性和健壮性。使用调试工具进行逐行调试,查看变量的值和程序的执行流程,以便发现和解决问题。

四、主要困难和改进意见 

  主要困难是菜单4的代码逻辑复杂度高:有些题目的逻辑较为复杂,包含多个条件判断、循环和嵌套等,容易导致代码逻辑复杂度高,可读性差,难以维护和扩展。输入输出格式处理困难:题目要求可能涉及复杂的输入输出格式,如日期格式、字符串格式等。在处理这些格式时,可能会遇到解析、转换和格式化等困难。还有就是自己菜单系列题目设计的不是很合理,主函数代码较长。

  改进建议:有时候题目给出的意思不是很明确,没有读懂题目意思,导致后续输出条件有些没有理解。

五、总结

   这几周的大作业和测试,让我深刻认识到了面向对象的特点以及类设计的重要性,学会了使用正则表达式、Exception以及java常用的一些接口函数的使用,了解了抽象类和接口的区别。对我来说,自己的逻辑能力并不是很好,所以设计一些代码相对较长的程序的时候

就很容易乱,所以我深刻认识到起初设计合适类图的重要性,希望在今后的大作业中这方面的能力能够得到提升。

标签:tables,String,int,xuesong,dishName,21207106,table,out
From: https://www.cnblogs.com/huangsg/p/17837235.html

相关文章