BLOG-2

发布时间 2023-11-17 01:12:12作者: 你好k

一、 前言

无论是期中考试还是4、5次大作业,题目每题之间都是密不可分的,比如第一题写好的代码你在第二题中可能就要用它作为一个子类。期中考试没有取得好成绩,一方面是因为第一次正式考试,没有实战经验,导致到了考试时间还没有将代码提交上去的情况,少拿了很多分。但是归根到底,这还是我知识不够扎实的原因,如果基础知识足够扎实,无论是什么题目都不应该出现时间不够的问题。而第4、5次作业是在前几次作业上的再次拓展,由菜单3拓展到菜单4,再由菜单3拓展到菜单5,这无疑极大的增加了题目的复杂度,不仅因为之前基础子类体系没有完善好,再加上菜单4的各情况要求判断十分复杂,这都让题目变得十分棘手。但总的来说,题目是老师精心挑选出来的,题量以及难易度肯定是没得说,都在可接受范围内,只要我们认真结合老师点拨与网络资源,这几次作业可以成为我们学习路上典例中的典例,值得不断探索学习。

二、 设计与分析

7-3 判断两个日期的先后,计算间隔天数、周数

从键盘输入两个日期,格式如:2022-06-18。判断两个日期的先后,并输出它们之间间隔的天数、周数(不足一周按0计算)。

预备知识:通过查询Java API文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。

输入格式:

输入两行,每行输入一个日期,日期格式如:2022-06-18

输出格式:

第一行输出:第一个日期比第二个日期更早(晚)
第二行输出:两个日期间隔XX天
第三行输出:两个日期间隔XX周

输入样例1:

2000-02-18
2000-03-15

输出样例1:

第一个日期比第二个日期更早
两个日期间隔26天
两个日期间隔3周

输入样例2:

2022-6-18
2022-6-1

输出样例2:

第一个日期比第二个日期更晚
两个日期间隔17天
两个日期间隔2周
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
这个题目我感觉是我做过最简单的因为我只需要调用库函数即可
 1 import java.text.DateFormat;
 2 import java.text.SimpleDateFormat;
 3 import java.time.LocalDate;
 4 import java.time.Year;
 5 import java.time.LocalDate;
 6 import java.time.temporal.ChronoUnit;
 7 import java.util.Scanner;
 8 import java.time.format.DateTimeFormatter;
 9 public class Main {
10     public static void main(String[] args) {
11         Scanner input = new Scanner(System.in);
12         String line = input.nextLine() ;
13         String line2 = input.nextLine() ;
14         String[] arr1 = line.split("-");
15         int year1 = Integer.parseInt(arr1[0]) ;
16         int month1 = Integer.parseInt(arr1[1]) ;
17         int day1 = Integer.parseInt(arr1[2]) ;
18         String[] arr2 = line2.split("-") ;
19         int year2 = Integer.parseInt(arr2[0]) ;
20         int month2 = Integer.parseInt(arr2[1]) ;
21         int day2 = Integer.parseInt(arr2[2]) ;
22         LocalDate cal1 = LocalDate.of(year1,month1,day1) ;
23         LocalDate cal2 = LocalDate.of(year2,month2,day2) ;
24         long day3 = ChronoUnit.DAYS.between(cal1,cal2);
25         if(day3<0)
26         {
27             System.out.println("第一个日期比第二个日期更晚") ;
28             day3=-day3;
29         }
30         else
31             System.out.println("第一个日期比第二个日期更早") ;
32         System.out.println("两个日期间隔"+day3+"天") ;
33         System.out.println("两个日期间隔"+(int)day3/7+"周") ;
34 
35     }

代码类图:

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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
class Dish {
    String name;//菜品名称
    int unit_price; //单价
    int getPrice(int portion) {
        int price = 0;
        if (portion == 1) {
            price = unit_price ;
        } else if (portion == 2) {
            price = Math.round((float) (unit_price * 1.5)) ;
        } else if (portion == 3) {
            price = (unit_price * 2) ;
        }
        return price;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    }
}
class Menu {
    Dish[] dishs = new Dish[30];//菜品数组,保存所有菜品信息
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--)
        {
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd==null){
            System.out.println(dishName+" does not exist");
        }
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,int unit_price){
        Dish dh = new Dish();
        dh.name = dishName;
        dh.unit_price = unit_price;
       // count++;
        return dh;
    }//添加一道菜品信息
}
class Record {
    int orderNum;//序号\
    Dish d = new Dish();//菜品\
    int num = 0;
    int portion;//份额(1/2/3代表小/中/大份)\
    //int exist = 1;
    int getPrice(){
        return d.getPrice(portion)*num;
    }//计价,计算本条记录的价格\
}
class Order {
    Record[] records = new Record[30];//保存订单上每一道的记录
    int count = 0;//订单数量
    void addARecord(int orderNum,String dishName,int portion,int num){
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].portion = portion;
        records[count].num = num;
    }//添加一条菜品信息到订单中。
    int delARecordByOrderNum(int orderNum){
        if(orderNum>count||orderNum<=0){
            System.out.println("delete error;");
            return 0;
        }else {
            return records[orderNum - 1].getPrice();
        }
    }//根据序号删除一条记录
}
class Table {
    int tableNum;
    String tableDtime;
    int week,hh,mm,ss;
    int sum = 0;//一桌价格 ;
    Order odt = new Order();
    float discnt = -1;
    void Gettottalprice(){
        if(discnt>0){
            sum = Math.round(sum*discnt);
            System.out.println("table " + tableNum + ": " + sum);
        }else {
            System.out.println("table " + tableNum + " out of opening hours");
        }
    }
    void AheadProcess(String tableDtime){
        this.tableDtime = tableDtime;
        processTime();
        discount();
    }
    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");
        tableNum = Integer.parseInt(temp[1]);
        String[] temp2 = temp[3].split("/");
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/M/d");
        LocalDate date = LocalDate.parse(temp[2], formatter1); // 将字符串解析为LocalDate对象
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        if(dayOfWeek ==DayOfWeek.WEDNESDAY )
            week = 3;
        else if(dayOfWeek ==DayOfWeek.MONDAY)
            week= 1;
        else if(dayOfWeek ==DayOfWeek.TUESDAY)
            week = 2;
        else if(dayOfWeek ==DayOfWeek.THURSDAY)
            week = 4;
        else if(dayOfWeek ==DayOfWeek.FRIDAY)
            week = 5;
        else if(dayOfWeek ==DayOfWeek.SATURDAY)
            week = 6;
        else if(dayOfWeek ==DayOfWeek.SUNDAY)
            week = 7;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);
    }
    void discount(){
        int timeu= hh * 10000 + mm * 100 + ss;
        if(week>=1 && week<=5)
        {
            if(timeu >= 170000 && timeu <= 203000)
                discnt = 0.8F;
            else if(timeu >= 103000 && timeu <= 143000)
                discnt = 0.6F;
        }
        else
        {
            if(timeu >= 93000 && timeu <= 213000)
                discnt = 1.0F;
        }
    }
}

