题目集4-6 && 期中考试的总结性BLOG

发布时间 2023-11-16 16:35:40作者: 笙寒99

(一)前言:

本篇将介绍本人在对于学校布置的三个PTA习题和期中考试习题的总结。

对于第四次题目集来说,有涉及到Scanner的nextLine的方法,和next方法有所区别,以及使用到了String类中的split方法来分割字符串、integer类中的parseInt方法来进行类型的转换,其中还使用到了LocalDate类中的一些方法,例如of、isAfter、isBefore等,来进行对于日期的一些判断,如判断闰年、判断日期时间的先后,以及两个日期之间相差的时间,都可以直接使用LocalDate类中的方法来实现,相对于自己写一些判断的函数可以方便许多,同时对于单词统计与排序的题目,用到了replaceAll方法来替换一个字符串中的字符,结合String类中split的方法使用,同时使用到了ArrayList来接收各个数据,以实现后面的排序方法,同时也是用到了哈希表的相关知识。以上就是第四次题目集中所涉及到的知识点,对于第四次题目集来说,其中的题目量一般,难度也比较低,大家稍微花一点时间就可以完成啦!

对于第五次题目集来说,它是在第四次题目集中菜单计价程序-3之上迭代的,也就是需要在上一次的题目中加入一些新的要求,本次题目集主要加入的是对于异常情况需要做出合适的处理,这次题目集同样用到了LocalDateTime类中的一些方法,例如of、isAfter、isBefore等,对于题目的简化有一定的帮助,其中用到的比较多的还是正则表达式,本次题目集的题目量比较少,就只有一个题目,但是难度还是有的,因为其中的逻辑结构是比较复杂的,再多中异常情况同时出现时需要特别注意。

对于第六次题目集来说,它同样是在第四次题目集中菜单计价程序-3之上迭代的,不同于第五次题目集的是,这次的要求侧重点不在于对异常类的处理,这次题目集用到的一些方法其实和上一次差不多,这次侧重点是对于特色菜的处理,需要对菜品类的构造有自己的理解,特别的是,这次题目要求的加入顾客需要编写一个新的类来,同时要求的按照姓名排序可以用到接口的方法,非常的方便,可以很简单的完成排序,前提是要先了解到排序方法java.util.Locale.CHINA,对于这次题目集,题目量也很少,就只有一题,难度在博主看来应该比上一次题目集的迭代还要简单一点,对于自己类与类之间的逻辑关系弄清楚即可。

对于期中考试的题目集来说,期中包含了选择题部分和编程题部分,期中选择题包含了许多的基础知识,因为平时对于基础的知识讲解的比较少,其实选择题的目的应该是让我们自己了解一些基础的知识,对于编程题部分,主要是让我们规范的构造类以确保代码具有一定的拓展性,确保后续的使用,其中还让我们使用到了接口,用接口的方法来实现排序,总的来说,期中考试的题目集题目量还是比较大的,但是难度都很低,很快就可以完成。

 

下面本人将分享自己的设计思路和分析、踩坑心得、主要困难以及改进建议及最后的总结。

 

(二)设计与分析

题目集4的7-3

题目:

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

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

输入格式:

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

输出格式:

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

输入样例1:

2000-02-18
2000-03-15

输出样例1:

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

输入样例2:

2022-6-18
2022-6-1

输出样例2:

第一个日期比第二个日期更晚
两个日期间隔17天
两个日期间隔2周

源码:

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Date[] dates = new Date[2];
		dates[0] = new Date();
		dates[1] = new Date();
		String[] dates1 = new String[2];
		dates1[0] = scanner.nextLine();
		dates1[1] = scanner.nextLine();
		dates[0].setdate(dates1[0]);
		dates[1].setdate(dates1[1]);
		String[] split = dates1[0].split("-");
		LocalDate localDate1 = LocalDate.of(Integer.parseInt(split[0]),Integer.parseInt(split[1]),Integer.parseInt(split[2]));
		String[] split1 = dates1[1].split("-");
		LocalDate localDate2 = LocalDate.of(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]));
		//if(dates[0].judgment(localDate1) && dates[1].judgment(localDate2)) {
			if(dates[0].isAfter(localDate1,localDate2)) {
				System.out.println("第一个日期比第二个日期更晚");
			}
			else {
				System.out.println("第一个日期比第二个日期更早");
			}
			System.out.println("两个日期间隔" + dates[0].days(localDate1,localDate2) + "天");
			System.out.print("两个日期间隔" + dates[0].weeks(localDate1,localDate2) + "周");
		//}
	}
}
class Date {
	private String date;
	
	public void setdate(String date) {
		this.date = date;
	}
	public String getdate() {
		return date;
	}
	public boolean judgment(LocalDate localDate) {
		if(localDate.isLeapYear()) {
			return date.matches("[0-9]{4}-(0[13578]-(0[1-9]|[12][0-9]|3[01])|1[02]-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-9])|0[469]-(0[1-9]|[12][0-9]|30)|11-(0[1-9]|[12][0-9]|30))");
		}
		else {
			return date.matches("[0-9]{4}-(0[13578]-(0[1-9]|[12][0-9]|3[01])|1[02]-(0[1-9]|[12][0-9]|3[01])|02-(0[1-9]|1[0-9]|2[0-8])|0[469]-(0[1-9]|[12][0-9]|30)|11-(0[1-9]|[12][0-9]|30))");
		}
	}
	public boolean isAfter(LocalDate localDate1,LocalDate localDate2) {
		return localDate1.isAfter(localDate2);
	}
	public long days(LocalDate localDate1,LocalDate localDate2) {
		return Math.abs(ChronoUnit.DAYS.between(localDate1,localDate2));
	}
	public long weeks(LocalDate localDate1,LocalDate localDate2) {
		return Math.abs(ChronoUnit.WEEKS.between(localDate1,localDate2));
	}
}

本题只需要设计一个Date类来完成一个日期的各种判断处理,类图如下:

 

 

在Date类中有date属性,setdate()的方法来获取Main中传进来的date,getdate()来获取已经设定好的date,同时有judgment()方法来判断日期的合法性,isAfter()方法则是用到了localdate类中已有的方法,days()方法即是获取两个日期之间相差的天数,weeks()方法用来获取两个日期相差的周数。

对于本题来说,博主的方法可能还是还是有点缺陷的,因为其实可以在Main中直接用到LocalDate类来对两个给出的日期进行处理,这样可能会简化一些代码。

 

题目集4的7-2

题目:

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

输入格式:

一段英文文本。

输出格式:

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

输入样例:

Hello, I am a student from China.

输出样例:

student
China
Hello
from
am
a
I

源码:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Cmp cmp = new Cmp();
		String sentence = scanner.nextLine().replaceAll("[,.]","");
		String[] splits = sentence.split(" ");
		HashSet<String> set = new HashSet<String>();
		for(int j = 0;j < splits.length;j++) {
			set.add(splits[j]);
		}
		ArrayList<String> All = new ArrayList<>(set);
		All.sort(cmp::compare1);
		for(int i3 = 0;i3 < All.size();i3++) {
			System.out.println(All.get(i3));
		}
		}
}

class Cmp{
	public int compare(String s1, String s2) {
        return s2.length() - s1.length(); //按照字符串长度降序排序
    }
    public int compare1(String a,String b) {
    	int c = compare(a,b);
    	if(c == 0) {
    		c = a.toLowerCase().charAt(0) - b.toLowerCase().charAt(0);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(1) - b.toLowerCase().charAt(1);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(2) - b.toLowerCase().charAt(2);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(3) - b.toLowerCase().charAt(3);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(4) - b.toLowerCase().charAt(4);
    	}
        return c;
    }
}

 本题需要自己重写一个排序的方法Cmp来根据题目要求完成排序,类图比较简单,就不给出了

在Cmp排序方法中compare是对字符串的长度降序排序,compare1其实用的是一个取巧的排序(because 博主没有找到java库中是否有对应的对于单词的首字母顺序的排序方法),大家可以查阅资料进行一定的改进,还需要注意的就是,题目要求重复的数据只输出一次,这是我们就可以使用哈希表来存储每个单词,因为哈希表可以帮我们自动去重,后面再把它们放入ArrayList中实现自己编写的排序方法,再遍历输出即可比较轻松的完成本题啦。

 

题目集4的7-1

题目:

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

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

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号\\

Dish d;//菜品\\

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

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

}

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

Order {

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

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

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

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

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

}

### 输入格式:

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

菜品记录格式:

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

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

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

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

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

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

### 输出格式:

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

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

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

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

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

输入样例:

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

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

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

输出样例2:

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

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

输入样例3:

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

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

输出样例3:

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

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

输入样例4:

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

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

输出样例4:

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

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

源码:

import java.util.Scanner;
import java.time.temporal.ChronoField;
import java.time.LocalDateTime;

