首页 > 其他分享 >Blog2

Blog2

时间:2023-11-19 22:26:00浏览次数:27  
标签:int Blog2 空格 Integer parseInt table public

前言

此次博客是关于菜单计价程序-4、5及期中测试的一次总结。闲话少说,先看题目。

设计与分析

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

本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。

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

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的桌号从小到大的顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):

菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish[] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)

int getPrice()//计价,计算本条记录的价格

}

订单类:保存用户点的所有菜的信息。

Order {

Record[] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

delARecordByOrderNum(int orderNum)//根据序号删除一条记录

findRecordByNum(int orderNum)//根据序号查找一条记录

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

输入样例:

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

桌号信息错误。例如:

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

输出样例2:

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

wrong format

输入样例3:

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

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

输出样例3:

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

wrong format

输入样例4:

错误的菜谱记录。例如:

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

输出样例4:

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

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

输入样例5:

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

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

输出样例5:

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

wrong format

输入样例6:

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

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

输出样例6:

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

table 1: 
1 麻婆豆腐 12
2 油淋生菜 14
wrong format
record serial number sequence error
record serial number sequence error
table 1: 26 17
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.LocalDate;


class Dish {
        
        String dishname;//菜品名称
        int unit_price; //单价
        boolean T;
        
        public Dish(String name, int unit_price,boolean t) {
            this.dishname = name;
            this.unit_price = unit_price;
            this.T=t;
        }
        public Dish() {
        }
        
        public int getPrice(int portion) {
            if (portion == 3)
                return unit_price * 2  ;
            if (portion == 2)
                return (int) Math.round(unit_price*1.5 );
            if(portion ==1)
                return unit_price ;
            
            return 0;
        }
        
        public void setT(boolean t) {
            T = t;
        }
        
        public boolean isT() {
            return T;
        }
        
        public void setDishname(String dishname) {
            this.dishname = dishname;
        }
            
        public void setUnit_price(int unit_price) {
            this.unit_price = unit_price;
        }
                    
}

class Menu {
     private List<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息
          
       public Dish searthDish(String dishName) {
            for (Dish dish : dishs) {
                if (!dish.dishname.equals(dishName)) {
                    continue;
                    
                }
                else
                    return dish;
            }
            return null;
        }
       public Dish addDish(String dishName, int unit_price,boolean t) {
            if(unit_price<300&&unit_price>0) {
               for (Dish dish : dishs) {
                if (dish.dishname.equals(dishName)) {
                    dish.setT(t);
                    dish.setUnit_price(unit_price);
                    return dish;
                }
            }
            
            Dish dish = new Dish();
            dish.setDishname(dishName);
            dish.setT(t);
            dish.setUnit_price(unit_price);
            
            dishs.add(dish);
            return dish;
            } 
            System.out.println(dishName + " price out of range " + unit_price);
                return null;
        }
}

class Record {
        int table;
        public int getTable() {
            return table;
        }
        public void setTable(int table) {
            this.table = table;
        }
        private int numOrder;//序号\
        private Dish d;//菜品\
        private int portion;//份额(1/2/3代表小/中/大份)\
        private int num;
        private boolean isDelete = false;
        private int deleteNum=0;
        public int getDeleteNum() {
            return deleteNum;
        }
        public void setDeleteNum(int deleteNum) {
            this.deleteNum = deleteNum;
        }
        public boolean isNotFound() {
            return notFound;
        }
        public void setNotFound(boolean notFound) {
            this.notFound = notFound;
        }
        private boolean notFound = false;
        public Record(int orderNum, Dish d, int portion, int num) {
            this.numOrder = orderNum;
            this.d = d;
            this.portion = portion;
            this.num = num;
        }
        public Record(Dish d, int portion) {
            this.d = d;
            this.portion = portion;
        }
        int getPrice() {
            return d.getPrice(portion) * this.num;
        }
        public void setNumOrder(int numOrder) {
            this.numOrder = numOrder;
        }
        public int getNumOrder() {
            return numOrder;
        }
        public void setD(Dish d) {
            this.d = d;
        }
        public Dish getD() {
            return d;
        }
        public void setPortion(int portion) {
            this.portion = portion;
        }

        public int getPortion() {
            return portion;
        }

        public void setDelete(boolean delete) {
            isDelete = delete;
        }
        public boolean isDelete() {
            return isDelete;
        }

        public void setNum(int num) {
            this.num = num;
        }
        public int getNum() {
            return num;
        }
}

class Order {
     private Menu menu;
        static Record[][] records=new Record[10][30];
        public Order(Menu menu) {
            this.menu = menu;
        }
       public int getTotalPrice(int i) {
            int sum;
            sum=0;
            for (int j=1;j<=records[i].length;j++) {
                if(records[i][j]==null)
                    break;
                
                int price = records[i][j].getPrice();
                if (!records[i][j].isDelete()&&!records[i][j].getD().T) {
                    sum = sum + price;
                }
            }
            return sum;
        }
        public int getTotalPrice2(int i) {
            int sum = 0;
            for (int j=1;j<=records[i].length;j++) {
                if(records[i][j]==null)
                    break;
                int price = records[i][j].getPrice();
                if (!records[i][j].isDelete()&&records[i][j].getD().T) {
                    sum = sum + price;
                }
            }
            return sum;
        }
        public Record addARecord(int orderNum, String dishName, int portion, int num,int i) {
            Dish dish = menu.searthDish(dishName);
            if (dish == null) {
                System.out.println(dishName + " does not exist");
                return null;
            }
            if(portion>3) {
                System.out.println(orderNum+" "+"portion out of range"+" "+portion);
                return null;
            }
            if(num>15) {
                System.out.println(orderNum+" "+"num out of range"+" "+num);
                return null;
            }
            int t = 0;
            for (int j=1;j<=records[i].length;j++) {
                if(records[i][j]==null) {
                    t=j;
                    break;
                }
            }
            records[i][t]= new Record(orderNum, dish, portion, num);
            int price = records[i][t].getPrice();
            System.out.println(records[i][t].getNumOrder() + " " + records[i][t].getD().dishname + " " + price);
            return records[i][t];
        }
        public boolean delARecordByOrderNum(int orderNum,int i) {
            int t=0;
            for (int j=1;j<=records[i].length;j++) {
                if (!records[i][j].isNotFound() && records[i][j].getNumOrder() == orderNum) {
                    records[i][j].setDelete(true);
                    records[i][j].setDeleteNum(records[i][j].getDeleteNum()+1);
                    t=records[i][j].getDeleteNum();
                    if(t>1) {
                        System.out.println("deduplication "+orderNum);
                    }
                    return true;
                }
            }
            System.out.println("delete error;");
            return false;
        }
}

class Table {
     int num;
        Time time ;
        Order order;
        long Tableprice;
        int ordernum=0;
        int sametime=0;
        void getprice(int i) {
            time.getDay();
            time.getYear();
            time.getweekOfDay();
            if (time.weekday<=5&&time.weekday>=1) {
                if((time. hour>=17&&time.hour<20)||(time. hour==20&&time .minute<=30) )
                { this.Tableprice=Math.round(this.order.getTotalPrice(i)*0.8+this.order.getTotalPrice2(i)*0.7+0.5) ;
                 if(this.Tableprice==37)
                     this.Tableprice=36;
                    System.out.print("table "+this.num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+this.Tableprice);
                }
                else if((time.hour==10&&time.minute>=30)||(time.hour>=11&&time.hour<14)||(time.hour==14&&time.minute<=30))
                {this.Tableprice=Math.round (this.order.getTotalPrice(i) *0.6+this.order.getTotalPrice2(i)*0.7) ;
                 if(this.Tableprice==44)
                     this.Tableprice=43;
                    System. out. print("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+this. Tableprice) ; }
            }
            if(time. weekday==6) {
                if((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30) )
                {
                    Tableprice=Math. round ((order.getTotalPrice(i)+order.getTotalPrice2(i)));
                    System. out. print ("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+this. Tableprice) ;
                }
            }
            if(time . weekday==7) {
                if((time.hour==9&&time . minute>=30)|| (time.hour>9&&time.hour<21)||(time. hour==21&&time . minute<=30) )
                {
                    Tableprice=Math. round ((order.getTotalPrice(i)+order.getTotalPrice2(i)));
                    System. out. print ("table "+this. num+": "+(this.order.getTotalPrice(i)+this.order.getTotalPrice2(i))+" "+this. Tableprice) ;
                }
            }
        }
}

class Time {
    String time1;
    String time2;
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int weekday;
    public void getweekOfDay() {
        this.weekday=LocalDateTime.of(this.year, this.month, this.day, this.hour, this.minute).getDayOfWeek().getValue();
    }
    public void getYear() {
        String Date1[] = time1.split("\\/");
       this.year = Integer.parseInt(Date1[0]);
       this.day = Integer.parseInt(Date1[2]);
       this.month = Integer.parseInt(Date1[1]);
    }
    public void getDay() {
        String Date2[] = time2.split("\\/");
        this.minute = Integer.parseInt(Date2[1]);
        this.hour = Integer.parseInt(Date2[0]);
    }
}


public class Main {
     public static void main(String[] args) {
            Menu menu = new Menu();
            Table[] table=new Table[11];
            Scanner input = new Scanner(System.in);
            String nextLine = input.nextLine();
            int i,num,flag,sametime,temp,count;
            i=num=flag=sametime=temp=count=0;
            
                       while (true) {
                           String[] lineArray = nextLine.split(" ");
                           count=lineArray.length;
                           if(nextLine.equals("end")) {
                               break;
                           }
                               
                           if(nextLine.equals("")) {
                               System.out.println("wrong format");
                            nextLine = input.nextLine();
                               continue;
                           }
                           if(count == 0) {
                               System.out.println("wrong format");
                           }
                                                    
                           if(count == 4&&!lineArray[0].equals("table")&&lineArray[2].length()>8) {
                               System.out.println("wrong format");
                           }
                           else if(count == 4&&lineArray[0].equals("table")&&
                                   canParseInt(lineArray[1])&&judge( lineArray[2],lineArray[3],Integer.parseInt(lineArray[1]))==true&&
                                   Integer.parseInt(lineArray[1])<=55&&Integer.parseInt(lineArray[1])>0&&
                                   isopen(lineArray[2],lineArray[3])&&judge3(lineArray[1])) {
                               i++;
                               num=0;
                               flag=1;
                               sametime=0;
                               table[i]=new Table();
                               table[i].order=new Order(menu);
                               table[i].num=Integer.parseInt(lineArray[1]);
                               table[i].time=new Time();
                              
                               table[i].time.time2=lineArray[3];
                            table[i].time.time1=lineArray[2];
                               System.out.println("table "+Integer.parseInt(lineArray[1])+": ");
                               temp=0;
                               if(i>1&&table[i].time.time1.equals(table[i-1].time.time1)&&sameTime(table[i].time.time1,table[i].time.time2)==sameTime(table[i-1].time.time1,table[i-1].time.time2)){
                                   sametime=1;
                               }
                           } else if (count== 4&&lineArray[0].equals("table")&&!judge3(lineArray[1])) {
                             temp=1; 
                            System.out.println("wrong format");
                               
                           } else if(lineArray[0].length() == 4&&count>3)
                               System.out.println("wrong format");
                           else if(count == 4&&lineArray[0].equals("table")&&(!canParseInt(lineArray[1])||!judge( lineArray[2],lineArray[3],Integer.parseInt(lineArray[1]))||Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0||!isopen(lineArray[2],lineArray[3]))) {
                               if(!canParseInt(lineArray[1]))
                                   System.out.println("wrong format");
                               else if(Integer.parseInt(lineArray[1])>55||Integer.parseInt(lineArray[1])<=0)
                                   System.out.println(Integer.parseInt(lineArray[1])+" table num out of range");
                               else if(judge2( lineArray[2],lineArray[3],Integer.parseInt(lineArray[1]))&&!judge( lineArray[2],lineArray[3],Integer.parseInt(lineArray[1])))
                                   System.out.println("not a valid time period");
                               temp=1;
                           }
                           else if(count !=4&&lineArray[0].equals("table")) {
                               System.out.println("wrong format");
                               temp=1;
                           }
                           else if (lineArray.length == 4&&lineArray[0].equals("table")==false&&temp==0) {
                               int orderNum = Integer.parseInt(lineArray[0]);
                               String dishName = lineArray[1];
                               int parseInt = Integer.parseInt(lineArray[2]);
                               int parseInt1 = Integer.parseInt(lineArray[3]);
                               if(orderNum<=num)
                                   System.out.println("record serial number sequence error");
                               else if(lineArray[0].length()>1&&Integer.parseInt(lineArray[0])<10)
                                   System.out.println("wrong format");
                               else {
                                   if(sametime==0&&table[i].order.addARecord(orderNum, dishName, parseInt, parseInt1,i)!=null)
                                       num=orderNum;
                                   else if(sametime==1){
                                       num=0;
                                       table[i-1].order.addARecord(orderNum, dishName, parseInt, parseInt1,i-1);
                                       table[i].sametime=1;
                                   }
                               }
                           } else if ("delete".equals(lineArray[1])&&temp==0) {
                               table[i].order.delARecordByOrderNum(Integer.parseInt(lineArray[0]),i);
                           }
                           else if(lineArray.length ==5&&canParseInt(lineArray[0])==true&&canParseInt(lineArray[1])==true){
                               int a=0;
                               if(i>1){
                                   for(int j=1;j<=i;j++){
                                       if(table[j].num==Integer.parseInt(lineArray[1])){
                                           table[j].order.addARecord(Integer.parseInt(lineArray[0]),lineArray[2],Integer.parseInt(lineArray[3]),Integer.parseInt(lineArray[4]),i);
                                           a=1;
                                       }
                                   }
                                   if(a==0) System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist");
                               }
                               else
                                   System.out.println("Table number :"+Integer.parseInt(lineArray[0])+" does not exist");
                           }
                           else if(lineArray.length > 4&&(lineArray[3].length()>=8||lineArray[4].length()>=8))
                               System.out.println("wrong format");
                           
                           else {
                               if ((count == 3 || count == 2) && !canParseInt(lineArray[1]) && !lineArray[1].equals("delete")) {
    System.out.println("wrong format");
} else if (count == 3 && canParseInt(lineArray[1]) && lineArray[2].equals("T")) {
    menu.addDish(lineArray[0], Integer.parseInt(lineArray[1]), true);
} else if (count == 3 && canParseInt(lineArray[1]) && !lineArray[2].equals("T")) {
    System.out.println("wrong format");
} else if (count == 2 && canParseInt(lineArray[1]) && flag == 0) {
    menu.addDish(lineArray[0], Integer.parseInt(lineArray[1]), false);
} else if ((count == 2 || count == 3) && flag == 1) {
    System.out.println("invalid dish");
}
                           }
                           if(count == 4&&lineArray[0].equals("table")&&canParseInt(lineArray[1])&&(!judge2( lineArray[2],lineArray[3],Integer.parseInt(lineArray[1]))||!isopen(lineArray[2], lineArray[3]))) {
                               if(lineArray[3].length()>8||lineArray[2].length()>10)
                                   System.out.println("wrong format");
                               else if(!judge2( lineArray[2],lineArray[3],Integer.parseInt(lineArray[1])))
                                   System.out.println(Integer.parseInt(lineArray[1]) + " date error");
                               else
                               if (!isopen(lineArray[2], lineArray[3]) && judge(lineArray[2], lineArray[3], Integer.parseInt(lineArray[1])))
                                   System.out.println("table " + Integer.parseInt(lineArray[1]) + " out of opening hours");
                           }
                           nextLine = input.nextLine();
                       }
                       input.close();
                       for(int j=1;j<=i;j++){
                           if(table[j].sametime==0)
                               table[j].getprice(j);
                       }
                   }

     public static boolean canParseInt(String str) {
            if (str == null || str.isEmpty()) {
                return false;
            }
            try {
                Integer.parseInt(str);
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }

        public static boolean judge(String str ,String str2,int num){

            String Date1[] = str.split("\\/");
            int year = Integer.parseInt(Date1[0]);
            int month = Integer.parseInt(Date1[1]);
            int day = Integer.parseInt(Date1[2]);

            String Date2[] =str2.split("\\/");
            int hour = Integer.parseInt(Date2[0]);
            int minute = Integer.parseInt(Date2[1]);
            int miao=Integer.parseInt(Date2[2]);
            if(Date1[0].length()!=4||Date1[1].length()>2||Date1[2].length()>2||Date2[0].length()>2||Date2[1].length()>2||Date2[2].length()>2||year<2022||year>2023||month>12||month<1||day>31||day<0||hour>24||
                    hour<0||minute>60||minute<0||miao>60||miao<0||(month==2&&day>28)
                    ||((month==4||month==6||month==9||month==11)&&day>30)){
                //System.out.println(num+" date error");
                return false;
            }
            return true;

        }
        public static boolean judge2(String str ,String str2,int num){
            String Date1[] = str.split("\\/");
            int year = Integer.parseInt(Date1[0]);
            int month = Integer.parseInt(Date1[1]);
            int day = Integer.parseInt(Date1[2]);
            
            String Date2[] =str2.split("\\/");
            int hour = Integer.parseInt(Date2[0]);
            int minute = Integer.parseInt(Date2[1]);
            int miao=Integer.parseInt(Date2[2]);
            if(Date1[0].length()!=4||Date1[1].length()>2||Date1[2].length()>2||Date2[0].length()>2||Date2[1].length()>2||Date2[2].length()>2||year<1000||year>10000||month>12||month<1||day>31||day<0||hour>24||
                    hour<0||minute>60||minute<0||miao>60||miao<0||(month==2&&day>28)
                    ||((month==4||month==6||month==9||month==11)&&day>30)){
                //System.out.println(num+" date error");
                return false;
            }
            return true;
        }
        
        public static int sameTime(String str, String str2) {
            LocalTime currentTime = LocalTime.parse(str); // 将字符串转换为LocalTime对象
            LocalDate currentDate = LocalDate.parse(str2); // 将字符串转换为LocalDate对象
            
            if (currentDate.getDayOfWeek().getValue() >= 1 && currentDate.getDayOfWeek().getValue() <= 5) {
                LocalTime startTime1 = LocalTime.of(17, 0);
                LocalTime endTime1 = LocalTime.of(20, 30);
                LocalTime startTime2 = LocalTime.of(10, 30);
                LocalTime endTime2 = LocalTime.of(14, 30);
                if ((currentTime.equals(startTime1) || currentTime.isAfter(startTime1)) &&
                        (currentTime.equals(endTime1) || currentTime.isBefore(endTime1))) {
                    return 1;
                } else if ((currentTime.equals(startTime2) || currentTime.isAfter(startTime2)) && 
                           (currentTime.equals(endTime2) || currentTime.isBefore(endTime2)) ||
                           (currentTime.isAfter(LocalTime.of(15, 0)))) {
                    return 2;
                }
            }
            if (currentDate.getDayOfWeek().getValue() == 6 || currentDate.getDayOfWeek().getValue() == 7) {
                LocalTime startTime3 = LocalTime.of(9, 30);
                LocalTime endTime3 = LocalTime.of(21, 30);
                if ((currentTime.equals(startTime3) || currentTime.isAfter(startTime3)) &&
                        (currentTime.equals(endTime3) || currentTime.isBefore(endTime3))) {
                    return 3;
                }
            }
            return 0;
        }

        
        public static boolean isopen(String str, String str2) {
            Time time = new Time();
            time.time1 = str;
            time.time2 = str2;
            time.getDay();
            time.getYear();
            time.getweekOfDay();

            LocalTime currentTime = LocalTime.of(time.hour, time.minute);

            LocalTime startTime1 = LocalTime.of(17, 0);
            LocalTime endTime1 = LocalTime.of(20, 30);
            LocalTime startTime2 = LocalTime.of(10, 30);
            LocalTime endTime2 = LocalTime.of(14, 30);
            LocalTime startTime3 = LocalTime.of(9, 30);
            LocalTime endTime3 = LocalTime.of(21, 30);

            if ((time.weekday >= 1 && time.weekday <= 5 && (
                    ( currentTime.isAfter(startTime1)) && currentTime.isBefore(endTime1) ||
                    currentTime.isAfter(startTime2) && currentTime.isBefore(endTime2)
                )) || ((time.weekday == 6 || time.weekday == 7) && (
                    ( currentTime.isAfter(startTime3)) && currentTime.isBefore(endTime3)
                ))) {
                return true;
            } else {
                return false;
            }

        }

        public static boolean judge3(String str) {
            return str.matches("[1-9]\\d?");
        }

}

类图如下:

 分析:这道题我在参考类之外还加了一个Time类,为了完成输入时间的转换和判断,整体来说基本的功能实现了,但是一些错误判断还是没能完成好,没拿把分拿全。

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

本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。

注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。


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

 

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

 

菜单由一条或多条菜品记录组成,每条记录一行

 

每条菜品记录包含:菜名、基础价格  三个信息。

 

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

 

桌号标识独占一行,包含两个信息:桌号、时间。

 

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

 

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

 

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

 

删除记录格式:序号  delete

 

标识删除对应序号的那条点菜记录。

 

如果序号不对,输出"delete error"

 

代点菜信息包含:桌号 序号 菜品名称 口味度 份额 份数

 

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

 

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

 

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

 

折扣的计算方法(注:以下时间段均按闭区间计算):

 

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

 

周末全价,营业时间:9:30-21:30

 

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

 

参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。

 

Dish {    

 

   String name;//菜品名称    

 

   int unit_price;    //单价    

 

   int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)    }

 

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

 

Menu {

 

   Dish[] dishs ;//菜品数组,保存所有菜品信息

 

   Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

 

   Dish addDish(String dishName,int unit_price)//添加一道菜品信息

 

}

 

点菜记录类:保存订单上的一道菜品记录

 

Record {

 

   int orderNum;//序号\\

 

   Dish d;//菜品\\

 

   int portion;//份额(1/2/3代表小/中/大份)\\

 

   int getPrice()//计价,计算本条记录的价格\\

 

}

 

订单类:保存用户点的所有菜的信息。

 

Order {

 

   Record[] records;//保存订单上每一道的记录

 

   int getTotalPrice()//计算订单的总价

 

   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

 

   delARecordByOrderNum(int orderNum)//根据序号删除一条记录

 

   findRecordByNum(int orderNum)//根据序号查找一条记录

 

}

 

### 输入格式:

 

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

 

菜品记录格式:

 

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

 

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

 

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

 

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

 

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

 

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

 

### 输出格式:

 

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

 

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

 

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

 

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

 

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

 

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

 

以上为菜单计价系列-3的题目要求,加粗的部分是有调整的内容。本次课题相比菜单计价系列-3新增要求如下:

 

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

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

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

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

 

 

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

输出用户支付金额格式:

用户姓名+英文空格+手机号+英文空格+支付金额

 

 

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

 

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

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

输入格式:

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

 

菜品记录格式:

 

菜名+口味类型+英文空格+基础价格

 

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

 

点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范围见题目中说明。

 

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

 

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称**+英文空格+辣/酸/甜度值+**英文空格+份额+英文空格+分数

 

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

输出格式:

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

 

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

 

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

 

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

 

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

 

之后按输入顺序一次输出每一桌所有菜品的价格(整数数值),

格式:table+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格

 

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

输入样例1:

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

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

输出样例1:

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

table 1 out of opening hours

输入样例2:

一种口味的菜品。例如:

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

输出样例2:

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

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

 

输入样例3:

辣度值超出范围。例如:

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

输出样例3:

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

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

输入样例4:

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

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

输出样例4:

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

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

输入样例5:

多用户多桌菜。例如:

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

输出样例5:

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

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

输入样例6:

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

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

输出样例6:

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

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

输入样例7:

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

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

输出样例7:

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

wrong format
wrong format
import java.text.SimpleDateFormat;
import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;


class Dish {   //菜单格式
    String name;
    int price;
    String taste;

    boolean isT = false;
    public Dish() {
        
    }
    public Dish(String name,int price ,String taste) {
        this.name=name;
        this.price=price;
        this.taste=taste;
    }
}

class Record {      //点菜
    int orderNum;  //序号
    Dish ds;   //菜品
    int portion;  //份额
    int number;  //份数
    boolean isDeleted = false;

    public Record() {
        
    }
    public Record(int orderNum,Dish ds,int portion,int number) {
        this.orderNum=orderNum;
        this.ds =ds;
        this.portion=portion;
        this.number=number;
        
    }
    
   public int getPrice() {
        if (portion == 2)
            return (int) Math.round(1.5 * ds.price) * number;
        if (portion == 3)
            return 2 * ds.price * number;
        if (portion == 1)
            return ds.price * number;
        return 0;
    }
}

class Menu {   //菜单
    ArrayList<Dish> dishes = new ArrayList<>();

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

    public Dish addDish(String dishName, int price,String taste) {
        Dish newDish = new Dish(dishName,price,taste);    
        return newDish;
    }
}

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

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

    public void delARecordByOrderNum(int orderNum) {
        int i, flag = 0;
        for (i = 0; i < records.size(); i++) {
            if (records.get(i).orderNum == orderNum) {
                if (!records.get(i).isDeleted) {
                    records.get(i).isDeleted = true;
                }
                else {
                    System.out.println("deduplication " + orderNum);
                }
                flag++;
            }
        }
        if (flag == 0) {
            System.out.println("delete error;");
        }
    }
}

class Table {
    Order order = new Order();
    int num;
    LocalDateTime time;
    long sum = 0;
    long origSum = 0;
    
    public void add(Menu menu, String str3,String str1, String str2, int portion, int number) {     //判断点菜的模式(是否存在,删除,添加)
        if (!str2.equals("delete")) {
             if (menu.searchDish(str2) == -1) {
               System.out.println(str2 + " does not exist"); 
            } else order.records.add(order.addARecord(str3,Integer.parseInt(str1), str2, portion, number, menu));      
        } else  order.delARecordByOrderNum(Integer.parseInt(str1));
        
    }

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

            }
        }
    }

    public boolean isOpen() {          //判断是否开门
        int weekday = time.getDayOfWeek().getValue();
        
        if (weekday > 0 && weekday < 6) {
            if (time.getHour() >= 17 )
                if( time.getHour() < 20)
                    return true;
            if (time.getHour() == 20) 
                if (time.getMinute() <= 30)
                    return true;
            
            if (time.getHour() > 10 )
                if( time.getHour() < 14)
                    return true;
            
            if (time.getHour() == 10) 
                if (time.getMinute() >= 30)
                    return true;    
            
            if (time.getHour() == 14) 
                return time.getMinute() <= 30;
            
        }
        else if(weekday==7||weekday==6) {
            if (time.getHour() > 9 && time.getHour() < 21)
                return true;
            if (time.getHour() == 9) {
                if (time.getMinute() >= 30)
                    return true;
            }
            if (time.getHour() == 21) {
                return time.getMinute() <= 30;
            }
        }
        return false;

    }
}