public class Main {
    public static void main(String[] args) {
         String price = "([1-9]?[0-9]|100)";
         String Type = "(delete)";
         String tbl ="(table)";
         String name = "\\S{1,10}";//1到10个非空格(TAB)字符
         String date = "(\\d{4}/\\d{1,2}/\\d{1,2})";
         String time ="(\\d{1,2}/\\d{1,2}/\\d{1,2})";
       /* String ordeInput = price + " " + name + " " + price + " "+price;
         String meuninput = name +" "+ price;
         String delinput = price +" "+ Type;
         String table = tbl +" "+ price+" "+date+" "+time;
         String dai = price + " " + price +" "+name+" "+price+" "+price;*/
        Scanner sc = new Scanner(System.in);
        Menu mu = new Menu();
        //inputin mat = new inputin() ;
        Table[] tablemes = new Table[30];
        int j = 0;//菜单数
        int l = 0;//订单数
        int k = 0;//代点菜数
        Dish tt;
        int cntTable = 0;//桌号
        int count;
        String[] temp;
        int a1,a2,a3,a4,a5,k1=0;
        while (true) {
            String st = sc.nextLine();
            temp = st.split(" ");
            if(st.equals("end"))
                break;
            count = temp.length;
            if (count == 2) {//一个空格
                if (temp[1].equals("delete")) {//第二个为delete
                    a1 = Integer.parseInt(temp[0]);
                    int c = tablemes[cntTable].odt.delARecordByOrderNum(a1);
                    tablemes[cntTable].sum -= c;
                } else {//菜单添加
                    a2 = Integer.parseInt(temp[1]);
                    mu.dishs[j] = mu.addDish(temp[0], a2);
                    j++;
                    mu.count = j;
                }
            }
            else if (count == 4) {//三个空格
                if (temp[0].equals("table")) {//桌号
                    cntTable++;//跳过0;
                    tablemes[cntTable] = new Table();
                    tablemes[cntTable].AheadProcess(st);
                    System.out.println("table " + cntTable + ": ");
                } else {//增加订单的情况;
                    if(cntTable != k1){
                        l= 0;
                        k1 = cntTable ;
                        tablemes[cntTable].odt.count = l;
                    }
                    a3 =Integer.parseInt(temp[0]);
                    a4 = Integer.parseInt(temp[2]);
                    a5=Integer.parseInt(temp[3]);
                    tablemes[cntTable].odt.addARecord(a3, temp[1],a4 , a5);
                    tt = mu.searthDish(temp[1]);
                    if (tt != null) {
                        tablemes[cntTable].odt.records[l].d = tt;
                        int a = tablemes[cntTable].odt.records[l].getPrice();
                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );
                        tablemes[cntTable].sum += a;
                    }
                    l++;
                    tablemes[cntTable].odt.count = l;
                }
            }
            else if (count == 5) {//代点菜
                a1 = Integer.parseInt(temp[1]);
                a2 = Integer.parseInt(temp[3]);
                a3 = Integer.parseInt(temp[4]);
                tablemes[cntTable].odt.addARecord( a1, temp[2], a2, a3);
                tt = mu.searthDish(temp[2]);
                if (tt != null) {
                    tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price;
                    int b = tablemes[cntTable].odt.records[l].getPrice();
                    System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
                    tablemes[cntTable].sum += b;
                }
                l++;
                tablemes[cntTable].odt.count = l;
            }
        }
        for (int i = 1; i < cntTable + 1; i++) {
            tablemes[i].Gettottalprice();
        }
    }
}
class inputin{
    static String price = "([1-9]?[0-9]|100)";
    static String Type = "(delete)";
    static String tbl ="(table)";
    static String name = "\\S{1,10}";//1到10个非空格(TAB)字符
    static String date = "(\\d{4}/\\d{1,2}/\\d{1,2})";
    static String time ="(\\d{1,2}/\\d{1,2}/\\d{1,2})";
    static String ordeInput = price + " " + name + " " + price + " "+price;
    static String meuninput = name +" "+ price;
    static String delinput = price +" "+ Type;
    static String table = tbl +" "+ price+" "+date+" "+time;
    static String dai = price + " " + price +" "+name+" "+price+" "+price;
    public int matchingInput(String s) {
        if (s.matches(ordeInput) )
            return 1;
        if (s.matches(meuninput))
            return 2;
        if(s.matches(delinput))
            return 0;
        if(s.matches(table))
            return 3;
        if(s.matches(dai) )
            return 4;
        return -1;
    }

}

类图如下:

 

在执行这段代码时会运行超时,多次提交后过了,经过我多次的验证后发现是我创建的那个类,类的new和调用都会消耗时间,而且在新创建的类的代码越复杂消耗的时间就越多。

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

其他用例请参考公开的测试用例