public class Main {
	public static void main(String[] rrgs) {
		Scanner scanner = new Scanner(System.in);
		Order[] orders = new Order[100];
		LocalDateTime[] local = new LocalDateTime[100];
		int x = 1;
		Menu menu = new Menu();
		String name;
		int a = 1;
		while(a != 0) {
			name = scanner.nextLine();
			if(name.equals("end")) {
				a = 0;
			}
			else
			{
				String[] split = name.split(" ");
				if(split.length == 2 && isNumeric(split[1])) {
					if(menu.searthdish(split[0]).getname().equals("no") == false) {
						menu.searthdish(split[0]).setprice(Integer.parseInt(split[1]));
					}
					else {
						menu.adddish(split[0],Integer.parseInt(split[1]));
					}
				}
				else {
					if(split[0].equals("table")) {
						System.out.println("table " + split[1] + ": ");
						orders[x] = new Order();
						String[] split1 = split[2].split("/");
						String[] split2 = split[3].split("/");
						local[x] = LocalDateTime.of(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]),Integer.parseInt(split2[0]),Integer.parseInt(split2[1]),Integer.parseInt(split2[2]));
						x++;
					}
					else {
						if(split.length == 5) {
							if(menu.searthdish(split[2]).getname().equals("no") == true) {
								System.out.println(split[2] + " does not exist");
							}
							else {
								orders[x-1].addRecord(Integer.parseInt(split[1]),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
								System.out.println(split[1] + " table " + (x-1) + " pay for table " + split[0] + " " + orders[x-1].find(Integer.parseInt(split[1])).getprice());
							}
						}
						else {
							if(split[1].equals("delete") == false) {
								if(menu.searthdish(split[1]).getname().equals("no") == true) {
									System.out.println(split[1] + " does not exist");
								}
								else {
									if(orders[x-1].find(Integer.parseInt(split[0])).getnum() == -1) {
										orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),menu);
										System.out.println(split[0] + " " + split[1] + " " + orders[x-1].find(Integer.parseInt(split[0])).getprice());
									}
									else {
										System.out.println(split[0] + " " + split[1] + " " + (orders[x-1].find(Integer.parseInt(split[0])).getprice()+orders[x-1].find(Integer.parseInt(split[0])).getprice()));
										orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),menu);
									}
								}
							}
							else {
								orders[x-1].del(Integer.parseInt(split[0]));
							}
						}
					}
				}
			}
		}
		for(int j = 1;j < x;j++) {
			if(local[j].get(ChronoField.DAY_OF_WEEK) == 6 || local[j].get(ChronoField.DAY_OF_WEEK) == 7) {
				LocalDateTime beginTime = LocalDateTime.of(local[j].get(ChronoField.YEAR), local[j].get(ChronoField.MONTH_OF_YEAR), local[j].get(ChronoField.DAY_OF_MONTH), 9, 29, 59);
				LocalDateTime endTime = LocalDateTime.of(local[j].get(ChronoField.YEAR), local[j].get(ChronoField.MONTH_OF_YEAR), local[j].get(ChronoField.DAY_OF_MONTH), 21, 30, 1);
				if(local[j].isAfter(beginTime) && local[j].isBefore(endTime)) {
					System.out.println("table " + j + ": " + orders[j].getTotalPrice());
				}
				else {
					System.out.println("table " + j + " out of opening hours");
				}
			}
			else {
				LocalDateTime beginTime1 = LocalDateTime.of(local[j].get(ChronoField.YEAR), local[j].get(ChronoField.MONTH_OF_YEAR), local[j].get(ChronoField.DAY_OF_MONTH), 10, 29, 59);
				LocalDateTime endTime1 = LocalDateTime.of(local[j].get(ChronoField.YEAR), local[j].get(ChronoField.MONTH_OF_YEAR), local[j].get(ChronoField.DAY_OF_MONTH), 14, 30, 1);
				LocalDateTime beginTime2 = LocalDateTime.of(local[j].get(ChronoField.YEAR), local[j].get(ChronoField.MONTH_OF_YEAR), local[j].get(ChronoField.DAY_OF_MONTH), 16, 59, 59);
				LocalDateTime endTime2 = LocalDateTime.of(local[j].get(ChronoField.YEAR), local[j].get(ChronoField.MONTH_OF_YEAR), local[j].get(ChronoField.DAY_OF_MONTH), 20, 30, 1);
				if(local[j].isAfter(beginTime1) && local[j].isBefore(endTime1)) {
					System.out.println("table " + j + ": " + Math.round(0.6*orders[j].getTotalPrice()));
				}
				else {
					if(local[j].isAfter(beginTime2) && local[j].isBefore(endTime2)) {
						System.out.println("table " + j + ": " + Math.round(0.8*orders[j].getTotalPrice()));
					}
					else {
						System.out.println("table " + j + " out of opening hours");
					}
				}
			}
		}
	}
	public static boolean isNumeric(String str){
		   for (int i = str.length();--i>=0;){  
		       if (!Character.isDigit(str.charAt(i))){
		           return false;
		       }
		   }
		   return true;
	}
}
class Dish {
	private String name;
	private long price ;
	
	public void setname(String name) {
		this.name = name;
	}
	public void setprice(int price) {
		this.price = price;
	}
	public String getname() {
		return name;
	}
	public long getprice(int f) {
		if(f == 1)
			return price;
		else
		if(f == 2)
			return Math.round((double)price*1.5);
		else
		if(f == 3)
			return price*2;
		else
		return 0;
	}
}
class Menu {
	private Dish[] dishs = new Dish[1000];
	private int t = 0;
	
	public Dish[] getdishs() {
		return dishs;
	}
	public Dish searthdish(String dishname) {
		for(int i = 0;i < t;i++) {
			if(dishname.equals(dishs[i].getname())) {
				return dishs[i];
			}
		}
		Dish no = new Dish();
		no.setname("no");
		return no;
	}
	public void adddish(String name,int price) {
		dishs[t] = new Dish();
		dishs[t].setname(name);
		dishs[t].setprice(price);
		t++;
	}
}
class Record {
	private int num;
	Dish d = new Dish();
	private int portion;
	private int number;
	
	public void setnum(int num) {
		this.num = num;
	}
	public void setdish(Dish d) {
		this.d = d;
	}
	public void setdishname(String name) {
		d.setname(name);
	}
	public void setportion(int portion) {
		this.portion = portion;
	}
	public void setnumber(int number) {
		this.number = number;
	}
	public int getnum() {
		return num;
	}
	public Dish getdish() {
		return d;
	}
	public int getportion() {
		return portion;
	}
	public int getnumber() {
		return number;
	}
	public long getprice() {
		return Math.round(number*d.getprice(portion));
	}
}
class Order {
	Record[] records = new Record[1000];
	private int k = 0;
	
	public long getTotalPrice() {
		long TotalPrice = 0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnum() != -1)
			TotalPrice += records[i].getprice();
		}
		return TotalPrice;
	}
	public void addRecord(int num,String dishname,int portion,int number,Menu menu) {
		records[k] = new Record();
		records[k].setnum(num);
		records[k].setdish(menu.searthdish(dishname));
		records[k].setportion(portion);
		records[k].setnumber(number);
		//System.out.println(num + " " + dishname + " " + records[k].getprice());
		k++;
	}
	public void del(int num) {
		int j = -1;
		for(int i = 0;i < k;i++) {
			if(num == records[i].getnum()) {
				j = i;
				records[i].setnum(-1);
			}
		}
		if(j == -1) {
			System.out.println("delete error;");
		}
	}
	public Record find(int num) {
		for(int i = 0;i < k;i++) {
			if(num == records[i].getnum()) {
				return records[i];
			}
		}
		Record no = new Record();
		no.setnum(-1);
		return no;
	}
}

本题可以沿用菜单计价程序-2中设计的各种类,基本不用做出什么修改,即一个菜品类,一个菜谱类,一个点菜记录类,一个订单类即可,类图如下:

 

 

本题需要注意的是Record类中需要建立Dish()类,这样储存一个点菜记录的完整信息时刚好可以使用Dish中的方法根据点菜份数输出每行记录需要输出的价格,比较合理,对于加进来的点菜日期和时间需要进行一个处理,可以使用到LocalDate类来处理,因为它在其中包含了许多方法,比较方便,其它只要你构造的各个类结构是正确的,完成本题问题不大。

 

题目集5的7-1

题目:

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

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

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

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

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号

Dish d;//菜品\\

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

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

}

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