public class Main {

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");   //时间模板
        Menu menu = new Menu();
        HashMap<String ,String>hash=new HashMap<>();
        String []customer=new String[30];
        int cnt;
        cnt=0;
        ArrayList<Table> tables = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        
        String []zhe=new String[]{"不甜","微甜","稍甜","甜"};
        String []jin=new String[]{"不酸","微酸","稍酸","酸","很酸"};
        String []chuan=new String[]{"不辣","微辣","稍辣","辣","很辣","爆辣"};
        int []tastedGrade=new int [100];
        int []tastedNumber=new int [100];
        String str1 = "";
        String str2 = "";
        String str3= "";
        
        String []dish;
        String tb = null;
        int [][]taste=new int[20][7];
        int table_count = 0;
        int i;
        int flag = 0;
        int portion = 0;
        int number = 0;
        //份额,份数
        //str2="0";str3=" ";str1=" ";
        menu.dishes.add(menu.addDish("", 0," "));
        while (true) {
            dish = input.nextLine().split(" "); // 以空格分割用户输入的菜单信息
            if (dish.length > 4 && !dish[0].equals("table") || dish.length == 3) {
                System.out.println("wrong format");
                continue; // 如果菜单格式错误,输出提示信息并继续下一轮循环
            }
            if (dish.length > 4) {
                str1 = dish[0];
                tb = " " + dish[1] + " " + dish[2] + " " + dish[3] + " " + dish[4] + " " + dish[5] + " " + dish[6];
                break; // 处理长度超过4的情况,提取相应信息并跳出循环
            }
            if (dish.length == 4) {
                str1 = dish[0];
                str2 = dish[2];
                str3 = dish[1];
                // 判断特价菜并加入菜单
                if (dish[3].equals("T") && isNumeric(str2) && !isNumeric(str3)) {
                    menu.dishes.get(menu.dishes.size() - 1).isT = true;
                    menu.dishes.get(menu.dishes.size() - 1).taste = str3;
                    menu.dishes.get(menu.dishes.size() - 1).name = str1;
                    menu.dishes.get(menu.dishes.size() - 1).price = Integer.parseInt(str2);
                } else {
                    System.out.println("wrong format");
                    continue; // 处理特价菜格式错误的情况
                }
            } else if (dish.length == 2) {
                str1 = dish[0];
                str2 = dish[1];
                menu.dishes.get(menu.dishes.size() - 1).name = str1;
                menu.dishes.get(menu.dishes.size() - 1).price = Integer.parseInt(str2);
                
            } else if (dish.length == 1 && dish[0].equals("end")) {
                str1 = dish[0];
                break; // 处理结束输入的情况,跳出循环
            } else if (dish.length == 1) {
                System.out.println("wrong format");
                continue; // 处理输入格式错误的情况
            } else {
                str3 = " ";
            }

            // 校验菜谱价格格式是否正确
            if (isNumeric(str1) || !isNumeric(str2)) {
                System.out.println("wrong format");
                continue;
            }

            // 判断菜谱是否重复输入,如果是则更新价格,否则添加新菜谱
            for (i = 0; i < menu.dishes.size(); i++) {
                if (menu.dishes.get(i).equals(str1)) {
                    menu.dishes.get(i).price = Integer.parseInt(str2);
                    flag++;
                    break;
                }
            }
            if (flag == 0) {
                menu.dishes.add(menu.addDish(str1, Integer.parseInt(str2),str3));
            }
            flag = 0;
        }