代码长度限制
50 KB
时间限制
1000 ms
内存限制
64 MB
  1 import java.time.LocalDate;
  2 import java.time.LocalDateTime;
  3 import java.util.Scanner;
  4 public class Main{
  5     public static void main(String[] args){
  6         int j=0,flag=1,i=-1,k=0,m=0,n=0,p=0,q=0,flag3=0;
  7         Scanner input=new Scanner(System.in);
  8         Table[] tables=new Table[30];
  9         Menu menu=new Menu();
 10         //Rt rt=new Rt();
 11         String p1 = "([1-9]|[1-9][0-9]+)";
 12         String p2 = "[\u4E00-\u9FA5]+";
 13         String p3 = "[0-9]";
 14         String p4 = "([1-9][0-9]?|[1-2][0-9]{2})";
 15         String p5 = "table[ ]([1-9]|[1-9][0-9]+)[ ][0-9]{4}";
 16         String p6 = "\\/([0-9]|[0-9]{2})";
 17         String p7 = "\\/([0-9]|[0-9]{2})[ ]([0-9]|[0-9]{2})" ;
 18         String p8 = "([0-9]|[0-1][0-9]|20|21|22|23)";
 19         String p9 = "[/]([0-5][0-9])";
 20         String tabl1 =  p5+ p6+p7+p6+p6;
 21         String date1 = "^((\\d{2}(([02468][048])|([13579][26]))" +
 22                 "[\\-\\/\\s]?((((0?[13578])|(1[02]))" +
 23                 "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" +
 24                 "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))" +
 25                 "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))" +
 26                 "[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
 27         String time5 = p8+p9+p9;
 28         String delete1 = p1+" "+"delete";
 29         String order3 = p1+" "+ p2+" "+p3+" "+p1;
 30         String order2 = p1+" "+order3;
 31         String table2 = "[1-9]|[1-4][0-9]|5[0-5]";
 32         String menu1 = p2+" "+p1;
 33         String t = "T";
 34         String menu2 = menu1+" "+t;
 35         String menu3 = p2+" "+p4;
 36         String menu4 = menu3+" "+t ;
 37         String gh2 = "[1-3]";
 38         String gh3 = "[1-9]|1[0-5]";
 39         String tabl2 = "t(.*)";
 40         String tabl3 = "table(.*)";
 41         Panduan panduan=new Panduan();
 42         while(true) {
 43             String s = input.nextLine();
 44             if (s.equals("end"))
 45                 break;
 46             if (s.matches(tabl2 )) {
 47                 if(s.matches(tabl3 )) {
 48                     if(s.matches(tabl1))
 49                     {
 50                         String[] te=s.split(" |\\/");
 51                         String shijian=te[5]+"/"+te[6]+"/"+te[7];
 52                         LocalDate localDate1=LocalDate.of(2022,1,1);
 53                         LocalDate localDate2=LocalDate.of(2023,12,31);
 54                         String riqi=te[2]+"-"+te[3]+"-"+te[4];
 55 
 56                         if(!(te[1].matches(table2))){
 57                             flag=0;
 58                             System.out.println(te[1]+" table num out of range");
 59                         } else if (!(riqi.matches(date1))||!shijian.matches(time5)) {
 60                             flag=0;
 61                             System.out.println(te[1]+" date error");
 62                         }else{
 63                             int kn = Integer.parseInt(te[2]);
 64                             int kn1 = Integer.parseInt(te[3]);
 65                             int kn2 = Integer.parseInt(te[4]);
 66                             int kn3 = Integer.parseInt(te[5]);
 67                             int kn4 = Integer.parseInt(te[6]);
 68                             int kn5 = Integer.parseInt(te[7]);
 69                             LocalDate localDate=LocalDate.of(kn, kn1,kn2 );
 70                             LocalDateTime localDateTime=LocalDateTime.of(kn, kn1, kn2,kn3 , kn4, kn5 );
 71                             if (panduan.panduan2(localDateTime)==0) {
 72                                 System.out.println("table " + te[1] + " out of opening hours");
 73                                 flag=0;
 74                             }
 75                             else if(localDate.isBefore(localDate1)||localDate.isAfter(localDate2)){
 76                                 System.out.println("not a valid time period");
 77                                 flag=0;} else{
 78                                 i++;
 79                                 int km =Integer.parseInt(te[1]);
 80                                 tables[i] = new Table();
 81                                 tables[i].tablenum = km;
 82                                 tables[i].localDateTime=localDateTime;
 83                                 flag=1;
 84                                 System.out.println("table"+' '+te[1]+": ");
 85                             }
 86                         }
 87                     }
 88                     else{
 89                         System.out.println("wrong format");
 90                         flag=0;
 91                     }
 92                 }
 93                 else{
 94                     System.out.println("wrong format");}
 95                 //这里进行i不加对上一个桌的继承。
 96             } else {
 97                 if (flag == 1) {
 98                     if (s.matches(delete1)) {
 99                         String[] dd = s.split(" ");
100                         int ke = Integer.parseInt(dd[0]);
101                         if ((tables[i].findRecordByNum(tables[i], ke, tables[i].i) != 1) && (tables[i].findRecorddByNum(tables[i], ke, tables[i].j) != 1))
102                             System.out.println("delete error;");
103                     } else if (s.matches(order2 )) {
104                         String[] dd = s.split(" ");
105                         flag3=0;
106                         int ky = Integer.parseInt(dd[0]);
107                         for (j = 0; j <= i; j++) {//判断自己给自己点菜
108                             if (tables[j].tablenum == ky ){
109                                 flag3=1;
110                                 break;}
111                         }
112                         if(flag3==1){
113                             if (menu.searthDish(dd[2]) == null)
114                                 System.out.println(dd[2] + " " + "does not exist");
115                             else if(tables[j].tablenum==tables[i].tablenum){
116                                 System.out.println("Table number :" + dd[0] + " does not exist");
117                             }
118                             else{
119                                 int ky1 = Integer.parseInt(dd[1]);
120                                 int ky2 = Integer.parseInt(dd[3]);
121                                 int ky3 = Integer.parseInt(dd[4]);
122                                 int ky4 = tables[i].j;
123                                 tables[i].recordsd[ky4] = tables[i].addaRecordd(menu, ky, ky1, dd[2], ky2,ky3 );
124                                 System.out.println(tables[i].recordsd[ky4].orderNum + " table " + tables[i].tablenum + " pay for table " + dd[0] + " " + tables[i].recordsd[ky4].getPrice);
125                                 tables[i].j++;
126                             }
127                         } else
128                             System.out.println("Table number :" + dd[0] + " does not exist");
129                     }
130                     else if (s.matches(order3 )) {
131                         String[] bz = s.split(" ");
132                         int kj = Integer.parseInt(bz[0]);
133                         int kj5 = tables[i].i;
134                         if (kj5!=0){
135                             if(kj <= tables[i].records[kj5-1].orderNum){
136                                 System.out.println("record serial number sequence error");
137                                 continue;}
138                         }
139                         if (menu.searthDish(bz[1])== null)
140                             System.out.println(bz[1] + " " + "does not exist");
141                         else if (!bz[2].matches(gh2) && menu.searthDish(bz[1]).flag == 0) {
142                             System.out.println(bz[0] + " portion out of range " + bz[2]);
143                         }else if (!bz[3].matches(gh3)) {
144                             System.out.println(bz[0] + " num out of range " + bz[3]);
145                         }
146                         else if (!bz[2].matches(gh2) && menu.searthDish(bz[1]).flag == 1)
147                             System.out.println(bz[0] + " portion out of range " + bz[2]);
148                         else{
149                             int kj1 = Integer.parseInt(bz[2]);
150                             int kj2 = Integer.parseInt(bz[3]);
151                             tables[i].records[tables[i].i] = tables[i].addaRecord(menu, kj, bz[1],kj1 , kj2);
152                             System.out.println(bz[0] + " " + bz[1] + " " + tables[i].records[tables[i].i].getPrice);
153                             tables[i].i++;}
154                     }
155                     else if (s.matches(menu1) || s.matches(menu2)) {//进入菜单
156                         if (i == -1) {
157                             int flag1 = 0, flag2 = 1;
158                             boolean cheak1 = s.matches(menu1 );
159                             boolean cheak2 = s.matches(menu2 );
160                             String[] tokens = s.split(" ");
161                             int kl = Integer.parseInt(tokens[1]);
162                             if (cheak2) {
163                                 if (s.matches(menu4)) {
164                                     menu.addDish(tokens[0], kl, flag2);
165                                     menu.f ++;
166                                 }
167                                 else
168                                     System.out.println(tokens[0] + " price out of range " + tokens[1]);
169                             }
170                             if (cheak1) {
171                                 if (s.matches(menu3)) {
172                                     menu.addDish(tokens[0], kl, flag1);
173                                     menu.f ++;
174                                 }
175                                 else
176                                     System.out.println(tokens[0] + " price out of range " + tokens[1]);
177                             }
178                         } else
179                             System.out.println("invalid dish");
180                     } else {
181                         System.out.println("wrong format");
182                     }
183                 }
184             }
185         }
186         for(j=0;j<=i;j++) {
187             if(tables[j].flag==1){
188                 for(k=j+1;k<=i;k++) {
189                     if(tables[k].tablenum==tables[j].tablenum){
190                         if(panduan.panduan( tables[k].localDateTime, tables[j].localDateTime)!=0||panduan.panduan1( tables[k].localDateTime, tables[j].localDateTime)!=0){
191                              tables[k].flag=0;
192                             for(m=0;m< tables[j].i;m++){
193                                 if( tables[j].records[m].flag==1){
194                                     for(n=m+1;n< tables[j].i;n++){
195                                         if( tables[j].records[n].portion== tables[j].records[m].portion&& tables[j].records[n].d.name== tables[j].records[m].d.name){
196                                              tables[j].records[n].flag=0;
197                                              tables[j].records[m].num+= tables[j].records[n].num;
198                                         }
199                                     }
200                                 }
201                                 for (p=0; p<  tables[k].i; p++) {
202                                     if ( tables[k].records[p].portion ==  tables[j].records[m].portion &&  tables[k].records[p].d.name ==  tables[j].records[m].d.name) {
203                                          tables[k].records[p].flag = 0;
204                                          tables[j].records[m].num +=  tables[k].records[p].num;
205                                     }
206                                 }
207                             }
208                         }//if
209                     }
210                     else{
211                         for(m=0;m< tables[j].i;m++){
212                             if( tables[j].records[m].flag==1){
213                                 for(n=m+1;n< tables[j].i;n++){
214                                     if( tables[j].records[n].portion== tables[j].records[m].portion&& tables[j].records[n].d.name== tables[j].records[m].d.name){
215                                          tables[j].records[m].num+= tables[j].records[n].num;
216                                          tables[j].records[n].flag=0;
217                                     }
218                                 }
219                             }
220                             for (p=0; p<  tables[j].j; p++) {
221                                 if ( tables[j].recordsd[p].portion ==  tables[j].records[m].portion &&  tables[j].records[p].d.name ==  tables[j].records[m].d.name) {
222                                      tables[j].records[m].num +=  tables[j].recordsd[p].num;
223                                      tables[j].recordsd[p].flag = 0;
224                                 }
225                             }
226                         }
227                     }
228                 }
229             }
230         }
231         for(j=0;j<=i;j++){
232             for(k=0;k< tables[j].j;k++)
233                  tables[j].TotalPrice +=  tables[j].recordsd[k].d.getPrice( tables[j].recordsd[k].portion)* tables[j].recordsd[k].num;
234             for(k=0;k< tables[j].i;k++)
235                  tables[j].TotalPrice +=  tables[j].records[k].d.getPrice( tables[j].records[k].portion)* tables[j].records[k].num;
236             for(k=0;k< tables[j].j;k++)
237                  tables[j].TotalPricez+=panduan.jisuan( tables[j], tables[j].recordsd[k]);
238             for(k=0;k< tables[j].i;k++)
239                  tables[j].TotalPricez+=panduan.jisuan( tables[j], tables[j].records[k]);
240 
241             if( tables[j].flag==1)
242                 System.out.println("table"+" "+ tables[j].tablenum+": "+  tables[j].TotalPrice+" "+ tables[j].TotalPricez);
243         }
244     }
245 }
246 class Dish {
247     String name;
248     int unit_price;
249     public Dish(String name,int unit_price){
250         this.unit_price = unit_price;
251         this.name = name;
252     }
253     int flag=0;
254     public int getPrice(int portion){
255         int price1=unit_price;
256         if(portion==3)
257             price1=2*unit_price;
258         else if(portion==2)
259             price1=Math.round((float)(1.5*unit_price));
260         return price1;
261     }
262 }
263 class  Menu {
264     public Dish[] dishs=new Dish[30];
265     int f=0;
266     Dish searthDish(String dishName){
267         int flag=0;
268         int i;
269         for(i=f-1;i>=0;i--){
270             if(dishs[i].name.equals(dishName)){
271                 flag=1;
272                 break;
273             }
274             flag=0;
275         }
276 
277         if(!(flag != 1))
278             return dishs[i];
279 
280             return null;
281     }
282     public void addDish(String dishName,int unit_price,int flag){
283         dishs[f]=new Dish(dishName,unit_price);
284         dishs[f].flag=flag;
285         //f++;
286     }
287 }
288 class Record {
289     int tableNum;
290     Dish d;//菜品
291     int getPrice;
292     int orderNum;//序号\
293     int portion;//份额(1/2/3代表小/中/大份)\
294     int flag=1;
295     int num;
296 }
297 class Table {
298     Record[] records = new Record[20];
299     LocalDateTime localDateTime;
300     Record[] recordsd = new Record[20];
301     int TotalPrice;
302     int TotalPricez;
303     int i = 0;
304     int j = 0;
305     int tablenum;
306     int flag=1;
307     public Record addaRecord(Menu menu, int orderNum, String dishName, int portion, int num) {
308         Record record = new Record();
309         record.orderNum = orderNum;
310         record.num = num;
311         record.d = menu.searthDish(dishName);
312         record.portion = portion;
313         if ((record.d != null))
314             record.getPrice = record.d.getPrice(portion) * num;
315         return record;
316     }
317     public Record addaRecordd(Menu menu, int tablenum, int orderNum, String dishName, int portion, int num) {
318         Record record = new Record();
319         record.tableNum = tablenum;
320         record.orderNum = orderNum;
321         record.num = num;
322         record.d = menu.searthDish(dishName);
323         record.portion = portion;
324         if ((record.d != null))
325             record.getPrice = record.d.getPrice(portion) * num;
326         return record;
327     }
328     public int findRecordByNum(Table table, int orderNum, int i) {
329         for (int k = 0; k < i; k++)
330             if (!(table.records[k].orderNum != orderNum)) {
331                 if(table.records[k].num ==0){
332                     System.out.println("deduplication "+orderNum);
333                     return 1;
334 
335                 }else{
336                     table.records[k].num = 0; //这里做删除;
337                     return 1;}
338             }
339         return 0;
340     }//根据序号查找一条记录
341     public int findRecorddByNum(Table table, int orderNum, int j) {
342         for (int m = 0; m < j; m++)
343             if(table.records[m].num ==0){
344                 System.out.println("deduplication "+orderNum);
345                 return 1;
346             }else{
347                 table.records[m].num= 0; //这里做删除;
348                 return 1;}
349         return 0;
350     }//根据序号查找一条记录
351 }
352 class Panduan{
353 
354     int panduan(LocalDateTime localDateTime1,LocalDateTime localDateTime2) {
355         int h,m,s,time,h2,m2,s2,time2;
356         h = localDateTime1.getHour() * 10000;
357         m = localDateTime1.getMinute()*100;
358         s = localDateTime1.getSecond();
359         h2 = localDateTime2.getHour() * 10000;
360         m2 = localDateTime2.getMinute()*100;
361         s2 = localDateTime2.getSecond();
362         time = h+m+s;
363         time2 = h2+m2+s2;
364         int week1 = localDateTime1.getDayOfWeek().getValue();
365         int week2 = localDateTime2.getDayOfWeek().getValue();
366         if (week1 <= 5 && week1 >= 1 && week1 == week2 ) {
367             if (( time >= 170000 && time  <= 203000)&&(time2 >= 170000 && time2 <= 203000))
368                 return 1;
369             if ((time >= 103000 && time  <= 143000)&&(time2 >= 103000 && time2 <= 143000))
370                 return 1;
371         }
372         return 0;
373     }
374     int panduan1(LocalDateTime localDateTime1,LocalDateTime localDateTime2){
375         int h,m,s,time,h2,m2,s2,time2;
376         h = localDateTime1.getHour() * 10000;
377         m = localDateTime1.getMinute()*100;
378         s = localDateTime1.getSecond();
379         h2 = localDateTime2.getHour() * 10000;
380         m2 = localDateTime2.getMinute()*100;
381         s2 = localDateTime2.getSecond();
382         time = h+m+s;
383         time2 = h2+m2+s2;
384         int week1 = localDateTime1.getDayOfWeek().getValue();
385         int week2 = localDateTime2.getDayOfWeek().getValue();
386         if(week1<=7&&week1>=6&&week1 == week2) {
387             if (Math.abs(time - time2) < 10000)
388                 return 1;
389         }
390         return 0;
391     }
392 
393     int panduan2(LocalDateTime localDateTime){
394         int h,m,s,time;
395         h = localDateTime.getHour() * 10000;
396         m = localDateTime.getMinute()*100;
397         s = localDateTime.getSecond();
398         time = h+m+s;
399         int week = localDateTime.getDayOfWeek().getValue() ;
400         if(week<=5&&week>=1){
401             if((time >= 170000  && time  <= 203000 )||(time >= 103000 && time <= 143000))
402                 return 1;}
403         if(week<=7 && week>=6){
404             if(time >= 9.3 * 10000 && time <= 21.3*10000)
405                 return 1;
406         }
407         return 0;
408     }
409     int jisuan(Table table,Record record){
410         int h,m,s,time;
411         h = table.localDateTime.getHour()* 10000;
412         m = table.localDateTime.getMinute()*100;
413         s = table.localDateTime.getSecond();
414         time = h+m+s;
415         int week = table.localDateTime.getDayOfWeek().getValue();
416         if(record.d.flag==0){
417             if(week<=5&&week>=1){
418                 if (time >= 103000 && time <= 143000){
419                 return (int)Math.round(record.d.getPrice(record.portion)*record.num*0.6);
420                 }
421               else if(time>= 170000 && time <= 203000)
422                     return (int)Math.round(record.d.getPrice(record.portion)*record.num*0.8);
423 
424             }
425             else{
426                 return record.d.getPrice(record.portion)*record.num;
427             }
428         }
429         else{
430             if(week<=5&&week>=1)
431                 return (int)Math.round(record.d.getPrice(record.portion)*record.num*0.7);
432             else
433                 return record.d.getPrice(record.portion)*record.num;
434         }
435         return 1;
436     }
437 }

 类图如下:

 

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
代码长度限制
50 KB
时间限制
1000 ms
内存限制
 
 

 

  1 import java.io.BufferedReader;
  2 import java.io.IOException;
  3 import java.io.InputStreamReader;
  4 import java.time.LocalDate;
  5 import java.util.ArrayList;
  6 import java.time.format.DateTimeFormatter;
  7 import java.time.*;
  8 public class Main {
  9     public static void main(String[] args) throws IOException {
 10         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 11         Dish[] dishes = new Dish[20];
 12         SpecialDish[] specialDishes = new SpecialDish[20];
 13         Table table = new Table();
 14         Record[] records = new Record[20];
 15         PersonPay[] personPays = new PersonPay[10];
 16         Order order = new Order();
 17         Menu menu = new Menu();
 18         caicage cage = new caicage() ;
 19         lastPrint print = new lastPrint();
 20         inputmat mat = new inputmat() ;
 21         ArrayList<Table> tables = new ArrayList<>();
 22         int x = 0 , y = 0 , z = 0 , z1 = 0 , p = 0 ;
 23         String menuName , name , tableName , telephoneNum , taste;
 24         int tableNum , dishNum , portion , num , unit_price , deleteNum , tasteNum;
 25         boolean isHaveTable = false , forOther = false;
 26         boolean  falg = true;
 27         while(falg){
 28             String input = in.readLine();
 29             if(input.equals("end")){
 30                 if(x!=0){
 31                     table.order = order;
 32                     tables.add(x-1 , table);
 33                 }
 34 
 35                 break;
 36             }
 37             String[] getInput = input.split(" ");
 38             int flag = mat.inpumat(input);
 39             switch(flag) {
 40                 case 1: {
 41                     if (x != 0) {
 42                         table.order = order;
 43                         tables.add(x - 1, table);
 44                         table = new Table();
 45                         y = 0;
 46                     }
 47                     isHaveTable = true;
 48                     String  dayTime ;
 49                     String  hourTime ;
 50                     tableNum = Integer.parseInt(getInput[1]);
 51                     tableName = getInput[3];
 52                     telephoneNum = getInput[4];
 53                     dayTime = getInput[5];
 54                     hourTime = getInput[6];
 55                     table.tableNum = tableNum;
 56                     table.tableName = tableName;
 57                     table.telephoneNum = telephoneNum;
 58                     table.mainTime = dayTime;
 59                     table.remainTime = hourTime;
 60                     if (!table.timeJudgement1()) {
 61                         isHaveTable = false;
 62                         System.out.println("table " + table.tableNum + " out of opening hours");
 63                         continue;
 64                     }
 65                     order = new Order();
 66                     records = new Record[10];
 67                     if (x == 0) {
 68                         personPays[p] = new PersonPay();   //人名
 69                         personPays[p].name = tableName;
 70                         personPays[p].telephoneNum = telephoneNum;  //电话号码
 71                         p++;
 72                         print.p = p;   //应该输出多少次
 73                     } else {
 74                         for (int i = 0; i < p; i++) {
 75                             if (personPays[i].name.equals(tableName) && personPays[i].telephoneNum.equals(telephoneNum))
 76                                 break;  //判断是否已存在该人
 77                             if (i == p - 1) {
 78                                 personPays[p] = new PersonPay();
 79                                 personPays[p].name = tableName;
 80                                 personPays[p].telephoneNum = telephoneNum;
 81                                 p++;
 82                                 print.p = p;
 83                             }
 84                         }
 85                     }
 86                     System.out.println("table " + tableNum + ": ");
 87                     x++;
 88                     break;
 89                 }
 90                 case 2: {//普通菜单
 91                     int i = 0;
 92                     menuName = getInput[0];
 93                     unit_price = Integer.parseInt(getInput[1]);
 94                     if (z == 0) {
 95                         dishes[0] = menu.addDish(menuName, unit_price);
 96                         z++;
 97                     } else {
 98                         for (; i < z; i++) { //菜单覆盖问题
 99                             if (menuName.equalsIgnoreCase(dishes[i].name)) {
100                                 dishes[i].unit_price = unit_price;
101                                 break;
102                             }
103                         }
104                     }
105                     if (i == z) {
106                         dishes[z] = menu.addDish(menuName, unit_price);
107                         z++;
108                     }
109                     menu.dishes = dishes;
110                     menu.dishNum = z;
111                     break;
112                 }
113                 case 3:{  //特色菜
114                     int i = 0;
115                     menuName = getInput[0];
116                     unit_price = Integer.parseInt(getInput[2]);
117                     if (z1 == 0) { //初状态
118                         taste = getInput[1];
119                         specialDishes[0] = menu.addSpecialDish(menuName, unit_price, taste);
120                         z1++;
121                     } else {
122                         for (i=0; i < z1; i++) {
123                             if (menuName.equalsIgnoreCase(specialDishes[i].name)) {//覆盖问题
124                                 specialDishes[i].unit_price = unit_price;
125                                 break;
126                             }
127                         }
128                     }
129                     if (i == z1) {  //加入特色菜
130                         taste = getInput[1];
131                         specialDishes[z1] = menu.addSpecialDish(menuName, unit_price, taste);
132                         z1++;
133                     }
134                     menu.specialDishes = specialDishes;
135                     menu.dishNum1 = z1;
136                     break;
137                 }
138                 case 4: {
139                     if (!isHaveTable)
140                         continue;
141                     name = getInput[1];
142                     if (menu.searthDish(name) != null) {
143                         dishNum = Integer.parseInt(getInput[0]);
144                         portion = Integer.parseInt(getInput[2]);
145                         num = Integer.parseInt(getInput[3]);
146                         records[y] = new Record();
147                         records[y] = order.addARecord(dishNum, name, portion, num, menu);
148                         records[y].isSpecialDish = false;
149                         System.out.println(records[y].orderNum + " " + records[y].d.name + " " + records[y].getPrice());
150                         y++;
151                         order.records = records;
152                         order.dishNum = y;
153                     } else {
154                         System.out.println(name + " does not exist");
155                     }
156                     break;
157                 }
158                 case 5: {
159                     if (!isHaveTable)
160                         continue;
161                     name = getInput[1];
162                     if (menu.searthSpecialDish(name) != null) {
163                         tasteNum = Integer.parseInt(getInput[2]);
164                         num = Integer.parseInt(getInput[4]);
165                         int falg1 = mat.findcage(menu.searthSpecialDish(name).taste);
166                         if ((falg1 == 1 && tasteNum >= 0 && tasteNum <= 5)
167                                 || (falg1 == 2 && tasteNum >= 0 && tasteNum <= 4)
168                                 || (falg1 == 3 && tasteNum >= 0 && tasteNum <= 3)) {
169                             records[y] = new Record();
170                             dishNum = Integer.parseInt(getInput[0]);
171                             portion = Integer.parseInt(getInput[3]);
172                             records[y] = order.addASpecialRecord(dishNum, name, portion, num, menu, tasteNum, forOther);
173                             records[y].isSpecialDish = true;
174                             System.out.println(records[y].orderNum + " " + records[y].d.name + " " + records[y].getPrice());
175                             y++;
176                             order.records = records;
177                             order.dishNum = y;
178                         }
179                     } else {
180                         System.out.println(name + " does not exist");
181                         continue;
182                     }
183                     int flag1 = mat.findcage(menu.searthSpecialDish(name).taste);
184                     switch (flag1) {
185                         case 1: {
186                             mat.error(tasteNum ,flag1) ;
187                             break;
188                         }
189                         case 2: {
190                             mat.error(tasteNum ,flag1);
191                             break;
192                         }
193                         case 3: {
194                             mat.error(tasteNum ,flag1);
195                             break;
196                         }
197                         default:
198                             break;
199                     }
200                     break;
201                 }
202                 case 6: {
203                     if (!isHaveTable)
204                         continue;
205                     deleteNum = Integer.parseInt(getInput[0]);
206                     if (order.findRecordByNum(deleteNum) == 1) {
207                         order.delARecordByOrderNum(deleteNum);
208                     } else
209                         System.out.println("delete error;");
210                     break;
211                 }
212                 case 7: {
213                     /*if (!isHaveTable)
214                         continue;*/
215                     int t = Integer.parseInt(getInput[0]);
216                     name = getInput[2];
217                     for (int i = 0; i < x - 1; i++) {
218                         if (tables.get(i).tableNum == t) {
219                             if (menu.searthDish(name) != null) {
220                                 records[y] = new Record();
221                                 dishNum = Integer.parseInt(getInput[1]);
222                                 portion = Integer.parseInt(getInput[3]);
223                                 num = Integer.parseInt(getInput[4]);
224                                 records[y] = order.addARecord(dishNum, name, portion, num, menu);
225                                 records[y].isSpecialDish = false;
226                                 System.out.println(dishNum + " table " + table.tableNum + " pay for table " + t + " " + records[y].getPrice());
227                                 y++;
228                                 order.records = records;
229                                 order.dishNum = y;
230                             }
231                             break;
232                         }
233                     }
234                     break;
235                 }
236                 case 8:{
237                     /*if (!isHaveTable)
238                         continue;*/
239                     int t = Integer.parseInt(getInput[0]);
240                     name = getInput[2];
241                     tasteNum = Integer.parseInt(getInput[3]);
242                     int flag1 = mat.findcage(menu.searthSpecialDish(name).taste) ;
243                     for (int i = 0; i < x - 1; i++) {
244                         if (tables.get(i).tableNum == t) {
245                             if (menu.searthSpecialDish(name) != null) {
246                                 if (((flag1 == 1 && tasteNum >= 1 && tasteNum <= 5) ||
247                                         (flag1 == 2 && tasteNum <= 4)
248                                         || (flag1 == 3 && tasteNum <= 3))) {
249                                     records[y] = new Record();
250                                     dishNum = Integer.parseInt(getInput[1]);
251                                     portion = Integer.parseInt(getInput[4]);
252                                     num = Integer.parseInt(getInput[5]);
253                                     records[y] = order.addASpecialRecord(dishNum, name, portion, num, menu, tasteNum, !forOther);
254                                     records[y].isSpecialDish = true;
255                                     System.out.println(dishNum + " table " + table.tableNum + " pay for table " + t + " " + records[y].getPrice());
256                                     tables.get(i).giveTaste(name, num, tasteNum, menu);
257                                     y++;
258                                     order.records = records;
259                                     order.dishNum = y;
260                                 }
261                             }
262 
263                             break;
264                         }
265                     }
266                     break;
267                 }
268                 default :{
269                     System.out.println("wrong format");
270                     break;
271                 }
272             }
273         }
274         print.tables = tables;
275         print.personPays = personPays;
276         for (Table table1 : tables) {
277             table1.order.getTotalPrice();
278             table1.getTaste();
279             if(table1.spicyNum==0&&table1.acidityNum==0&&table1.sweetnessNum==0)
280                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice()+" ");
281             if(table1.spicyNum!=0&&table1.acidityNum==0&&table1.sweetnessNum==0)
282                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 川菜 " +(int)table1.spicyNum+" "+mat.spicyLevel(table1.averageSpicy ));
283             if(table1.spicyNum==0&&table1.acidityNum!=0&&table1.sweetnessNum==0)
284                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 晋菜 " +(int)table1.acidityNum+" "+mat.acidityLevel(table1.averageAcidity ));
285             if(table1.spicyNum==0&&table1.acidityNum==0&&table1.sweetnessNum!=0)
286                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 浙菜 " +(int)table1.sweetnessNum+" "+ mat.sweetnessLevel(table1.averageSweetness ));
287             if(table1.spicyNum!=0&&table1.acidityNum!=0&&table1.sweetnessNum==0)
288                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 川菜 " +(int)table1.spicyNum+" "+mat.spicyLevel(table1.averageSpicy )+" 晋菜 "+(int)table1.acidityNum+" "+mat.acidityLevel(table1.averageAcidity ) );
289             if(table1.spicyNum!=0&&table1.acidityNum==0&&table1.sweetnessNum!=0)
290                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 川菜 " +(int)table1.spicyNum+" "+mat.spicyLevel(table1.averageSpicy )+" 浙菜 "+(int)table1.sweetnessNum+" "+mat.sweetnessLevel(table1.averageSweetness ) );
291             if(table1.spicyNum==0&&table1.acidityNum!=0&&table1.sweetnessNum!=0)
292                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 晋菜 " +(int)table1.acidityNum+" "+mat.acidityLevel(table1.averageAcidity ) +" 浙菜 "+(int)table1.sweetnessNum+" "+mat.sweetnessLevel(table1.averageSweetness ));
293             if(table1.spicyNum!=0&&table1.acidityNum!=0&&table1.sweetnessNum!=0)
294                 System.out.println("table " + table1.tableNum + ": " + (table1.order.allCommonPrice + table1.order.allSpecialPrice) + " " + table1.tablePrice() + " 川菜 " +(int)table1.spicyNum+" "+mat.spicyLevel(table1.averageSpicy )+" 晋菜 "+(int)table1.acidityNum+" "+mat.acidityLevel(table1.averageAcidity ) +" 浙菜 "+(int)table1.sweetnessNum+" "+mat.sweetnessLevel(table1.averageSweetness ));
295         }
296         print.payPrint();
297     }
298 }
299 class Dish{
300     String name;
301     String taste;
302     int unit_price;
303     public int getPrice(int portion){
304         int Price=0;
305         if(portion==1)
306             Price=unit_price;
307         else if(portion==3)
308             Price=unit_price*2;
309         else if(portion==2)
310             Price=(int)Math.round((float)unit_price*1.5);
311         return Price;
312     }
313 }
314 class SpecialDish extends Dish{
315 }
316 class Menu{
317     Dish[] dishes;
318     SpecialDish[] specialDishes;
319     int dishNum;
320     int dishNum1;
321     public Dish searthDish(String dishName){
322         for(int k=0;k<dishNum;k++) {
323             if(dishName.equals(dishes[k].name)){
324                 return dishes[k];
325             }
326         }
327         return null;
328     }
329     public SpecialDish searthSpecialDish(String dishName){
330         for(int k=0;k<dishNum1;k++) {
331             if(dishName.equals(specialDishes[k].name)){
332                 return specialDishes[k];
333             }
334         }
335         return null;
336     }
337     public Dish addDish(String dishName,int unit_price){
338         Dish addDish=new Dish();
339         addDish.name=dishName;
340         addDish.unit_price=unit_price;
341         return addDish;
342     }
343     public SpecialDish addSpecialDish(String dishName,int unit_price,String taste){
344         SpecialDish addDish=new SpecialDish();
345         addDish.name=dishName;
346         addDish.unit_price=unit_price;
347         addDish.taste=taste;
348         return addDish;
349     }
350 }
351 class Record{
352     int orderNum;
353     Dish d;
354     int portion;
355     int num;
356     int spicy;
357     int acidity;
358     int sweetness;
359     boolean isSpecialDish;
360     public int getPrice() {
361         return d.getPrice(portion)*num;
362     }
363 }
364 class Order {
365     int dishNum;
366     int allCommonPrice;
367     int allSpecialPrice;
368     Record[] records = new Record[30] ;
369 
370     public void getTotalPrice(){
371         for(int k=0;k<dishNum;k++) {
372             if(!records[k].isSpecialDish)
373                 allCommonPrice+=records[k].getPrice();
374             else
375                 allSpecialPrice+=records[k].getPrice();
376         }
377     }
378     public int getTotalPrice(double commonDiscount,double specialDiscount){
379         int all = 0;
380         for(int k=0;k<dishNum;k++) {
381             if(!records[k].isSpecialDish)
382                 all+=Math.round(records[k].getPrice()*commonDiscount);
383             else
384                 all+=Math.round(records[k].getPrice()*specialDiscount);
385         }
386         return all;
387     }
388     public Record addARecord(int orderNum,String dishName,int portion,int num , Menu menu){
389         Record order=new Record();
390         order.d=menu.searthDish(dishName);
391         order.orderNum=orderNum;
392         order.portion=portion;
393         order.num=num;
394         return order;
395     }
396     public Record addASpecialRecord(int orderNum,String dishName,int portion,int num ,Menu menu,int tasteNum , boolean forOther){
397         Record order=new Record();
398         order.d=menu.searthSpecialDish(dishName);
399         order.orderNum=orderNum;
400         order.portion=portion;
401         order.num=num;
402         if(!forOther) {
403             inputmat mt = new inputmat();
404             int flag = mt.findcage(menu.searthSpecialDish(dishName).taste);
405             switch (flag) {
406                 case 1: {
407                     order.spicy = tasteNum;
408                     order.acidity = -1;
409                     order.sweetness = -1;
410                     break;
411                 }
412                 case 3: {
413                     order.spicy = -1;
414                     order.acidity = -1;
415                     order.sweetness = tasteNum;
416                     break;
417                 }
418                 case 2: {
419                     order.spicy = -1;
420                     order.acidity = tasteNum;
421                     order.sweetness = -1;
422                     break;
423                 }
424                 default :
425                     break;
426             }
427         }
428         else{
429             order.spicy = -1;
430             order.acidity = -1;
431             order.sweetness = -1;
432         }
433 
434         return order;
435     }
436     public void delARecordByOrderNum(int orderNum){
437         for(int k=0;k<dishNum;k++) {
438             if(orderNum==records[k].orderNum) {
439                 records[k].num=0;
440             }
441         }
442     }
443     public int findRecordByNum(int orderNum){
444         for(int k = 0; k<dishNum;k++) {
445             if(records[k].orderNum==orderNum) {
446                 return 1;
447             }
448         }
449         return 0;
450     }
451 }
452 class Table{
453     int tableNum;
454     String tableName;
455     String telephoneNum;
456     String mainTime;
457     String remainTime;
458     Order order;
459     double commonDiscount;
460     double specialDiscount;
461     int averageSpicy;
462     int averageAcidity;
463     int averageSweetness;
464     double spicyNum;
465     double acidityNum;
466     double sweetnessNum;
467     int week;
468     public boolean timeJudgement1(){
469         String[] time1 = remainTime.split("/");
470         String[] date = mainTime.split("/");
471         inputmat mat = new  inputmat();
472         int hour = Integer .parseInt(time1[0]);
473         int  minute = Integer.parseInt(time1[1]) ;
474         int  second = Integer .parseInt(time1[2]) ;
475         int time = hour*10000  + minute*100 + second ;
476        week = mat.datetime(mainTime) ;
477        /* LocalDate date = LocalDate.of(year,month,day);
478         int weekOfDay = date.getDayOfWeek().getValue();*/
479         if (week == 6 || week == 7) {
480             if (time>=93000 && time <=213000) {
481                 specialDiscount = commonDiscount = 1;
482                 return true;
483             }
484             else {
485                 return false;
486             }
487         }
488         if (week >= 1 && week <= 5) {
489             if (time >=103000 && time <= 143000) {
490                 commonDiscount = 0.6;
491                 specialDiscount = 0.7;
492                 return true;
493             }
494             else if (time >= 170000 && time <= 203000) {
495                 commonDiscount = 0.8;
496                 specialDiscount = 0.7;
497                 return true;
498             }
499             else {
500                 return false;
501             }
502         }
503         return false;
504     }
505     public int tablePrice(){
506         return this.order.getTotalPrice(commonDiscount,specialDiscount);
507     }
508     public void getTaste(){
509         for(int k = 0;k<this.order.dishNum;k++){
510             if(order.records[k].isSpecialDish){
511                 if(order.records[k].acidity!=-1){
512                     averageAcidity+=order.records[k].acidity*order.records[k].num;
513                     acidityNum=order.records[k].num+acidityNum;
514                 }
515                 if(order.records[k].spicy!=-1){
516                     averageSpicy+=order.records[k].spicy*order.records[k].num;
517                     spicyNum=order.records[k].num+spicyNum;
518                 }
519                 if(order.records[k].sweetness!=-1){
520                     averageSweetness+=order.records[k].sweetness*order.records[k].num;
521                     sweetnessNum=order.records[k].num+sweetnessNum;
522                 }
523             }
524         }
525         if(spicyNum!=0){
526             averageSpicy=(int)Math.round(averageSpicy/spicyNum);
527         }
528         if(sweetnessNum!=0){
529             averageSweetness=(int)Math.round(averageSweetness/sweetnessNum);
530         }
531         if(acidityNum!=0){
532             averageAcidity=(int)Math.round(averageAcidity/acidityNum);
533         }
534 
535     }
536     public void giveTaste(String name , int num , int tasteLevel , Menu menu){
537         inputmat mt = new inputmat() ;
538         int flag = mt.findcage(menu.searthSpecialDish(name).taste) ;
539         switch(flag) {
540             case 1 :{
541                 averageSpicy += tasteLevel * num;
542                 spicyNum += num;
543                 break;
544             }
545             case 2: {
546                 averageAcidity += tasteLevel * num;
547                 acidityNum += num;
548                 break;
549             }
550             case 3: {
551                 averageSweetness += tasteLevel * num;
552                 sweetnessNum += num;
553                 break;
554             }
555         }
556     }
557 }
558 class lastPrint{
559     ArrayList<Table> tables = new ArrayList<>();
560     PersonPay[] personPays;
561     int p;
562     inputmat mat = new inputmat() ;
563     public void payPrint(){
564         for(int k=0;k<p;k++){
565             for(Table table:tables){
566                 if(table.tableName.equals(personPays[k].name)){
567                     personPays[k].payPrice+=table.tablePrice();
568                 }
569             }
570         }
571         for(int k=0;k<p-1;k++){
572             for(int l=k+1;l<p;l++){
573                 if(personPays[k].name.toLowerCase().compareTo(personPays[l].name.toLowerCase())>0) {
574                     PersonPay x;
575                     x=personPays[l];
576                     personPays[l]=personPays[k];
577                     personPays[k]=x;
578                 }
579             }
580         }
581         for(int k=0;k<p;k++){
582             System.out.println(personPays[k].name+" "+personPays[k].telephoneNum+" "+personPays[k].payPrice);
583         }
584     }
585 }
586 class PersonPay{
587     String name;
588     String telephoneNum;
589     int payPrice;
590 }
591 class caicage{
592 
593 }
594 class inputmat{
595     String name1 = "川菜";
596     String name2 = "晋菜";
597     String name3 = "浙菜";
598     int flag ;
599     int datetime(String time){
600         DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/M/d");
601         LocalDate date1= LocalDate.parse(time , formatter1); // 将字符串解析为LocalDate对象
602         DayOfWeek dayOfWeek = date1.getDayOfWeek();
603          int week =0;
604         if(dayOfWeek ==DayOfWeek.WEDNESDAY ) {
605             week = 3;
606             return week;
607         }
608         else if(dayOfWeek ==DayOfWeek.MONDAY) {
609             week = 1;
610             return week;
611         }
612         else if(dayOfWeek ==DayOfWeek.TUESDAY){
613             week = 2;
614         return week;
615         }
616         else if(dayOfWeek ==DayOfWeek.THURSDAY){
617             week = 4;
618         return week;}
619         else if(dayOfWeek ==DayOfWeek.FRIDAY) {
620             week = 5;
621             return week;
622         }
623         else if(dayOfWeek ==DayOfWeek.SATURDAY) {
624             week = 6;
625             return week;
626         }
627         else if(dayOfWeek ==DayOfWeek.SUNDAY) {
628             week = 7;
629             return week;
630         }
631         return 0;
632     }
633     int findcage(String s) {
634         flag = 0;
635         if (s.matches(name1)) {
636             flag = 1;
637             return flag;
638         }
639         else if(s.matches(name2) ){
640             flag = 2;
641             return flag;
642         }
643         else if(s.matches(name3) ){
644             flag = 3;
645             return flag;
646         }
647         return flag;
648     }
649     public String spicyLevel(int averageSpicy){
650         if(averageSpicy==0)
651             return "不辣";
652         if(averageSpicy==1)
653             return "微辣";
654         if(averageSpicy==2)
655             return "稍辣";
656         if(averageSpicy==3)
657             return "辣";
658         if(averageSpicy==4)
659             return "很辣";
660         if(averageSpicy==5)
661             return "爆辣";
662         return null;
663     }
664     public String acidityLevel(int averageAcidity){
665         if(averageAcidity==0)
666             return "不酸";
667         else if(averageAcidity==1)
668             return "微酸";
669         else  if(averageAcidity==2)
670             return "稍酸";
671         else if(averageAcidity==3)
672             return "酸";
673         else if(averageAcidity==4)
674             return "很酸";
675         return null;
676     }
677     public String sweetnessLevel(int averageSweetness){
678         if(averageSweetness==0)
679             return "不甜";
680         if(averageSweetness==1)
681             return "微甜";
682         if(averageSweetness==2)
683             return "稍甜";
684         if(averageSweetness==3)
685             return "甜";
686         return null;
687     }
688     String p1 = "([1-9][0-9]*)";
689     String p2 = "(\\S+)";
690     String p3 = "(delete)";
691     String p4 = "([0-9]*)";
692     String p5 = "T";
693     String table = "^(table)( )([1-9][0-9]*)( )(:)( )(\\S+)( )((136|133|135|180|181|189)[0-9]{8})( )([0-9]{4})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})( )([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})(/)([0-9]|[0-9]{2})$";
694     String menu1 = p2+"( )([1-9][0-9]*)$";
695     String menu2 = "^(\\S+)( )(\\S+)( )([1-9][0-9]*)( )"+p5;
696     String oeder1 = "^([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$";
697     String oder2 = "^([1-9][0-9]*)( )(\\S+)( )([0-9]+)( )([1-9][0-9]*)( )([1-9][0-9]*)$";
698     String oder3 = p1+" "+p3 ;//"([1-9][0-9]*)( )(delete)";
699     String oder4 = "^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$";
700     String oder5 = "^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )"+p4+"( )([1-9][0-9]*)( )([1-9][0-9]*)$";
701     int inpumat(String s){
702         if(s.matches(table) )
703             return 1;
704         else if(s.matches(menu1) )
705             return 2;
706         else if(s.matches(menu2) )
707             return 3;
708         else if(s.matches(oeder1) )
709             return 4;
710         else if(s.matches(oder2) )
711             return 5;
712         else if(s.matches(oder3) )
713             return 6;
714         else if(s.matches(oder4) )
715             return 7;
716         else if(s.matches(oder5) )
717             return 8;
718         return 0;
719     }
720     void error(int a ,int flag){
721         if((a < 0 || a > 5)&&flag==1){
722             System.out.println("spicy num out of range :" + a);
723         }
724         else if ((a < 0 || a > 4)&&flag==2)
725             System.out.println("acidity num out of range :" + a);
726         else if ((a < 0 || a > 3)&&flag==3)
727             System.out.println("sweetness num out of range :" + a);
728     }
729 }

