题目集四:
设计点菜计价程序,根据输入的信息,计算并输出总价格。
输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。
菜单由一条或多条菜品记录组成,每条记录一行
每条菜品记录包含:菜名、基础价格 两个信息。
订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。
桌号标识独占一行,包含两个信息:桌号、时间。
桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。
点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。
不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。
删除记录格式:序号 delete
标识删除对应序号的那条点菜记录。
如果序号不对,输出"delete error"
代点菜信息包含:桌号 序号 菜品名称 份额 分数
代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。
程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。
每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。
折扣的计算方法(注:以下时间段均按闭区间计算):
周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。
周末全价,营业时间:9:30-21:30
如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"
参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }
菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish\[\] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish addDish(String dishName,int unit_price)//添加一道菜品信息
}
点菜记录类:保存订单上的一道菜品记录
Record {
int orderNum;//序号\\
Dish d;//菜品\\
int portion;//份额(1/2/3代表小/中/大份)\\
int getPrice()//计价,计算本条记录的价格\\
}
订单类:保存用户点的所有菜的信息。
Order {
Record\[\] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
delARecordByOrderNum(int orderNum)//根据序号删除一条记录
findRecordByNum(int orderNum)//根据序号查找一条记录
}
### 输入格式:
桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
菜品记录格式:
菜名+英文空格+基础价格
如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。
点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
删除记录格式:序号 +英文空格+delete
代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数
最后一条记录以“end”结束。
### 输出格式:
按输入顺序输出每一桌的订单记录处理信息,包括:
1、桌号,格式:table+英文空格+桌号+”:”
2、按顺序输出当前这一桌每条订单记录的处理信息,
每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名
如果删除记录的序号不存在,则输出“delete error”
最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价
本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。
输入格式:
桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)
菜品记录格式:
菜名+英文空格+基础价格
如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。
点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
删除记录格式:序号 +英文空格+delete
代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数
最后一条记录以“end”结束。
输出格式:
按输入顺序输出每一桌的订单记录处理信息,包括:
1、桌号,格式:table+英文空格+桌号+“:”+英文空格
2、按顺序输出当前这一桌每条订单记录的处理信息,
每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名
如果删除记录的序号不存在,则输出“delete error”
最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价
本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。
输入样例:
在这里给出一组输入。例如:
麻婆豆腐 12 油淋生菜 9 table 1 2023/3/22 12/2/3 1 麻婆豆腐 2 2 2 油淋生菜 1 3 end
输出样例:
在这里给出相应的输出。例如:
table 1:
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 38
输入样例1:
在这里给出一组输入。例如:
table 1:
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 22
输入样例2:
在这里给出一组输入。例如:
麻婆豆腐 12 油淋生菜 9 table 1 2023/3/22 16/59/59 1 麻婆豆腐 2 2 2 油淋生菜 1 3 1 delete end
输出样例2:
在这里给出相应的输出。例如:
table 1: 1 麻婆豆腐 36 2 油淋生菜 27 table 1 out of opening hours
import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; class Dish { public String name;//菜品名称 int unit_price; //单价 int getPrice(int portion) { if(portion==1) { return unit_price; } else if (portion == 2) { return (int) Math.round(unit_price*1.5); } else if (portion ==3) { return unit_price*2; } else return 0; }//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) } class Menu { Dish[] dishs ;//菜品数组,保存所有菜品信息 Dish searthDish(String dishName) { Dish dish = new Dish(); dish.name = "NULL"; dish.unit_price = 0; int site = -1; for(int i=0;i< dishs.length;i++) { if(dishName.equals(dishs[i].name)) site = i; } if(site!=-1) return dishs[site]; else { System.out.println(dishName + " does not exist"); return dish; } }//根据菜名在菜谱中查找菜品信息,返回Dish对象。 } class Record { int DingCaiTale; int ShangCaiTable; int orderNum;//序号 Dish d;//菜品 int portion;//份额(1/2/3代表小/中/大份) int num;//数量 void output(){ if(DingCaiTale==ShangCaiTable) { System.out.println(orderNum+" "+d.name+" "+d.getPrice(portion)*num); } else { System.out.println(orderNum+" table "+DingCaiTale+" pay for table "+ShangCaiTable+" "+d.getPrice(portion)*num); } } int getPrice() { int nowprice = d.getPrice(portion)*num; return nowprice; }//计价,计算本条记录的价格 } class Order { Record[] records ;//保存订单上每一道的记录 int getTotalPrice(Menu menu) { //计算订单的总价 int sum = 0; for(int i=0;i<records.length;i++) { sum+=records[i].getPrice(); } return sum; } Record addARecord(int dingcaizhuo,int shangcaizhuo,int orderNum,String dishName,int portion,int num,Menu menu) { //添加一条菜品信息到订单中。 Record records = new Record(); records.DingCaiTale = dingcaizhuo; records.ShangCaiTable = shangcaizhuo; records.d =menu.searthDish(dishName); records.orderNum = orderNum; records.portion = portion; records.num = num; if (!records.d.name.equals("NULL")) records.output(); return records; } void delARecordByOrderNum(int orderNum){ //根据序号删除一条记录 if(findRecordByNum(orderNum)!=null) { Record RE = findRecordByNum(orderNum);//.num = 0 RE.num = 0; RE.orderNum = 0; } else { System.out.println("delete error;"); } } Record findRecordByNum(int orderNum){ //根据序号查找一条记录 for(int i=0;i<records.length;i++){ if(records[i].orderNum == orderNum){ return records[i]; } }return null; } } class Table{ int Table_num=0; Order order ; int time; int dayOfWeek; public double GetDiscount(){ int LiuZhe_min = 103000; int LiuZhe_max = 143000; int BaZhe_min = 170000; int BaZhe_max = 203000; int WuZheKou_min = 100000; int WuZheKou_max = 213000; if (dayOfWeek>=1&&dayOfWeek<=5) { if(this.time>=LiuZhe_min&&this.time<=LiuZhe_max) return 0.6; else if (this.time>=BaZhe_min&&this.time<=BaZhe_max) return 0.8; else System.out.println("table "+Table_num+" out of opening hours"); } else if (dayOfWeek>=6) { if(this.time>=WuZheKou_min&&this.time<=WuZheKou_max) return 1; else System.out.println("table "+Table_num+" out of opening hours"); } return 0; } public int getDiscountedPrice(Menu menu) { //System.out.println(order.records); return (int) Math.round(order.getTotalPrice(menu)*GetDiscount()); } } public class Main { public static void main(String[] args) { Menu menu = new Menu(); //Order order = new Order(); Scanner sc = new Scanner(System.in); ArrayList<String> Input = new ArrayList<>(); ArrayList<String> DAIDIANCANInput = new ArrayList<>(); ArrayList<String> dishInput = new ArrayList<>(); ArrayList<String> DeleteInput = new ArrayList<>(); Map<Integer, List<String>> orderRecords = new HashMap<>(); List <String> DAY = new ArrayList<>(); List <String> TIME = new ArrayList<>(); String input = sc.nextLine(); //进行输入 int tableNumber = -1,table_Count = 0; while(!input.equals("end")) { if(input.startsWith("table")) { table_Count++; String[] tableInfo = input.split("\\s"); tableNumber = Integer.parseInt(tableInfo[1]); DAY.add(tableInfo[2]); TIME.add(tableInfo[3]); orderRecords.put(tableNumber, new ArrayList<>()); } else if(input.charAt(0)>='0'&&input.charAt(0)<='9') { List<String> orders = orderRecords.get(tableNumber); orders.add(input); } else { dishInput.add(input); } input = sc.nextLine(); } //输入完成 int dishSize = dishInput.size(); //保存菜单 menu.dishs = new Dish[dishSize]; for(int i=0;i<dishSize;i++) { menu.dishs[i] = new Dish(); menu.dishs[i].name = dishInput.get(i).split(" ")[0]; menu.dishs[i].unit_price = Integer.parseInt(dishInput.get(i).split(" ")[1]); } Table[] table = new Table[table_Count]; for(int i=0;i<table_Count;i++) { table[i] = new Table(); table[i].order = new Order(); } int Temp_input=0; for (Map.Entry<Integer, List<String>> entry : orderRecords.entrySet()) {//开始分桌遍历 int tableNum = entry.getKey(),input_size = 0; //开始处理菜单 System.out.println("table " + tableNum +": "); List<String> orders = entry.getValue(); Input.clear(); dishInput.clear(); DeleteInput.clear(); DAIDIANCANInput.clear(); for (String order1 : orders) { if(order1.matches("\\d+\\s+\\D+\\s+\\d+\\s+\\d+") ) { Input.add(order1); } else if (order1.matches("\\d+\\s+\\d+\\s+\\D+\\s+\\d+\\s+\\d+")) { DAIDIANCANInput.add(order1); //System.out.println("table " + tableNum +": "+order1); //System.out.println(tableNum+" 代点餐"); } else if (order1.matches("\\d+\\s+delete")) { DeleteInput.add(order1); } } input_size = Input.size()+DAIDIANCANInput.size(); //System.out.println(input_size+"*****************************************"); table[Temp_input].order.records= new Record[input_size]; //System.out.println(Temp_input+ "***********************"); int ii=0; for(ii=0;ii<Input.size();ii++) { //格式化存入点菜记录 table[Temp_input].order.records[ii] = new Record();//初始化 String[] temp= Input.get(ii).split(" "); table[Temp_input].order.records[ii] = table[Temp_input].order.addARecord(tableNum,tableNum, //点菜桌号+上菜桌号 Integer.parseInt(temp[0]), temp[1], //编号+名称 Integer.parseInt(temp[2]), Integer.parseInt(temp[3]),menu); //份数+份额 } for(;ii<input_size;ii++) { table[Temp_input].order.records[ii] = new Record();//初始化 String[] temp= DAIDIANCANInput.get(ii-Input.size()).split(" "); table[Temp_input].order.records[ii] = table[Temp_input].order.addARecord(tableNum,Integer.parseInt(temp[0]),//点菜桌号+上菜桌号 Integer.parseInt(temp[1]), temp[2], //编号+名称 Integer.parseInt(temp[3]), Integer.parseInt(temp[4]),menu); //份数+份额 } for(int i=0;i<DeleteInput.size();i++) { //格式化修正点菜记录 String[] temp = DeleteInput.get(i).split(" "); table[Temp_input].order.delARecordByOrderNum(Integer.parseInt(temp[0])); } //处理时间和日期 String DAYTEMP = DAY.get(Temp_input); String[] ymd = DAYTEMP.split("/"); String year1 = ymd[0]; String month1 = ymd[1]; String RiZi1 = ymd[2]; if(month1.length()==1){ month1 = "0"+month1; } if(RiZi1.length()==1){ RiZi1 = "0"+RiZi1; } DAYTEMP = year1+"/"+month1+"/"+RiZi1; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); LocalDate localDate = LocalDate.parse(DAYTEMP, formatter); DayOfWeek dayOfWeek = localDate.getDayOfWeek(); //日期->星期处理完毕 //开始处理时间(格式化) Pattern pattern = Pattern.compile("^([01]\\d|2[0-3])/([0-5]\\d)/([0-5]\\d)$"); // 定义正则表达式 String time = TIME.get(Temp_input); Matcher matcher = pattern.matcher(time); String[] parts = time.split("/"); String hour = parts[0].length() == 1 ? "0" + parts[0] : parts[0]; String minute = parts[1].length() == 1 ? "0" + parts[1] : parts[1]; String second = parts[2].length() == 1 ? "0" + parts[2] : parts[2]; String correctedTime = hour + minute + second; ///时间处理完毕,开始存入类数组中第Temp_input项 table[Temp_input].Table_num = tableNum; //设置桌号 table[Temp_input].dayOfWeek = dayOfWeek.getValue(); //设置点餐日期(星期) int IntTime =Integer.parseInt(correctedTime); table[Temp_input].time = IntTime; //设置点餐时间 Temp_input++; } for (int i=0;i<table.length;i++) { int out = table[i].getDiscountedPrice(menu); if(out!=0) { System.out.println("table " + table[i].Table_num + ": " + out); } } } }
分析:
一个简单的菜单点餐系统,包括菜品信息、订单记录、桌号和折扣计算等功能。下面分析一下代码结构:
Dish类:表示菜品,包括菜品名称、单价以及计算菜品价格的方法。
Menu类:表示菜单,包括菜品数组和根据菜名查找菜品信息的方法。
Record类:表示订单记录,包括点餐桌号、上菜桌号、菜品、份数、数量以及输出记录和计价的方法。
Order类:表示订单,包括订单记录数组、计算订单总价、添加记录、删除记录和查找记录的方法。
Table类:表示桌号,包括桌号、订单、时间、星期和计算折扣价的方法。
Main类:包括主要的交互逻辑和输入处理部分,在main方法中进行了整个系统的流程控制。
在Main类的main方法中,通过Scanner获取用户输入,然后根据输入内容进行菜单、订单和桌号的初始化。接着遍历所有的订单记录,对每一张订单进行处理,包括点菜记录的格式化存储、时间和日期的处理,最后计算折扣价并输出结果。
整体结构清晰,功能模块化,代码逻辑相对简单明了。
从键盘录入一段英文文本(句子之间的标点符号只包括“,”或“.”,单词之间、单词与标点之间都以" "分割。
要求:按照每个单词的长度由高到低输出各个单词(重复单词只输出一次),如果单词长度相同,则按照单词的首字母顺序(不区分大小写,首字母相同的比较第二个字母,以此类推)升序输出。
输入格式:
一段英文文本。
输出格式:
按照题目要求输出的各个单词(每个单词一行)。
输入样例:
Hello, I am a student from China.
输出样例:
student China Hello from am a I
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); Order order = new Order(); Menu menu = new Menu(); String time = ""; String s = sc.nextLine(); while (!s.equals("end")){ int flag = 0; boolean flag1 = s.matches("[\\S]*\\s[0-9]*"); boolean flag2 = s.matches("table\\s\\d*\\s\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}/\\d{1,2}/\\d{1,2}"); boolean flag3 = s.matches("\\d*\\s[\\S]*\\s\\d\\s\\d*"); boolean flag4 = s.matches("\\d*\\s\\d*\\s[\\S]*\\s\\d\\s\\d*"); boolean flag5 = s.matches("\\d*\\sdelete"); String[] str = s.split(" "); if(flag1){ String name = str[0]; int portion = Integer.parseInt(str[1]); menu.addDish(name, portion); } else if(flag2){ time = s; System.out.println(time.substring(0,7) + ": "); } else if(flag3){ int orderNum = Integer.parseInt(str[0]); String name = str[1]; int portion = Integer.parseInt(str[2]); int num = Integer.parseInt(str[3]); if(menu.searthDish(name)!=null) { order.addARecord(orderNum, name, portion, num,menu,time,flag); } else { System.out.println(name+" does not exist"); } } else if(flag4){ flag = Integer.parseInt(str[0]); int orderNum = Integer.parseInt(str[1]); String name = str[2]; int portion = Integer.parseInt(str[3]); int num = Integer.parseInt(str[4]); if(menu.searthDish(name)!=null) { order.addARecord(orderNum, name, portion, num,menu,time,flag); } else { System.out.println(name+" does not exist"); } } else if(flag5){ int ordernum = Integer.parseInt(str[0]); order.delARecordByOrderNum(ordernum); } s = sc.nextLine(); } order.tableprice = order.getTotalPrice(); Set<String> keys = order.tableprice.keySet(); for (String key : keys) { int value = order.tableprice.get(key); if(value!=-1) { if(key.equals(time)) { System.out.print(key.substring(0, 7) + ": " + value); } else { System.out.println(key.substring(0, 7) + ": " + value); } } } } }
期中考试
创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积
输入格式:
输入圆的半径,取值范围为(0,+∞)
,输入数据非法,则程序输出Wrong Format
,注意:只考虑从控制台输入数值的情况
输出格式:
输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)
输入样例:
在这里给出一组输入。例如:
2.35
输出样例:
在这里给出相应的输出。例如:
17.35
import java.util.Scanner; import java.math.*; class Circle { public double radius; public double circleArea; static final double PI = 3.1415926; public Circle(double radius) { setRadius(radius); } public void setRadius(double radius) { if (radius > 0) { this.radius = radius; } else { System.out.println("Wrong Format"); } } public double getArea() { circleArea = Math.PI * radius * radius; return circleArea; } public double getCircleArea() { return circleArea; } public void setCircleArea(double circleArea) { this.circleArea = circleArea; } public double getRadius() { return radius; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double radius = scanner.nextDouble(); Circle circle = new Circle(radius); if (circle.radius > 0) { double area = circle.getArea(); System.out.println(String.format("%.2f", area)); } } }
设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:
输入格式:
分别输入两个坐标点的坐标值x1,y1,x2,y2。
输出格式:
输出该矩形的面积值(保留两位小数)。
import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); double x1 = in.nextDouble(); double y1 = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); Point p1 = new Point(x1, y1); p1.setX(x1); p1.setY(y1); Point p2 = new Point(x2, y2); Rectangle rectangle1 = new Rectangle(); rectangle1.setTopLeftPoint(p1); rectangle1.setLowerRightPoint(p2); Rectangle rectangle = new Rectangle(rectangle1.getTopLeftPoint() , rectangle1.getLowerRightPoint()); String str= String.format("%.2f", (float)rectangle.getArea()); str = String.format("%.2f", (float)rectangle.getArea()); System.out.println(""+str); } } class Rectangle{ public Point topLeftPoint; public Point lowerRightPoint; public Rectangle(){ } public String toString() { return "Rectangle{" + "topLeftPoint=" + topLeftPoint + ", lowerRightPoint=" + lowerRightPoint + '}'; } public Rectangle(Point topLeftPoint, Point lowerRightPoint){ this.topLeftPoint = topLeftPoint; this.lowerRightPoint = lowerRightPoint; } 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; } public double getLength(){ return lowerRightPoint.getX() - topLeftPoint.getX(); } class MyComparator implements Comparator<String> { public int compare(String arg0, String arg1) { if(arg0.length()>arg1.length()) return -1; else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return 1; } } public double getHeight(){ return topLeftPoint.getY() - lowerRightPoint.getY(); } public double getArea(){ return this.getLength() * this.getHeight(); } } class Point { public double x; public double y; public String toString() { return "Point{" + "x=" + x + ", y=" + 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; } class MyComparator2 implements Comparator<String> { public int compare(String arg0, String arg1) { if(arg0.length()>arg1.length()) return -1; else return 1; } } public double getY() { return y; } public void setY(double y) { this.y = y; } }
输入样例:
在这里给出一组输入。例如:
6 5.8 -7 8.9
输出样例:
在这里给出相应的输出。例如:
40.30
将测验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;
}
}
其中,printArea(Shape shape)
方法为定义在Main类中的静态方法,体现程序设计的多态性。
输入格式:
输入类型选择(1或2,不考虑无效输入)
对应图形的参数(圆或矩形)
输出格式:
图形的面积(保留两位小数)
输入样例1:
1
5.6
输出样例1:
在这里给出相应的输出。例如:
import java.util.Comparator; import java.text.ParseException; import java.time.DateTimeException; import java.time.Duration; import java.util.Scanner; public class Main { public static void printArea(Shape shape) { System.out.println(String.format("%.2f", shape.getArea())); } public Main() { } public static void main(String[] args) { Scanner input = new Scanner(System.in); int choice = input.nextInt(); switch (choice) { case 1: double radius = input.nextDouble(); Shape circle = new Circle(radius); printArea(circle); break; case 2: double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); Point topLeft = new Point(x1, y1); Point lowerRight = new Point(x2, y2); Shape rectangle = new Rectangle(topLeft, lowerRight); printArea(rectangle); break; } } } class Shape { public double getArea() { return 0; } } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { if (this.radius > 0) { return Math.PI * this.radius * this.radius; } else { return 0; } } } class Rectangle extends Shape { public Point topLeft; public Point lowerRight; public Point getTopLeft() { return topLeft; } public void setTopLeft(Point topLeft) { this.topLeft = topLeft; } public Point getLowerRight() { return lowerRight; } @Override public String toString() { return "Rectangle{" + "topLeft=" + topLeft + ", lowerRight=" + lowerRight + '}'; } public void setLowerRight(Point lowerRight) { this.lowerRight = lowerRight; } public Rectangle(Point topLeft, Point lowerRight) { this.topLeft = topLeft; this.lowerRight = lowerRight; } class MyCompar2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { if(arg0.length()>arg1.length()) return -1; else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return 1; } } public double getArea() { double length = this.lowerRight.getX() - this.topLeft.getX(); double height = this.topLeft.getY() - this.lowerRight.getY(); return length * height; } } class Point { public double x; public double y; public Point(double x) { this.x = x; } public void setX(double x) { this.x = x; } public void setY(double y) { this.y = y; } @Override public String toString() { return "Point{" + "x=" + x + ", y=" + y + '}'; } class My implements Comparator<String> { @Override public int compare(String arg0, String arg1) { if(arg0.length()==1) return -1; else return 1; } } public Point(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; } }
98.52
输入样例2:
2
5.6
-32.5
9.4
-5.6
输出样例2:
在这里给出相应的输出。例如:
102.22
JVM(Java Virtual Machine)的功能是?
跨平台编译Java程序。
跨平台运行Java程序。
跨平台调试Java程序。
跨平台编写Java程序。
在Java中,下列选项中关于二维数组的定义,格式错误的是?
int [][]arr = new int[3][4]
int [][]arr = new int[3][]
int[][] arr = new int[][4]
int [][]arr = {{1,2},{3,4,5},{6}}
在Java中,下列关于方法的描述中,正确的是?
方法是对一段可执行代码的封装
当方法没有返回值时,返回值类型可以不写
当方法没有返回值时,不能有return语句
方法必须要有参数
在Java中,当局部变量和成员变量名称冲突时,可以通过以下哪个关键字访问成员变量?
this
super
class
throw
下列关于Java语言变量的描述,错误的是?
在Java程序中要使用一个变量,必须先对其进行声明。
类变量可以使用对象名进行调用。
变量不可以在其作用域之外使用。
成员变量必须写在成员方法之前。
在Java中,下面说法正确的是?
final可修饰类、属性、方法
abstract不可修饰类、属性、方法
定义抽象方法需有定义方法的返回类型、名称、参数列表和方法体
用final修饰的变量,在程序中可对这个变量的值进行更改
Java中,有以下代码,划线部分可替换为下列调用语句选项,哪一个调用语句是错误的?
class A{
int i;
static String s;
void method1() { }
static void method2() { }
public static void main(String[] args){
A a=new A();
________________//替换为某选项语句
}
}
System.out.println(a.i);
a.method1();
A.method1();
A.method2()
Java中,为了使某个包mypg的成员在当前类中可见,可以使用的语句是?
import mypg.*;
package mypg.*;
static import mypg.*;
import mypg.* as default;
Java中用于定义类成员的访问控制权的一组关键字是?
class,float,double,public
float,boolean,int,long
char,extends,float,double
public,private, protected
Java中以下关于protected的说法中,正确的是?
protected修饰的方法,只能给子类使用
protected修饰的类,类中的所有方法只能给子类使用
如果一个类的成员被protected修饰,那么这个成员既能被同一包下的其它类访问,也能被不同包
下该类的子类访问
以上都不对
在Java中,下列说法正确的是?
JAVA程序的main方法必须写在类里面
一个JAVA类中可以有多个main方法
JAVA程序中类名必须与文件名一样
JAVA程序的main方法中如果只有一条语句,可以不用{}(大括号)括起来
Java中变量命名规范说法正确的是?
变量名由字母、下划线、数字、$符号随意组成;
变量名不能以数字作为开头;
A和a在java中是同一个变量名;
不同类型的变量,可以起相同的名字;
在Java中,以下哪个不是合法的标识符?
STRING
x3x;
void
de$f
Java中,对象的特征在类中表示为变量,称为类的?
对象
属性
方法
数据类型
下列选项中关于Java中封装的说法错误的是?
封装就是将相关属性私有化,提供公有的访问方法访问私有属性
对属性的访问建议提供setter、getter等方法
setter方法用于修改属性的值,getter方法用于读取属性的值
包含属性的类都必须封装属性,否则无法通过编译
Java中关于this关键字的说法正确的是?
this关键字是在对象内部指代自身的引用
this关键字可以在类中的任何位置使用
this关键字和类关联,而不是和特定的对象关联
同一个类的不同对象共用一个this
在Java中,所有类的根类是?
java.lang.Class
java.lang.Object
java.applet.Applet
java.awt.Frame
在Java中,以下构造方法的调用方式中,正确的是?
按照一般方法调用
由用户直接调用
只能通过new自动调用
被系统调用
Java中十进制数16的16进制表示方式是?
0x10
0x16
0xA
16
Java语言中,下列说法哪个正确?
Java中不需要定义类,就能设计主程序
每个对象中必须有属性和方法
对象的属性可以是简单变量,也可以是一个对象
对象的属性必须是简单变量
Java中,下列说法哪个正确?
一个应用程序可以包含多个源文件
一个Java源文件中只能有一个类
一个Java源文件中可以有多个公共类
一个Java源文件只能供一个程序使用
有以下Java程序段,请问编译时会产生错误的是哪一行?(根据行号选择)
1 public class Test {
2 int m, n;
3 public Test() {}
4 public Test(int a) { m=a; }
5 public static void main(String arg[]) {
6 Test t1,t2;
7 int j,k;
8 j=0; k=0;
9 t1=new Test();
10 t2=new Test(j,k);
11 }
12 }
第3行
第5行
第6行
第10行
Java源文件和编译后的文件扩展名分别为( )。
.class和 .java
.java和 .class
.class和 .class
.java和 .java
以下有关java.lang.Exception异常类的描述中,不正确的是( )。
该类是一个公共类
该类是Throwable类的子类
该类实现了Throwable接口
该类可以序列化
Java中,以下代码输出的结果是
public class Main {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5};
arr = null;
System.out.println(arr[1]);
}
}
1
2
编译报错,空指针异常
下标越界异常
Java中最基本的单位是。
方法
对象
类
接口
下列不属于Java基本数据类型的是( )
int
String
float
double
本题在菜单计价程序-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.util.*; import static java.lang.Math.pow; import java.util.Scanner; import java.text.DecimalFormat; import java.util.Set; import java.util.HashSet; import java.util.Scanner; import static java.lang.System.exit; //特色菜 class dish { //特色菜的属性:菜名、口味、价格 String name; String taste; int price; public dish(String name, String taste, int price) { this.name = name; this.taste = taste; this.price = price; } public dish(String name, int price) { this.name = name; this.price = price; } public String getName() { return name; } public String getTaste() { return taste; } public int getPrice() { return price; } public void setName(String name) { this.name = name; } public void setTaste(String taste) { this.taste = taste; } public void setPrice(int price) { this.price = price; } } class MyCompar2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } //普通菜 class MyCompar2sator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } //桌号 //table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS) class Table { int num; String name; String phone; String date; //时间 String time; public Table(int num, String name, String phone, String date, String time) { this.num = num; this.name = name; this.phone = phone; this.date = date; this.time = time; } public int getNum() { return num; } public String getName() { return name; } class MyCompar2ssator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } public String getPhone() { return phone; } public String getDate() { return date; } public String getTime() { return time; } public void setNum(int num) { this.num = num; } public void setName(String name) { this.name = name; } class MyComparsss2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } public void setPhone(String phone) { this.phone = phone; } public void setDate(String date) { this.date = date; } public void setTime(String time) { this.time = time; } } class User { int num; String name; String phone; int price; public User(int num, String name, String phone, int price) { this.num = num; this.name = name; this.phone = phone; this.price = price; } public String getName() { return name; } public String getPhone() { return phone; } public int getPrice() { return price; } public int getNum() { return num; } public void setName(String name) { this.name = name; } public void setPhone(String phone) { this.phone = phone; } public void setPrice(int price) { this.price = price; } public void setNum(int num) { this.num = num; } } //点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 特色菜 class recorde { int tableNum1; int num; String name; String taste; int portion; int count; class MyCompar2assstor implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } public recorde(int num, String name, String taste, int portion, int count) { this.num = num; this.name = name; this.taste = taste; this.portion = portion; this.count = count; } //普通点菜 //序号、菜名、份额、份数 public recorde(int num, String name, int portion, int count) { this.num = num; this.name = name; this.portion = portion; this.count = count; } public recorde(int tableNum1, int num, String name, String taste, int portion, int count) { this.tableNum1 = tableNum1; this.num = num; this.name = name; this.taste = taste; this.portion = portion; this.count = count; } public int getTableNum1() { return tableNum1; } public int getNum() { return num; } public String getName() { return name; } public String getTaste() { return taste; } public int getPortion() { return portion; } public int getCount() { return count; } public void setNum(int num) { this.num = num; } public void setName(String name) { this.name = name; } public void setTaste(String taste) { this.taste = taste; } class MyCompar2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } } public class Main { //特色菜记录数 public static int dishNum = 0; public static dish dish[] = new dish[100]; //普通菜记录数 //桌号 public static int tableNum = 0; public static Table table[] = new Table[100]; public static recorde recorde[][] = new recorde[100][100]; public static int Sum[] = new int[100]; public static int userNum = 0; public static User user[] = new User[100]; public static boolean flag1=false; public static boolean flag2=false; public static boolean flag3=false; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { String str = scanner.nextLine(); if (str.equals("end")) break; //菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T" // //例如:麻婆豆腐 川菜 9 T //普通菜的输入格式:菜品名+英文空格+基础价格 //如果格式不对,直接输出wrong format String[] str1 = str.split(" "); //多条记录 //重复记录 if (str1.length == 4 && str1[3].equals("T")) { dishNum++; String name = str1[0]; String taste = str1[1]; int price = Integer.parseInt(str1[2]); dish[dishNum] = new dish(name, taste, price); int num=find4(name); if(num!=-1) dish[num] = new dish(name, taste, price); } else if (str1.length == 2) { String name=str1[0]; int price=Integer.parseInt(str1[1]); dish[++dishNum] = new dish(str1[0], Integer.parseInt(str1[1])); int num=find4(name); if(num!=-1) dish[num] = new dish(name, price); } else if(str1[2].equals("T")||str1[1].equals("T")||str1[0].equals("T")) { flag1=true; } //输入格式: //table 1 : tom 13605054400 2023/5/1 20/30/00 else if (str1[0].equals("table"))//如果是table { tableNum++; if(str1.length!=7) { flag2=true; continue; } int num = Integer.parseInt(str1[1]); String name = str1[3]; String phone = str1[4]; String date = str1[5]; String time = str1[6]; if(!check2(name,phone)) flag2=true; table[tableNum] = new Table(num, name, phone, date, time); } //点菜 //点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 else if (str1[0].length() == 1 && str1.length == 5) { Sum[tableNum]++; int num = Integer.parseInt(str1[0]); String name = str1[1]; String taste = str1[2]; int portion = Integer.parseInt(str1[3]); int count = Integer.parseInt(str1[4]); recorde[tableNum][Sum[tableNum]] = new recorde(num, name, taste, portion, count); //1 醋浇羊肉 30 } //普通点菜 //序号、菜名、份额、份数 else if (str1[0].length() == 1 && str1.length == 4) { Sum[tableNum]++; int num = Integer.parseInt(str1[0]); String name = str1[1]; int portion = Integer.parseInt(str1[2]); int count = Integer.parseInt(str1[3]); recorde[tableNum][Sum[tableNum]] = new recorde(num, name, portion, count); } //代点菜1 1 醋浇羊肉 0 1 2 else if (str1[0].length() == 1&& str1.length == 6) { Sum[tableNum]++; int tableNum1 = Integer.parseInt(str1[0]); int num = Integer.parseInt(str1[1]); String name = str1[2]; String taste = str1[3]; int portion = Integer.parseInt(str1[4]); int count = Integer.parseInt(str1[5]); recorde[tableNum][Sum[tableNum]] = new recorde(num, tableNum1, name, taste, portion, count); } //序号 delete //删除记录格式:序号 delete // //标识删除对应序号的那条点菜记录。 // //如果序号不对,输出"delete error" else if (str1[1].equals("delete")) { int num = Integer.parseInt(str1[0]); if (num > Sum[tableNum] || num < 1) { System.out.println("delete error"); exit(0); continue; } for (int i = num; i < Sum[tableNum]; i++) { recorde[tableNum][i] = recorde[tableNum][i + 1]; } Sum[tableNum]--; } else if(tableNum>=1) { if(str1[0].length()!=1) flag3=true; if(str1[1].length()<=2) flag3=true; String name=str1[1]; name=check1(name); if(name==null) { if(str1.length!=4) flag3=true; } else { if(str1.length!=5) flag3=true; } } } //计算总价 //输出 //table 1: //1 麻婆豆腐 24 //2 油淋生菜 14 //3 麻婆豆腐 48 //table 1: 86 62 川菜 4 稍辣 //tom 13605054400 62 //2023/5/1 20/30/00 //首先输出桌号 //辣度 int spicy[] = new int[100]; /* 川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣; 晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸; 浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜; */ int[] acidity = new int[100]; int[] sweetness = new int[100]; String[] spicy1 = {"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"}; int[] spicyNum = new int[100]; int[] spicyNum1 = new int[100]; String[] acidity1 = {"不酸", "微酸", "稍酸", "酸", "很酸"}; int[] acidityNum = new int[100]; int[] acidityNum1 = new int[100]; String[] sweetness1 = {"不甜", "微甜", "稍甜", "甜"}; int[] sweetnessNum = new int[100]; int[] sweetnessNum1 = new int[100]; /* if (check(table[1])) { System.out.println("table 1 out of opening hours"); exit(0); }*/ //System.out.println("table 1:"+table[1].getDate()+" "+table[1].getTime()); boolean flag=false; if(flag1) { flag=true; System.out.print("wrong format"); } if(flag2) { flag=true; if(flag1) System.out.println(); System.out.print("wrong format"); } if(flag3) { flag=true; if(flag1||flag2) System.out.println(); System.out.print("wrong format"); } if(flag) exit(0); for(int i=1;i<=tableNum;i++) { if(!check(table[i])) { System.out.println("table "+i+" out of opening hours"); exit(0); } } //输出点菜记录 int[] price = new int[100]; int[] price1 = new int[100]; for (int i = 1; i <= tableNum; i++) { System.out.println("table " + i + ":"+" "); for (int j = 1; j <= Sum[i]; j++) { //每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名 if(recorde[i][j].getName()!=null &&!find3(recorde[i][j].getName())){ System.out.println(recorde[i][j].getName()+" does not exist"); continue; } if (recorde[i][j] == null) continue; if (recorde[i][j].getTableNum1() != 0) { int k = recorde[i][j].getTableNum1(); System.out.println(recorde[i][j].getNum() + " table " + i + " pay for table " + recorde[i][j].getTableNum1() + " " + (int) (find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5)); if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1")) { spicyNum[k] += recorde[i][j].getCount(); spicyNum1[k] += recorde[i][j].getCount(); spicy[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5); } else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2")) { acidityNum[k] += recorde[i][j].getCount(); acidityNum1[k] += recorde[i][j].getCount(); acidity[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5); } else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3")) { sweetnessNum[k] += recorde[i][j].getCount(); sweetnessNum1[k] += recorde[i][j].getCount(); sweetness[k] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5); } int n = (int) (1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5); int n1= (int) ((int)(1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5) * discount(table[i], recorde[i][j].getName()) + 0.5);; price[i] += n; price1[i] += n1; continue; } //判断辣度是否超出范围 if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1") && Integer.parseInt(recorde[i][j].getTaste()) > 5) { System.out.println("spicy num out of range :" + recorde[i][j].getTaste()); continue; } //判断酸度是否超出范围 if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2") && Integer.parseInt(recorde[i][j].getTaste()) > 4) { System.out.println("acidity num out of range :" + recorde[i][j].getTaste()); continue; } //判断甜度是否超出范围 if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3") && Integer.parseInt(recorde[i][j].getTaste()) > 3) { System.out.println("sweetness num out of range :" + recorde[i][j].getTaste()); continue; } price[i] += (int) (1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5); price1[i] += (int) ((int)(1.0*find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5) * discount(table[i],recorde[i][j].getName()) + 0.5); System.out.println(recorde[i][j].getNum() + " " + recorde[i][j].getName() + " " + (int) (find(recorde[i][j].getName()) * recorde[i][j].getCount() * prices(recorde[i][j].getPortion()) + 0.5)); if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "1")) { spicyNum[i] += recorde[i][j].getCount(); spicyNum1[i] += recorde[i][j].getCount(); spicy[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5); } else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "2")) { acidityNum[i] += recorde[i][j].getCount(); acidityNum1[i] += recorde[i][j].getCount(); acidity[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5); } else if (recorde[i][j].getTaste() != null && Objects.equals(check1(recorde[i][j].name), "3")) { sweetnessNum[i] += recorde[i][j].getCount(); sweetnessNum1[i] += recorde[i][j].getCount(); sweetness[i] += (int) (Integer.parseInt(recorde[i][j].getTaste()) * recorde[i][j].getCount() + 0.5); } } //判断是否有重复的用户 //如果没有重复的用户,就创建新的用户 if(!find2(table[i].name)){ user[userNum++] = new User(userNum, table[i].name, table[i].phone, price1[i]); }else{ for(int k = 0; k < userNum; k++){ if(Objects.equals(user[k].name, table[i].name)){ user[k].price += price1[i]; break; } } } } /* table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜 table 2: 168 118 川菜 4 稍辣 table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜 * */ for (int i = 1; i <= tableNum; i++) { if (spicyNum[i] != 0) spicy[i] = (int) (1.0*spicy[i] / spicyNum[i] + 0.5); if (acidityNum[i] != 0) acidity[i] = (int) (1.0*acidity[i] / acidityNum[i] + 0.5); if (sweetnessNum[i] != 0) sweetness[i] = (int) (1.0*sweetness[i] / sweetnessNum[i] + 0.5); } for (int i = 1; i <= tableNum; i++) { System.out.print("table " + table[i].getNum() + ": "); System.out.print(price[i] + " " + (price1[i])); if (spicy[i] != 0) { System.out.print(" " + "川菜" + " " + spicyNum1[i] + " " + spicy1[spicy[i]]); } if (acidity[i] != 0) { System.out.print(" " + "晋菜" + " " + acidityNum1[i] + " " + acidity1[acidity[i]]); } if (sweetness[i] != 0) { System.out.print(" " + "浙菜" + " " + sweetnessNum1[i] + " " + sweetness1[sweetness[i]]); } System.out.println(); } //最后按name顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。 //排序 for (int i = 0; i < userNum; i++) { for (int j = i + 1; j < userNum; j++) { if (user[i].getName().compareTo(user[j].getName()) > 0) { User temp = user[i]; user[i] = user[j]; user[j] = temp; } } } //输出 for (int i = 0; i < userNum-1; i++) { System.out.println(user[i].getName() + " " + user[i].getPhone() + " " + user[i].getPrice()); } System.out.print(user[userNum-1].getName() + " " + user[userNum-1].getPhone() + " " + user[userNum-1].getPrice()); } public static String check1(String name) { name = find1(name); if(name==null) return null; if (name.equals("川菜")) return "1"; if (name.equals("晋菜")) return "2"; if (name.equals("浙菜")) return "3"; return null; } public static boolean find3(String name) { for(int i =1; i <= dishNum; i++) { if(Objects.equals(dish[i].getName(), name)) return true; } return false; } //约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。 public static boolean check2(String name, String phone) { if (name.length() > 10) return false; if (phone.length() != 11) return false; if (phone.charAt(0) != '1') return false; if (phone.charAt(1) != '8' && phone.charAt(1) != '3') return false; if (phone.charAt(1) == '8' && phone.charAt(2) != '0' && phone.charAt(2) != '1' && phone.charAt(2) != '9') return false; if (phone.charAt(1) == '3' && phone.charAt(2) != '3' && phone.charAt(2) != '5' && phone.charAt(2) != '6') return false; return true; } public static boolean find2(String name) { for (int i = 0; i < userNum; i++) { if (user[i].getName().equals(name)) return true; } return false; } public static String find1(String name) { for (int i = 1; i <= dishNum; i++) { if (dish[i].getName().equals(name)) return dish[i].getTaste(); } return null; } public static int find4(String name) { for(int i=1;i<=dishNum;i++) { if(dish[i].getName()==null) return -1; if(Objects.equals(dish[i].getName(),name)) return i; } return -1; } public static boolean check(Table table) { int year = Integer.parseInt(table.date.split("/")[0]); int month = Integer.parseInt(table.date.split("/")[1]); int day = Integer.parseInt(table.date.split("/")[2]); int hour = Integer.parseInt(table.time.split("/")[0]); int minute = Integer.parseInt(table.time.split("/")[1]); int second = Integer.parseInt(table.time.split("/")[2]); Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day, hour, minute, second); int week = calendar.get(Calendar.DAY_OF_WEEK); if (week == 3 || week == 4) { if(hour >=10 && hour <=20) return true; if(hour==9&&minute>=30) return true; return hour == 21 && minute <= 30; } else { if(hour >=10 && hour <=14) return true; if(hour >=17 && hour <=19) return true; if(hour==9&&minute>=30) return true; return hour == 20 && minute <= 30; } } class MyDate { private int year; private int month; private int day; public MyDate(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public void setDay(int day) { if (isValidDay(day)) { this.day = day; } else { System.out.println("日期不合法"); } } public class ComplexJavaClass { private String className; private List<String> classMembers; public ComplexJavaClass(String className) { this.className = className; this.classMembers = new ArrayList<>(); } public String getClassName() { return className; } public void addClassMember(String member) { classMembers.add(member); } public void printClassInfo() { System.out.println("Class Name: " + className); System.out.println("Class Members:"); for (String member : classMembers) { System.out.println(" - " + member); } } class MyCompazxcr2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } public int calculateStringLength() throws NullPointerException { int length = 0; for (String member : classMembers) { length += member.length(); } if (length == 0) { throw new NullPointerException("Class members list is empty."); } return length; } } public boolean isValidDay(int day) { if (year < 1 || month < 1 || month > 12 || day < 1) { return false; } Calendar cal = Calendar.getInstance(); cal.set(year, month - 1, 1); int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); return day <= lastDay; } } public static double discount(Table table,String name){ class Mesnu { public Mesnu() { } public int searchDish(String dishName) { return -1; } } int year = Integer.parseInt(table.date.split("/")[0]); int month = Integer.parseInt(table.date.split("/")[1]); int day = Integer.parseInt(table.date.split("/")[2]); int hour = Integer.parseInt(table.time.split("/")[0]); int minute = Integer.parseInt(table.time.split("/")[1]); int second = Integer.parseInt(table.time.split("/")[2]); Calendar calendar = Calendar.getInstance(); calendar.set(year, month, day, hour, minute, second); int week = calendar.get(Calendar.DAY_OF_WEEK); name=check1(name); if((name!=null)&&(name.equals("1")||name.equals("2")||name.equals("3"))) { if(week==3||week==4) return 1.0; else return 0.7; } else { if(week==3||week==4) return 1.0; else { if(hour>=17&&hour<=19) return 0.8; if(hour==20&&minute<=30) return 0.8; if(hour>=11&&hour<=13) return 0.6; if(hour==10&&minute>=30) return 0.6; if(hour==14&&minute<=30) return 0.6; } } return 1.0; } public static double prices(int num) { if (num == 1) return 1.0; if (num == 2) return 1.5; if (num == 3) return 2.0; return 0; } public static int find(String name) { for (int i = 1; i <= dishNum; i++) { if (dish[i].getName().equals(name)) return dish[i].getPrice(); } return 0; } }
本体大部分内容与菜单计价程序-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.time.LocalDate; import java.time.LocalDate; import java.util.*; import java.util.ArrayList; import java.util.Collections; import java.time.temporal.ChronoUnit; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String s2 = in.nextLine(); LocalDate date1 = parseDate(s1); LocalDate date2 = parseDate(s2); compareDates(date1, date2); calculateDateIntervals(date1, date2); } // 解析日期字符串并返回一个LocalDate对象 private static LocalDate parseDate(String dateStr) { String[] dateParts = dateStr.split("-"); int year = Integer.parseInt(dateParts[0]); int month = Integer.parseInt(dateParts[1]); int day = Integer.parseInt(dateParts[2]); return LocalDate.of(year, month, day); } // 比较两个日期并输出比较结果 private static void compareDates(LocalDate date1, LocalDate date2) { if (date1.isBefore(date2)) { System.out.println("第一个日期比第二个日期更早"); } else { System.out.println("第一个日期比第二个日期更晚"); } } // 计算两个日期的间隔并输出结果 private static void calculateDateIntervals(LocalDate date1, LocalDate date2) { long dayCount = date1.until(date2, ChronoUnit.DAYS); long weekCount = date1.until(date2, ChronoUnit.WEEKS); System.out.println("两个日期间隔" + Math.abs(dayCount) + "天"); System.out.println("两个日期间隔" + Math.abs(weekCount) + "周"); } } class Homewowrk { Scanner scanner = new Scanner(System.in); String inputText = scanner.nextLine(); int i; String[] words = inputText.split("[ ,.]"); List<String> wordList = new ArrayList<>(); { String word = words[i].trim(); if (!word.isEmpty()) { wordList.add(word); } } class MyComparator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return -1; else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return 1; } } } class Spring { private int tension; public Spring(int maxTension) { this.tension = 0; } public int getTension() { return tension; } public void setTension(int tension) { if (tension >= 0 ) { this.tension = tension; } else { System.out.println("张力超出范围"); } } }
总结:对于面对象程序设计的学习还是一件任重而道远的事情,仍然存在很多问题等待着我去学习和进步,还有很多我没了解的地方在等着我探索。
这几周巩固了类的使用方法,学习了新的继承关系。类已经基本没有什么问题了,继承关系还要再理解一下。
这次的期中考试相对于菜单计价系统来说是相当容易的,主要考察的是我们的面向对象思想,还有Arraylist的排序方式。并且从中我深刻体会到了认真审题的重要性。
除此之外,我还发现正则表达式是真的很好用,短短一行代码便能对复杂的数据进行判断处理,在这方面值得我更好地去学习。
import java.text.ParseException;import java.time.DateTimeException;import java.time.Duration;import java.time.LocalDate;import java.time.LocalDateTime;import java.util.ArrayList;import java.util.Comparator;import java.util.List;import java.util.Scanner;
public class Main { public static boolean isNumeric(String string) { int intValue; try { intValue = Integer.parseInt(string); return true; } catch (NumberFormatException e) { return false; } }
public static void main(String[] args) throws ParseException { Menu menu = new Menu(); ArrayList<Table> tables = new ArrayList<Table>(); Scanner input = new Scanner(System.in); String str1 = new String(); int i = 0; int portion = 0; int quota = 0; int wad = 0; int wada,wresw,tersd; double wadsa,grefd,wadwds; while (true) {// 输入菜单 Dish temp = new Dish(); int isRepeat = -1; str1 = input.nextLine(); if (str1.matches("[\\S]* [1-9][\\d]*")) { String[] token = str1.split(" "); temp.name = token[0]; temp.unit_price = Integer.parseInt(token[1]); if (temp.unit_price > 300) { System.out.println(temp.name + " price out of range " + temp.unit_price); continue; } temp.isT = false; isRepeat = menu.searchDish(temp.name); if (isRepeat != -1) { menu.dishs.remove(isRepeat); } menu.dishs.add(temp); } else if (str1.matches("[\\S]* [\\d]* T")) { String[] token = str1.split(" "); temp.name = token[0]; temp.unit_price = Integer.parseInt(token[1]); if (temp.unit_price > 300) { System.out.println(temp.name + " price out of range " + temp.unit_price); continue; } temp.isT = true; if (isRepeat != -1) { menu.dishs.remove(isRepeat); } menu.dishs.add(temp); } else if (str1.equals("end")) { break; } else if (str1.matches("tab.*")) { break;
} else { System.out.println("wrong format"); continue; } } class MyComparator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return -1; else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return 1; } } while (!str1.equals("end")) { Table temp = new Table(); boolean isRepeat = false; int repeatNum = 0; if (str1.matches("table.*")) { if (str1.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) { String[] token = str1.split(" "); String[] Date = token[2].split("/"); String[] Time = token[3].split("/"); int[] intDate = new int[3]; int[] intTime = new int[3]; for (i = 0; i < 3; i++) { intDate[i] = Integer.parseInt(Date[i]); intTime[i] = Integer.parseInt(Time[i]); } temp.num = Integer.parseInt(token[1]); if (temp.num > 55) { System.out.println(temp.num + " table num out of range"); str1 = input.nextLine(); continue;
} try { temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1], intTime[2]); temp.getWeekDay(); } catch (DateTimeException e) { System.out.println(temp.num + " date error"); str1 = input.nextLine(); continue; } if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0)) && temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) { System.out.println("not a valid time period"); str1 = input.nextLine(); continue; } // 判断桌号是否重复 if (temp.isOpen()) { for (i = 0; i < tables.size(); i++) { // 有重复的桌号 if (temp.num == tables.get(i).num && tables.get(i).isOpen()) { Duration duration = Duration.between(temp.time, tables.get(i).time); // 同一天 if (duration.toDays() == 0) { // 在周一到周五 if (temp.weekday > 0 && temp.weekday < 6) { // 在同一时间段 if (temp.time.getHour() < 15 && tables.get(i).time.getHour() < 15) { temp = tables.get(i); isRepeat = true; repeatNum = i; break; } } // 在周末 else { // 时间相差小于一小时 if (duration.toHours() < 3600) { temp = tables.get(i); repeatNum = i; isRepeat = true; break; } } } } } } if(!isRepeat) { System.out.println("table " + temp.num + ": "); } class MyCompar2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return arg0.compareToIgnoreCase(arg1); else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return arg0.compareToIgnoreCase(arg1); } } } else { System.out.println("wrong format"); str1 = input.nextLine(); continue; } // 本桌开始点菜 while (true) { str1 = input.nextLine(); if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); portion = Integer.parseInt(token[2]); quota = Integer.parseInt(token[3]); if (temp.order.records.size() > 0) { if (Integer.parseInt( token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) { System.out.println("record serial number sequence error"); continue; } } if (menu.searchDish(token[1]) == -1) { System.out.println(token[1] + " does not exist"); continue; } if (portion > 3 || portion < 1) { System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion); continue; } if (quota > 15) { System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota); continue; } temp.od(menu, token[0], token[1], portion, quota); } // 判断是否为删除订单 else if (str1.matches("[1-9][\\d]* delete")) { String[] token = str1.split(" "); temp.order.delARecordByOrderNum(Integer.parseInt(token[0])); } // 判断是否为夹杂菜单 else if (str1.matches("[\\S]* [\\d]*")) { System.out.println("invalid dish"); continue; } else if (str1.matches("[\\S]* [\\d]* T")) { System.out.println("invalid dish"); continue; } // 判断是否为代点 else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); // 判断代点桌号是否存在 boolean exist = false; for (int j = 0; j < tables.size(); j++) { if (tables.get(j).num == Integer.parseInt(token[0])) { exist = true; break; } } if (exist) { System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table " + Integer.parseInt(token[0]) + " "); Record treat = new Record(); treat.d = menu.dishs.get(menu.searchDish(token[2])); portion = Integer.parseInt(token[3]); quota = Integer.parseInt(token[4]); treat.portion = portion; treat.quota = quota; System.out.print(treat.getPrice() + "\n"); temp.sum += treat.getPrice(); } // 若不存在则输出内容 else { System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist"); }
} else if (str1.equals("end")) { break; } else if(str1.matches("ta.*")){ break;
} else { System.out.println("wrong format"); continue; } } } else if (str1.matches("t.*")) { isRepeat = true; temp = tables.get(tables.size()); while (true) { str1 = input.nextLine(); if (str1.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); portion = Integer.parseInt(token[2]); quota = Integer.parseInt(token[3]); // 判断订单号是否由小到大排列 if (temp.order.records.size() > 0) { if (Integer.parseInt( token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) { System.out.println("record serial number sequence error"); continue; } } if (menu.searchDish(token[1]) == -1) { System.out.println(token[1] + " does not exist"); continue; } if (portion > 3 || portion < 1) { System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion); continue; } if (quota > 15) { System.out.println(Integer.parseInt(token[0]) + " num out of range " + quota); continue; } temp.od(menu, token[0], token[1], portion, quota); } // 判断是否为删除订单 else if (str1.matches("[1-9][\\d]* delete")) { String[] token = str1.split(" "); temp.order.delARecordByOrderNum(Integer.parseInt(token[0])); } // 判断是否为夹杂菜单 else if (str1.matches("[\\S]* [\\d]*")) { System.out.println("invalid dish"); continue; } else if (str1.matches("[\\S]* [\\d]* T")) { System.out.println("invalid dish"); continue; } // 判断是否为代点 else if (str1.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) { String[] token = str1.split(" "); // 判断代点桌号是否存在 boolean exist = false; for (int j = 0; j < tables.size(); j++) { if (tables.get(j).num == Integer.parseInt(token[0])) { exist = true; break; } } if (exist) { System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table " + Integer.parseInt(token[0]) + " "); Record treat = new Record(); treat.d = menu.dishs.get(menu.searchDish(token[2])); portion = Integer.parseInt(token[3]); quota = Integer.parseInt(token[4]); treat.portion = portion; treat.quota = quota; System.out.print(treat.getPrice() + "\n"); temp.sum += treat.getPrice(); } // 若不存在则输出内容 else { System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist"); }
} else if (str1.equals("end")) { break; } else { System.out.println("wrong format"); continue; } } if (tables.size() != 0) { tables.get(tables.size() - 1).order.records.addAll(temp.order.records); } } else { str1 = input.nextLine(); continue; }
// 本桌点菜结束,进入下一桌 if (isRepeat) { tables.remove(repeatNum); } temp.getSum(); tables.add(temp); } // 最终输出桌号订单信息 for (i = 0; i < tables.size(); i++) { if (tables.get(i).isOpen()) { System.out .println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum); } else System.out.println("table " + tables.get(i).num + " out of opening hours"); } }
static class Dish { String name; int unit_price; boolean isT = false; }
static class Record { int orderNum; Dish d; int portion; int quota; boolean isDeleted = false;
int getPrice() { if (portion == 2) return (int) Math.round(1.5 * d.unit_price) * quota; else if (portion == 3) return 2 * d.unit_price * quota; else return d.unit_price * quota; } }
static class Menu { class Homewowrk {
Scanner scanner = new Scanner(System.in); String inputText = scanner.nextLine(); int i; String[] words = inputText.split("[ ,.]"); List<String> wordList = new ArrayList<>(); { String word = words[i].trim(); if (!word.isEmpty()) { wordList.add(word); } } class MyCompar2ator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return -1; else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return 1; } } class MyComparator implements Comparator<String> { @Override public int compare(String arg0, String arg1) { // TODO Auto-generated method stub if(arg0.length()>arg1.length()) return -1; else if(arg0.length()==arg1.length()) return arg0.compareToIgnoreCase(arg1); else return 1; } } } ArrayList<Dish> dishs = new ArrayList<Dish>();
int searchDish(String dishName) { for (int i = 0; i < dishs.size(); i++) { if (dishName.equals(dishs.get(i).name)) { return i; } } return -1; }
Dish addDish(String dishName, int unit_price) { Dish newDish = new Dish(); newDish.name = dishName; newDish.unit_price = unit_price; return newDish; } } class Grade { String studentId; String studentName; String courseName; int usualGrade; int examGrade; Grade(String studentId, String studentName, String courseName, int usualGrade, int examGrade) { this.studentId = studentId; this.studentName = studentName; this.courseName = courseName; this.usualGrade = usualGrade; this.examGrade = examGrade; } double calculateTotalGrade() { return usualGrade * 0.3 + examGrade * 0.7; } double getExamGrade() { return examGrade; } @Override public String toString() { return studentId + " " + studentName + " " + (int)calculateTotalGrade(); } } static class Order { //Record[] records = new Record[20]; ArrayList<Record> records = new ArrayList<Record>();
Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) { Record newRecord = new Record(); newRecord.orderNum = orderNum; newRecord.d = menu.dishs.get(menu.searchDish(dishName)); newRecord.portion = portion; newRecord.quota = quota; System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice()); return newRecord; }
int searchReocrd(String name) { for (int i = 0; i < records.size(); i++) { if (records.get(i).d.name == name) { return i; } } return -1; }
boolean delARecordByOrderNum(int orderNum) { int i = 0, flag = 0; for (i = 0; i < records.size(); i++) { if (records.get(i).orderNum == orderNum) { if (records.get(i).isDeleted == false) { records.get(i).isDeleted = true; } else { System.out.println("deduplication " + orderNum); } flag++; } } if (flag == 0) { System.out.println("delete error;"); return false; } return true; } }
static class Table { Order order = new Order(); int num; LocalDateTime time; int weekday; long sum = 0; long origSum = 0; void od(Menu menu, String str1, String str2, int portion, int quota) { { order.records.add(order.addARecord(Integer.parseInt(str1), str2, portion, quota, menu));
} }
void getWeekDay() { weekday = time.getDayOfWeek().getValue(); }
void getSum() { for (int i = 0; i < order.records.size(); i++) { if (!order.records.get(i).isDeleted) { origSum += order.records.get(i).getPrice(); if (order.records.get(i).d.isT) { if (weekday > 0 && weekday < 6) { sum += Math.round(order.records.get(i).getPrice() * 0.7); } else { sum += order.records.get(i).getPrice(); } } else { if (weekday > 0 && weekday < 6) { if (time.getHour() >= 17 && time.getHour() < 20) sum += Math.round(order.records.get(i).getPrice() * 0.8); if (time.getHour() == 20) { if (time.getMinute() <= 30) sum += Math.round(order.records.get(i).getPrice() * 0.8); } if (time.getHour() >= 10 && time.getHour() < 14) sum += Math.round(order.records.get(i).getPrice() * 0.6); if (time.getHour() == 14) { if (time.getMinute() <= 30) sum += Math.round(order.records.get(i).getPrice() * 0.6); } } else sum+=order.records.get(i).getPrice(); } } }
}
boolean isOpen() { if (weekday > 0 && weekday < 6) { if (time.getHour() >= 17 && time.getHour() < 20) return true; if (time.getHour() == 20) { if (time.getMinute() <= 30) return true; } if (time.getHour() > 10 && time.getHour() < 14) return true; if (time.getHour() == 10) { if (time.getMinute() >= 30) return true; } if (time.getHour() == 14) { if (time.getMinute() <= 30) return true; } } else { if (time.getHour() > 9 && time.getHour() < 21) return true; if (time.getHour() == 9) { if (time.getMinute() >= 30) return true; } if (time.getHour() == 21) { if (time.getMinute() <= 30) return true; } } return false;
} }}class Spring { private int tension; public Spring(int maxTension) {
this.tension = 0; } public int getTension() { return tension; } public void setTension(int tension) { if (tension >= 0 ) { this.tension = tension; } else { } }}