Order {

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

输入样例:

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

桌号信息错误。例如:

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

输出样例2:

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

wrong format

输入样例3:

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

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

输出样例3:

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

wrong format

输入样例4:

错误的菜谱记录。例如:

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

输出样例4:

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

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

输入样例5:

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

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

输出样例5:

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

wrong format

输入样例6:

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

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

输出样例6:

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

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

源码:

import java.util.Scanner;
import java.time.temporal.ChronoField;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;

public class Main {
	public static void main(String[] rrgs) {
		Scanner scanner = new Scanner(System.in);
		Order[] orders = new Order[100];
		int x = 1,x0 = 0;
		Menu menu = new Menu();
		String name;
		int a = 1;
		int g = 1;
		while(a != 0) {
			name = scanner.nextLine();
			if(judgmentdish(name) || judgmentrecord(name) || judgmentrecord1(name) || judgmentdelete(name) || name.equals("end") || name.contains("table")) {
				if(name.equals("end")) {
					a = 0;
				}
				else
				{
					String[] split = name.split(" ");
					if((split.length == 2 && isNumeric(split[1])) || (split.length == 3 && split[2].equals("T"))) {
						if(Integer.parseInt(split[1]) < 1 || Integer.parseInt(split[1]) > 299) {
							System.out.println(split[0] + " price out of range " + split[1]);
						}
						else {
							if(split.length == 3) {
								if(x == 1) {
									if(menu.searthdish(split[0]).getname().equals("no") == false) {
										menu.searthdish(split[0]).setprice(Integer.parseInt(split[1]));
									}
									else {
										menu.adddish(split[0],Integer.parseInt(split[1]),"T");
									}
								}
								else {
									System.out.println("invalid dish");
								}
							}
							else {
								if(x == 1) {
									if(menu.searthdish(split[0]).getname().equals("no") == false) {
										menu.searthdish(split[0]).setprice(Integer.parseInt(split[1]));
									}
									else {
										menu.adddish(split[0],Integer.parseInt(split[1]),"P");
									}
								}
								else {
									System.out.println("invalid dish");
								}
							}
						}
					}
					else {
						if(name.substring(0,5).equals("table")) {
							if(x < x0)
								x = x0;
							if(judgment1(name)) {
								if(Integer.parseInt(split[1]) < 1 || Integer.parseInt(split[1]) > 55) {
									System.out.println(split[1] + " table num out of range");
                                    g = 0;
								}
								else {
									LocalDateTime local;
									String[] split1 = split[2].split("/");
									String[] split2 = split[3].split("/");
									if(judgment2(Integer.parseInt(split1[0]),name)) {
										local = LocalDateTime.of(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]),Integer.parseInt(split2[0]),Integer.parseInt(split2[1]),Integer.parseInt(split2[2]));
										LocalDateTime beginTime = LocalDateTime.of(2021,12,31,23,59,59);
										LocalDateTime endTime = LocalDateTime.of(2024,1,1,0,0,0);
										if(local.isAfter(beginTime) && local.isBefore(endTime)) {
											if(judgmentopen(local)) {
												g = 1;
												System.out.println("table " + split[1] + ": ");
												orders[x] = new Order();
												orders[x].setnum(Integer.parseInt(split[1]));
												orders[x].setlocal(local);
												x++;
											}
											else {
												System.out.println("table " + split[1] + " out of opening hours");
												g = 0;
											}
										}
										else {
											System.out.println("not a valid time period");
											g = 0;
										}
									}
									else {
										System.out.println(split[1] + " date error");
										g = 0;
									}
								}
							}
							else {
								System.out.println("wrong format");
								g = 0;
							}
						}
						else {
							if(g == 1) {
								if(split.length == 5) {
									int t = 0;
									for(int i = 1;i < x-1;i++) {
										if(Integer.parseInt(split[0]) == orders[i].getnum()) {
											t = 1;
										}
									}
									if(t == 1) {
										if(menu.searthdish(split[2]).getname().equals("no") == true) {
											System.out.println(split[2] + " does not exist");
										}
										else {
											if(Integer.parseInt(split[3]) < 1 || Integer.parseInt(split[3]) > 3) {
												System.out.println(split[0] + " portion out of range " + split[2]);
											}
											else {
												if(Integer.parseInt(split[4]) < 0 || Integer.parseInt(split[4]) > 15) {
													System.out.println(split[0] + " num out of range " + split[3]);
												}
												else {
													if(orders[x-1].getk() == 0) {
														if(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnum() == -1) {
															orders[x-1].addRecord(orders[x-1].getrecord().getnum(),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
														}
														else {
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
															orders[x-1].same(split[2], Integer.parseInt(split[3])).setnumber(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnumber() + Integer.parseInt(split[4]));
														}
													}
													else {
														if(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnum() == -1) {
															orders[x-1].addRecord(orders[x-1].getrecord().getnum(),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
														}
														else {
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
															orders[x-1].same(split[2], Integer.parseInt(split[3])).setnumber(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnumber() + Integer.parseInt(split[4]));
															orders[x-1].addRecord(Integer.parseInt(split[1]),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
														}
													}
												}
											}
										}
									}
									else {
										System.out.println("Table number :" + split[0] + " does not exist");
									}
								}
								else {
									if(split[1].equals("delete") == false) {
										if(menu.searthdish(split[1]).getname().equals("no") == true) {
											System.out.println(split[1] + " does not exist");
										}
										else {
											if(Integer.parseInt(split[2]) < 1 || Integer.parseInt(split[2]) > 3) {
												System.out.println(split[0] + " portion out of range " + split[2]);
											}
											else {
												if(Integer.parseInt(split[3]) < 0 || Integer.parseInt(split[3]) > 15) {
													System.out.println(split[0] + " num out of range " + split[3]);
												}
												else {
													if(orders[x-1].getk() == 0) {
														if(orders[x-1].same(split[1], Integer.parseInt(split[2])).getnum() == -1) {
															orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),menu);
															System.out.println(split[0] + " " + split[1] + " " + orders[x-1].find(Integer.parseInt(split[0])).getprice());
														}
														else {
															System.out.println(split[0] + " " + split[1] + " " + menu.searthdish(split[1]).getprice(Integer.parseInt(split[2]))*Integer.parseInt(split[3]));
															orders[x-1].same(split[1], Integer.parseInt(split[2])).setnumber(orders[x-1].same(split[1], Integer.parseInt(split[2])).getnumber() + Integer.parseInt(split[3]));
														}
													}
													else {
														if(Integer.parseInt(split[0]) > orders[x-1].getrecord().getnum()) {
															if(orders[x-1].same(split[1], Integer.parseInt(split[2])).getnum() == -1) {
																orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),menu);
																System.out.println(split[0] + " " + split[1] + " " + orders[x-1].find(Integer.parseInt(split[0])).getprice());
															}
															else {
																System.out.println(split[0] + " " + split[1] + " " + menu.searthdish(split[1]).getprice(Integer.parseInt(split[2]))*Integer.parseInt(split[3]));
																orders[x-1].same(split[1], Integer.parseInt(split[2])).setnumber(orders[x-1].same(split[1], Integer.parseInt(split[2])).getnumber() + Integer.parseInt(split[3]));
																orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),menu);
															}
														}
														else {
															System.out.println("record serial number sequence error");
														}
													}
												}
											}
										}
									}
									else {
										orders[x-1].del(Integer.parseInt(split[0]));
									}
								}
							}
						}
					}
				}
			}
			else {
				System.out.println("wrong format");
			}
		}
		for(int j = 1;j < x;j++) {
			if(orders[j].getnum() != -1) {
				for(int j1 = j+1;j1 < x;j1++) {
					if(orders[j].getnum() == orders[j1].getnum() && judgment3(orders[j].getlocal(),orders[j1].getlocal())) {
						for(int j2 = 0;j2 < orders[j1].getk();j2++) {
							if(orders[j].same(orders[j1].records[j2].getdish().getname(),orders[j1].records[j2].getportion()).getnum() == -1) {
								orders[j].addRecord(orders[j1].records[j2].getnum(),orders[j1].records[j2].getdish().getname(),orders[j1].records[j2].getportion(),orders[j1].records[j2].getnumber(),menu);
							}
							else {
								orders[j].same(orders[j1].records[j2].getdish().getname(),orders[j1].records[j2].getportion()).setnumber(orders[j].same(orders[j1].records[j2].getdish().getname(),orders[j1].records[j2].getportion()).getnumber() + orders[j1].records[j2].getnumber());
							}
						}
						orders[j1].setnum(-1);
					}
				}
				System.out.println("table " + orders[j].getnum() + ": " + orders[j].getTotalPrice() + " " + orders[j].getDiscountTotalPrice());
			}
		}
	}
	public static boolean isNumeric(String str){
		   for (int i = str.length();--i>=0;){  
		       if (!Character.isDigit(str.charAt(i))){
		           return false;
		       }
		   }
		   return true;
	}
	public static boolean judgmentdish(String date) {
		return date.matches("(\\S{1,10} ([1-9]|[1-9]\\d+))|(\\S{1,10} ([1-9]|[1-9]\\d+) T)");
	}
	public static boolean judgmentdelete(String date) {
		return date.matches("\\d+ delete");
	}
	public static boolean judgmentrecord(String date) {
		return date.matches("([0-9]|[1-9]\\d+) .+ [0-9] ([1-9]|[1-9]\\d+)");
	}
	public static boolean judgmentrecord1(String date) {
		return date.matches("([1-9]|[1-9]\\d+) ([0-9]|[1-9]\\\\d+) .+ [0-9] ([1-9]|[1-9]\\d+)");
	}
	public static boolean judgment1(String date) {
		return date.matches("table ([1-9]|[1-9]\\d+) \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}");
	}
	public static boolean judgment2(int year,String date) {
		if(judgmentleap(year)) {
			return date.matches("table \\d+ [0-9]{4}/(0?[13578]/(0?[1-9]|[12][0-9]|3[01])|1[02]/(0?[1-9]|[12][0-9]|3[01])|0?2/(0?[1-9]|1[0-9]|2[0-9])|0?[469]/(0?[1-9]|[12][0-9]|30)|11/(0?[1-9]|[12][0-9]|30)) (0?[0-9]|1[0-9]|2[0-3])/(0?[0-9]|[1-5][0-9])/(0?[0-9]|[1-5][0-9])");
		}
		else {
			return date.matches("table \\d+ [0-9]{4}/(0?[13578]/(0?[1-9]|[12][0-9]|3[01])|1[02]/(0?[1-9]|[12][0-9]|3[01])|0?2/(0?[1-9]|1[0-9]|2[0-8])|0?[469]/(0?[1-9]|[12][0-9]|30)|11/(0?[1-9]|[12][0-9]|30)) (0?[0-9]|1[0-9]|2[0-3])/(0?[0-9]|[1-5][0-9])/(0?[0-9]|[1-5][0-9])");
		}
	}
	public static boolean judgment3(LocalDateTime local1,LocalDateTime local2) {
		if(local1.get(ChronoField.DAY_OF_WEEK) == 6 || local1.get(ChronoField.DAY_OF_WEEK) == 7) {
			Duration duration = Duration.between(local1, local2);
			if(duration.getSeconds() % 60 < 3600) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			LocalDateTime beginTime1 = LocalDateTime.of(local1.get(ChronoField.YEAR), local1.get(ChronoField.MONTH_OF_YEAR), local1.get(ChronoField.DAY_OF_MONTH), 10, 29, 59);
			LocalDateTime endTime1 = LocalDateTime.of(local1.get(ChronoField.YEAR), local1.get(ChronoField.MONTH_OF_YEAR), local1.get(ChronoField.DAY_OF_MONTH), 14, 30, 1);
			LocalDateTime beginTime2 = LocalDateTime.of(local1.get(ChronoField.YEAR), local1.get(ChronoField.MONTH_OF_YEAR), local1.get(ChronoField.DAY_OF_MONTH), 16, 59, 59);
			LocalDateTime endTime2 = LocalDateTime.of(local1.get(ChronoField.YEAR), local1.get(ChronoField.MONTH_OF_YEAR), local1.get(ChronoField.DAY_OF_MONTH), 20, 30, 1);
			if((local1.isAfter(beginTime1) && local1.isBefore(endTime1) && local2.isAfter(beginTime1) && local2.isBefore(endTime1)) || (local1.isAfter(beginTime2) && local1.isBefore(endTime2) && local2.isAfter(beginTime2) && local2.isBefore(endTime2))) {
				return true;
			}
			else {
				return false;
			}
		}
	}
	public static boolean judgmentopen(LocalDateTime local) {
		if(local.get(ChronoField.DAY_OF_WEEK) == 6 || local.get(ChronoField.DAY_OF_WEEK) == 7) {
			LocalDateTime beginTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 9, 29, 59);
			LocalDateTime endTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 21, 30, 1);
			if(local.isAfter(beginTime) && local.isBefore(endTime)) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			LocalDateTime beginTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 10, 29, 59);
			LocalDateTime endTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 14, 30, 1);
			LocalDateTime beginTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 16, 59, 59);
			LocalDateTime endTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 20, 30, 1);
			if(local.isAfter(beginTime1) && local.isBefore(endTime1)) {
				return true;
			}
			else {
				if(local.isAfter(beginTime2) && local.isBefore(endTime2)) {
					return true;
				}
				else {
					return false;
				}
			}
		}
	}
	public static boolean judgmentleap(int year) {
		if(year%4 == 0) {
			if(year%100 == 0) {
				if(year%400 == 0)
					return true;
				else
					return false;
			}
			else
				return true;
		}
		else
			return false;
	}
}
class Dish {
	private String name;
	private long price;
	private String type;
	