类图如下:

 

期中考试:

我认为期中考试题目不是很难,但是会有一些你在课上听过但是还没有尝试,还导致你错误还有就是要按照题目要求写,不按照题目要求写,可能都是对的,但是分数依然是零分

7-1 测验1-圆类设计
分数 10
作者 段喜龙
单位 南昌航空大学

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

输入格式:

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

输出格式:

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

输入样例:

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

2.35

输出样例:

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

17.35
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
 1 import java.util.*;
 2 
 3 public class Main {
 4 
 5 
 6     public static void main(String[] args) {
 7 
 8         Scanner in=new Scanner(System.in);
 9 
10         double a,b;
11         a=in.nextDouble();
12         b=a*a*Math.PI;
13 
14         if(a>0)System.out.printf("%.2f", b);
15 
16         else
17             System.out.printf("Wrong Format");
18 
19 
20 
21     }
22 
23 
24 
25 }

其实没啥好写的

7-2 测验2-类结构设计
分数 10
作者 段喜龙
单位 南昌航空大学

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


image.png

输入格式:

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

输出格式:

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

输入样例:

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

6 5.8 -7 8.9

输出样例:

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

40.30
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.*;
public class Main {
    private double x1;
    private double y1;
    private double x2;
    private double y2;

    public Main(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    public double getArea() {
        double width = Math.abs(x2 - x1);
        double height = Math.abs(y2 - y1);
        return width * height;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();

        Main rectangle = new Main(x1, y1, x2, y2);
        double area = rectangle.getArea();
        System.out.format("%.2f%n", area);
    }
}

这个题会看你的类结构和是否使用double类型否者是答案错误

7-3 测验3-继承与多态
分数 20
作者 段喜龙
单位 南昌航空大学

将测验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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.*;
public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch(choice) {
            case 1:
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                if(radiums>0)printArea(circle);
                else System.out.printf("Wrong Format");

                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;
        }
    }
    private static void printArea(Shape rectangle) {
        System.out.printf("%.2f",rectangle.getArea());
    }
}
  class Shape 
{
    public double getArea() {
        return 0;
    }
}
 class Circle extends Shape
{
    double r;
    public Circle(double radiums) {
        r=radiums;
    }
    public double getArea()
    {
        return r*r*Math.PI;
    }
    
}
class Point{
    double x,y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

}
class Rectangle extends Shape {
    double lpPoint,lRightPoint;

