前言:
第四次题目集:主要是学习运用了ArrayList类,ArrayList类可以存入对象类的数据,相对比较广泛和方便,还有LinkedHashSet类对传入的数据进行有序排序,还练习了运用for循环对数据进行遍历进行题目要求的特定排序,如去判断是否有重复数据,题目之中还有StringBuilde类的各种方法的运用,7-5题面向对象编程(封装性)的练习,7-6题则是对数据转化的练习,其中还学习运用了String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。本次pta难度小,题量较大,需要学习的知识点较多。
第五次题目集:主要是正则表达式的运用练习,StringBuilder的练习运用,对前面日期类的问题进行进一步的练习,加一些其他的要求,进行聚合的运用和练习,分别创建DateUtil类和year,day,month类来进行聚合,其中分别设置相应的数据,创建相应的方法,对逻辑的考察大,7-6和7-5的题目差不多,但7-6则是在7-5的基础之上再改变各类之中的数据和方法。本次pta难度中等,题目量较大。
第六次题目集:本次题目集之中只有一题,主要是考察书写代码的逻辑和设计,为对问题更好地解决,需要思索其中需要创建的类,如table类来存储每一桌订单的数据,以可以对每一桌数据进行判断,是否有重复的桌号在同一时间之内吃饭,来确定是否要将两桌的钱纳入同一桌之中,还有Menu类,Dish类,Record类,Order类,对菜单于订单的数据进行分类与封装,其间还运用了ArrayList类存入各类数据以进行判断数据是否合法,运用了众多if语句来进行判断出输入错误数据来进行相应的输出,还有运用LocalDateTime来录入一个输入的吃饭时间,用LocalDateTime其中的方法判断其吃饭的时间是否为周末来给相应的折扣,再LocalDateTime转化为LocalDate来进行是否在开业的年月日之内,LocalDateTime转化为LocalTime来进行是否在一天开业规定的时间之内,还运用了正则表达式来判断输入的数据是否合法,运用while语句来判断一桌的订单有没有结束,判断是否到“end”来判断输入是否结束。本次题目集难度大,非常考察逻辑思维,题目量小。
题目集五7-6:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
DateUtil dateUtil1 = new DateUtil(day, month, year);
if (month < 1 || month > 12) { //检验数据是否合法
System.out.println("Wrong Format");
System.exit(0);
} else {
if (choice == 1) { //进入下n天的判断
int n = input.nextInt();
if (!dateUtil1.month.validate() || n < 0 || dateUtil1.day.value < 1 || dateUtil1.day.value > 31 || !dateUtil1.year.validate()) { //检验数据是否合法
System.out.println("Wrong Format");
System.exit(0);
} else {
dateUtil1.getNextNDays(n);
System.out.print(year + "-" + month + "-" + day + " next " + n + " days is:" + dateUtil1.showDate());
}
} else if (choice == 2) { //进入前n天的判断
int n = input.nextInt();
if (!dateUtil1.month.validate() || n < 0 || dateUtil1.day.value < 1 || dateUtil1.day.value > 31 || !dateUtil1.year.validate()) { //检验数据是否合法
System.out.println("Wrong Format");
System.exit(0);
} else {
DateUtil newDate = dateUtil1.getPreviousNDays(n);
System.out.print(year + "-" + month + "-" + day + " previous " + n + " days is:" + newDate.showDate());
}
} else if (choice == 3) { //判断两天之间的间隔
int year2 = input.nextInt();
int month2 = input.nextInt();
int day2 = input.nextInt();
DateUtil dateUtil2 = new DateUtil(day2, month2, year2);
if (!dateUtil1.month.validate() || dateUtil1.day.value < 1 || dateUtil1.day.value > dateUtil1.mon_maxnum[dateUtil1.month.value - 1] || !dateUtil2.month.validate() || dateUtil2.day.value < 1 || dateUtil2.day.value > dateUtil2.mon_maxnum[dateUtil2.month.value - 1] || !dateUtil1.year.validate() || !dateUtil2.year.validate()) { //检验数据是否合法
System.out.println("Wrong Format");
System.exit(0);
} else {
if (dateUtil2.equalTwoDates(dateUtil1))
System.out.println(0);
else {
System.out.print("The days between " + dateUtil1.showDate() + " and " + dateUtil2.showDate() + " are:" + dateUtil1.getDaysofDates(dateUtil2));
}
}
} else System.out.println("Wrong Format");//选择不合法情况
}
}
}
class DateUtil {
Year year;
Month month;
Day day;
int[] mon_maxnum = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public DateUtil() {
}
public DateUtil(int day, int month, int year) {
Year newYear=new Year(year);
Month newMonth=new Month(month);
Day newDay=new Day(day);
this.year=newYear;
this.day=newDay;
this.month=newMonth;
}
public boolean checkInputValidity() {
if (month.validate() && year.validate())
return true;
else return false;
}
public boolean compareDates(DateUtil date) { //比较两个日期的大小
if(this.year.value > date.year.value)
return true;
else if (this.month.value > date.month.value && this.year.value == date.year.value)
return true;
else if (this.day.value > date.day.value && this.month.value == date.day.value && this.year.value == date.year.value)
return true;
else return false;
}
public boolean equalTwoDates(DateUtil date) { //判断日期是否相等
if (date.day.value == this.day.value && date.month.value == this.month.value && date.year.value == this.year.value)
return true;
else return false;
}
public String showDate() { //输出格式化字符串
String string = year.value + "-" + month.value + "-" + day.value;
return string;
}
public DateUtil getNextNDays(int n) { //获取下n天的日期
n = day.value + n;
day.value = 0;
for (; n > 400; ) { //判断n是否大于400,若是则直接减去一年的天数
if (this.month.value < 3) {
if (year.isLeapYear() == true)
n = n - 366;
else n = n - 365;
year.value++;
} else {
Year newYear = new Year(this.year.value + 1);
if (newYear.isLeapYear() == true)
n = n - 366;
else n = n - 365;
year.value++;
}
}
mon_maxnum[1] = 28;
if (year.isLeapYear() == true)
mon_maxnum[1] = 29;
for (; n > mon_maxnum[month.value - 1]; ) { //分别减去每个月的天数,最终得到最后的月份
mon_maxnum[1] = 28;
if (year.isLeapYear() == true)
mon_maxnum[1] = 29;
if (month.value == 12) {
n = n - mon_maxnum[month.value - 1];
year.value++;
month.value = 1;
} else {
n = n - mon_maxnum[month.value - 1];
month.value++;
}
}
day.value = n;
DateUtil newDateUtil = new DateUtil(day.value, month.value, year.value);
return newDateUtil;
}
public DateUtil getPreviousNDays(int n) {//获取前n天的日期
for (; n > 400; ) {//判断n是否大于400,若是则直接减去一年的天数
if (year.isLeapYear() == true)
n = n - 366;
else n = n - 365;
year.value--;
}
for (; n >= day.value; ) {//分别减去每个月的天数,最终得到最后的月份
mon_maxnum[1] = 28;
if (year.isLeapYear() == true)
mon_maxnum[1] = 29;
n = n - day.value;
if (month.value == 1) {
year.value--;
month.value = 12;
} else {
month.value--;
}
day.value = mon_maxnum[month.value - 1];
}
day.value = day.value - n;
DateUtil newDateUtil = new DateUtil(day.value, month.value, year.value);
return newDateUtil;
}
public int getDaysofDates(DateUtil date) { //得到两个日期间的间隔
if (compareDates(date)) {//比较两个日期的大小,若是日期1大的话,两个日期互换
DateUtil newDate = new DateUtil(this.day.value, this.month.value, this.year.value);
this.day = date.day;
this.month.value = date.month.value;
this.year.value = date.year.value;
date = newDate;
}
int sum;
sum = -this.day.value;
this.day.value = 0;
for (; this.year.value < date.year.value; ) { //叠加年天数
if (this.year.isLeapYear() == true)
this.mon_maxnum[1] = 29;
for (; this.month.value <= 12; ) {
this.day.value = this.mon_maxnum[this.month.value - 1];
sum = sum + this.day.value;
this.month.value++;
}
this.month.value = 1;
this.year.value++;
this.mon_maxnum[1] = 28;
}
this.mon_maxnum[1] = 28;
if (this.year.isLeapYear() == true)//叠加月天数
this.mon_maxnum[1] = 29;
for (; this.month.value < date.month.value; ) {
this.day.value = this.mon_maxnum[this.month.value - 1];
sum = sum + this.day.value;
this.month.value++;
}
sum = sum + date.day.value;//最后加上最后的天数
return sum;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public Day getDay() {
return day;
}
public void setDay(Day day) {
this.day = day;
}
}
class Day {
int value;
public Day() {
}
public Day(int value) {
this.value = value;
}
public void dayIncrement() {
value++;
}
public void dayReduction() {
value--;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
class Month {
int value;
public Month() {
}
public Month(int value) {
this.value = value;
}
public void resetMin() {
value = 1;
}
public void resetMax() {
value = 12;
}
public boolean validate() {
if (value >= 1 && value <= 12)
return true;
else return false;
}
public void monthIncrement() {
if (value == 12) {
resetMin();
} else value++;
}
public void monthReduction() {
if (value == 1) {
resetMax();
} else value--;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
class Year {
int value;
public Year() {
}
public Year(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isLeapYear() {
if (value % 400 == 0)
return true;
else if (value % 4 == 0 && value % 100 != 0)
return true;
else return false;
}
public boolean validate() {
if (value >= 1820 && value <= 2020)
return true;
else return false;
}
public void yearIncrement() {
value = value - 1;
}
public void yearReduction() {
value = value + 1;
}
}t
本题一共设计了DateUtil、Year、Month、Day。Year、Month、Day三个类分别录入年月日的信息,通过DateUtil将这三个类联系起来,并通过这三个类相应地联系来实现相应的判断方法,如判断前n天和后n天等,随之再输出得到的相应时间。
SourceMonitor的生成报表内容





PowerDesigner生成内容
根据生成的数据可知,代码质量一般,不是非常高,对代码冗长方面还可以提高。
题目集六:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double discount = 1;
int number;
Menu newMenu = new Menu();
Scanner input = new Scanner(System.in);
String string = input.nextLine();
while (string.charAt(0) != 't') {//录入菜单
if (string.charAt(string.length() - 1) == 'T') {//特色菜情况
String[] str = string.split(" ");
if (str[1].contains(".")) {
System.out.println("wrong format");
} else if (str.length!=3) {
System.out.println("wrong format");
} else if (!str[1].matches("^\\+?[1-9][0-9]*$+")) {
System.out.println("wrong format");
} else if (Integer.parseInt(str[1]) < 0 || Integer.parseInt(str[1]) >= 300) {//判断菜价有没有超过范围
System.out.println(str[0] + " price out of range " + Integer.parseInt(str[1]));
} else if (str[1].charAt(0) == '0') {//判断最高位是不是以0开头
System.out.println("wrong format");
} else
newMenu.addDish(str[0], Integer.parseInt(str[1]), true);
} else {//普通菜情况
String[] str = string.split(" ");
if (str[1].contains(".")) {
System.out.println("wrong format");
} else if (str.length!=2) {
System.out.println("wrong format");
}else if (!str[1].matches("^\\+?[1-9][0-9]*$+")) {
System.out.println("wrong format");
}else if (Integer.parseInt(str[1]) < 0 || Integer.parseInt(str[1]) >= 300) {//判断菜价有没有超过范围
System.out.println(str[0] + " price out of range " + Integer.parseInt(str[1]));
} else if (str[0].charAt(0) == '0') {//判断最高位是不是以0开头
System.out.println("wrong format");
} else
newMenu.addDish(str[0], Integer.parseInt(str[1]), false);
}
string = input.nextLine();
}
ArrayList<Integer> recordTable=new ArrayList<>();
while (string.compareTo("end") != 0) {//判断是否结束
ArrayList<Integer> helpOrderNum=new ArrayList<>();
ArrayList<Integer> helpOrderTable=new ArrayList<>();
int judgeTimeMater=0;
int sum = 0;
int sum1=0;
String[] str = string.split(" ");//将string中的数据分开存到str中
if (str[0].compareTo("table") == 0 && (!(str[1].matches("^\\+?[1-9][0-9]*$+")) || str[1].charAt(0) == '0')||str.length!=4) {//判断桌号出现非数值情况
System.out.println("wrong format");//输出报错
string = input.nextLine();//输入
while (string.charAt(0) != 't') {//判断输入是否为新的一桌,是的话跳出循环
string = input.nextLine();//输入
if (string.compareTo("end") == 0) {//判断是否结束
System.exit(1);//退出程序
}
}
} else if (str[0].compareTo("table") == 0 && Integer.parseInt(str[1]) > 55) {//判断桌号大于55的情况
System.out.println(Integer.parseInt(str[1]) + " table num out of range");
string = input.nextLine();
while (string.charAt(0) != 't' || string.compareTo("end") != 0) {//判断输入是否为新的一桌,是的话跳出循环
string = input.nextLine();//输入
if (string.compareTo("end") == 0) {//判断是否结束
System.exit(1);//退出程序
}
}
} else if (str[0].compareTo("table") == 0 && (str[1].matches("[1-9][0-5]") || str[1].matches("[1-9]"))) {
ArrayList<Integer> deleteOrder = new ArrayList<>();
String[] time1 = str[2].split("/"); //将年,月,日的信息分开来传入到time1字符串数组中
String[] time2 = str[3].split("/"); //将时,分,秒的信息分开来传入到time2字符串数组中
if (!(time1[0].length() == 4 && (time1[1].length() == 1 || time1[1].length() == 2) && (time1[2].length() == 1 || time1[2].length() == 2) && (time2[0].length() == 1 || time2[0].length() == 2) && (time2[1].length() == 1 || time2[1].length() == 2) && (time2[2].length() == 1 || time2[2].length() == 2))) {
System.out.print("wrong format");//判断输入的时间格式是否合法
string = input.nextLine();//录入新一桌的信息
while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
string = input.nextLine();
}
judgeTimeMater = 1;
} else if (!(Integer.parseInt(time1[0]) > 1000 && (Integer.parseInt(time1[1]) <= 12 && Integer.parseInt(time1[1]) >= 1) && (Integer.parseInt(time1[2]) >= 1 && Integer.parseInt(time1[2]) <= 31) && (Integer.parseInt(time2[0]) >= 0 && Integer.parseInt(time2[0]) < 24) && (Integer.parseInt(time2[1]) >= 0 && Integer.parseInt(time2[1]) <= 60) && (Integer.parseInt(time2[2]) >= 0 && Integer.parseInt(time2[2]) <= 60))) {
System.out.print(Integer.parseInt(str[1]) + " date error");//判断输入的时间数据是否非法
string = input.nextLine();//录入新一桌的信息
while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
string = input.nextLine();
}
judgeTimeMater = 1;
}
if (judgeTimeMater == 0) {
LocalDateTime orderTime = LocalDateTime.of(Integer.parseInt(time1[0]), Integer.parseInt(time1[1]), Integer.parseInt(time1[2]), Integer.parseInt(time2[0]), Integer.parseInt(time2[1]), Integer.parseInt(time2[2]));
LocalDate restaurantStart = LocalDate.of(2022, 1, 1); //时间有效开始
LocalDate restaurantEnd = LocalDate.of(2023, 12, 31);//时间有结束
LocalDate eatTime = orderTime.toLocalDate(); //就餐时间
LocalTime weekStartTime=LocalTime.of(9,30,00);
LocalTime weekEndTime=LocalTime.of(21,30,00);
LocalTime NotweekStartMorningTime=LocalTime.of(10,30,00);
LocalTime NotweekStartTAfternoonTime=LocalTime.of(17,00,00);
LocalTime NotweekEndMorningTime=LocalTime.of(14,30,00);
LocalTime NotweekEndAfternoonTime=LocalTime.of(20,30,00);
LocalTime eatTime1 = orderTime.toLocalTime(); //就餐时间
if (!(eatTime.isBefore(restaurantEnd) && eatTime.isAfter(restaurantStart))) {//判断就餐时间是否在时间有效范围内
System.out.print("not a valid time period");
string = input.nextLine();//录入新一桌的信息
while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
string = input.nextLine();
}
} else if (((!(orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY))&&((!(eatTime1.isAfter(NotweekStartMorningTime)&&eatTime1.isBefore(NotweekEndMorningTime)))&&(!(eatTime1.isAfter(NotweekStartTAfternoonTime)&&eatTime1.isBefore(NotweekEndAfternoonTime)))))||((orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY)&&(!(eatTime1.isAfter(weekStartTime)&&eatTime1.isBefore(weekEndTime))))) {
System.out.print("table "+ Integer.parseInt(str[1]) +" out of opening hours");
string = input.nextLine();//录入新一桌的信息
while (string.compareTo("end") != 0 && string.charAt(0) != 't') {
string = input.nextLine();
}
} else {
double singlePrice=0;
System.out.println("table " + Integer.parseInt(str[1]) + ": "); //输出
recordTable.add(Integer.parseInt(str[1]));
if (!(orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY)) {//判断是周一到周五还是周末来赋予相应折扣
discount = 0.7;
}
String orderMessage;
orderMessage = input.nextLine(); //录入一条订单的信息
number = 0; //以判断订单号是否是顺序的
Order newOrder = new Order(); //创建一个新的订单信息
int judgeTableMater = 0;
while ((orderMessage.charAt(0) != 't' && orderMessage.charAt(0) != 'e') || judgeTableMater == 1) { //判断订单号是否结束
judgeTableMater = 0;
String[] orderMessage1 = orderMessage.split(" ");//将一条订单的各种信息分开
if (orderMessage1.length == 5) {
helpOrderNum.add(Integer.parseInt(orderMessage1[1]));
helpOrderTable.add(Integer.parseInt(orderMessage1[0]));
orderMessage = input.nextLine();
newOrder.addARecord(Integer.parseInt(orderMessage1[1]), orderMessage1[2], Integer.parseInt(orderMessage1[3]), Integer.parseInt(orderMessage1[4]));
} else {
if (!(orderMessage1[0].matches("[1-9]"))) { //判断是否订单录入菜单
System.out.println("invalid dish");
orderMessage = input.nextLine(); //录入一条订单的信息
} else if(orderMessage1[1].compareTo("delete")!=0) {
newOrder.addARecord(Integer.parseInt(orderMessage1[0]), orderMessage1[1], Integer.parseInt(orderMessage1[2]), Integer.parseInt(orderMessage1[3]));//将订单信息传入到newOrder中
orderMessage = input.nextLine();
}
String[] orderMessage2 = orderMessage.split(" ");
if ((orderMessage2[0].compareTo("table") != 0 && !(orderMessage2[0].matches("[1-9]"))) && orderMessage.compareTo("end") != 0) {
orderMessage = input.nextLine();
judgeTableMater = 1;
}
while (orderMessage.charAt(0) != 'e' && orderMessage.charAt(2) == 'd') {
deleteOrder.add(Integer.parseInt(orderMessage2[0]));
/*if(newOrder.findRecordByNum(Integer.parseInt(orderMessage2[0]))) {
newOrder.delARecordByOrderNum(Integer.parseInt(orderMessage2[0]));
}
else{
System.out.println("deduplication "+Integer.parseInt(orderMessage2[0]));
}*/
orderMessage = input.nextLine();
orderMessage2 = orderMessage.split(" ");
if (orderMessage.compareTo("end") == 0) {
break;
}
}
}
}
for (int j = 1; j <= newOrder.getRecords().size(); j++) {//遍历newOrder中的每一条记录
int judgeSameRecords = 1;
ArrayList<Dish> dishNew = newMenu.getDishs();//将newMenu中的dishs信息放到dishNew中以遍历
if (newOrder.getRecords().get(j - 1).getOrderNum() <= number) {
System.out.println("record serial number sequence error");
if (number < newOrder.getRecords().get(j - 1).getOrderNum()) {
number = newOrder.getRecords().get(j - 1).getOrderNum(); //记录当前的订单的序号
}
} else {
for (int i = 0; i < newMenu.getDishs().size(); i++) {//将一条订单与菜单匹配
int recordHelpNum = 0;
int judgeHelpOrder = 0;
if (dishNew.get(i).getName().compareTo(newOrder.getRecords().get(j - 1).getD().getName()) == 0) {
newOrder.getRecords().get(j - 1).setD(dishNew.get(i));
if ((newOrder.getRecords().get(j - 1).getPortion() > 3 && newOrder.getRecords().get(j - 1).getPortion() <= 9)) {//普通菜份额超出范围
System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " portion out of range " + newOrder.getRecords().get(j - 1).getPortion());
} else if (newOrder.getRecords().get(j - 1).getPortion() > 9) {
System.out.println("wrong format"); //份额非法输入
} else if (newOrder.getRecords().get(j - 1).getNum() > 15) {
System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " num out of range " + newOrder.getRecords().get(j - 1).getNum());//份数超出范围
} else {
int existTable = 1;
int deleteOrderMater = 0;
for (recordHelpNum = 0; recordHelpNum < helpOrderNum.size(); recordHelpNum++) {
if (helpOrderNum.get(recordHelpNum) == newOrder.getRecords().get(j - 1).getOrderNum()) {
judgeHelpOrder = 1;
for (int k = 0; k < recordTable.size(); k++) {
if (recordTable.get(k) == helpOrderTable.get(recordHelpNum)) {
existTable = 0;
}
if (k == recordTable.size() - 1 && existTable == 1) {
existTable = 2;
}
}
break;
}
}
if (existTable == 2) {
System.out.println("wrong format");
} else {
if (judgeHelpOrder == 1) {
System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + "table " + str[1] + " pay for table " + helpOrderTable.get(recordHelpNum) + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
} else {
System.out.println(newOrder.getRecords().get(j - 1).getOrderNum() + " " + newOrder.getRecords().get(j - 1).getD().getName() + " " + Math.round(newOrder.getRecords().get(j - 1).getPrice()));
}
for (int l = 0; l < deleteOrder.size(); l++) {
if (deleteOrder.get(l) == newOrder.getRecords().get(j - 1).getOrderNum()) {
deleteOrderMater = 1;
break;
}
}
if (deleteOrderMater == 1) {
break;
}
for (int k = j - 1; k > 0; k--) {
if (newOrder.getRecords().get(j - 1).getD().getName().compareTo(newOrder.getRecords().get(k - 1).getD().getName()) == 0 && newOrder.getRecords().get(j - 1).getNum() == newOrder.getRecords().get(k - 1).getNum()) {
for (int l = 0; l < deleteOrder.size(); l++) {
if (deleteOrder.get(l) == newOrder.getRecords().get(k).getOrderNum()) {
break;
}
if(l==deleteOrder.size()-1){
judgeSameRecords=0;
}
}
}
}
if (judgeSameRecords == 1) {
for (int k = j; k < newOrder.getRecords().size(); k++) {
if (newOrder.getRecords().get(j - 1).getD().getName().compareTo(newOrder.getRecords().get(k).getD().getName()) == 0 && newOrder.getRecords().get(j - 1).getNum() == newOrder.getRecords().get(k).getNum()) {
for (int l = 0; l < deleteOrder.size(); l++) {
if (deleteOrder.get(l) == newOrder.getRecords().get(k).getOrderNum()) {
break;
}
if(l==deleteOrder.size()-1){
judgeSameRecords++;
}
}
}
}
}
if (!(orderTime.getDayOfWeek() == DayOfWeek.SUNDAY || orderTime.getDayOfWeek() == DayOfWeek.SATURDAY)) {
if (!(newOrder.getRecords().get(j - 1).getD().isSpecialty())&&(eatTime1.isAfter(NotweekStartMorningTime)&&eatTime1.isBefore(NotweekEndMorningTime))) {
singlePrice=Math.round(newOrder.getRecords().get(j - 1).getPrice())*0.6*judgeSameRecords;
} else if (!(newOrder.getRecords().get(j - 1).getD().isSpecialty())&&(eatTime1.isAfter(NotweekStartTAfternoonTime)&&eatTime1.isBefore(NotweekEndAfternoonTime))) {
singlePrice=Math.round(newOrder.getRecords().get(j - 1).getPrice())*0.8*judgeSameRecords;
} else if (newOrder.getRecords().get(j - 1).getD().isSpecialty()) {
singlePrice=Math.round(newOrder.getRecords().get(j - 1).getPrice())*0.7*judgeSameRecords;
}
}
sum =Math.round((float) singlePrice) + sum;
sum1=sum1+Math.round(newOrder.getRecords().get(j - 1).getPrice())*judgeSameRecords;
}
}
break;
}
if (dishNew.get(i).getName().compareTo(newOrder.getRecords().get(j - 1).getD().getName()) != 0 && i == newMenu.getDishs().size() - 1) { //输出菜单中不存在订单菜品的情况
System.out.println(newOrder.getRecords().get(j - 1).getD().getName() + " does not exist");
}
}
if (number < newOrder.getRecords().get(j - 1).getOrderNum()) {
number = newOrder.getRecords().get(j - 1).getOrderNum(); //记录当前的订单的序号
}
}
}
for (int i = 0; i < deleteOrder.size(); i++) {
for(int k=0;k<newOrder.getRecords().size();k++){
if(deleteOrder.get(i)==newOrder.getRecords().get(k).getOrderNum()){
break;
}
if(k==newOrder.getRecords().size()-1){
System.out.println("delete error;");
}
}
for (int j = i - 1; j >= 0; j--) {
if (deleteOrder.get(i) == deleteOrder.get(j)) {
System.out.println("deduplication " + deleteOrder.get(i));
}
}
}
string = orderMessage;
System.out.print("table " + Integer.parseInt(str[1]) + ": " + sum1 + " " + (sum)); //输出一桌的总价
}
}
}
if(string.compareTo("end")!=0){
System.out.println();
}
}
}
}
class Menu {
private ArrayList<Dish> dishs=new ArrayList<>();
public Menu() {
}
public Menu(ArrayList<Dish> dishs) {
this.dishs = dishs;
}
public Dish searthDish(String dishName){
int i;
for(i=0;i<=dishs.size()-1;i++){
if(dishs.get(i).getName()==dishName)
break;
}
return dishs.get(i);
}
public void addDish(String dishName,int unit_price,boolean specialty){
Dish newdish=new Dish(dishName,unit_price,specialty);
this.dishs.add(newdish);
}
public ArrayList<Dish> getDishs() {
return dishs;
}
public void setDishs(ArrayList<Dish> dishs) {
this.dishs = dishs;
}
public String toString() {
return "Menu{dishs = " + dishs + "}";
}
}
class Dish {
private String name;
private int unit_price;
private boolean specialty=false;
public Dish() {
}
public Dish(String name, int unit_price) {
this.name = name;
this.unit_price = unit_price;
}
public Dish(String name, int unit_price, boolean specialty) {
this.name = name;
this.unit_price = unit_price;
this.specialty = specialty;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnit_price() {
return unit_price;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}
public int getPrice(int portion){
float newUnit_price=1;
switch (portion){
case 1:newUnit_price=unit_price;break;
case 2:newUnit_price= (float) (1.5*unit_price);break;
case 3:newUnit_price=2*unit_price;break;
}
return Math.round(newUnit_price);
}
public boolean isSpecialty() {
return specialty;
}
public void setSpecialty(boolean specialty) {
this.specialty = specialty;
}
public String toString() {
return "Dish{name = " + name + ", unit_price = " + unit_price + ", specialty = " + specialty + "}";
}
}
class Order {
private ArrayList<Record> records=new ArrayList<>();
public Order() {
}
public Order(ArrayList<Record> records) {
this.records = records;
}
public int getTotalPrice(){
int sum=0;
for(int i=0;i<=records.size()-1;i++){
sum=sum+records.get(i).getPrice();
}
return sum;
}
public Record addARecord(int orderNum,String dishName,int portion,int num){
Record newRecord=new Record(orderNum,dishName,portion,num);
this.records.add(newRecord);
return this.records.get(records.size()-1);
}
public void delARecordByOrderNum(int orderNum){
int i;
for(i=0;i<=records.size()-1;i++){
if(records.get(i).getOrderNum()==orderNum);
}
}
public void findRecordByNum(int orderNum){
int i;
for(i=0;i<=records.size()-1;i++){
if(records.get(i).getOrderNum()==orderNum);
}
}
public ArrayList<Record> getRecords() {
return records;
}
public void setRecords(ArrayList<Record> records) {
this.records = records;
}
public String toString() {
return "Order{records = " + records + "}";
}
}
class Record {
private int orderNum;
private Dish d;
private int portion;
private int num;
public Record() {
}
public Record(int orderNum, String name, int portion, int num) {
Dish newDish=new Dish();
this.orderNum = orderNum;
newDish.setName(name);
this.d=newDish;
this.portion = portion;
this.num = num;
}
public int getPrice(){
return d.getPrice(portion)*num;
}
public int getOrderNum() {
return orderNum;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public Dish getD() {
return d;
}
public void setD(Dish d) {
this.d = d;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String toString() {
return "Record{orderNum = " + orderNum + ", d = " + d + ", portion = " + portion + ", num = " + num + "}";
}
}
代码开始为录入菜单,用string.charAt(0)!='t'来进行判断,若是输入的一行之中首字母不为t则输入的还是为菜单的信息,再判断最后一个字符是否为'T',来判断是否为特色菜,对普通菜和特殊菜分别进行不同的操作,将其中可能出现的各种输入的数据和格式错误用if语句排除完,输出相应的报错信息,若是输入的菜品信息没错则将其信息传进Menu之中的ArrayList类的Dish类型的数组之中,随后便进入到输入每一桌的数据之中,用while循环来判断是否输入到"end"语句,是否结束程序的运行。将一桌输入的一句信息用split()函数以" "分开传进到一个字符串数组之中,之后再对这每一部分的信息进行判断,判断其是否输入了错误的信息,还是出现了格式错误,若出现则输入相应的错误信息,对时间的判断则使用LocalDateTime类来进行判断是否为周末,再转化为LocalTime类和LocalDate来和已经创建的数据进行比较,来判断所输入的时间是否为开业时间,还有在对应的时间之内再给予相应的折扣。对一桌之中输入的每一条信息用if语句进行比对,来判断输入的信息是否为订单信息,还是要删除订单的信息,若是判断为是删除订单的信息则将其放入到已经创建好的ArrayList的数组之中,为后续计算金额作准备。再将输入的一条条订单信息放入到Order中的Dish为对象的ArrayList类之中。在录入完一桌的信息之后再将这一条条信息用for循环与菜单之中的一条条dish信息比较,找到菜单之中相应的dish信息,再输出这一条订单的价格,再计算完要打的折扣之后用Math.round()函数将其乘以相应用餐时间的折扣四舍五入算入到总价sum中,将一桌的所有订单信息进行遍历,直到遍历完全,其间还得将一条订单的订单号于之前录入的删除订单信息的ArrayLsit数组之中的订单号进行对比,若是删除订单信息的数组有这条订单号则不算入到总价sum之中,若是再菜单之中找不到相应的菜品,或者出现超出数额和份额的情况输出相应的报错信息,遍历完全之后再进入到下一桌的录入,判断,直到录入信息为end,程序结束。
SourceMonitor的生成报表内容





PowerDesigner生成内容

根据生成的数据分析可知,代码太过冗长,复杂度较高,以导致代码的质量低下,之后还得多加思索,运用更加巧妙的方法来对所需进行实践,避免代码太过复杂,冗长。
踩坑心得:
问题1:本题要求对输入的数据进行去除输入的相同数据,并对去重之后的数据进行排序。一开始以创立了ArrayList类设为Integer类型,再对ArrayList中的数据进行遍历先排序,在去除相同的数据,但只能过一个测试点,其他的均为运行超时。
解决方法:运行LinkeHashset类,对输入的数据自主进行排序和去重。


问题2:一直纠结如何将输入的一个语句之中的各个单词分开,没有思绪。
解决方法:对一个语句之中的每一个字符进行判断,判断不为‘,’,‘ ’,‘.’时将字符添加到stringBulider中,若是判断到为‘,’,‘ ’,‘.’时就是一个单词的结束,而此时stringBulider中则是语句之中的一个单词,再将这个单词添加入arrayList之中,判断到input1.length-1则是输入的字符串之中的字符结束,而此时arrayList之中则存有输入语句之中的所有单词。


问题3:一开始直接对两个字符串比较,并没有区分大小写,以至于得到的答案是按照大小写排序之后得到的顺序。
解决方法:先对输入的字符串全部转化为小写,再对这小写的字符串进行比较。
问题4:没有看清题目就一直盲目地去写,没有注意到题目要求的重复单词只输出一次的去重要求,以至于所提交的代码一直报错,浪费了大量的时间。
解决方法:仔细看题,再用for循环来对arrayList之中的数据遍历,除去重复的数据。



问题5:日期类问题之中,一开始在构造方法中直接将把数据monthValue和yearValue放入到this.month之中,但一直报错,说对象为空。
解决方法:在构造方法之中先创建一个newMonth的Month类的对象,先将数据monthValue和yearValue放入到newMonth之中,随后再将newMonth赋给month。

问题6:题目集六之中规定需要将数据四舍五入。
解决方法:用Math.round方法对数据四舍五入,前提数据得是float类型的,若不是则需要转化为float类型先。


改进建议:
1.

当时书写了一连串的if()语句来进行输入字符的判断,如此以来分支过多,代码的复杂程度高,而且书写起来冗长,而且其都是判断输入的数据和格式是否为非法,所要输出的都相同,这类判断条件都可以合并进到同一个if语句之中,以减少代码分支过多的问题,降低代码的复杂程度,使看起来更为简练。
2.

当时写这代码判断时,题目是要求不区分大小写的判断,我是先将遍历需要判断的字符串都先转化为小写,再来对这都是小写的两个字符串进行比较,可以运用equalsIgnoreCase()方法进行两个字符串的比较,不区分大小写。
3.

当时书写这代码的时候是直接将输入的时间代码进行遍历,输入的时间需要2023-06-07这种格式,若是2023-6-7这种格式则是无法通过字符串来输入进date1的信息,所以我进行了特定序列的判断,来给像输入的为2023-6-7这样的字符串加上0使之变成为2023-06-07这样的语句,但这样书写太过于复杂,而且会增加代码的长度,可以运用split()方法来对字符串进行‘-’的分开,得到一个含有年月日数据的字符串数组,再将其转化整型数据,赋予构造时间date1的方法。
总结:通过这三次pta的练习我学习了ArrayList的具体用法,还有LinkedHashSet的用法,有序不重复,可以更好地去处理需要剔除重复数据和对输入的数据排序,还有迭代器的使用,还学习了正则表达的使用规则,并通过第五次pta的作业加强了其用法的了解,对规范输入有实质性的作用,通过了第六次pta的练习我熟悉了三个时间类的运用,LocalDateTime,LocalTime,LocalDate的运用,LocalDateTime可以记录年月日时分秒,LocalDate则是可以记录年月日,LocalTime则可以记录时分秒,更加熟悉了这三个时间类的各种比较时间的方法,还有判断星期几的方法,以及LocalDateTime与另外两个时间类的转换,还有String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。而且通过这三次pta的练习加强了我对java之中对类的封装还有java之中聚合性的理解,加强了我对其用法的熟悉。通过了对第六次pta菜单问题一个星期的打磨,使我对代码设计的逻辑思维有了进一步的提升,以后再处理这种问题会更加得心应手。