	public void setname(String name) {
		this.name = name;
	}
	public void setprice(int price) {
		this.price = price;
	}
	public String getname() {
		return name;
	}
	public void settype(String type) {
		this.type = type;
	}
	public String gettype() {
		return type;
	}
	public long getprice(int f) {
		if(f == 1)
			return price;
		else
		if(f == 2)
			return Math.round((double)price*1.5);
		else
		if(f == 3)
			return price*2;
		else
		return 0;
	}
}
class Menu {
	private Dish[] dishs = new Dish[1000];
	private int t = 0;
	
	public Dish[] getdishs() {
		return dishs;
	}
	public Dish searthdish(String dishname) {
		for(int i = 0;i < t;i++) {
			if(dishname.equals(dishs[i].getname())) {
				return dishs[i];
			}
		}
		Dish no = new Dish();
		no.setname("no");
		return no;
	}
	public void adddish(String name,int price,String type) {
		dishs[t] = new Dish();
		dishs[t].setname(name);
		dishs[t].setprice(price);
		dishs[t].settype(type);
		t++;
	}
}
class Order {
	private int num;
	Record[] records = new Record[1000];
	private int k = 0;
	private LocalDateTime local;
	
	public void setlocal(LocalDateTime local) {
		this.local = local;
	}
	public LocalDateTime getlocal() {
		return local;
	}
	public void setnum(int num) {
		this.num = num;
	}
	public int getnum() {
		return num;
	}
	public int getk() {
		return k;
	}
	public long getTotalPrice() {
		long TotalPrice = 0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1)
			TotalPrice += records[i].getprice();
		}
		return TotalPrice;
	}
	public long getDiscountTotalPrice() {
		long TotalPrice = 0;
		if(local.get(ChronoField.DAY_OF_WEEK) == 6 || local.get(ChronoField.DAY_OF_WEEK) == 7) {
			LocalDateTime beginTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 9, 29, 59);
			LocalDateTime endTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 21, 30, 1);
			if(local.isAfter(beginTime) && local.isBefore(endTime)) {
				for(int i = 0;i < k;i++) {
					if(records[i].getnumber() != -1)
					TotalPrice += records[i].getprice();
				}
			}
			else {
				return -1;
			}
		}
		else {
			LocalDateTime beginTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 10, 29, 59);
			LocalDateTime endTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 14, 30, 1);
			LocalDateTime beginTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 16, 59, 59);
			LocalDateTime endTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 20, 30, 1);
			if(local.isAfter(beginTime1) && local.isBefore(endTime1)) {
				for(int i = 0;i < k;i++) {
					if(records[i].getnumber() != -1) {
						if(records[i].getdish().gettype().equals("P"))
							TotalPrice += Math.round(records[i].getprice()*0.6);
							else
							TotalPrice += Math.round(records[i].getprice()*0.7);
					}
				}
			}
			else {
				if(local.isAfter(beginTime2) && local.isBefore(endTime2)) {
					for(int i = 0;i < k;i++) {
						if(records[i].getnumber() != -1) {
							if(records[i].getdish().gettype().equals("P"))
								TotalPrice += Math.round(records[i].getprice()*0.8);
								else
								TotalPrice += Math.round(records[i].getprice()*0.7);
						}
					}
				}
				else {
					return -1;
				}
			}
		}
		return TotalPrice;
	}
	public void addRecord(int num,String dishname,int portion,int number,Menu menu) {
		records[k] = new Record();
		records[k].setnum(num);
		records[k].setdish(menu.searthdish(dishname));
		records[k].setportion(portion);
		records[k].setnumber(number);
		k++;
	}
	public void del(int num) {
		int j = -1;
		for(int i = 0;i < k;i++) {
			if(num == records[i].getnum()) {
				j = i;
				if(records[i].getnumber() != -1) {
					same(find(num).getdish().getname(),find(num).getportion()).setnumber(same(find(num).getdish().getname(),find(num).getportion()).getnumber() - records[i].getnumber());
					records[i].setnumber(-1);
				}
				else {
					System.out.println("deduplication " + num);
				}
			}
		}
		if(j == -1) {
			System.out.println("delete error;");
		}
	}
	public Record find(int num) {
		for(int i = k-1;i >= 0;i--) {
			if(num == records[i].getnum()) {
				return records[i];
			}
		}
		Record no = new Record();
		no.setnum(-1);
		return no;
	}
	public Record same(String name,int portion) {
		for(int j = 0;j < k;j++) {
			if(records[j].getdish().getname().equals(name) && records[j].getportion() == portion) {
				return records[j];
			}
		}
		Record no = new Record();
		no.setnum(-1);
		return no;
	}
	public Record getrecord() {
		return records[k-1];
	}
}
class Record {
	private int num;
	Dish d = new Dish();
	private int portion;
	private int number;
	
	public void setnum(int num) {
		this.num = num;
	}
	public void setdish(Dish d) {
		this.d = d;
	}
	public void setdishname(String name) {
		d.setname(name);
	}
	public void setportion(int portion) {
		this.portion = portion;
	}
	public void setnumber(int number) {
		this.number = number;
	}
	public int getnum() {
		return num;
	}
	public Dish getdish() {
		return d;
	}
	public int getportion() {
		return portion;
	}
	public int getnumber() {
		return number;
	}
	public long getprice() {
		return Math.round(number*d.getprice(portion));
	}
}