    public double getArea() {
        return lpPoint * lRightPoint;
    }

    public Rectangle(Point leftTopPoint, Point lowerRightPoint) {
        this.lpPoint = Math.abs(leftTopPoint.x - lowerRightPoint.x);
        this.lRightPoint = Math.abs(leftTopPoint.y - lowerRightPoint.y);
    }
}
7-4 测验4-抽象类与接口
分数 20
作者 段喜龙
单位 南昌航空大学

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

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

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

        int choice = input.nextInt();

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

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

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

输入格式:

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

输入图形所需参数

输出格式:

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

输入样例:

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

1
2.3
2
3.2
3
6
5
1
2.3
0

输出样例:

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

5.60 16.62 16.62 
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();

        int choice = input.nextInt();

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

        list.sort(Comparator.naturalOrder()); // 正向排序
        for (int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }
    }
}
class Point {
    private double x,y;

    public double getY() {
        return y;
    }
    public double getx() {
        return x;
    }
    public Point(double a, double b) {
        y = b;
        x = a;
    }

}
interface Shape extends Comparable<Shape>{
    default double getArea(){
        return 0;
    };
}

class Rectangle implements Shape {
    private Point lpTop,lRight;
    public Rectangle(Point leftTop, Point lowerRight) {
        lpTop = leftTop;
        lRight = lowerRight;
    }
    public double getArea() {
        double f;
        f=lRight.getx() - lpTop.getx();
        double m = lRight.getY() - lpTop.getY();
        if(f<0)
            f = -f;
        if(m <0)
            m=-m;
        return Math.abs(f * m) ;
    }