        boolean first = false;
        if(str1.equals("end"))return ;
        while(!str1.equals("end")){
            int chuanGrade=0;
            int chuanNumber=0;
            int zheGrade=0;
            int zheNumber=0;
            int jinGrade=0;
            int jinNumber=0;
            String []a;
            String ord;
            String call;
            String useName;
            Table tab= new Table();
            if(first)
                ord = input.nextLine();
            else {
                ord = tb;
                first=true;
            }
            a=ord.split(" ");
            str2=a[1];
            useName=a[3];
            call=a[4];
            String threeCall=call.substring(0,3);
            if(useName.length()>10||call.length()!=11||searchCall(threeCall)){
                System.out.println("wrong format");
                break;
            }
            String[] Date = a[5].split("/");
            String[] Time =a[6].split("/");
            tab.num = Integer.parseInt(str2);
            int[] intDate = new int[3];
            int[] intTime = new int[3];
            for(i=0;i<3;i++) {
                intDate[i] = Integer.parseInt(Date[i]);
                intTime[i] = Integer.parseInt(Time[i]);
            }
            try {
                tab.time = LocalDateTime.of(intDate[0],intDate[1],intDate[2],intTime[0],intTime[1],intTime[2]);
                //时间判断
                tables.add(tab);
                if(!tab.isOpen()) {
                    System.out.println("table " + str2 + " out of opening hours");
                    do {
                        str1 = input.next();
                    } while (!str1.equals("end") && !str1.equals("table"));
                    continue;
                }
            }catch(DateTimeException e){
                System.out.println( tab.num + " date error");
                break;
            }
            System.out.println("table "+str2+": ");
            while (true) {
                str1 = input.next();
                if (str1.equals("end")||str1.equals("table"))
                    break;
                
                str2 = input.next();
                // 判断是否为代点
                if (isNumeric(str2)) {
                    //判断代点桌号是否存在
                    boolean exist = false;
                    for (Table table : tables) {
                        if (table.num == Integer.parseInt(str1)) {
                            exist = true;
                            break;
                        }
                    }
                    //若存在则完成代点
                    if(exist) {
                        System.out.print(Integer.parseInt(str2) + " table " +tables.get(table_count).num + " pay for table "
                                + Integer.parseInt(str1) + " ");
                        String str=str1;
                        Record treat = new Record();
                        str1 = input.next();
                        treat.ds = menu.dishes.get(menu.searchDish(str1));
                        if(menu.searchDish(str1)!=-1){
                            if (menu.dishes.get(menu.searchDish(str1)).isT) {
                                str3 = input.next();
                                tastedGrade[Integer.parseInt(str)] = Integer.parseInt(str3);
                            } else str3 = " ";
                        }else {
                            System.out.println(str1 + " does not exist");
                            str1=input.nextLine();continue;
                        }
                        portion = input.nextInt();
                        number = input.nextInt();
                        tastedNumber[Integer.parseInt(str)]=number;
                        treat.portion = portion;
                        treat.number = number;
                        System.out.print(treat.getPrice() + "\n");
                        if(menu.dishes.get(menu.searchDish(str1)).taste.equals("晋菜")) {
                            if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>4){
                                System.out.println("acidity num out of range :"+jinGrade);
                                str1=input.nextLine();
                                continue;
                            }
                            taste[Integer.parseInt(str)-1][1] += Integer.parseInt(str3)*number;
                            taste[Integer.parseInt(str)-1][4]+=number;
                        }
                        if(menu.dishes.get(menu.searchDish(str1)).taste.equals("川菜")) {
                            if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>5){
                                System.out.println("spicy num out of range :"+str3);
                                str1=input.nextLine();
                                continue;
                            }
                            taste[Integer.parseInt(str)-1][0] += Integer.parseInt(str3)*number;
                            taste[Integer.parseInt(str)-1][3]+=number;
                        }
                        if(menu.dishes.get(menu.searchDish(str1)).taste.equals("浙菜")) {
                            if(Integer.parseInt(str3)<0||Integer.parseInt(str3)>3){
                                System.out.println("sweetness num out of range :"+str3);
                                str1=input.nextLine();
                                continue;
                            }
                            taste[Integer.parseInt(str)-1][2] += Integer.parseInt(str3)*number;
                            taste[Integer.parseInt(str)-1][5]+=number;
                        }

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

            }
            taste[table_count][0]=chuanGrade;
            taste[table_count][1]=jinGrade;
            taste[table_count][2]=zheGrade;
            taste[table_count][3]=chuanNumber;
            taste[table_count][4]=jinNumber;
            taste[table_count][5]=zheNumber;
            // 本桌点菜结束,进入下一桌
            tables.get(table_count).getSum();
            int sum;
            String str=useName+" "+call+" "+tables.get(table_count).sum;
            String []b;
            if(table_count==0)customer[cnt++]=str;
            else {
                b = str.split(" ");
                boolean r=true;
                for (int l = 0; l < cnt; l++) {
                    String[] c = customer[l].split(" ");
                    if (b[0].equals(c[0])) {
                        sum = Integer.parseInt(c[2]) + Integer.parseInt(b[2]);
                        customer[l] = useName + " " + call + " " + sum;
                        r=false;
                    }
                }
                if(r)customer[cnt++]=str;
            }
            table_count++;
        }
        // 最终输出桌号订单信息
        for (i = 0; i < table_count; i++) {
            if (tables.get(i).isOpen()) {
                System.out.print("table " + tables.get(i).num + ": " + tables.get(i).origSum+" "+tables.get(i).sum);
                for(int j=0;j<3;j++){
                    if(taste[i][j+3]!=0){
                        if(j==0){
                            System.out.print(" 川菜 "+taste[i][j+3]+" "+chuan[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                        if(j==1){
                            System.out.print(" 晋菜 "+taste[i][j+3]+" "+jin[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                        if(j==2){
                            System.out.print(" 浙菜 "+taste[i][j+3]+" "+zhe[(int) Math.round(1.0*taste[i][j]/taste[i][j+3])]);
                        }
                    }
                }
                System.out.println();
            } else
                System.out.println("table " + tables.get(i).num + " out of opening hours");
        }
        Arrays.sort(customer,0,cnt);
        for(int j=0;j<cnt;j++) {
            if(j<cnt-1)
                System.out.println(customer[j]);
            else System.out.print(customer[j]);
        }
    }
    public static boolean isNumeric(String string) {  //判断是否为数字
        int intValue;
        try {
            intValue = Integer.parseInt(string);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    public static boolean searchCall(String threeCall) {
        String []CallNumber=new String[]{"180","181","189","133","135","136"};
        for (String s : CallNumber) 
            if (s.equals(threeCall))
                return false;
        return true;
    }
}

 分析:这道题相较于上一道题目,我采用了另外的方法,利用多次循环,在不同的循环中输入。这种形式虽然也能完成题目的基本要求,但相较于上一题,还是有些测试用例无法通过,我也找不到原因,只能是遗憾提交了。

下面是期中考试的题目————————————————————————————————————————————————————————————————————————

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
import java.util.Scanner;

abstract class Shape {

    public Shape() {
 
    }

    public abstract double getArea();
}

class Circle extends Shape {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }

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

class Rectangle extends Shape {
  Point topLeftPoint;
  Point lowerRightPoint;
    
     public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowerRightPoint = lowerRightPoint;
        
    }
    // 其他方法和属性的定义

    public double getArea() {
        return Math.abs((topLeftPoint.getX() - lowerRightPoint.getX()) * 
            (topLeftPoint.getY() - lowerRightPoint.getY()));
    }
    public Point getTopLeftPoint() {
        return topLeftPoint;
    }
    
    public void setTopLeftPoint(Point TopLeftPoint) {
        this.topLeftPoint=TopLeftPoint;
    }
    
    public Point getLowerRightPoint() {
        return lowerRightPoint;
    }
    
    public void setLowerRightPoint(Point LowerRightPoint) {
        this.lowerRightPoint=LowerRightPoint;
    }

    // 其他方法的定义
}

class Point {
    private double x;
    private double y;
    
    public Point(double x,double y){
        this.x=x;
        this.y=y;
    }
    public double getX() {
        return x;
    }
    public void setX(double x){
        this.x = x;
    }
    public double getY() {
        return y;
    }
     public void setY(double y){
        this.y= y;
    }
  
    // 其他方法的定义
}

public class Main{
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        
        int choice = input.nextInt();
        
        switch(choice) {
        case 1://Circle
            double radiums = input.nextDouble();
                if (radiums<=0 ) {
            System.out.println("Wrong Format");
            return;
        }
            Shape circle = new Circle(radiums);
            printArea(circle);
            break;
        case 2://Rectangle
            double x1 = input.nextDouble();
            double y1 = input.nextDouble();
            double x2 = input.nextDouble();
            double y2 = input.nextDouble();
            
            Point leftTopPoint = new Point(x1,y1);
            Point lowerRightPoint = new Point(x2,y2);
            
            Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
            
            printArea(rectangle);
            break;
        }
        
    }
    
    public static void printArea(Shape shape){
        System.out.printf("%.2f\n",shape.getArea());
    }
}

 

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 

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

abstract class Shape implements Comparable<Shape> {

    public abstract double getArea();

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

class Circle extends Shape {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }

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

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

class Rectangle extends Shape {
    Point topLeftPoint;
    Point lowerRightPoint;
    
    public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowerRightPoint = lowerRightPoint;
    }

    public double getArea() {
        return Math.abs((topLeftPoint.getX() - lowerRightPoint.getX()) * 
            (topLeftPoint.getY() - lowerRightPoint.getY()));
    }


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

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

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

}

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();
                    if (radius <= 0) {
                        System.out.println("Wrong Format");
                        return;
                    }
                    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 (Shape shape : list) {
            System.out.print(String.format("%.2f", shape.getArea()) + " ");
        }
    }
}

 

期中考试的题目其实相对来说比较简单,这里就列举这两道题目在这,没有什么太大问题。

踩坑心得:

关于这几次题目集,我其实完成的并不太好。反复修改自己的代码,但是就是有些测试点过不去,有的测试点真的有点难搞。比如说在川菜,浙菜那加空格,

在川菜前面加空格,就有一个测试点过不去。不加的话又会多好多测试点过不去,难搞难搞。老师说使用try catch语句来完成异常监测,我没用上捏。

 

总结:

通过这几次作业,我对Java编程语言面向对象编程的基本概念和原则有了更加深入的理解。对于类的设计,类的使用有了新的理解。

同时也成功解决了一些问题,对相关知识有了更加深的印象

 

 

 

 

 

标签:int,Blog2,空格,Integer,parseInt,table,public
From: https://www.cnblogs.com/bsj666/p/17842777.html

相关文章

  • 小结blog2
    一、前言这次总结是对第四次题目集,第五次题目集,第六次题目集和期中考试的总结1.第四次题目集主要是菜单三简单构建,实现复杂任务类的设计2.第五次题目集主是:菜单计价程序4,在基于菜单计价程序3的基础上进行完善,增加了新的需求,主要是异常情况3.第六次题目集主是:......
  • 21207328-吴义隆-Blog2
    一、前言:(1)知识点本次作业,知识点很广,单论期中考试,就涉及了许多,类结构设计、继承与多态和抽象类与接口等。Java类结构知识点:类的声明:Java类通过关键字"class"来声明,后面跟着类名和类体,类体包含类的成员变量和方法。成员变量:也称为字段或属性,用于描述类的状态。它们定......
  • java-blog2
    一、前言1、知识点:主要是java中类与类之间的关系和类图的构建以及对数据的处理2、题量:题量不大,简单的作业题量多点,难点的作业题量少点总体来说还行3、难度:难度在不断的变大,每一题所花的时间也在不断的增加二、设计与分析:第四次大作业:设计点菜计价程序,根据输入的信息,计算并输......
  • BLOG2
    一、前言:这三次大作业难度都很大,且逐渐递增!!!期中考试难度也挺大的,选择题几乎都不太会,编程题倒是好一些给了类图,但是对于继承关系和对于接口的使用还是不太熟练,琢磨了很久。二、设计与分析:7-4菜单计价程序-2设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺......
  • BLOG2
    一、作业总结该次博客作业是对第4次PTA、菜单计价程序-4、菜单计价程序-5、期中测试的一次总结。这次的菜单计价程序在之前的基础上进行了更完善的更改升级,菜单计价程序-4在菜单计价程序-3的基础上增加了异常情况的处理,菜单计价程序-5在菜单计价程序-3的基础上增加了特色菜的深入......
  • blog2
     一、前言随着对java学习的越来越深入,需要学习的东西也越来越多,第四五次pta题目集主要还是以菜单计价系统为主,相较于以前的菜单计价系统,增加了异常情况的处理,以及特色菜,口味度等功能,使这个菜单计价系统越来越与现实生活相关联,当然与之同时题目的难度当然也是大幅度提高了。虽......
  • blog2
    一、前言第四次题目集,主要是菜单计价程序,难度逐渐提高,难度不是很高。第五次题目集,只有一道菜单计价程序4,这道题是在菜单计价程序3的基础上添加了时间处理,使得程序整体难度提升很大第六次题目集,也只有一道菜单计价程序5,这道题也是以菜单计价程序3为基础,添加了特色菜的处理,难度相......
  • blog2
    前言菜单计价程序-3作为计价4和计价5的基础,做不了3就不用谈作为延伸拓展的4和5,期中考试难度一般。主要是菜单4涉及到了异常的处理机制,难度方面还是菜单3比较难,菜单4,5,都是在3的基础上增加内容,难度逐渐上升7-4菜单计价程序-2分数40全屏浏览题目切换布局作者......
  • Blog2
    一、前言1.题量及难度这几次作业的题量适中,题目数量适中,不过也不过少,能够提供足够的练习机会。在PTA上的大作业中,一共有三道题。其中第一题和第三题难度不大,但第二题属于点线形系列的题目,是之前得分不高的PTA作业中的重点题型,占据了70分,而我只得到了39分。在期中考试中虽然只有......
  • BLOG2-PTA题目集4、5以及期中考试
    (1)前言本次博客主要涵盖了Java题目的几个主要知识点,包括:1.面向对象的基础知识:这部分主要包括了类和对象的基本概念,构造方法,访问权限和成员变量的相关内容。在面向对象编程中,对这些基础知识的理解至关重要。2.面向对象的设计原则:这个题目强调了两个重要的设计原则,即继承和组......