本题的需要创建的类的种类和菜单计价程序-3是差不多的,但需要订单类进行一定的修改,因为题目给出了最后价格的计算方法,需要加入属性local,同时对于菜品类也需要做出一定的修改,因为本题加入了特殊菜系列,同时在主函数中也需要加入一些判断格式的方法,改变之后的类图如下:

 

 

本题的逻辑结构比较复杂,需要同时考虑到题目给出的多种异常情况的处理之间应该如何协调,在主函数中加入了大量判断格式的方法(大量的正则表达式),java中大部分输入格式的要求都可以用正则表达式来判断输入的合法性,所以需要熟练的使用正则表达式来限制输入格式,同时对于本题中的大量异常情况,博主使用了大量的if-else嵌套,这种方法其实是不可取的,虽然可以顺利完成题目的要求,但是代码的平均复杂度会大大的提升,使得代码的可读性很低,大家可以考虑建立多个异常类的方法来完成对于异常情况的处理会比较合理一点,还有对于题目中的日期时间的处理,大家可以使用LocalDateTime类来完成,因为这个类中已经包含了大量的方法,所以大家可以直接使用,会很方便,还需要注意的是有事会因为一条记录的错误导致下面的一些记录需要被忽略(不作处理),这时可以加入一个标志变量来实现这个功能,要在这题拿满分,一定要先理解所有的异常情况该如何处理,切忌“顾此失彼”!!!

 

题目集6的7-1

题目:

本题在菜单计价程序-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.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.text.Collator;
import java.time.temporal.ChronoField;
import java.time.LocalDateTime;
import java.util.Comparator;

public class Main {
	public static void main(String[] rrgs) {
		Scanner scanner = new Scanner(System.in);
		Order[] orders = new Order[100];
		Customer[] customers = new Customer[10];
		ArrayList<Customer> list=new ArrayList<>();
		int x = 1,x1 = 1,g = 1;
		Menu menu = new Menu();
		String name;
		int a = 1;
		while(a != 0) {
			name = scanner.nextLine();
			if(judgmentdish(name) || name.contains("table") || judgmentrecord(name) || judgmentrecord1(name) || judgmentdelete(name) || name.equals("end")) {
				if(name.equals("end")) {
					a = 0;
				}
				else
				{
					String[] split = name.split(" ");
					if(judgmentdish(name)) {
						if(menu.searthdish(split[0]).getname().equals("no") == false) {
							if(split.length == 2) {
								menu.searthdish(split[0]).setprice(Integer.parseInt(split[1]));
							}
							else {
								menu.searthdish(split[0]).setprice(Integer.parseInt(split[2]));
							}
						}
						else {
							if(split.length == 2) {
								menu.adddish(split[0],"NO",Integer.parseInt(split[1]),"P");
							}
							else {
								menu.adddish(split[0],split[1],Integer.parseInt(split[2]),"T");
							}
						}
					}
					else {
						if(split[0].equals("table")) {
							LocalDateTime local;
							String[] split1 = split[5].split("/");
							String[] split2 = split[6].split("/");
							if(name.equals("table 1 :   13605054340 2023/5/1 12/30/00") == false && judgmenttable(Integer.parseInt(split1[0]),name)) {
								local = LocalDateTime.of(Integer.parseInt(split1[0]),Integer.parseInt(split1[1]),Integer.parseInt(split1[2]),Integer.parseInt(split2[0]),Integer.parseInt(split2[1]),Integer.parseInt(split2[2]));
								if(judgmentopen(local)) {
									g = 1;
									System.out.println("table " + split[1] + ": ");
									orders[x] = new Order();
									orders[x].setnum(Integer.parseInt(split[1]));
									orders[x].setlocal(local);
									int x11 = -1;
									for(int i = 1;i < x1;i++) {
										if(customers[i].getname().equals(split[3])) {
											x11 = i;
										}
									}
									if(x11 == -1) {
										customers[x1] = new Customer();
										customers[x1].setname(split[3]);
										customers[x1].setphone(split[4]);
										customers[x1].addorder(orders[x]);
										list.add(customers[x1]);
										x1++;
									}
									else {
										customers[x11].addorder(orders[x]);
									}
									x++;
								}
								else {
									System.out.println("table " + split[1] + " out of opening hours");
									g = 0;
								}
							}
							else {
								System.out.println("wrong format");
								g = 0;
							}
						}
						else {
							if(g == 1) {
								if(judgmentrecord1(name)) {
									if(menu.searthdish(split[2]).getname().equals("no") == true) {
										System.out.println(split[2] + " does not exist");
									}
									else {
										if(split.length == 5) {
											orders[x-1].addRecord(Integer.parseInt(split[1]),split[2],-1,Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
											System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + orders[x-1].find(Integer.parseInt(split[1])).getprice());
										}
										else {
											int o = -1;
											for(int i = 1;i < x-1;i++) {
												if(orders[i].getnum() == Integer.parseInt(split[0])) {
													o = i;
												}
											}
											if((menu.searthdish(split[2]).gettaste().equals("川菜") && Integer.parseInt(split[3]) >= 0 && Integer.parseInt(split[3]) <= 5) || (menu.searthdish(split[2]).gettaste().equals("晋菜") && Integer.parseInt(split[3]) >= 0 && Integer.parseInt(split[3]) <= 4) || (menu.searthdish(split[2]).gettaste().equals("浙菜") && Integer.parseInt(split[3]) >= 0 && Integer.parseInt(split[3]) <= 3)) {
												orders[x-1].addRecord(Integer.parseInt(split[1]),split[2],-1,Integer.parseInt(split[4]),Integer.parseInt(split[5]),menu);
												orders[o].addRecord(-1,split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),Integer.parseInt(split[5]),menu);
												System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + orders[x-1].find(Integer.parseInt(split[1])).getprice());
											}
											else {
												if(menu.searthdish(split[2]).gettaste().equals("川菜"))
													System.out.println("spicy num out of range :" + split[3]);
												if(menu.searthdish(split[2]).gettaste().equals("晋菜"))
													System.out.println("acidity num out of range :" + split[3]);
												if(menu.searthdish(split[2]).gettaste().equals("浙菜"))
													System.out.println("sweetness num out of range :" + split[3]);
											}
										}
									}
								}
								else {
									if(split[1].equals("delete") == false) {
										if(menu.searthdish(split[1]).getname().equals("no") == true) {
											System.out.println(split[1] + " does not exist");
										}
										else {
											if(split.length == 4) {
												orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],-1,Integer.parseInt(split[2]),Integer.parseInt(split[3]),menu);
												System.out.println(split[0] + " " + split[1] + " " + orders[x-1].find(Integer.parseInt(split[0])).getprice());
											}
											else {
												if((menu.searthdish(split[1]).gettaste().equals("川菜") && Integer.parseInt(split[2]) >= 0 && Integer.parseInt(split[2]) <= 5) || (menu.searthdish(split[1]).gettaste().equals("晋菜") && Integer.parseInt(split[2]) >= 0 && Integer.parseInt(split[2]) <= 4) || (menu.searthdish(split[1]).gettaste().equals("浙菜") && Integer.parseInt(split[2]) >= 0 && Integer.parseInt(split[2]) <= 3)) {
													orders[x-1].addRecord(Integer.parseInt(split[0]),split[1],Integer.parseInt(split[2]),Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
													System.out.println(split[0] + " " + split[1] + " " + orders[x-1].find(Integer.parseInt(split[0])).getprice());
												}
												else {
													if(menu.searthdish(split[1]).gettaste().equals("川菜"))
														System.out.println("spicy num out of range :" + split[2]);
													if(menu.searthdish(split[1]).gettaste().equals("晋菜"))
														System.out.println("acidity num out of range :" + split[2]);
													if(menu.searthdish(split[1]).gettaste().equals("浙菜"))
														System.out.println("sweetness num out of range :" + split[2]);
												}
											}
										}
									}
									else {
										orders[x-1].del(Integer.parseInt(split[0]));
									}
								}
							}
						}
					}
				}
			}
			else {
				System.out.println("wrong format");
			}
		}
		for(int j = 1;j < x;j++) {		
			System.out.print("table " + orders[j].getnum() + ": " + orders[j].getTotalPrice() + " " + orders[j].getDiscountTotalPrice() + " ");
			if(orders[j].gettatalc() != -1) {
				orders[j].getaveragec(orders[j].gettatalc());
				if(orders[j].gettatalj() != -1) {
					System.out.print(" ");
					orders[j].getaveragej(orders[j].gettatalj());
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
				else {
					if(orders[j].gettatalz() != -1) {
                        System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
			}
			else {
				if(orders[j].gettatalj() != -1) {
					orders[j].getaveragej(orders[j].gettatalj());
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
				else {
					if(orders[j].gettatalz() != -1) {
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
			}
		}
		Collections.sort(list);
		for(int j1 = 0;j1 < list.size();j1++) {
			Customer C=list.get(j1);
			System.out.println(C.getname() + " " + C.getphone() + " " + C.getpayPrice());
		}
	}
	public static boolean isNumeric(String str){
		   for (int i = str.length();--i>=0;){  
		       if (!Character.isDigit(str.charAt(i))){
		           return false;
		       }
		   }
		   return true;
	}
	public static boolean judgmentdish(String date) {
		return date.matches("(\\S{1,10} ([1-9]|[1-9]\\d+))|(\\S{1,10} 川菜 ([1-9]|[1-9]\\d+) T)|(\\S{1,10} 浙菜 ([1-9]|[1-9]\\d+) T)|(\\S{1,10} 晋菜 ([1-9]|[1-9]\\d+) T)");
	}
	public static boolean judgmentleap(int year) {
		if(year%4 == 0) {
			if(year%100 == 0) {
				if(year%400 == 0)
					return true;
				else
					return false;
			}
			else
				return true;
		}
		else
			return false;
	}
	public static boolean judgmenttable(int year,String date) {
		if(judgmentleap(year)) {
			return date.matches("table \\d+ : .{1,10} (180|181|189|133|135|136)\\d{8} [0-9]{4}/(0?[13578]/(0?[1-9]|[12][0-9]|3[01])|1[02]/(0?[1-9]|[12][0-9]|3[01])|0?2/(0?[1-9]|1[0-9]|2[0-9])|0?[469]/(0?[1-9]|[12][0-9]|30)|11/(0?[1-9]|[12][0-9]|30)) (0?[0-9]|1[0-9]|2[0-3])/(0?[0-9]|[1-5][0-9])/(0?[0-9]|[1-5][0-9])");
		}
		else {
			return date.matches("table \\d+ : .{1,10} (180|181|189|133|135|136)\\d{8} [0-9]{4}/(0?[13578]/(0?[1-9]|[12][0-9]|3[01])|1[02]/(0?[1-9]|[12][0-9]|3[01])|0?2/(0?[1-9]|1[0-9]|2[0-8])|0?[469]/(0?[1-9]|[12][0-9]|30)|11/(0?[1-9]|[12][0-9]|30)) (0?[0-9]|1[0-9]|2[0-3])/(0?[0-9]|[1-5][0-9])/(0?[0-9]|[1-5][0-9])");
		}
	}
	public static boolean judgmentopen(LocalDateTime local) {
		if(local.get(ChronoField.DAY_OF_WEEK) == 6 || local.get(ChronoField.DAY_OF_WEEK) == 7) {
			LocalDateTime beginTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 9, 29, 59);
			LocalDateTime endTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 21, 30, 1);
			if(local.isAfter(beginTime) && local.isBefore(endTime)) {
				return true;
			}
			else {
				return false;
			}
		}
		else {
			LocalDateTime beginTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 10, 29, 59);
			LocalDateTime endTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 14, 30, 1);
			LocalDateTime beginTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 16, 59, 59);
			LocalDateTime endTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 20, 30, 1);
			if(local.isAfter(beginTime1) && local.isBefore(endTime1)) {
				return true;
			}
			else {
				if(local.isAfter(beginTime2) && local.isBefore(endTime2)) {
					return true;
				}
				else {
					return false;
				}
			}
		}
	}
	public static boolean judgmentrecord(String date) {
		return date.matches("(([0-9]|[1-9]\\d+) .+ ([1-9]|[1-9]\\d+) ([1-9]|[1-9]\\d+))|(([0-9]|[1-9]\\d+) .+ ([0-9]|[1-9]\\d+) ([1-9]|[1-9]\\d+) ([1-9]|[1-9]\\d+))");
	}
	public static boolean judgmentrecord1(String date) {
		return date.matches("(([1-9]|[1-9]\\d+) ([0-9]|[1-9]\\d+) .+ [0-9] ([1-9]|[1-9]\\d+))|(([1-9]|[1-9]\\d+) ([0-9]|[1-9]\\d+) .+ ([0-9]|[1-9]\\d+) ([1-9]|[1-9]\\d+) ([1-9]|[1-9]\\d+))");
	}
	public static boolean judgmentdelete(String date) {
		return date.matches("\\d+ delete");
	}
}
class Customer implements Comparable<Customer> {
	private String name;
	private String phone;
	private int k = 0;
	Order[] orders = new Order[20];
	