    public int compareTo(Shape b) {
        double d = this.getArea() - b.getArea();
        if (d > 0) {
            return 1;
        }
        if (d < 0) {
            return -1;
        }
        return 0;
    }
}

class Circle implements  Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }
    public double getArea() {
        return  radius * radius * Math.PI;
    }
    public int compareTo(Shape other) {
        double d = this.getArea() - other.getArea();
        if (d> 0) {
            return 1;
        }
        if (d < 0) {
            return -1;
        }

        return 0;
    }
}

 

三、踩坑心得

  对于这几次作业,遇到了非常多问题,大多是因为自身基础知识不扎实而导致的。常见的有因为一开始没有意识到对冗余点进行排除的算法的重要性,导致先前代码有许多可替换部分,使用判断形状函数不仅可以使代码简洁合理,更能完善算法,考虑情况更加全面。另外代码重复性高也是这几次作业的通病,没有好好思考如何合理排布代码,灵活运用方法将代码简洁化。

四、改进建议

  应在程序中更加凸显类与对象这一概念,有许多可由之前写过的代码衍生而来的算法都未很好的利用起来,没有明确的类的定义,导致后面要用之前的算法时只能复制黏贴这样的笨办法,应多学习如何通过类之间的关系做到灵活调用方法,为代码的可移植性打好基础。实际上题目的层次关系十分明显,就是为了引导我们能够实现方法的调用,以后做题学习时应多注意此类关系紧密的类型,尽力完善方法间的套用关系。完善类的同时,算法的优化也十分重要,算法优化不仅可以节省时间提高效率,还可以使程序更加简洁美观。

