面向对象程序设计第四、五次大作业及期中考试总结
第四次大作业
7 - 1 菜单计价程序 - 4
设计点菜计价程序,根据输入的信息,计算并输出总价格。
输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。
菜单由一条或多条菜品记录组成,每条记录一行
每条菜品记录包含:菜名、基础价格 两个信息。
订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。
桌号标识独占一行,包含两个信息:桌号、时间。
桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。
点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。
不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。
删除记录格式:序号 delete
标识删除对应序号的那条点菜记录。
如果序号不对,输出"delete error"
代点菜信息包含:桌号 序号 菜品名称 份额 分数
代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。
程序最后按输入的桌号从小到大的顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。
每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。
折扣的计算方法(注:以下时间段均按闭区间计算):
周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
周末全价,营业时间:9:30-21:30
如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"
参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
点菜记录类:保存订单上的一道菜品记录
Record {
int orderNum;//序号
Dish d;//菜品\\
int portion;//份额(1/2/3代表小/中/大份)
int getPrice()//计价,计算本条记录的价格
}
订单类:保存用户点的所有菜的信息。
Order {
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
delARecordByOrderNum(int orderNum)//根据序号删除一条记录
findRecordByNum(int orderNum)//根据序号查找一条记录
}
本次课题比菜单计价系列-3增加的异常情况:
1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:"invalid dish"
2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"
3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。
4、重复删除,重复的删除记录输出"deduplication :"+序号。
5、代点菜时,桌号不存在,输出"Table number :"+被点菜桌号+" does not exist";本次作业不考虑两桌记录时间不匹配的情况。
6、菜谱信息中出现重复的菜品名,以最后一条记录为准。
7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。
8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。
9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。
10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。
11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。
12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。
13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:"not a valid time period"
14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。
15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:"record serial number sequence error"。当前记录忽略。(代点菜信息的序号除外)
16、所有记录其它非法格式输入,统一输出"wrong format"
17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。
本次作业比菜单计价系列-3增加的功能:
菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+"T"
例如:麻婆豆腐 9 T
菜价的计算方法:
周一至周五 7折, 周末全价。
注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:
计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。
最后将所有记录的菜价累加得到整桌菜的价格。
输入格式:
桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
菜品记录格式:
菜名+英文空格+基础价格
如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。
点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
删除记录格式:序号 +英文空格+delete
代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数
最后一条记录以“end”结束。
输出格式:
按输入顺序输出每一桌的订单记录处理信息,包括:
1、桌号,格式:table+英文空格+桌号+”:”+英文空格
2、按顺序输出当前这一桌每条订单记录的处理信息,
每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名
如果删除记录的序号不存在,则输出“delete error”
最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价
输入样例:
在这里给出一组输入。例如:
麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 1 2
2 delete
2 delete
end
输出样例:
在这里给出相应的输出。例如:
table 31:
1 num out of range 16
2 油淋生菜 18
deduplication 2
table 31: 0 0
输入样例1:
份数超出范围+份额超出范围。例如:
麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 4 2
end
输出样例1:
份数超出范围+份额超出范围。例如:
table 31:
1 num out of range 16
2 portion out of range 4
table 31: 0 0
输入样例2:
桌号信息错误。例如:
输出样例2:
在这里给出相应的输出。例如:
麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
输入样例3:
混合错误:桌号信息格式错误+混合的菜谱信息(菜谱信息忽略)。例如:
麻婆豆腐 12
油淋生菜 9 T
table 55 2023/3/31 12/000/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end
输出样例3:
在这里给出相应的输出。例如:
wrong format
输入样例4:
错误的菜谱记录。例如:
wrong format
table 55:
invalid dish
麻婆豆腐 does not exist
2 油淋生菜 14
table 55: 14 10
输出样例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
其他用例请参考公开的测试用例
代码详情:
public static void main(String[] args) { Scanner input = new Scanner(System.in); Menu menu = new Menu();//定义菜谱类 JudgeWork judge_table = new JudgeWork(); Table[] tables = new Table[56];//桌类 String input_data = new String();//输入数据 String illegal = "end";//用于判断输入的菜名是否能识别 String[] arr = new String[10000];//存储按照空格分割后的输入数据 int i=0,cnt = 0;//cnt用于记录订单信息条数,portion为输入的份额 int arr_length = 0,j = 1; int i_table = 0;//多少桌 int flag = 0;//记录当前是否在营业时间 int[] table_SumPrice = new int[20]; int only = 0;//0表示仅有菜谱信息,防止非0返回 int lastdish=0;//记录点菜序号 Boolean ok_tableNum = true;//记录桌号信息格式是否正确 Boolean when_inputRecord = false;//记录当前是否正在输入订单信息,用于忽略夹在订单信息中的菜谱信息 Boolean ok_Record = true;//记录订单信息格式是否正确 Boolean ok_Menu = true;//记录输入的菜谱信息格式是否正确 int cntHelp = 0;//记录代点菜的次数,防止因为点菜记录为代点菜时,cnt+1而导致“点菜记录的序号必须按从小到大的顺序排列”的判断出错 while(true)//循环输入 { input_data = input.nextLine(); if(input_data.compareTo("") == 0) { System.out.println("wrong format"); continue; } if(input_data.compareTo(illegal)==0)//判断是否结束输入 { if(only != 0) { table_SumPrice[i_table] = (int)Math.ceil((double)tables[i_table].order.getTotalPrice(cnt,tables[i_table]));//计算当前桌订单折扣前总价 } break; } arr = input_data.split(" "); arr_length = arr.length; if((arr[0].compareTo("table") == 0||(arr_length >=5&&!arr[0].matches("^[0-9]$"))))//输入的是桌号信息,桌号加1 { if(!(arr[0].matches("^[0-9]|[1-5][0-9]$"))) { when_inputRecord = true; //判断桌号信息格式是否错误 ok_tableNum = judge_table.Judge_tableNum(arr,input_data); if(ok_tableNum == false) { if(!arr[0].equals("table"))//并入上一桌计算 ok_tableNum = true; if(judge_table.tableNum_time_error == true)//桌号时间错误 { System.out.println("wrong format");//桌号除时间错误外的其他格式错误 continue; } else if(judge_table.tableNum == true) { System.out.println(arr[1]+" date error"); continue; } if(judge_table.tableNum == false) { System.out.println(arr[1] +" table num out of range");//桌号时间无效 continue; } } if(judge_table.Judge_validTime(arr) == false) { System.out.println("not a valid time period");//桌号时间无效 ok_tableNum = false; continue; } i_table++; only = 1; if(i_table != 1) table_SumPrice[i_table-1] = (int)Math.ceil((double)tables[i_table-1].order.getTotalPrice(cnt,tables[i_table-1]));//计算当前桌订单折扣后的总价 lastdish=0; i = 0; cnt = 0;//订单信息重新开始记录 tables[i_table] = new Table(); tables[i_table].tableNum = Integer.parseInt(arr[1]); //判断当前是否在营业时间 tables[i_table].work = judge_table.judge(arr[2], arr[3], tables[i_table]); if(tables[i_table].work == true) System.out.println("table "+tables[i_table].tableNum+": "); if(tables[i_table].work == false)//当前不在营业时间 { flag = 1; System.out.println("table "+arr[1]+" out of opening hours"); ok_tableNum = false;//忽略本桌点菜记录 continue;//跳出本次循环 } else flag = 0; } } if(ok_tableNum == false)//直到输入下一桌桌号信息格式正确时,才进行后续操作 continue; if(arr[0].compareTo("table") == 0) continue; if(arr_length == 2||arr_length == 3)//表示输入的是菜品信息(3为特色菜)或者删除菜单的数据信息 { if(arr[1].compareTo("delete") != 0)//表示输入的数据是菜品信息 { if(when_inputRecord == true) { System.out.println("invalid dish"); continue; } ok_Menu = judge_table.Judge_menu(input_data); if(ok_Menu == false) { System.out.println("wrong format"); continue; } if(judge_table.menuTime_error == false) { System.out.println(arr[0]+" price out of range "+arr[1]); continue; } menu.dishs[j]=new Dish(); boolean special = false;//标记当前菜品是否为特色菜 if(arr_length == 3)//是特色菜 special = true; menu.dishs[j] = menu.addDish(arr[0], Integer.parseInt(arr[1]),special); j++; } else//表示输入的是删除菜单信息的数据 { tables[i_table].order.findRecordByNum(Integer.parseInt(arr[0]),cnt); } } else//表示输入的是订单信息 { int judge_help = 0; int beHelp_tableNum = 0; if(arr_length == 5)//输入的是代点菜信息 { judge_help = 1; cntHelp++; tables[i_table].order.records[i] = new Record(); tables[i_table].order.records[i].i_BeHelpTable = Integer.parseInt(arr[0]); beHelp_tableNum = Integer.parseInt(arr[0]); String s =arr[0]; arr[0]=arr[1]; arr[1]=arr[2]; arr[2]=arr[3]; arr[3]=arr[4]; arr[4] = s; } if(arr_length == 4) ok_Record = judge_table.Judge_record(input_data); if(ok_Record == false) { System.out.println("wrong format"); continue; } if(arr.length==4) { System.out.println("record serial number sequence error"); continue; } if(arr.length==5) tables[i_table].order.record_cnt++; if(tables[i_table].order.records[i].getPrice()!=0) { tables[i_table].order.records[i].judge_help =true;//记录当前点菜记录为代点菜记录 if(judge_help == 1) { boolean exit_helpTableNum = false;//标记被代点的桌号是否存在 for(int p=1;p<i_table;p++) { if(tables[p].tableNum == Integer.parseInt(arr[4])) { exit_helpTableNum = true; break; } } if(exit_helpTableNum == false) } i++; cnt++;//订单数加一 } } for(int k = 1;k <= i_table;k++) { if(tables[k].work == true) { tables[k].SumPrice = table_SumPrice[k]; for(int m = 1;m<k;m++)//可以优化时间 { boolean sameTime = judge_table.sameTime(tables[m], tables[k]); if(tables[k].tableNum == tables[m].tableNum&&(tables[m].work == true)&&sameTime == true) { tables[k].before_sum_price += tables[m].before_sum_price; tables[k].SumPrice += table_SumPrice[m]; } } } boolean output = true;//记录当前桌总价是否要输出(即判断后面有没有重复桌号,有则不输出) for(int k = 1;k <= i_table;k++) { if(tables[k].work == true) { output = true; for(int m = k+1;m <= i_table;m++)//可以优化时间 { boolean sameTime = judge_table.sameTime(tables[m], tables[k]); if(tables[k].tableNum == tables[m].tableNum&&tables[m].work == true&&sameTime == true)//当第k桌与第l+1桌的桌号相同时 { for(int cntk=0;cntk<tables[k].order.record_cnt;cntk++)//将第k+1桌点菜记录合并到第k桌 { tables[m].order.records[tables[m].order.record_cnt]=tables[k].order.records[cntk]; tables[m].order.record_cnt++; } //合并完后,重新计算总价 tables[m].order.sum_price = 0; tables[m].before_sum_price = 0; tables[m].SumPrice = (int)Math.ceil((double)tables[m].order.getTotalPrice(tables[m].order.record_cnt,tables[m])); output =false; break; } } if(output == true) System.out.println("table "+tables[k].tableNum+": "+tables[k].before_sum_price+" "+tables[k].SumPrice); } } } }
代码分析:
类图展示:
餐桌类:记录当前桌的订单及餐桌信息
Table{
Order order = new Order();
int year=0,month=0,day=0,ww=0,hh=0,mm=0,ss=0;
int tableNum;//桌号
String time;//点餐时间
int discount;//记录当前时间的折扣
int T_discount = 10;
int before_sum_price = 0;//折扣前的总价
int SumPrice = 0;//当前桌总价
boolean work = false;//判断当前是否在营业状态,1表示在营业
}
判断异常类:
Judge{
boolean menuTime_error = true;
boolean tableNum_time_error = true;
boolean validTime = true;
boolean tableNum = true;//[1-55]
boolean judge(String time1,String time2,Table table)//判断是否在营业时间
boolean Judge_tableNum(String[] arr,String input_data)//判断桌号信息是否有误
boolean Judge_record(String input_data) //判断点菜记录是否错误
boolean Judge_menu(String input_data)//判断菜单信息是否错误
boolean Judge_validTime(String[] arr)//判断桌号时间是否有误
boolean sameTime(Table a,Table b)//判断两桌是否在同一时间段
}
期中考试
7-1 测试1-圆类设计
创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积
输入格式:
输入圆的半径,取值范围为(0,+∞)
,输入数据非法,则程序输出Wrong Format
,注意:只考虑从控制台输入数值的情况
输出格式:
输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)
输入样例:
在这里给出一组输入。例如: 2.35
输出样例:
在这里给出相应的输出。例如: 17.35
源码展示:
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); double f = input.nextDouble(); if(f <= 0) System.out.println("Wrong Format"); else { Circle circle = new Circle(); circle.setRadius(f); double area = circle.getArea(f); System.out.printf("%.2f\n",area); } } } class Circle{ private double radius; public void setRadius(double radius){ this.radius = radius; } public double getRadius(){ return this.radius; } public double getArea(double radius){ double area; area = Math.PI * radius * radius; return area; } }
7-2 测试2- 类结构设计
设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:
输入格式:
分别输入两个坐标点的坐标值x1,y1,x2,y2。
输出格式:
输出该矩形的面积值(保留两位小数)。
输入样例:
在这里给出一组输入。例如:65.8 - 78.9
输出样例:
在这里给出相应的输出。例如: 40.30
源码展示:
import java.util.*; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double d = input.nextDouble(); Point p1 = new Point(a, b); Point p2 = new Point(c, d); Rectangle rectangle = new Rectangle(p1, p2); double area = rectangle.getArea(rectangle.getLength(), rectangle.getHeight()); System.out.printf("%.2f\n",area); } } class Rectangle{ private Point topLeftPoint; private Point lowerRightPoint; Rectangle(){ } Rectangle(Point topLeftPoint, Point lowerRightPoint){ this.topLeftPoint = topLeftPoint; this.lowerRightPoint = lowerRightPoint; } public Point getTopLeftPoint(){ return this.topLeftPoint; } public void setTopLeftPoint(Point topLeftPoint){ this.topLeftPoint = topLeftPoint; } public Point getLowerRightPoint(){ return this.lowerRightPoint; } public void setLowerRightPoint(Point lowerRightPoint){ this.lowerRightPoint = lowerRightPoint; } public double getLength(){ return Math.abs(this.topLeftPoint.getX() - this.lowerRightPoint.getX()); } public double getHeight(){ return Math.abs(this.topLeftPoint.getY() - this.lowerRightPoint.getY()); } public double getArea(double length, double height){ double area; area = length * height; return area; } } class Point{ private double x; private double y; Point(){ } Point(double x, double y){ this.x = x; this.y = y; } public double getX(){ return this.x; } public void setX(double x){ this.x = x; } public double getY(){ return this.y; } public void setY(double y){ this.y = y; } }
7-3 测验3-继承与多态:
将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类,类图如下所示:
试编程完成如上类图设计,主方法源码如下(可直接拷贝使用):
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; } }
源码展示:
import java.util.*; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int choice = input.nextInt(); switch(choice) { case 1://Circle double radiums = input.nextDouble(); if(radiums <= 0) { System.out.println("Wrong Format"); break; } else{ Circle 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; } } static void printArea(Shape shape) { double area = shape.getArea(); System.out.printf("%.2f",area); } } abstract class Shape{ Shape(){ } public abstract double getArea(); } class Circle extends Shape{ private double radius; Circle(){ } Circle(double radius){ this.radius = radius; } public void setRadius(double radius){ this.radius = radius; } public double getRadius(){ return this.radius; } public double getArea(){ double area; area = Math.PI * this.getRadius() * this.getRadius(); return area; } } class Rectangle extends Shape{ private Point topLeftPoint; private Point lowerRightPoint; Rectangle(){ } Rectangle(Point topLeftPoint, Point lowerRightPoint){ this.topLeftPoint = topLeftPoint; this.lowerRightPoint = lowerRightPoint; } public Point getTopLeftPoint(){ return this.topLeftPoint; } public void setTopLeftPoint(Point topLeftPoint){ this.topLeftPoint = topLeftPoint; } public Point getLowerRightPoint(){ return this.lowerRightPoint; } public void setLowerRightPoint(Point lowerRightPoint){ this.lowerRightPoint = lowerRightPoint; } public double getLength(){ return Math.abs(this.topLeftPoint.getX() - this.lowerRightPoint.getX()); } public double getHeight(){ return Math.abs(this.topLeftPoint.getY() - this.lowerRightPoint.getY()); } public double getArea(){ double area; area = this.getLength() * getHeight(); return area; } } class Point{ private double x; private double y; Point(){ } Point(double x, double y){ this.x = x; this.y = y; } public double getX(){ return this.x; } public void setX(double x){ this.x = x; } public double getY(){ return this.y; } public void setY(double y){ this.y = y; } }
代码分析:
父类Shape类:
包含构造方法shape( )
抽象方法getArea( ) :求面积
子类Circle类(圆类):
私有属性radius :半径
构造方法Circle( )
带参构造方法Circle(double radius)
setRadius() : 设置半径;getRadius() : 获取半径
子类Ractangle类(矩形类):
私有属性radius :左上角点坐标 和 右下角点坐标
构造方法Ractangle( )
带参构造方法Rectangle(Point topLeftPoint, Point lowerRightPoint);
7-4 测验4-抽象类与接口:
在测验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
源码展示:
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、PTA中编写代码不会实时报错,代码量一多,运行时可能一下报出十几个错,CPU都给看没了,所以建议在eclipse或者idea等编译器上编写。
2、在使用类中的属性或者方法时,务必记得创建对象;同时在创建类的数组,需要使用其元素时也需要对当前元素new一下,否则会报空指针或者非零返回。
3、使用类时一定要new明白,不能重复创建,重复创建会导致当前所使用的类中的数据变为空,出现空指针或者非零返回的问题,切记切记。
4、类与类间是平行的,不能在类中创建类。
5、在类中不能调用属性和方法,只有在方法中才能调用。
6、对于带返回值的感受务必要记得return,否则会非零返回,在编译器上编写代码时如果没有带回返回值·会报错,但是PTA编译却没问题,这时候编译器实时报错的好处又在此体现出来了。
7、在编写代码时遇到的很多问题可能都和圈复杂度较高有关,例如运行超时等。所以编写代码时,可以借用SourceMonitor来查看圈复杂度,尽量控制在8以内(这样代码可读性较高,修改也方便)。
8、还有就是一些细节语法上的错误,要尽量减少这类失误,虽然这个没什么好说的,但是很多问题的出现都是因为一个个细节没有处理好而累积导致的后果。
第五次大作业
7-1 菜单计价程序-5:
本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。
注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。
设计点菜计价程序,根据输入的信息,计算并输出总价格。
输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。
菜单由一条或多条菜品记录组成,每条记录一行
每条菜品记录包含:菜名、基础价格 三个信息。
订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。
桌号标识独占一行,包含两个信息:桌号、时间。
桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。
点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。
不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。
删除记录格式:序号 delete
标识删除对应序号的那条点菜记录。
如果序号不对,输出"delete error"
代点菜信息包含:桌号 序号 菜品名称 口味度 份额 份数
代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。
程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。
每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。
折扣的计算方法(注:以下时间段均按闭区间计算):
周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
周末全价,营业时间:9:30-21:30
如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"
参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
点菜记录类:保存订单上的一道菜品记录
Record {
int orderNum;//序号\\
Dish d;//菜品\\
int portion;//份额(1/2/3代表小/中/大份)\\
int getPrice()//计价,计算本条记录的价格\\
}
订单类:保存用户点的所有菜的信息。
Order {
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
delARecordByOrderNum(int orderNum)//根据序号删除一条记录
findRecordByNum(int orderNum)//根据序号查找一条记录
}
### 输入格式:
桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
菜品记录格式:
菜名+英文空格+基础价格
如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。
点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
删除记录格式:序号 +英文空格+delete
代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数
最后一条记录以“end”结束。
### 输出格式:
按输入顺序输出每一桌的订单记录处理信息,包括:
1、桌号,格式:table+英文空格+桌号+”:”
2、按顺序输出当前这一桌每条订单记录的处理信息,
每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名
如果删除记录的序号不存在,则输出“delete error”
最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价
以上为菜单计价系列-3的题目要求,加粗的部分是有调整的内容。本次课题相比菜单计价系列-3新增要求如下:
1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"
例如:麻婆豆腐 川菜 9 T
菜价的计算方法:
周一至周五 7折, 周末全价。
特色菜的口味类型:川菜、晋菜、浙菜
川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;
晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;
浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;
例如:麻婆豆腐 川菜 9 T
输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数
例如:1 麻婆豆腐 4 1 9
单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:
acidity num out of range : 5
输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。
一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。
如果整桌菜没有特色菜,则只输出table的基本信息,格式如下:
table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格
例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜
计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。
注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。
2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:
格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
例如:table 1 : tom 13670008181 2023/5/1 21/30/00
约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。
输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。
输出用户支付金额格式:
用户姓名+英文空格+手机号+英文空格+支付金额
注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:
计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。
将所有记录的菜价累加得到整桌菜的价格。
输入格式:
桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
菜品记录格式:
菜名+口味类型+英文空格+基础价格
如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。
点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范围见题目中说明。
删除记录格式:序号 +英文空格+delete
代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称**+英文空格+辣/酸/甜度值+**英文空格+份额+英文空格+分数
最后一条记录以“end”结束。
输出格式:
按输入顺序输出每一桌的订单记录处理信息,包括:
1、桌号,格式:table+英文空格+桌号+“:”+英文空格
2、按顺序输出当前这一桌每条订单记录的处理信息,
每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名
如果删除记录的序号不存在,则输出“delete error”
之后按输入顺序一次输出每一桌所有菜品的价格(整数数值),
格式:table+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格
最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。
输入样例1:
桌号时间超出营业范围。例如:
麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 21/30/00
1 麻婆豆腐 3 1 2
2 油淋生菜 2 1
3 麻婆豆腐 2 3 2
end
输出样例1:
在这里给出相应的输出。例如:
table 1 out of opening hours
输入样例2:
一种口味的菜品。例如:
麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 20/30/00
1 麻婆豆腐 2 1 2
2 油淋生菜 2 1
3 麻婆豆腐 2 3 2
end
输出样例2:
在这里给出相应的输出。例如:
table 1:
1 麻婆豆腐 24
2 油淋生菜 14
3 麻婆豆腐 48
table 1: 86 62 川菜 4 稍辣
tom 13605054400 62
输入样例3:
辣度值超出范围。例如:
麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 18/30/00
1 麻婆豆腐 6 1 2
2 油淋生菜 1 1
3 麻婆豆腐 5 3 2
end
输出样例3:
在这里给出相应的输出。例如:
table 1:
spicy num out of range :6
2 油淋生菜 9
3 麻婆豆腐 48
table 1: 57 41 川菜 2 爆辣
tom 13605054400 41
输入样例4:
同一用户对应多桌菜。例如:
麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 18/30/00
1 麻婆豆腐 1 1 2
2 油淋生菜 1 1
3 麻婆豆腐 2 2 2
table 2 : tom 13605054400 2023/5/6 18/30/00
1 麻婆豆腐 2 1 2
2 麻辣鸡丝 2 2
3 麻婆豆腐 2 1 1
end
输出样例4:
在这里给出相应的输出。例如:
table 1:
1 麻婆豆腐 24
2 油淋生菜 9
3 麻婆豆腐 36
table 2:
1 麻婆豆腐 24
2 麻辣鸡丝 30
3 麻婆豆腐 12
table 1: 69 49 川菜 4 稍辣
table 2: 66 66 川菜 3 稍辣
tom 13605054400 115
输入样例5:
多用户多桌菜。例如:
东坡肉 浙菜 25 T
油淋生菜 9
蜜汁灌藕 浙菜 10 T
刀削面 晋菜 10 T
醋浇羊肉 晋菜 30 T
麻婆豆腐 川菜 12 T
麻辣鸡丝 川菜 15 T
table 1 : tom 13605054400 2023/5/6 12/30/00
1 醋浇羊肉 4 1 1
3 刀削面 1 1 3
2 东坡肉 3 2 1
4 麻辣鸡丝 2 1 1
table 2 : jerry 18100334566 2023/5/1 12/30/00
1 醋浇羊肉 1 1 2
3 麻婆豆腐 2 2 1
4 麻辣鸡丝 2 3 3
table 3 : jerry 18100334566 2023/5/1 12/30/00
1 醋浇羊肉 2 1 1
3 蜜汁灌藕 1 1 2
2 东坡肉 2 2 1
4 麻辣鸡丝 5 1 1
end
输出样例5:
在这里给出相应的输出。例如:
table 1:
1 醋浇羊肉 30
3 刀削面 30
2 东坡肉 38
4 麻辣鸡丝 15
table 2:
1 醋浇羊肉 60
3 麻婆豆腐 18
4 麻辣鸡丝 90
table 3:
1 醋浇羊肉 30
3 蜜汁灌藕 20
2 东坡肉 38
4 麻辣鸡丝 15
table 1: 113 113 川菜 1 稍辣 晋菜 4 稍酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣 晋菜 2 微酸
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
jerry 18100334566 191
tom 13605054400 113
输入样例6:
多用户多桌菜含代点菜。例如:
东坡肉 浙菜 25 T
油淋生菜 9
蜜汁灌藕 浙菜 10 T
刀削面 晋菜 10 T
醋浇羊肉 晋菜 30 T
麻婆豆腐 川菜 12 T
麻辣鸡丝 川菜 15 T
table 1 : tom 13605054400 2023/5/6 12/30/00
1 醋浇羊肉 4 1 1
3 刀削面 1 1 3
2 东坡肉 3 2 1
4 麻辣鸡丝 2 1 1
table 2 : jerry 18100334566 2023/5/1 12/30/00
1 1 醋浇羊肉 0 1 2
3 麻婆豆腐 2 2 1
4 麻辣鸡丝 2 3 3
table 3 : lucy 18957348763 2023/5/1 12/30/00
1 醋浇羊肉 2 1 1
3 蜜汁灌藕 1 1 2
2 东坡肉 2 2 1
4 麻辣鸡丝 5 1 1
end
输出样例6:
在这里给出相应的输出。例如:
table 1:
1 醋浇羊肉 30
3 刀削面 30
2 东坡肉 38
4 麻辣鸡丝 15
table 2:
1 table 2 pay for table 1 60
3 麻婆豆腐 18
4 麻辣鸡丝 90
table 3:
1 醋浇羊肉 30
3 蜜汁灌藕 20
2 东坡肉 38
4 麻辣鸡丝 15
table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
jerry 18100334566 118
lucy 18957348763 73
tom 13605054400 113
输入样例7:
错误的菜品记录和桌号记录,用户丢弃。例如:
东坡肉 25 T
油淋生菜 9
table 1 : tom 136050540 2023/5/1 12/30/00
2 东坡肉 3 2 1
end
输出样例7:
在这里给出相应的输出。例如:
wrong format wrong format
源码展示:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.time.LocalDate; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Dish[] dishes = new Dish[20]; SpecialDish[] specialDishes = new SpecialDish[20]; ArrayList<Table> tables = new ArrayList<>(); Table table = new Table(); Record[] records = new Record[20]; PersonPay[] personPays = new PersonPay[10]; Order order = new Order(); Menu menu = new Menu(); lastPrint print = new lastPrint(); int x = 0 , y = 0 , z = 0 , z1 = 0 , p = 0 , tableNum , dishNum , portion , num , unit_price , deleteNum , tasteNum; String menuName , dayTime , hourTime , name , tableName , telephoneNum , taste; boolean isHaveTable = false , forOther = false; for(;;){ String input = in.readLine(); if(input.equals("end")){ if(x!=0){ table.order = order; tables.add(x-1 , table); } print.tables = tables; print.personPays = personPays; print.print(); print.payPrint(); break; } String[] getInput = input.split(" "); if(input.matches("^(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})$")) { if(x != 0) { table.order = order; tables.add(x-1 , table); table = new Table(); y = 0; } isHaveTable = true; tableNum = Integer.parseInt(getInput[1]); tableName = getInput[3]; telephoneNum = getInput[4]; dayTime = getInput[5]; hourTime = getInput[6]; table.tableNum = tableNum; table.tableName = tableName; table.telephoneNum = telephoneNum; table.mainTime = dayTime; table.remainTime = hourTime; if(!table.timeJudgement1()){ isHaveTable = false; System.out.println("table " +table.tableNum + " out of opening hours"); continue; } order = new Order(); records = new Record[10]; if(x==0){ personPays[p] = new PersonPay(); personPays[p].name = tableName; personPays[p].telephoneNum = telephoneNum; p++; print.p = p; } else{ for(int i=0;i<p;i++){ if(personPays[i].name.equals(tableName)&&personPays[i].telephoneNum.equals(telephoneNum)) break; if(i==p-1){ personPays[p] = new PersonPay(); personPays[p].name = tableName; personPays[p].telephoneNum = telephoneNum; p++; print.p = p; } } } System.out.println("table "+tableNum+": "); x++; } else if(input.matches("^(\\S+)( )([1-9][0-9]*)$")){ int i = 0; menuName = getInput[0]; unit_price = Integer.parseInt(getInput[1]); if(z == 0) { dishes[0] = menu.addDish(menuName , unit_price); z++; } else{ for(;i < z ; i++) { if(menuName.equalsIgnoreCase(dishes[i].name)) { dishes[i].unit_price = unit_price; break; } } } if(i == z){ dishes[z] = menu.addDish(menuName , unit_price); z++; } menu.dishes = dishes; menu.dishNum = z; } else if(input.matches("^(\\S+)( )(\\S+)( )([1-9][0-9]*)( )(T)$")){ int i = 0; menuName = getInput[0]; taste = getInput[1]; unit_price = Integer.parseInt(getInput[2]); if(z1 == 0) { specialDishes[0] = menu.addSpecialDish(menuName , unit_price , taste); z1++; } else{ for(;i < z1 ; i++) { if(menuName.equalsIgnoreCase(specialDishes[i].name)) { specialDishes[i].unit_price = unit_price; break; } } } if(i == z1){ specialDishes[z1] = menu.addSpecialDish(menuName , unit_price , taste); z1++; } menu.specialDishes = specialDishes; menu.dishNum1 = z1; } else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){ if(!isHaveTable) continue; dishNum = Integer.parseInt(getInput[0]); name = getInput[1]; portion = Integer.parseInt(getInput[2]); num = Integer.parseInt(getInput[3]); if(menu.searthDish(name) != null) { records[y] = new Record(); records[y] = order.addARecord(dishNum , name , portion , num , menu); records[y].isSpecialDish = false; System.out.println(records[y].orderNum+" "+records[y].d.name+" "+records[y].getPrice()); y++; order.records = records; order.dishNum = y; } else{ System.out.println(name+" does not exist"); } } else if(input.matches("^([1-9][0-9]*)( )(\\S+)( )([0-9]+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")){ if(!isHaveTable) continue; dishNum = Integer.parseInt(getInput[0]); name = getInput[1]; tasteNum = Integer.parseInt(getInput[2]); portion = Integer.parseInt(getInput[3]); num = Integer.parseInt(getInput[4]); if(menu.searthSpecialDish(name) != null) { if(((menu.searthSpecialDish(name).taste.equals("川菜") && tasteNum >= 0 && tasteNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晋菜") && tasteNum>=0 && tasteNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tasteNum>=0 && tasteNum <= 3))){ records[y] = new Record(); records[y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tasteNum , forOther); records[y].isSpecialDish = true; System.out.println(records[y].orderNum+" "+records[y].d.name+" "+records[y].getPrice()); y++; order.records = records; order.dishNum = y; } } else{ System.out.println(name+" does not exist"); continue; } if(menu.searthSpecialDish(name).taste.equals("川菜")){ if(tasteNum<0||tasteNum>5) System.out.println("spicy num out of range :"+tasteNum); } if(menu.searthSpecialDish(name).taste.equals("晋菜")){ if(tasteNum<0||tasteNum>4) System.out.println("acidity num out of range :"+tasteNum); } if(menu.searthSpecialDish(name).taste.equals("浙菜")){ if(tasteNum<0||tasteNum>3) System.out.println("sweetness num out of range :"+tasteNum); } } else if(input.matches("([1-9][0-9]*)( )(delete)")) { if(!isHaveTable) continue; deleteNum = Integer.parseInt(getInput[0]); if(order.findRecordByNum(deleteNum) == 1){ order.delARecordByOrderNum(deleteNum); } else System.out.println("delete error;"); } else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) { if(!isHaveTable) continue; int t = Integer.parseInt(getInput[0]); dishNum = Integer.parseInt(getInput[1]); name = getInput[2]; portion = Integer.parseInt(getInput[3]); num = Integer.parseInt(getInput[4]); for(int i = 0;i<x-1;i++){ if(tables.get(i).tableNum == t){ if(menu.searthDish(name) != null) { records[y] = new Record(); records[y] = order.addARecord(dishNum , name , portion , num , menu); records[y].isSpecialDish = false; System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[y].getPrice()); y++; order.records = records; order.dishNum = y; } break; } } } else if(input.matches("^([1-9][0-9]*)( )([1-9][0-9]*)( )(\\S+)( )([0-9]*)( )([1-9][0-9]*)( )([1-9][0-9]*)$")) { if(!isHaveTable) continue; int t = Integer.parseInt(getInput[0]); dishNum = Integer.parseInt(getInput[1]); name = getInput[2]; tasteNum = Integer.parseInt(getInput[3]); portion = Integer.parseInt(getInput[4]); num = Integer.parseInt(getInput[5]); for(int i = 0;i<x-1;i++){ if(tables.get(i).tableNum == t){ if(menu.searthSpecialDish(name) != null) { if(((menu.searthSpecialDish(name).taste.equals("川菜") && tasteNum >= 1 && tasteNum <= 5) || (menu.searthSpecialDish(name).taste.equals("晋菜") && tasteNum <= 4) || (menu.searthSpecialDish(name).taste.equals("浙菜") && tasteNum <= 3))){ records[y] = new Record(); records[y] = order.addASpecialRecord(dishNum , name , portion , num , menu , tasteNum , !forOther); records[y].isSpecialDish = true; System.out.println(dishNum+" table "+table.tableNum+" pay for table "+t+" "+records[y].getPrice()); tables.get(i).giveTaste(name,num,tasteNum,menu); y++; order.records = records; order.dishNum = y; } } break; } } } else { System.out.println("wrong format"); } } } } class Dish{ String name; int unit_price; public int getPrice(int portion){ double getPrice=0; if(portion==1) getPrice=unit_price; else if(portion==2) getPrice=unit_price*1.5; else if(portion==3) getPrice=unit_price*2; return (int) Math.round(getPrice); } } class SpecialDish extends Dish{ String taste; } class Menu{ Dish[] dishes; SpecialDish[] specialDishes; int dishNum; int dishNum1; public Dish searthDish(String dishName){ for(int k=0;k<dishNum;k++) { if(dishName.equals(dishes[k].name)){ return dishes[k]; } } return null; } public SpecialDish searthSpecialDish(String dishName){ for(int k=0;k<dishNum1;k++) { if(dishName.equals(specialDishes[k].name)){ return specialDishes[k]; } } return null; } public Dish addDish(String dishName,int unit_price){ Dish addDish=new Dish(); addDish.name=dishName; addDish.unit_price=unit_price; return addDish; } public SpecialDish addSpecialDish(String dishName,int unit_price,String taste){ SpecialDish addDish=new SpecialDish(); addDish.name=dishName; addDish.unit_price=unit_price; addDish.taste=taste; return addDish; } } class Record{ int orderNum; Dish d; int portion; int num; int spicy; int acidity; int sweetness; boolean isSpecialDish; public int getPrice() { return d.getPrice(portion)*num; } } class Order { int dishNum; int allCommonPrice; int allSpecialPrice; Record[] records; public void addPortion() { for(int k = 0;k < dishNum-1;k++){ for(int l = k+1;l<dishNum;l++){ if(records[k].d.name.equals(records[l].d.name)&&records[k].portion == records[l].portion){ records[k].num+=records[l].num; records[l].num = 0; } } } } public void getTotalPrice(){ for(int k=0;k<dishNum;k++) { if(!records[k].isSpecialDish) allCommonPrice+=records[k].getPrice(); else allSpecialPrice+=records[k].getPrice(); } } public int getTotalPrice(double commonDiscount,double specialDiscount){ int all = 0; for(int k=0;k<dishNum;k++) { if(!records[k].isSpecialDish) all+=Math.round(records[k].getPrice()*commonDiscount); else all+=Math.round(records[k].getPrice()*specialDiscount); } return all; } public Record addARecord(int orderNum,String dishName,int portion,int num , Menu menu){ Record x=new Record(); x.d=menu.searthDish(dishName); x.orderNum=orderNum; x.portion=portion; x.num=num; return x; } public Record addASpecialRecord(int orderNum,String dishName,int portion,int num ,Menu menu,int tasteNum , boolean forOther){ Record x=new Record(); x.d=menu.searthSpecialDish(dishName); x.orderNum=orderNum; x.portion=portion; x.num=num; if(!forOther){ if(menu.searthSpecialDish(dishName).taste.equals("川菜")){ x.spicy = tasteNum; x.acidity = -1; x.sweetness = -1; } if(menu.searthSpecialDish(dishName).taste.equals("晋菜")){ x.spicy = -1; x.acidity = tasteNum; x.sweetness = -1; } if(menu.searthSpecialDish(dishName).taste.equals("浙菜")){ x.spicy = -1; x.acidity = -1; x.sweetness = tasteNum; } } else{ x.spicy = -1; x.acidity = -1; x.sweetness = -1; } return x; } public void delARecordByOrderNum(int orderNum){ for(int k=0;k<dishNum;k++) { if(orderNum==records[k].orderNum) { records[k].num=0; } } } public int findRecordByNum(int orderNum){ for(int k = 0; k<dishNum;k++) { if(records[k].orderNum==orderNum) { return 1; } } return 0; } } class Table{ int tableNum; String tableName; String telephoneNum; String mainTime; String remainTime; Order order; double commonDiscount; double specialDiscount; int averageSpicy; int averageAcidity; int averageSweetness; double spicyNum; double acidityNum; double sweetnessNum; public boolean timeJudgement1(){ String[] x = remainTime.split("/"); String[] y = mainTime.split("/"); int year = Integer.parseInt(y[0]); int month = Integer.parseInt(y[1]); int day = Integer.parseInt(y[2]); double hour = Double.parseDouble(x[0]); double minute = Double.parseDouble(x[1]); double second = Double.parseDouble(x[2]); LocalDate date = LocalDate.of(year,month,day); int weekOfDay = date.getDayOfWeek().getValue(); if (weekOfDay == 6 || weekOfDay == 7) { if (hour >= 10 && hour < 21 || hour == 9 && minute >= 30 || hour == 21 && minute < 30 ||hour == 21 && minute == 30 && second == 0) { specialDiscount = commonDiscount = 1; return true; } else { return false; } } if (weekOfDay >= 1 && weekOfDay <= 5) { if (hour > 17 && hour < 20 || hour == 17 && minute >= 0 || hour == 20 && minute < 30 || hour == 20 && minute == 30 && second == 0) { commonDiscount = 0.8; specialDiscount = 0.7; return true; } else if (hour > 10 && hour < 14 || hour == 10 && minute >= 30 || hour == 14 && minute < 30 || hour == 14 && minute == 30 && second == 0) { commonDiscount = 0.6; specialDiscount = 0.7; return true; } else { return false; } } return false; } public int tablePrice(){ return this.order.getTotalPrice(commonDiscount,specialDiscount); } public void getTaste(){ for(int k = 0;k<this.order.dishNum;k++){ if(order.records[k].isSpecialDish){ if(order.records[k].spicy!=-1){ averageSpicy+=order.records[k].spicy*order.records[k].num; spicyNum=order.records[k].num+spicyNum; } if(order.records[k].acidity!=-1){ averageAcidity+=order.records[k].acidity*order.records[k].num; acidityNum=order.records[k].num+acidityNum; } if(order.records[k].sweetness!=-1){ averageSweetness+=order.records[k].sweetness*order.records[k].num; sweetnessNum=order.records[k].num+sweetnessNum; } } } if(spicyNum!=0){ averageSpicy=(int)Math.round(averageSpicy/spicyNum); } if(acidityNum!=0){ averageAcidity=(int)Math.round(averageAcidity/acidityNum); } if(sweetnessNum!=0){ averageSweetness=(int)Math.round(averageSweetness/sweetnessNum); } } public String spicyLevel(){ if(averageSpicy==0) return "不辣"; if(averageSpicy==1) return "微辣"; if(averageSpicy==2) return "稍辣"; if(averageSpicy==3) return "辣"; if(averageSpicy==4) return "很辣"; if(averageSpicy==5) return "爆辣"; return null; } public String acidityLevel(){ if(averageAcidity==0) return "不酸"; if(averageAcidity==1) return "微酸"; if(averageAcidity==2) return "稍酸"; if(averageAcidity==3) return "酸"; if(averageAcidity==4) return "很酸"; return null; } public String sweetnessLevel(){ if(averageSweetness==0) return "不甜"; if(averageSweetness==1) return "微甜"; if(averageSweetness==2) return "稍甜"; if(averageSweetness==3) return "甜"; return null; } public void giveTaste(String name , int num , int tasteLevel , Menu menu){ if(menu.searthSpecialDish(name).taste.equals("川菜")){ averageSpicy+=tasteLevel*num; spicyNum+=num; } if(menu.searthSpecialDish(name).taste.equals("晋菜")){ averageAcidity+=tasteLevel*num; acidityNum+=num; } if(menu.searthSpecialDish(name).taste.equals("浙菜")){ averageSweetness+=tasteLevel*num; sweetnessNum+=num; } } } class lastPrint{ ArrayList<Table> tables = new ArrayList<>(); PersonPay[] personPays; int p; public void print(){ for (Table table : tables) { table.order.getTotalPrice(); table.getTaste(); if(table.spicyNum==0&&table.acidityNum==0&&table.sweetnessNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice()+" "); if(table.spicyNum!=0&&table.acidityNum==0&&table.sweetnessNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()); if(table.spicyNum==0&&table.acidityNum!=0&&table.sweetnessNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 晋菜 " +(int)table.acidityNum+" "+table.acidityLevel()); if(table.spicyNum==0&&table.acidityNum==0&&table.sweetnessNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 浙菜 " +(int)table.sweetnessNum+" "+table.sweetnessLevel()); if(table.spicyNum!=0&&table.acidityNum!=0&&table.sweetnessNum==0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()+" 晋菜 "+(int)table.acidityNum+" "+table.acidityLevel()); if(table.spicyNum!=0&&table.acidityNum==0&&table.sweetnessNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()+" 浙菜 "+(int)table.sweetnessNum+" "+table.sweetnessLevel()); if(table.spicyNum==0&&table.acidityNum!=0&&table.sweetnessNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 晋菜 " +(int)table.acidityNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.sweetnessNum+" "+table.sweetnessLevel()); if(table.spicyNum!=0&&table.acidityNum!=0&&table.sweetnessNum!=0) System.out.println("table " + table.tableNum + ": " + (table.order.allCommonPrice + table.order.allSpecialPrice) + " " + table.tablePrice() + " 川菜 " +(int)table.spicyNum+" "+table.spicyLevel()+" 晋菜 "+(int)table.acidityNum+" "+table.acidityLevel()+" 浙菜 "+(int)table.sweetnessNum+" "+table.sweetnessLevel()); } } public void payPrint(){ for(int k=0;k<p;k++){ for(Table table:tables){ if(table.tableName.equals(personPays[k].name)){ personPays[k].payPrice+=table.tablePrice(); } } } for(int k=0;k<p-1;k++){ for(int l=k+1;l<p;l++){ if(personPays[k].name.toLowerCase().compareTo(personPays[l].name.toLowerCase())>0) { PersonPay x; x=personPays[l]; personPays[l]=personPays[k]; personPays[k]=x; } } } for(int k=0;k<p;k++){ System.out.println(personPays[k].name+" "+personPays[k].telephoneNum+" "+personPays[k].payPrice); } } } class PersonPay{ String name; String telephoneNum; int payPrice; }
类间关系:
分析:
本题的主要难点在于对于特殊菜味道的处理,以及特殊代点菜对于价格和味道的处理和最后价格的统计。首先关于特殊菜味道的处理:在Order中增加三种不同的味道,当订单中含有特殊菜的时候会赋值给其中一种味道数据,没有则都赋值-1,这样最后处理味道只需要简单的计算即可。对于代点菜价格和味道的处理:在我上一个建立的框架上,如若仍然按照普通菜处理代点菜的方式处理肯定会出错,所以本题我在Table类中增加giveTaste方法,在Main类中对于数据进行处理,若识别到代点特殊菜,则会执行该方法,同时当前桌仍按普通菜处理,并且不给此特殊菜的味道赋值,这样就能达到题目要求的效果,关于计价,仍按照题目要求的方式即可。最后价格:对于此方式,我的解决方式为在桌号信息处理过程中若出现新个人信息,则创建新的personPay,用于储存用户信息,最后将名字与电话号信息相同的桌号价格求和即可。本题同样没有较大的卡点,唯一问题即当输出某桌信息时,若本桌的味道没有,则输出语句的最后有一个空格,注意即可。本题的设计思路:我在菜单四的基础上删除了原有的对数据的处理方法,在特殊菜类中增加了用于判断味道的String数据,在Record类中加入关于一桌菜的味道的计算,在Table中加入对于味道判断的方法,最后价格的计算放在personPay类中。
总结:
一、出错反思
bug通常来自三种原因:
1、对题面理解的疏忽
2、对自己代码架构细节出现问题(使用错误方法)
3、粗心导致
4、知识的缺乏
二、学到的东西
1、对正则表达式的使用
2、熟悉理解了类的设计与使用;
3、对面向对象编程(封装性)有了初步的认识;
4、能够自如的使用eclispe编译器;
5、对于各种报错能够理解、从容的去修改;
6、能够熟练对代码进行调试,查找调试过程中代码的各类数据的变化等;
7、当然,我认为最大的收获,还是对与Java这门语言有了一定的理解,能够逐渐窥见面向对象的神秘之处;
8、对于Java的学习还是不能够松懈,不能只跟着老师的进度走,需要将进度提高一些,否则面对大作业就会显得吃力了。