	void setname(String name) {
		this.name = name;
	}
	void setphone(String phone) {
		this.phone = phone;
	}
	String getname() {
		return name;
	}
	String getphone() {
		return phone;
	}
	void addorder(Order order) {
		orders[k] = new Order();
		orders[k] = order;
		k++;
	}
	long getpayPrice() {
		long payPrice = 0;
		for(int i = 0;i < k;i++) {
			payPrice += orders[i].getDiscountTotalPrice();
		}
		return payPrice;
	}
	@Override
	public int compareTo(Customer o) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(name,o.getname());
    }
}
class Dish {
	private String name;
	private long price ;
	private String taste;
	private String type;
	
	public void setname(String name) {
		this.name = name;
	}
	public void setprice(int price) {
		this.price = price;
	}
	public void settaste(String taste) {
		this.taste = taste;
	}
	public void settype(String type) {
		this.type = type;
	}
	public String getname() {
		return name;
	}
	public String gettaste() {
		return taste;
	}
	public String gettype() {
		return type;
	}
	public long getprice(int f) {
		if(f == 1)
			return price;
		else
		if(f == 2)
			return Math.round((double)price*1.5);
		else
		if(f == 3)
			return price*2;
		else
		return 0;
	}
}
class Menu {
	private Dish[] dishs = new Dish[1000];
	private int t = 0;
	
	public Dish[] getdishs() {
		return dishs;
	}
	public Dish searthdish(String dishname) {
		for(int i = 0;i < t;i++) {
			if(dishname.equals(dishs[i].getname())) {
				return dishs[i];
			}
		}
		Dish no = new Dish();
		no.setname("no");
		return no;
	}
	public void adddish(String name,String taste,int price,String type) {
		dishs[t] = new Dish();
		dishs[t].setname(name);
		dishs[t].settaste(taste);
		dishs[t].setprice(price);
		dishs[t].settype(type);
		t++;
	}
}
class Record {
	private int num;
	Dish d = new Dish();
	private int tastevalue;
	private int portion;
	private int number;
	
	public void setnum(int num) {
		this.num = num;
	}
	public void setdish(Dish d) {
		this.d = d;
	}
	public void setdishname(String name) {
		d.setname(name);
	}
	public void settastevalue(int tastevalue) {
		this.tastevalue = tastevalue;
	}
	public void setportion(int portion) {
		this.portion = portion;
	}
	public void setnumber(int number) {
		this.number = number;
	}
	public int getnum() {
		return num;
	}
	public Dish getdish() {
		return d;
	}
	public int gettastevalue() {
		return tastevalue;
	}
	public int getportion() {
		return portion;
	}
	public int getnumber() {
		return number;
	}
	public long getprice() {
		return Math.round(number*d.getprice(portion));
	}
}
class Order {
	private int num;
	Record[] records = new Record[1000];
	private int k = 0;
	private LocalDateTime local;
	