五、总结

 

通过这几次作业的学习,我初步认识到了java面向对象的编码理念,掌握了一些基本的类的操作,通过对象来管理属性,对数据格式的基础判断,java一些快捷语句的使用等等。同时也发现了自身许多问题,类的具象化还是不够完整清楚,不能让人直观明了,导致代码繁琐,复杂度高,阅读起来很困难,这都是需要改进的地方。老师课程与慕课等平台课程结合学习可以使学习效率最大化,老师也可以在课堂上与慕课内容互动联系,使学生更加印象深刻。作业的质量很高,难度适中且具一定挑战性,希望老师能继续出类似的高质量题目,为我们学习成长夯实基础,铸就典例。

7-3 测验3-继承与多态
分数 20
作者 段喜龙
单位 南昌航空大学

将测验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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
 
Java (javac)
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
importjava.util.*;
publicclassMain{
publicstaticvoidmain(String[]args){
 
Scannerinput=newScanner(System.in);
 
intchoice=input.nextInt();
 
switch(choice){
case1:
doubleradiums=input.nextDouble();
Shapecircle=newCircle(radiums);
if(radiums>0)printArea(circle);
elseSystem.out.printf("WrongFormat");
 
break;
case2://Rectangle
doublex1=input.nextDouble();
doubley1=input.nextDouble();
doublex2=input.nextDouble();
doubley2=input.nextDouble();
 
PointleftTopPoint=newPoint(x1,y1);
PointlowerRightPoint=newPoint(x2,y2);
 
Rectanglerectangle=newRectangle(leftTopPoint,lowerRightPoint);
 
printArea(rectangle);
break;
}
}
privatestaticvoidprintArea(Shaperectangle){
System.out.printf("%.2f",rectangle.getArea());
}
}
classShape