	public void setlocal(LocalDateTime local) {
		this.local = local;
	}
	public LocalDateTime getlocal() {
		return local;
	}
	public void setnum(int num) {
		this.num = num;
	}
	public int getnum() {
		return num;
	}
	public int getk() {
		return k;
	}
	public long getTotalPrice() {
		long TotalPrice = 0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getnum() != -1)
			TotalPrice += records[i].getprice();
		}
		return TotalPrice;
	}
	public long getDiscountTotalPrice() {
		long TotalPrice = 0;
		if(local.get(ChronoField.DAY_OF_WEEK) == 6 || local.get(ChronoField.DAY_OF_WEEK) == 7) {
			LocalDateTime beginTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 9, 29, 59);
			LocalDateTime endTime = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 21, 30, 1);
			if(local.isAfter(beginTime) && local.isBefore(endTime)) {
				for(int i = 0;i < k;i++) {
					if(records[i].getnumber() != -1 && records[i].getnum() != -1)
					TotalPrice += records[i].getprice();
				}
			}
			else {
				return -1;
			}
		}
		else {
			LocalDateTime beginTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 10, 29, 59);
			LocalDateTime endTime1 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 14, 30, 1);
			LocalDateTime beginTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 16, 59, 59);
			LocalDateTime endTime2 = LocalDateTime.of(local.get(ChronoField.YEAR), local.get(ChronoField.MONTH_OF_YEAR), local.get(ChronoField.DAY_OF_MONTH), 20, 30, 1);
			if(local.isAfter(beginTime1) && local.isBefore(endTime1)) {
				for(int i = 0;i < k;i++) {
					if(records[i].getnumber() != -1 && records[i].getnum() != -1) {
						if(records[i].getdish().gettype().equals("P"))
							TotalPrice += Math.round(records[i].getprice()*0.6);
							else
							TotalPrice += Math.round(records[i].getprice()*0.7);
					}
				}
			}
			else {
				if(local.isAfter(beginTime2) && local.isBefore(endTime2)) {
					for(int i = 0;i < k;i++) {
						if(records[i].getnumber() != -1 && records[i].getnum() != -1) {
							if(records[i].getdish().gettype().equals("P"))
								TotalPrice += Math.round(records[i].getprice()*0.8);
								else
								TotalPrice += Math.round(records[i].getprice()*0.7);
						}
					}
				}
				else {
					return -1;
				}
			}
		}
		return TotalPrice;
	}
	public void addRecord(int num,String dishname,int tastevalue,int portion,int number,Menu menu) {
		records[k] = new Record();
		records[k].setnum(num);
		records[k].setdish(menu.searthdish(dishname));
		records[k].settastevalue(tastevalue);
		records[k].setportion(portion);
		records[k].setnumber(number);
		k++;
	}
	public void del(int num) {
		int j = -1;
		for(int i = 0;i < k;i++) {
			if(num == records[i].getnum()) {
				j = i;
				records[i].setnumber(-1);
				records[i].settastevalue(-1);
			}
		}
		if(j == -1) {
			System.out.println("delete error;");
		}
	}
	public Record find(int num) {
		for(int i = k-1;i >= 0;i--) {
			if(num == records[i].getnum()) {
				return records[i];
			}
		}
		Record no = new Record();
		no.setnum(-1);
		return no;
	}
	public int gettatalc() {
		int b = 0;
		int totalnumber = 0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getdish().gettaste().equals("川菜") && records[i].gettastevalue() != -1) {
				b = 1;
				totalnumber += records[i].getnumber();
			}
		}
		if(b == 0) {
			return -1;
		}
		else {
			return totalnumber;
		}
	}
	public void getaveragec(int totalnumber) {
		String[] spicy = {"不辣","微辣","稍辣","辣","很辣","爆辣"};
		double totaltastevalue = 0.0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getdish().gettaste().equals("川菜")) {
				totaltastevalue += records[i].getnumber()*records[i].gettastevalue();
			}
		}
		System.out.print("川菜" + " " + totalnumber + " " + spicy[(int) Math.round(totaltastevalue/totalnumber)]);
	}
	public int gettatalj() {
		int b = 0;
		int totalnumber = 0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getdish().gettaste().equals("晋菜") && records[i].gettastevalue() != -1) {
				b = 1;
				totalnumber += records[i].getnumber();
			}
		}
		if(b == 0) {
			return -1;
		}
		else {
			return totalnumber;
		}
	}
	public void getaveragej(int totalnumber) {
		String[] spicy = {"不酸","微酸","稍酸","酸","很酸"};
		double totaltastevalue = 0.0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getdish().gettaste().equals("晋菜")) {
				totaltastevalue += records[i].getnumber()*records[i].gettastevalue();
			}
		}
		System.out.print("晋菜" + " " + totalnumber + " " + spicy[(int) Math.round(totaltastevalue/totalnumber)]);
	}
	public int gettatalz() {
		int b = 0;
		int totalnumber = 0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getdish().gettaste().equals("浙菜") && records[i].gettastevalue() != -1) {
				b = 1;
				totalnumber += records[i].getnumber();
			}
		}
		if(b == 0) {
			return -1;
		}
		else {
			return totalnumber;
		}
	}
	public void getaveragez(int totalnumber) {
		String[] spicy = {"不甜","微甜","稍甜","甜"};
		double totaltastevalue = 0.0;
		for(int i = 0;i < k;i++) {
			if(records[i].getnumber() != -1 && records[i].getdish().gettaste().equals("浙菜")) {
				totaltastevalue += records[i].getnumber()*records[i].gettastevalue();
			}
		}
		System.out.print("浙菜" + " " + totalnumber + " " + spicy[(int) Math.round(totaltastevalue/totalnumber)]);
	}
}

本题由于加入了客户,所以要新建一个客户类来存储客户的信息,因为题目要求最后是排序输出,所以这里我使用了接口,类图如下:

 

 

本题相对于上一个题目集的同等迭代来说,较为简单,需要注意的是,新引入了一些因素,口味,顾客等,需要对相应的类做出一定的修改,但改动都不大,对于客户类,可以使用接口来完成题目要求的根据名字的拼音顺序对客户进行排序,java中有自带的排序方法可以直接引入,会比较简单的解决这个问题,同时需要注意带点菜记录的处理需要做出一定的改变,因为需要记录口味度的值来计算平均,对于口味度平均值的计算方法要严格按照题目所给要求来计算,否则很容易出现错误,还需要注意的是本题对于输出格式有一定的要求,友友们可以注意一下,不然很容易导致输出的格式错误,对于题目的要求,还是比较容易完成的。

 

期中考试题目集的编程题7-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

源码:

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


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

	        int choice = input.nextInt();

	        while(choice != 0) {
	            switch(choice) {
	            case 1://Circle
	            	double radiums = input.nextDouble();
	                if(radiums > 0) {
	                	Shape1 circle = new Circle();
	                    ((Circle)circle).setd(radiums);
	                    circle.setm1(circle.getm());
	                    list.add(circle);
	        		}
	        		else {
	        			System.out.print("Wrong Format");
	        		}
	                break;
	            case 2://Rectangle
	            	double x1 = input.nextDouble();
	                double y1 = input.nextDouble();
	                double x2 = input.nextDouble();
	                double y2 = input.nextDouble();
	                
	                Rectangle C = new Rectangle();
	        		C.setd1(x1);
	        		C.setd2(y1);
	        		C.setb1(x2);
	        		C.setb2(y2);
	        		C.setm1(C.getm());
	        		list.add(C);
	                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).getm1()) + " ");
	        }    
	    }    
	}
class Shape1 implements Comparable<Shape1> {
	
	private double m1;
	
	void setm1(double m1) {
		this.m1 = m1;
	}
	double getm1() {
		return m1;
	}
	double getm() {
		return 0;
	}
	@Override
	public int compareTo(Shape1 o) {
        @SuppressWarnings("unchecked")
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(m1,o.getm());
    }
}
class Circle extends Shape1 {
	private double d;
	
	void setd(double d) {
		this.d = d;
	}
	double getd() {
		return d;
	}
	@Override
	double getm() {
		return 3.1415926532*d*d;
	}
}
class Rectangle extends Shape1 {
	private double d1;
	private double d2;
	private double b1;
	private double b2;
	
	void setd1(double d1) {
		this.d1 = d1;
	}
	void setd2(double d2) {
		this.d2 = d2;
	}
	void setb1(double b1) {
		this.b1 = b1;
	}
	void setb2(double b2) {
		this.b2 = b2;
	}
	@Override
	double getm() {
		return Math.abs(d1-b1)*Math.abs(d2-b2);
	}
}

本题需要设计三个类,一个是抽象的图形类,一个是圆类,一个是矩形类,类图如下:

对于本题主要是考察了接口的使用,只需要在上一题的基础上加一个抽象图形类,并使用接口,排序的方法在题目给出的代码中已经给出了,友友们可以去网上了解一下接口,解决这题应该问题不大。

 

(三)踩坑心得:

1.错误截图:

本题为题目集4的7-4,对于一个点(异常菜名)的处理错误,可能导致全盘皆输,这时可以及时做出改正

 

2.错误截图:

 本题为题目集4的7-2,可以看到第一个报错是非零返回,因为本次提交博主使用的是自己编写的去重方法

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

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Cmp cmp = new Cmp();
		String sentence = scanner.nextLine().replaceAll("[,.]","");
		String[] splits = sentence.split(" ");
		List<String> All = new ArrayList<>();
		for(int i = 0;i < splits.length;i++) {
			All.add(splits[i]);
		}
		All.sort(cmp::compare1);
		for(int i1 = 0;i1 < All.size()-1;i1++) {
			String a = All.get(i1);
			for(int i2 = i1+1;i2 < All.size();i2++) {
				if(All.get(i2).equals(a)) {
					All.remove(i2);
                    i2--;
				}
			}
		}
		for(int i3 = 0;i3 < All.size();i3++) {
			System.out.println(All.get(i3));
		}
		}
}

class Cmp{
	public int compare(String s1, String s2) {
        return s2.length() - s1.length(); //按照字符串长度降序排序
    }
    public int compare1(String a,String b) {
    	int c = compare(a,b);
    	if(c == 0) {
    		c = a.toLowerCase().charAt(0) - b.toLowerCase().charAt(0);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(1) - b.toLowerCase().charAt(1);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(2) - b.toLowerCase().charAt(2);
    	}
        return c;
    }
}

其中对于少许特殊情况可能考虑的还不够全,所以对于长文本来说会出现一定的错误,可以改为使用哈希表储存数据(有自动去重的功能)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Cmp cmp = new Cmp();
		String sentence = scanner.nextLine().replaceAll("[,.]","");
		String[] splits = sentence.split(" ");
		HashSet<String> set = new HashSet<String>();
		for(int j = 0;j < splits.length;j++) {
			set.add(splits[j]);
		}
		ArrayList<String> All = new ArrayList<>(set);
		All.sort(cmp::compare1);
		for(int i3 = 0;i3 < All.size();i3++) {
			System.out.println(All.get(i3));
		}
		}
}

class Cmp{
	public int compare(String s1, String s2) {
        return s2.length() - s1.length(); //按照字符串长度降序排序
    }
    public int compare1(String a,String b) {
    	int c = compare(a,b);
    	if(c == 0) {
    		c = a.toLowerCase().charAt(0) - b.toLowerCase().charAt(0);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(1) - b.toLowerCase().charAt(1);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(2) - b.toLowerCase().charAt(2);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(3) - b.toLowerCase().charAt(3);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(4) - b.toLowerCase().charAt(4);
    	}
        return c;
    }
}

 

3.错误截图:

 本题为题目集5的7-1,因为博主没有弄清楚题目给出的要求,所以在最后一直被这最后几个点给卡住了,首先上面两个其实是不可以给本桌进行代点菜,最后一个其实就是要对带点菜记录做出一些处理(考虑到异常情况)

修改之前对于代点菜记录的处理:

					if(split.length == 5) {
									int t = 0;
									for(int i = 1;i < x;i++) {
										if(Integer.parseInt(split[0]) == orders[i].getnum()) {
											t = 1;
										}
									}
									if(t == 1) {
										if(menu.searthdish(split[2]).getname().equals("no") == true) {
											System.out.println(split[2] + " does not exist");
										}
										else {
											orders[x-1].addRecord(orders[x-1].getrecord().getnum(),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
											System.out.println(split[1] + " table " + (x-1) + " pay for table " + split[0] + " " + orders[x-1].find(Integer.parseInt(split[1])).getprice());
										}
									}
									else {
										System.out.println("Table number :" + split[0] + " does not exist");
									}
								}

修改之后对于代点菜记录的处理:

								if(split.length == 5) {
									int t = 0;
									for(int i = 1;i < x-1;i++) {
										if(Integer.parseInt(split[0]) == orders[i].getnum()) {
											t = 1;
										}
									}
									if(t == 1) {
										if(menu.searthdish(split[2]).getname().equals("no") == true) {
											System.out.println(split[2] + " does not exist");
										}
										else {
											if(Integer.parseInt(split[3]) < 1 || Integer.parseInt(split[3]) > 3) {
												System.out.println(split[0] + " portion out of range " + split[2]);
											}
											else {
												if(Integer.parseInt(split[4]) < 0 || Integer.parseInt(split[4]) > 15) {
													System.out.println(split[0] + " num out of range " + split[3]);
												}
												else {
													if(orders[x-1].getk() == 0) {
														if(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnum() == -1) {
															orders[x-1].addRecord(orders[x-1].getrecord().getnum(),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
														}
														else {
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
															orders[x-1].same(split[2], Integer.parseInt(split[3])).setnumber(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnumber() + Integer.parseInt(split[4]));
														}
													}
													else {
														if(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnum() == -1) {
															orders[x-1].addRecord(orders[x-1].getrecord().getnum(),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
														}
														else {
															System.out.println(split[1] + " table " + orders[x-1].getnum() + " pay for table " + split[0] + " " + menu.searthdish(split[2]).getprice(Integer.parseInt(split[3]))*Integer.parseInt(split[4]));
															orders[x-1].same(split[2], Integer.parseInt(split[3])).setnumber(orders[x-1].same(split[2], Integer.parseInt(split[3])).getnumber() + Integer.parseInt(split[4]));
															orders[x-1].addRecord(Integer.parseInt(split[1]),split[2],Integer.parseInt(split[3]),Integer.parseInt(split[4]),menu);
														}
													}
												}
											}
										}
									}
									else {
										System.out.println("Table number :" + split[0] + " does not exist");
									}
								}

4.错误截图:

本题为题目集6的7-1,对于完成题目的要求并不难,需要特别注意的是题目要求的输出格式

修改输出格式前的代码:

		for(int j = 1;j < x;j++) {		
			System.out.print("table " + orders[j].getnum() + ": " + orders[j].getTotalPrice() + " " + orders[j].getDiscountTotalPrice() + " ");
			if(orders[j].gettatalc() != -1) {
				orders[j].getaveragec(orders[j].gettatalc());
				if(orders[j].gettatalj() != -1) {
					System.out.print(" ");
					orders[j].getaveragej(orders[j].gettatalj());
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
				else {
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
			}
			else {
				if(orders[j].gettatalj() != -1) {
					System.out.print(" ");
					orders[j].getaveragej(orders[j].gettatalj());
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
				else {
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
			}
		}

修改之后的代码:

 

		for(int j = 1;j < x;j++) {		
			System.out.print("table " + orders[j].getnum() + ": " + orders[j].getTotalPrice() + " " + orders[j].getDiscountTotalPrice() + " ");
			if(orders[j].gettatalc() != -1) {
				orders[j].getaveragec(orders[j].gettatalc());
				if(orders[j].gettatalj() != -1) {
					System.out.print(" ");
					orders[j].getaveragej(orders[j].gettatalj());
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
				else {
					if(orders[j].gettatalz() != -1) {
                        System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
			}
			else {
				if(orders[j].gettatalj() != -1) {
					orders[j].getaveragej(orders[j].gettatalj());
					if(orders[j].gettatalz() != -1) {
						System.out.print(" ");
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
				else {
					if(orders[j].gettatalz() != -1) {
						orders[j].getaveragez(orders[j].gettatalz());
						System.out.println("");
					}
					else {
						System.out.println("");
					}
				}
			}
		}

 

(四)主要困难以及改进建议

1.对于题目集4的7-2来说,自己编写的接口排序方法还是存在着一定的缺陷的

class Cmp{
	public int compare(String s1, String s2) {
        return s2.length() - s1.length(); //按照字符串长度降序排序
    }
    public int compare1(String a,String b) {
    	int c = compare(a,b);
    	if(c == 0) {
    		c = a.toLowerCase().charAt(0) - b.toLowerCase().charAt(0);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(1) - b.toLowerCase().charAt(1);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(2) - b.toLowerCase().charAt(2);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(3) - b.toLowerCase().charAt(3);
    	}
    	if(c == 0) {
    		c = a.toLowerCase().charAt(4) - b.toLowerCase().charAt(4);
    	}
        return c;
    }
}

对于单词首字母的排序方法只能覆盖到一部分的情况,其实可以用到java中自带的一些排序方法,可以解决这个问题。

 

2.对于期中考试的编程题7-4来说,也存在着一定的问题,就是在本地的虚拟机上运行会有预期的结果输出:

但将源码放入PTA之后,会发现没有输出结果,但也不报错

博主认为可能是接口没有正确的使用,还有一种情况是,头文件的引入顺序不对或缺失头文件的引入(即PTA没有自动帮你引入你需要的头文件)

 

3.对于这几次题目集来说,对于正则表达式的使用可以不仅限于格式的判断哦,也可以对输入的范围进行一定的限制

例如对于日期的输入

格式限制:

public static boolean judgment1(String date) {
		return date.matches("table ([1-9]|[1-9]\\d+) \\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}/\\d{1,2}/\\d{1,2}");
	}

范围+格式限制:

public static boolean judgment2(int year,String date) {
		if(judgmentleap(year)) {
			return date.matches("table \\d+ [0-9]{4}/(0?[13578]/(0?[1-9]|[12][0-9]|3[01])|1[02]/(0?[1-9]|[12][0-9]|3[01])|0?2/(0?[1-9]|1[0-9]|2[0-9])|0?[469]/(0?[1-9]|[12][0-9]|30)|11/(0?[1-9]|[12][0-9]|30)) (0?[0-9]|1[0-9]|2[0-3])/(0?[0-9]|[1-5][0-9])/(0?[0-9]|[1-5][0-9])");
		}
		else {
			return date.matches("table \\d+ [0-9]{4}/(0?[13578]/(0?[1-9]|[12][0-9]|3[01])|1[02]/(0?[1-9]|[12][0-9]|3[01])|0?2/(0?[1-9]|1[0-9]|2[0-8])|0?[469]/(0?[1-9]|[12][0-9]|30)|11/(0?[1-9]|[12][0-9]|30)) (0?[0-9]|1[0-9]|2[0-3])/(0?[0-9]|[1-5][0-9])/(0?[0-9]|[1-5][0-9])");
		}
	}

可以根据自己的需求灵活的运用正则表达式!!!

 

(五)总结

对于本阶段的这几个题目集,我也是都顺利的完成了,在完成的过程中也学到了许多,现在对于接口的使用也是越来越熟练了,也可以熟练的使用到正则表达式来满足题目所提出的要求,同时,我也深刻的领悟到了,对于每一题的类与类之间的关系以及结构的构建都极为重要,要尽量确保你的代码的可读性以及可拓展性,否则,当你想要在你原有的工程上加入一些内容时,会非常困难,甚至需要重构你的代码,这样就会降低你之前所建立起来的类结构的意义,还有就是我们应该清楚的认识到java语言已经产生了很久了,它的库中包含了很多很多已经存在的方法和类,可以直接调用来使用,会使你编写自己的程序时事半功倍,在以后的学习中,我还需要更深入的学习一些新的知识,不能浮于表面,总的来说,java的内容还有很多很多,让我们一起继续探索吧!