PTA 4-6次总结
阅读原文时间:2023年07月08日阅读:2

(1)前言:

  04:这次题目集主要学习了:使用LinkedHashSet删除arraylist中的重复数据,封装,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。题目的数目较多,但只有少数题目难度较大,比如菜单程序以及日期问题。

  05:这次题目集主要学习了:正则表达式,日期问题中的聚合。题目较为简单。

  06:这次题目只有一个菜单程序,难度较大。

(2)设计与分析:

  题目集4:7-1:这道题是大学以来见过的最头疼的题,由于本次题目集题量大、难度高、时间紧,我选择了先完成较简单的题目,而最难的菜单题选择了留到后面,等到结束也没能做出来,后面才知道老师把第一个题的难度设定的这样高,是为了提升我的能力,告诉我作为软件工程师不能怕困难,要迎难而上,我也下定决心做一个不怕困难的人。

  题目集5:7-5:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

// System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
}
else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.println(date.getPreviousNDays(n).showDate());
}
else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println(fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class DateUtil{
Day day=new Day();
public DateUtil(){

}
public DateUtil(int d,int m,int y){
this.day.value=d;
this.day.month.value=m;
this.day.month.year.value=y;
}
public int getDay() {
return day.value;
}
public void setDay(Day d) {
this.day=d;
}
public boolean checkInputValidity(){//检测输入的年、月、日是否合法
if(day.month.year.validate()&&day.month.validate()&&day.validate()){
if((day.month.year.isLeapYear(getDay())&&day.month.value==2&&day.month.value<=29)||day.validate())
return true;
else
return false;
}
else
return false;
}
public DateUtil getNextNDays(int m){//取得year-month-day的下n天日期
for(int i=0;i<m;i++){//用下一天循环
if(day.month.value==1||day.month.value==3||day.month.value==5||day.month.value==7||day.month.value==8||day.month.value==10||day.month.value==12){
if(day.month.value!=12&&day.value<31)
day.dayIncrement();
else if(day.month.value<12&&day.value==31){
day.restMin();
day.month.monthIncrement();
}
else if(day.month.value==12&&day.value==31){
day.restMin();
day.month.restMin();
day.month.year.yearIncrement();
}
else
day.dayIncrement();
}
else if(day.month.value==2){
if((day.month.year.isLeapYear(getDay())&&day.value==29)||(!day.month.year.isLeapYear(getDay())&&day.value==28)){
day.month.value=3;
day.dayIncrement();
}
else
day.dayIncrement();
}
else if(day.month.value==4||day.month.value==6||day.month.value==9||day.month.value==11){
if(day.value<30)
day.dayIncrement();
else if(day.value==30){
day.restMin();
day.month.monthIncrement();
}
}
}

return new DateUtil(this.day.month.year.value,this.day.month.value,this.day.value);

}
public DateUtil getPreviousNDays(int n){//取得year-month-day的前n天日期
for(int i=0;i<n;i++){//用上一天循环
if(day.month.value==2||day.month.value==4||day.month.value==6||day.month.value==8||day.month.value==9||day.month.value==11||day.month.value==1){
if(day.month.value==1&&day.value==1) {
day.month.year.yearReduction();
day.month.restMax();
day.restMax();
}
else if(day.value==1){
day.restMax();
day.month.monthReduction();
}
else
day.dayReduction();
}
else if(day.month.value==3){
if(day.month.year.isLeapYear(getDay())&&day.value==1){
day.month.value=2;
day.value=29;
}
else if(!day.month.year.isLeapYear(getDay())&&day.value==1) {
day.month.value=2;
day.value=28;
}
else
day.dayReduction();
}
else if(day.month.value==5||day.month.value==7||day.month.value==10||day.month.value==12){
if(day.value==1) {
day.month.monthReduction();
day.value=30;
}
else
day.dayReduction();
}
}
return new DateUtil(day.month.year.value,day.month.value,day.value);
}
public boolean compareDates(DateUtil date){//比较当前日期与date的大小(先后)
if(date.day.month.year.value<this.day.month.year.value) {
return false;
}
else if(date.day.month.year.value==this.day.month.year.value&&date.day.month.value<this.day.month.value) {
return false;
}
else if(date.day.month.year.value==this.day.month.year.value&&date.day.month.value==this.day.month.value&&date.day.value<this.day.value) {
return false;
}
else
return true;

}
public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等
if(this.day.month.year.value==date.day.month.year.value&&this.day.month.value==date.day.month.value&&this.day.value==date.day.value)
return true;
return false;
}
public int getDaysofDates(DateUtil date){//求当前日期与date之间相差的天数
DateUtil fromDate=this;
DateUtil toDate=date;
int i=0,j=0,k=0;
int[] mon_maxnum=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.equalTwoDates(date))
return 0;
else if(!this.compareDates(date)) {
fromDate=date;
toDate=this;
}
for(i=fromDate.day.month.year.value+1;i<toDate.day.month.year.value;i++) {
if(day.month.year.isLeapYear(i))
j=366;
else j=365;
k=k+j;
}

if (day.month.year.isLeapYear(fromDate.day.month.year.value)&&fromDate.day.month.value<=2) mon_maxnum[2]=29; else mon_maxnum[2]=28; int p=fromDate.day.month.value; for (i=fromDate.day.month.value;i<=12;i++){ if(i==p){ k=k+day.mon_maxnum[i]-fromDate.day.value; } else k=k+mon_maxnum[i]; } int t=toDate.day.month.value; if(day.month.year.isLeapYear(toDate.day.month.year.value)&&toDate.day.month.value>2)
mon_maxnum[2]=29;
else mon_maxnum[2]=28;
for (i=1;i<=toDate.day.month.value;i++){
if(i==t){
k=k+toDate.day.value;
}
else k=k+mon_maxnum[i];
}
return k;
}
public String showDate(){//以“year-month-day”格式返回日期值
return this.day.month.year.value+"-"+this.day.month.value+"-"+this.day.value;
}
}
class Day{
int value;//day
int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
Month month=new Month();
public Day() {}
public Day(int yearValue,int monthValue,int dayValue){

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth() {
return month;
}
public void setMonth(Month value) {
this.month = value;
}
public void restMin() {
value = 1;
}
public void restMax() {
value = mon_maxnum[month.value];
}
public boolean validate() {
if(value>=1&&value<=31) {
if((month.year.isLeapYear(month.year.value)&&month.value==2&&value<=29)||value<=mon_maxnum[month.value])
return true;
else
return false;
}
else
return false;

}
public void dayIncrement() {}
public void dayReduction() {}
}

class Month{
int value;
Year year=new Year();
public Month() {}
public Month(int yearValue,int monthValue){

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public void restMin() {
value = 1;
}
public void restMax() {
value = 12;
}
public boolean validate() {
if(value>=1&&value<=12)
return true;
else
return false;
}
public void monthIncrement() {
value=value+1;
}
public void monthReduction() {
value=value-1;
}
}

class Year{
int value;
public Year() {}
public Year(int yearValue){

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isLeapYear(int year){//判断year是否为闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}
public boolean validate() {
if(value>=1900&&value<=2050)
return true;
else
return false;
}
public void yearIncrement() {
value=value+1;
}

public void yearReduction() {
value=value-1;
}
}

这个题目是对之前日期问题的进一步加工使类之间具有聚合关系。我认为我做的并不好,当时并不理解聚合,现在知道了java中聚合是一种特殊的关联形式,它是两个类之间的关系,是一种HAS-A关系,是一种单向关联。如果类具有实体引用,则称为聚合。为什么要使用聚合?因为可以维护代码的可重用性。

       7-6:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);
if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

// System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
}
else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(year, month, day);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.println(date.getPreviousNDays(n).showDate());
}
else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day, month, day);
DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println(fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class DateUtil{
Day day=new Day();
public DateUtil(){

}
public DateUtil(int d,int m,int y){
this.day.value=d;
this.day.month.value=m;
this.day.month.year.value=y;
}
public int getDay() {
return day.value;
}
public void setDay(Day d) {
this.day=d;
}
public boolean checkInputValidity(){//检测输入的年、月、日是否合法
if(day.month.year.validate()&&day.month.validate()&&day.validate()){
if((day.month.year.isLeapYear(getDay())&&day.month.value==2&&day.month.value<=29)||day.validate())
return true;
else
return false;
}
else
return false;
}
public DateUtil getNextNDays(int m){//取得year-month-day的下n天日期
for(int i=0;i<m;i++){//用下一天循环
if(day.month.value==1||day.month.value==3||day.month.value==5||day.month.value==7||day.month.value==8||day.month.value==10||day.month.value==12){
if(day.month.value!=12&&day.value<31)
day.dayIncrement();
else if(day.month.value<12&&day.value==31){
day.restMin();
day.month.monthIncrement();
}
else if(day.month.value==12&&day.value==31){
day.restMin();
day.month.restMin();
day.month.year.yearIncrement();
}
else
day.dayIncrement();
}
else if(day.month.value==2){
if((day.month.year.isLeapYear(getDay())&&day.value==29)||(!day.month.year.isLeapYear(getDay())&&day.value==28)){
day.month.value=3;
day.dayIncrement();
}
else
day.dayIncrement();
}
else if(day.month.value==4||day.month.value==6||day.month.value==9||day.month.value==11){
if(day.value<30)
day.dayIncrement();
else if(day.value==30){
day.restMin();
day.month.monthIncrement();
}
}
}

return new DateUtil(this.day.month.year.value,this.day.month.value,this.day.value);

}
public DateUtil getPreviousNDays(int n){//取得year-month-day的前n天日期
for(int i=0;i<n;i++){//用上一天循环
if(day.month.value==2||day.month.value==4||day.month.value==6||day.month.value==8||day.month.value==9||day.month.value==11||day.month.value==1){
if(day.month.value==1&&day.value==1) {
day.month.year.yearReduction();
day.month.restMax();
day.restMax();
}
else if(day.value==1){
day.restMax();
day.month.monthReduction();
}
else
day.dayReduction();
}
else if(day.month.value==3){
if(day.month.year.isLeapYear(getDay())&&day.value==1){
day.month.value=2;
day.value=29;
}
else if(!day.month.year.isLeapYear(getDay())&&day.value==1) {
day.month.value=2;
day.value=28;
}
else
day.dayReduction();
}
else if(day.month.value==5||day.month.value==7||day.month.value==10||day.month.value==12){
if(day.value==1) {
day.month.monthReduction();
day.value=30;
}
else
day.dayReduction();
}
}
return new DateUtil(day.month.year.value,day.month.value,day.value);
}
public boolean compareDates(DateUtil date){//比较当前日期与date的大小(先后)
if(date.day.month.year.value<this.day.month.year.value) {
return false;
}
else if(date.day.month.year.value==this.day.month.year.value&&date.day.month.value<this.day.month.value) {
return false;
}
else if(date.day.month.year.value==this.day.month.year.value&&date.day.month.value==this.day.month.value&&date.day.value<this.day.value) {
return false;
}
else
return true;

}
public boolean equalTwoDates(DateUtil date){//判断两个日期是否相等
if(this.day.month.year.value==date.day.month.year.value&&this.day.month.value==date.day.month.value&&this.day.value==date.day.value)
return true;
return false;
}
public int getDaysofDates(DateUtil date){//求当前日期与date之间相差的天数
DateUtil fromDate=this;
DateUtil toDate=date;
int i=0,j=0,k=0;
int[] mon_maxnum=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.equalTwoDates(date))
return 0;
else if(!this.compareDates(date)) {
fromDate=date;
toDate=this;
}
for(i=fromDate.day.month.year.value+1;i<toDate.day.month.year.value;i++) {
if(day.month.year.isLeapYear(i))
j=366;
else j=365;
k=k+j;
}

if (day.month.year.isLeapYear(fromDate.day.month.year.value)&&fromDate.day.month.value<=2) mon_maxnum[2]=29; else mon_maxnum[2]=28; int p=fromDate.day.month.value; for (i=fromDate.day.month.value;i<=12;i++){ if(i==p){ k=k+day.mon_maxnum[i]-fromDate.day.value; } else k=k+mon_maxnum[i]; } int t=toDate.day.month.value; if(day.month.year.isLeapYear(toDate.day.month.year.value)&&toDate.day.month.value>2)
mon_maxnum[2]=29;
else mon_maxnum[2]=28;
for (i=1;i<=toDate.day.month.value;i++){
if(i==t){
k=k+toDate.day.value;
}
else k=k+mon_maxnum[i];
}
return k;
}
public String showDate(){//以“year-month-day”格式返回日期值
return this.day.month.year.value+"-"+this.day.month.value+"-"+this.day.value;
}
}
class Day{
int value;//day
int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
Month month=new Month();
public Day() {}
public Day(int yearValue,int monthValue,int dayValue){

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth() {
return month;
}
public void setMonth(Month value) {
this.month = value;
}
public void restMin() {
value = 1;
}
public void restMax() {
value = mon_maxnum[month.value];
}
public boolean validate() {
if(value>=1&&value<=31) {
if((month.year.isLeapYear(month.year.value)&&month.value==2&&value<=29)||value<=mon_maxnum[month.value])
return true;
else
return false;
}
else
return false;

}
public void dayIncrement() {}
public void dayReduction() {}
}

class Month{
int value;
Year year=new Year();
public Month() {}
public Month(int yearValue,int monthValue){

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public void restMin() {
value = 1;
}
public void restMax() {
value = 12;
}
public boolean validate() {
if(value>=1&&value<=12)
return true;
else
return false;
}
public void monthIncrement() {
value=value+1;
}
public void monthReduction() {
value=value-1;
}
}

class Year{
int value;
public Year() {}
public Year(int yearValue){

}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isLeapYear(int year){//判断year是否为闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}
public boolean validate() {
if(value>=1900&&value<=2050)
return true;
else
return false;
}
public void yearIncrement() {
value=value+1;
}

public void yearReduction() {
value=value-1;
}
}

这个日期与之前的7-5类与类之间的关系不同,但是方法基本相同。

  题目集6:7-1:

1 import java.util.*;
2
3 public class Main{
4 public static void main(String[] agrs) {
5 Scanner input = new Scanner(System.in);
6 int numberTable = 0;// 桌数
7 int numberRecord = 0;// 记录数
8 int numberDish = 0;// 菜品数
9 int flagTable = 0;// 判断输入错误的类型,0为正常情况
10 Menu tableMenu = new Menu();// 所有桌的菜谱
11 Time Time = new Time();// 时间类
12 Table table[] = new Table[20];// 桌子数组
13 int sumPriceTable[] = new int[20];// 用来统计各桌的价格总和
14 for(int i = 0; i < 20; i++) {// 用于给数组分配空间 15 table[i] = new Table(); 16 } 17 String data = new String(); 18 while(true) { 19 data = input.nextLine(); 20 if(data.equals("end")){ 21 break; 22 } 23 String dataThis[] = data.split(" "); 24 /* 完成 */ 25 if(data.length() > 12) {// 说明读到了桌子(此处没有对是否有输入完全4个参数再做判断,可能无法通过)
26 numberTable ++;// 桌数+1,0桌为空,无数据
27 table[numberTable].setTableInfo(data);// 先写入该桌数组,不管合法不合法,不合法该桌信息作废
28 numberRecord = 0;// 记录数复原
29 }
30 ///
31 else {// 没读到桌子
32 /* 完成 */
33 if(numberTable == 0) {// 桌数为0说明还在菜谱输入阶段
34 if(dataThis.length > 2) {// 说明输入的可能是特色菜
35 if(dataThis[2].charAt(0) == "T" && dataThis.length == 3) {// 判断输入是否合法
36 if(table[numberTable].getDish()[numberDish].checkPrice(dataThis[1])) {// 判断价格是否输入合法,合法就进行赋值
37 if(Integer.parseInt(dataThis[1]) > 0 && Integer.parseInt(dataThis[1]) < 300) 38 tableMenu.addDish(dataThis[0], Integer.parseInt(dataThis[1]), true, numberDish);// 直接使用定义的方法赋值 39 else { 40 if(Integer.parseInt(dataThis[1]) == 0) 41 System.out.println("wrong format"); 42 else 43 System.out.println(dataThis[0] + " price out of range " + dataThis[1]); 44 continue; 45 } 46 numberDish ++; 47 } 48 else {// 说明价格输入不合法 49 System.out.println("wrong format"); 50 } 51 } 52 else {// 说明没有按要求输入特色菜 53 System.out.println("wrong format"); 54 } 55 } 56 else if(dataThis.length == 2) {// 说明是非特色菜 57 if(table[numberTable].getDish()[numberDish].checkPrice(dataThis[1])) {// 判断价格是否输入合法,合法就进行赋值 58 if(Integer.parseInt(dataThis[1]) > 0 && Integer.parseInt(dataThis[1]) < 300) 59 tableMenu.addDish(dataThis[0], Integer.parseInt(dataThis[1]), false, numberDish);// 直接使用定义的方法赋值 60 else { 61 if(Integer.parseInt(dataThis[1]) == 0) 62 System.out.println("wrong format"); 63 else 64 System.out.println(dataThis[0] + " price out of range " + dataThis[1]); 65 continue; 66 } 67 numberDish ++; 68 } 69 else {// 说明价格输入不合法 70 System.out.println("wrong format"); 71 } 72 } 73 else {// 说明输入参数错误 74 System.out.println("wrong format"); 75 } 76 } 77 /// 78 /* 完成 */ 79 else {// 开始进行订单,输入了序号 80 table[numberTable].getOrderTable().getRecords()[numberRecord].setRecordTemp(data); 81 numberRecord ++; 82 table[numberTable].getOrderTable().addTotalNum();// 给记录数+1 83 } 84 /// 85 } 86 } 87 int tableNumber;// 临时桌号 88 String[] date; 89 String[] time; 90 int year; int month; int day; 91 int hour; int minute; int second; 92 for(int i = 1; i <= numberTable; i++) {// 这个循环用来将各个类中的临时信息进行处理 93 String[] tableInfo = table[i].getTableInfo().split(" "); 94 95 if(tableInfo[0].equals("table")) { 96 if(tableInfo.length > 4) {// 说明有多余字符
97 table[i].setWrongStatus(false);
98 continue;
99 }
100
101 if(table[i].checkTableNumber(tableInfo[1])) {// 说明桌号是数字
102 tableNumber = Integer.parseInt(tableInfo[1]);
103 table[i].setTableNumber(tableNumber);// 设置桌号
104 if(tableNumber >= 1 && tableNumber <= 55) {// 在桌号合法范围内 105 date = tableInfo[2].split("/");// 得到日期字符串数组 106 time = tableInfo[3].split("/");// 得到时间字符串数组 107 boolean timeFlag = true; 108 if(!table[i].getTableTime().validDate(date[0], date[1], date[2]) || !table[i].getTableTime().validTime(time[0], time[1], time[2])) { 109 timeFlag = false; 110 } 111 if(!timeFlag) { 112 table[i].setWrongStatus(timeFlag);// 将该桌视为异常情况1 wrong format 时间输入格式错误 113 continue;// 直接结束本桌循环 114 } 115 116 year = Integer.parseInt(date[0]); 117 month = Integer.parseInt(date[1]); 118 day = Integer.parseInt(date[2]); 119 hour = Integer.parseInt(time[0]); 120 minute = Integer.parseInt(time[1]); 121 second = Integer.parseInt(time[2]); 122 123 // 判断日期、时间是否合法输入——唯一正确tableFlag=0 124 if(table[i].getTableTime().checkDate(year, month, day) && table[i].getTableTime().checkTime(hour, minute, second)) { 125 table[i].getTableTime().setTableTime(year, month, day, hour, minute, second);// 设置好时间 126 127 for(int j = 0; j < table[i].getOrderTable().getTotalNum(); j++) { 128 String dishData[] = table[i].getOrderTable().getRecords()[j].getRecordTemp().split(" "); 129 if(dishData.length == 4) {// 说明是为本桌点菜 130 Dish dishTemp = new Dish(); 131 dishTemp.setName(dishData[1]);// 设置该记录菜名 132 table[i].getOrderTable().getRecords()[j].getDishThis().setName(dishData[1]);// 设置菜名 133 134 if(tableMenu.searthDish(dishTemp.getName()) != null) {// 说明菜谱内存在该菜 135 table[i].getOrderTable().getRecords()[j].setDishThis(tableMenu.searthDish(dishTemp.getName()));// 将该菜谱导入记录 136 table[i].getOrderTable().getRecords()[j].setExistStatus(true);// 复原至正常情况 137 } 138 else {// 说明菜谱中不存在 139 table[i].getOrderTable().getRecords()[j].setExistStatus(false);// 将该记录视为异常情况3 xxx does not exist 140 } 141 142 if(dishTemp.checkPortion(dishData[2])) {// 判断份额是不是数字 143 table[i].getOrderTable().getRecords()[j].setPortion(Integer.parseInt(dishData[2]));// 设置份额 144 } 145 else { 146 table[i].getOrderTable().getRecords()[j].setWrongStatus(true);// 将该记录视为异常情况2 wrong format 147 continue;// 直接结束本次记录循环 148 } 149 150 if(dishTemp.checkNumber(dishData[3])) {// 判断份数是不是数字 151 table[i].getOrderTable().getRecords()[j].setNum(Integer.parseInt(dishData[3]));// 设置份数 152 } 153 else { 154 table[i].getOrderTable().getRecords()[j].setWrongStatus(true);// 将该记录视为异常情况2 wrong format 155 continue;// 直接结束本次记录循环 156 } 157 if(dishTemp.checkNumber(dishData[0])) {// 判断序号是不是数字 158 table[i].getOrderTable().getRecords()[j].setOrderNum(Integer.parseInt(dishData[0]));// 设置序号 159 } 160 else { 161 table[i].getOrderTable().getRecords()[j].setNumError(true);// 将该记录视为异常情况4 wrong format,但是优先级最高 162 continue;// 直接结束本次记录循环 163 } 164 } 165 166 else if(dishData.length == 5) {// 说明是代点菜 167 table[i].getOrderTable().getRecords()[j].setReplaceStatus(true);// 是代点菜记录 168 boolean isTableNumFlag = true;// 判断代点桌号是否为数字 169 for(int check = 0; check < dishData[0].length(); check++) { 170 if(dishData[0].charAt(check) < "0" || dishData[0].charAt(check) > "9") {
171 isTableNumFlag = false;
172 break;
173 }
174 }
175 if(isTableNumFlag) {
176 table[i].getOrderTable().getRecords()[j].setReplaceTableNum(Integer.parseInt(dishData[0]));
177 boolean tableNumExist = false;// 判断代点桌号是否存在
178 for(int check = 1; check <= tableNumber; check++) { 179 if(table[check].getTableNumber() == Integer.parseInt(dishData[0])) { 180 tableNumExist = true; 181 break; 182 } 183 } 184 185 if(!tableNumExist) { 186 table[i].getOrderTable().getRecords()[j].setReplaceTableNumExist(false); 187 continue;// 结束本条记录 188 } 189 190 Dish dishTemp = new Dish(); 191 dishTemp.setName(dishData[2]);// 设置该记录菜名 192 table[i].getOrderTable().getRecords()[j].getDishThis().setName(dishData[2]);// 设置菜名 193 194 if(tableMenu.searthDish(dishTemp.getName()) != null) {// 说明菜谱内存在该菜 195 table[i].getOrderTable().getRecords()[j].setDishThis(tableMenu.searthDish(dishTemp.getName()));// 将该菜谱导入记录 196 table[i].getOrderTable().getRecords()[j].setExistStatus(true);// 复原至正常情况 197 } 198 else {// 说明菜谱中不存在 199 table[i].getOrderTable().getRecords()[j].setExistStatus(false);// 将该记录视为异常情况3 xxx does not exist 200 } 201 202 if(dishTemp.checkPortion(dishData[3])) {// 判断份额是不是数字 203 table[i].getOrderTable().getRecords()[j].setPortion(Integer.parseInt(dishData[3]));// 设置份额 204 } 205 else { 206 table[i].getOrderTable().getRecords()[j].setWrongStatus(true);// 将该记录视为异常情况2 wrong format 207 continue;// 直接结束本次记录循环 208 } 209 210 if(dishTemp.checkNumber(dishData[4])) {// 判断份数是不是数字 211 table[i].getOrderTable().getRecords()[j].setNum(Integer.parseInt(dishData[4]));// 设置份数 212 } 213 else { 214 table[i].getOrderTable().getRecords()[j].setWrongStatus(true);// 将该记录视为异常情况2 wrong format 215 continue;// 直接结束本次记录循环 216 } 217 if(dishTemp.checkNumber(dishData[1])) {// 判断序号是不是数字 218 table[i].getOrderTable().getRecords()[j].setOrderNum(Integer.parseInt(dishData[1]));// 设置序号 219 } 220 else { 221 table[i].getOrderTable().getRecords()[j].setNumError(true);// 将该记录视为异常情况4 wrong format,但是优先级最高 222 continue;// 直接结束本次记录循环 223 } 224 } 225 else { 226 table[i].getOrderTable().getRecords()[j].setWrongStatus(true);// 将该记录视为异常情况2 wrong format 227 continue;// 直接结束本次记录循环 228 } 229 } 230 231 else if(dishData.length == 3) {// 有可能是特色菜 232 table[i].getOrderTable().getRecords()[j].setIsMix(true);// 有夹杂 invalid dish 233 continue;// 直接结束本次记录循环 234 } 235 236 else if(dishData.length == 2) {// 说明是删除 237 if(dishData[1].equals("delete")) { 238 table[i].getOrderTable().getRecords()[j].setIsDeleteRecord(true);// 是删除记录 239 /// 此处序号可能还需要再改------------- 240 if(dishData[0].charAt(0) >= "0" && dishData[0].charAt(0) <= "9") {// 说明序号正确 241 int checkNumber = Integer.parseInt(dishData[0]); 242 boolean deleteFlag = false;// 删除的标志 243 for(int k = 0; k < table[i].getOrderTable().getTotalNum(); k++) { 244 if(checkNumber == table[i].getOrderTable().getRecords()[k].getOrderNum()) { 245 if(!table[i].getOrderTable().getRecords()[k].getDeleteStatus()) { 246 table[i].getOrderTable().getRecords()[k].setDeleteStatus(true);// 成功删除 247 deleteFlag = true; 248 break; 249 } 250 else { 251 table[i].getOrderTable().getRecords()[k].setDeleteRepeat(true);// 重复删除 252 deleteFlag = true; 253 break; 254 } 255 } 256 } 257 if(!deleteFlag) { 258 table[i].getOrderTable().getRecords()[j].setDeleteErrorStatus(true);// 将该记录视为异常情况1 delete error 259 continue;// 直接结束本记录循环 260 } 261 continue;// 直接结束本次记录循环 262 } 263 else { 264 table[i].getOrderTable().getRecords()[j].setDeleteErrorStatus(true);// 将该记录视为异常情况1 delete error 265 continue;// 直接结束本次记录循环 266 } 267 } 268 else {// 可能是夹杂在内的菜谱 269 if(dishData[1].length() <= 2) { 270 table[i].getOrderTable().getRecords()[j].setIsMix(true);// 有夹杂 invalid dish 271 continue;// 直接结束本次记录循环 272 } 273 table[i].getOrderTable().getRecords()[j].setDeleteErrorStatus(true);// 将该记录视为异常情况2 wrong format 274 continue;// 直接结束本次记录循环 275 } 276 } 277 else{ 278 table[i].getOrderTable().getRecords()[j].setDeleteErrorStatus(true);// 将该记录视为异常情况2 wrong format 279 continue;// 直接结束本次记录循环 280 } 281 } 282 } 283 else {// 说明日期、时间输入不合法 284 table[i].setTime_isValid(false);// 将该桌视为异常情况2 not a valid time period 285 continue;// 直接结束本桌循环 286 } 287 if(!table[i].getTableTime().isOpen(year, month, day, hour, minute, second)) { 288 table[i].setIsOpeningtime(false);// 将该桌视为异常情况5 "table " + t.tableNum + " out of opening hours" 289 } 290 } 291 else {// 说明超出了桌号范围 292 table[i].setTableNumber_Out(false);// 将该桌视为异常情况3 table num out of range 293 continue;// 直接结束本桌循环 294 } 295 } 296 else { 297 table[i].setWrongStatus(false);// 将该桌视为异常情况1 wrong format 桌号不是数字 298 continue;// 直接结束本桌循环 299 } 300 } 301 else { 302 if(tableInfo[0].substring(0, 5).equals("table")) { 303 table[i].setWrongStatus(false); 304 continue; 305 } 306 table[i].setTableStatus(false);// 将该桌视为异常情况4 wrong format + record serial number sequence error 307 continue;// 直接结束本桌输出 308 } 309 } 310 311 int checkRepeatNumber[] = new int[10];// 用于检查序号是否从小到大排序的 312 313 for(int i = 1; i <= numberTable; i++) {// 进行价格计算与输出每桌的点菜情况 314 int normalFlag = 0; 315 if(table[i].getWrongStatus() && table[i].getTime_isValid() && table[i].getTableNumber_Out() && 316 table[i].getTableStatus()) {// 正常情况 317 318 System.out.println("table " + table[i].getTableNumber() + ": ");// 输出桌号 319 320 for(int j = 0; j < table[i].getOrderTable().getTotalNum(); j++) {// 订单 321 if(table[i].getOrderTable().getRecords()[j].getIsMix()) {// 判断是否为夹杂的菜谱 322 System.out.println("invalid dish"); 323 continue;// 直接结束本条记录 324 } 325 326 if(!table[i].getOrderTable().getRecords()[j].getNumError()) {// 正常的符合格式的记录 327 if(table[i].getOrderTable().getRecords()[j].getPortion() > 3) {// 份额大于3
328 System.out.println(table[i].getOrderTable().getRecords()[j].getOrderNum() + " portion out of range " + table[i].getOrderTable().getRecords()[j].getPortion());
329 continue;// 直接结束本条记录
330 }
331 else if(table[i].getOrderTable().getRecords()[j].getPortion() > 9) {// 份额格式错误
332 System.out.println("not a valid portion");
333 continue;// 直接结束本条记录
334 }
335 if(table[i].getOrderTable().getRecords()[j].getNum() > 15) {
336 System.out.println(table[i].getOrderTable().getRecords()[j].getOrderNum() + " num out of range " + table[i].getOrderTable().getRecords()[j].getNum());
337 continue;// 直接结束本条记录
338 }
339
340 if(table[i].getOrderTable().getRecords()[j].getIsDeleteRecord()) {// 说明是一条删除记录
341 if(table[i].getOrderTable().getRecords()[j].getDeleteErrorStatus()) {
342 System.out.println("delete error");
343 }
344 continue;
345 }
346
347 if(normalFlag == 0 && table[i].getOrderTable().getRecords()[j].getExistStatus()) {// 说明是第一条正常的记录或者删除记录
348 checkRepeatNumber[normalFlag] = table[i].getOrderTable().getRecords()[j].getOrderNum();
349 // 计算该条记录的价格
350 float accountPercent = 1;// 折扣率
351 if(table[i].getOrderTable().getRecords()[j].getDishThis().getIsT()) {// 判断是否为特色菜
352 if(table[i].getTableTime().checkT()) {// 进一步判断当前时间特色菜是否打折
353 accountPercent = (float)0.7;
354 }
355 }
356 else {
357 accountPercent = table[i].getTableTime().account();// 普通菜
358 }
359 // 临时价格没有折扣和四舍五入
360 float priceTemp = table[i].getOrderTable().getRecords()[j].getDishThis().getPrice(table[i].getOrderTable().getRecords()[j].getPortion());
361 int priceTempInt = (int)(priceTemp + 0.5);
362 if(table[i].getOrderTable().getRecords()[j].getReplaceStatus()){
363 if(table[i].getOrderTable().getRecords()[j].getReplaceTableNumExist()) {// 说明代点成功
364 System.out.print(table[i].getOrderTable().getRecords()[j].getOrderNum() + " table " + table[i].getTableNumber() + " pay for table ");
365 System.out.println(table[i].getOrderTable().getRecords()[j].getReplaceTableNum() + " " + priceTempInt * table[i].getOrderTable().getRecords()[j].getNum());
366 }
367 else {
368 System.out.println("Table number :"+ table[i].getOrderTable().getRecords()[j].getReplaceTableNum() +" does not exist");
369 continue;// 结束本条记录,不进行求和
370 }
371 }
372 else {
373 boolean overFlag = false;
374 for(int k = j+1; k < table[i].getOrderTable().getTotalNum(); k++) { 375 if(table[i].getOrderTable().getRecords()[j].getDishThis().getName().equals(table[i].getOrderTable().getRecords()[k].getDishThis().getName())) { 376 overFlag = true; 377 break; 378 } 379 } 380 if(overFlag) { 381 continue; 382 } 383 System.out.print(checkRepeatNumber[normalFlag] + " " + table[i].getOrderTable().getRecords()[j].getDishThis().getName()); 384 System.out.println(" " + priceTempInt * table[i].getOrderTable().getRecords()[j].getNum());// 四舍五入输出点所有量该菜的价格 385 } 386 if(table[i].getOrderTable().getRecords()[j].getDeleteStatus()) { 387 if(table[i].getOrderTable().getRecords()[j].getDeleteRepeat()) {// 重复删除的一条记录 388 System.out.println("deduplication " + table[i].getOrderTable().getRecords()[j].getOrderNum()); 389 } 390 } 391 392 if(table[i].getOrderTable().getRecords()[j].getDeleteStatus()) {// 如果是删除了的,直接结束 393 continue;// 直接结束本条记录 394 } 395 int recordPrice = (int)((priceTempInt * table[i].getOrderTable().getRecords()[j].getNum()) * accountPercent + 0.5);// 本条记录的菜品价格总和 396 table[i].getOrderTable().addOrderPrice(recordPrice);// 求和 397 table[i].getOrderTable().addOrderPriceOrigin(priceTempInt * table[i].getOrderTable().getRecords()[j].getNum());// 求和 398 normalFlag ++; 399 } 400 else if(table[i].getOrderTable().getRecords()[j].getExistStatus()){ 401 boolean normalNumberFlag = true;// 判断序号是否从小到大排序 402 for(int k = 0; k < normalFlag; k++) { 403 // 若本条记录的序号小于前面每一条正常的记录的序号,说明序号错误 404 if(table[i].getOrderTable().getRecords()[j].getOrderNum() <= table[i].getOrderTable().getRecords()[k].getOrderNum() && 405 !table[i].getOrderTable().getRecords()[j].getReplaceStatus() && !table[i].getOrderTable().getRecords()[k].getReplaceStatus()) { 406 normalNumberFlag = false; 407 break; 408 } 409 } 410 if(normalNumberFlag) { 411 checkRepeatNumber[normalFlag] = table[i].getOrderTable().getRecords()[j].getOrderNum(); 412 // 计算该条记录的价格 413 float accountPercent = 1;// 折扣率 414 if(table[i].getOrderTable().getRecords()[j].getDishThis().getIsT()) {// 判断是否为特色菜 415 if(table[i].getTableTime().checkT()) {// 进一步判断当前时间特色菜是否打折 416 accountPercent = (float)0.7; 417 } 418 } 419 else { 420 accountPercent = table[i].getTableTime().account();// 非特色菜 421 } 422 // 临时价格没有折扣和四舍五入 423 float priceTemp = table[i].getOrderTable().getRecords()[j].getDishThis().getPrice(table[i].getOrderTable().getRecords()[j].getPortion()); 424 int priceTempInt = (int)(priceTemp + 0.5); 425 if(table[i].getOrderTable().getRecords()[j].getReplaceStatus()){ 426 if(table[i].getOrderTable().getRecords()[j].getReplaceTableNumExist()) {// 说明代点成功 427 System.out.print(table[i].getOrderTable().getRecords()[j].getOrderNum() + " table " + table[i].getTableNumber() + " pay for table "); 428 System.out.println(table[i].getOrderTable().getRecords()[j].getReplaceTableNum() + " " + priceTempInt * table[i].getOrderTable().getRecords()[j].getNum()); 429 } 430 else { 431 System.out.println("Table number :"+ table[i].getOrderTable().getRecords()[j].getReplaceTableNum() +" does not exist"); 432 continue;// 结束本条记录,不进行求和 433 } 434 } 435 else { 436 boolean overFlag = false; 437 for(int k = j+1; k < table[i].getOrderTable().getTotalNum(); k++) { 438 if(table[i].getOrderTable().getRecords()[j].getDishThis().getName().equals(table[i].getOrderTable().getRecords()[k].getDishThis().getName())) { 439 overFlag = true; 440 break; 441 } 442 } 443 if(overFlag) { 444 continue; 445 } 446 System.out.print(checkRepeatNumber[normalFlag] + " " + table[i].getOrderTable().getRecords()[j].getDishThis().getName()); 447 System.out.println(" " + priceTempInt * table[i].getOrderTable().getRecords()[j].getNum());// 四舍五入输出点所有量该菜的价格 448 } 449 if(table[i].getOrderTable().getRecords()[j].getDeleteStatus()) { 450 if(table[i].getOrderTable().getRecords()[j].getDeleteRepeat()) {// 重复删除的一条记录 451 System.out.println("deduplication " + table[i].getOrderTable().getRecords()[j].getOrderNum()); 452 } 453 } 454 455 if(table[i].getOrderTable().getRecords()[j].getDeleteStatus()) {// 如果是删除了的,直接结束 456 continue;// 直接结束本条记录 457 } 458 int recordPrice = (int)((priceTempInt * table[i].getOrderTable().getRecords()[j].getNum()) * accountPercent + 0.5);// 本条记录的菜品价格总和 459 table[i].getOrderTable().addOrderPrice(recordPrice);// 求和 460 table[i].getOrderTable().addOrderPriceOrigin(priceTempInt * table[i].getOrderTable().getRecords()[j].getNum());// 求和 461 normalFlag ++; 462 } 463 else { 464 System.out.println("record serial number sequence error"); 465 continue;// 直接结束本条记录 466 } 467 } 468 } 469 470 if(table[i].getOrderTable().getRecords()[j].getNumError()) {// 序号输入错误 471 System.out.println("wrong format"); 472 continue;// 结束本条记录 473 } 474 475 else if(!table[i].getOrderTable().getRecords()[j].getExistStatus() && !table[i].getOrderTable().getRecords()[j].getNumError()){// 该菜品不存在 476 System.out.println(table[i].getOrderTable().getRecords()[j].getDishThis().getName() + " does not exist"); 477 continue; 478 } 479 } 480 } 481 else if(!table[i].getWrongStatus()){// 一律wrong format 482 System.out.println("wrong format"); 483 continue; 484 } 485 else if(!table[i].getTableNumber_Out()) {// 桌号超出范围 486 System.out.println(table[i].getTableNumber() + " table num out of range"); 487 continue; 488 } 489 else if(!table[i].getTableStatus()) { 490 System.out.println("wrong format"); 491 for(int j = 0; j < table[i].getOrderTable().getTotalNum(); j++) { 492 System.out.println("record serial number sequence error"); 493 } 494 continue; 495 } 496 } 497 498 for(int i = 1; i <= numberTable; i++) {// 输出每桌总价格 499 if(table[i].getWrongStatus() && table[i].getTableNumber_Out() && table[i].getTableStatus()) { 500 501 if(!table[i].getIsOpeningtime()) { 502 System.out.println("table " + table[i].getTableNumber() + " out of opening hours"); 503 } 504 else if(!table[i].getTime_isValid()) { 505 System.out.print("table " + table[i].getTableNumber() + ": ");// 输出桌号 506 System.out.println("not a valid time period"); 507 } 508 else { 509 System.out.print("table " + table[i].getTableNumber() + ": "); 510 System.out.println(table[i].getOrderTable().getOrderPriceOrigin() + " " + table[i].getOrderTable().getOrderPrice()); 511 } 512 } 513 } 514 input.close(); 515 } 516 } 517 518 class Dish {// 菜品类:对应菜谱,包含饭店提供的所有菜的信息。 519 private String name;// 菜品名称 520 private int unit_price; // 原价 521 private boolean isT = false;// 是否为特色菜 522 523 public boolean getIsT() { 524 return isT; 525 } 526 527 public void setIsT(boolean isT) { 528 this.isT = isT; 529 } 530 531 public String getName() { 532 return name; 533 } 534 535 public void setName(String name) { 536 this.name = name; 537 } 538 539 public int getUnit_price() { 540 return unit_price; 541 } 542 543 public void setUnit_price(int unit_price) { 544 this.unit_price = unit_price; 545 } 546 547 public float getPrice(int portion) {// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) } 548 switch(portion){ 549 case 1:return this.unit_price; 550 case 2:return (float)this.unit_price * (float)1.5; 551 case 3:return (float)this.unit_price * 2; 552 default:return 0; 553 } 554 } 555 556 public boolean checkPrice(String price){// 检查价格是否为数字 557 boolean status = false; 558 for(int i = 0; i < price.length(); i++) { 559 if(price.charAt(i) >= "0" && price.charAt(i) <= "9") { 560 status = true; 561 } 562 else { 563 status = false; 564 break; 565 } 566 } 567 return status; 568 } 569 570 public boolean checkPortion(String portion){// 检查份额是否为数字 571 boolean status = false; 572 for(int i = 0; i < portion.length(); i++) { 573 if(portion.charAt(i) >= "0" && portion.charAt(i) <= "9") { 574 status = true; 575 } 576 else { 577 status = false; 578 break; 579 } 580 } 581 return status; 582 } 583 584 public boolean checkNumber(String number){// 检查份数是否为数字 585 boolean status = false; 586 for(int i = 0; i < number.length(); i++) { 587 if(number.charAt(i) >= "0" && number.charAt(i) <= "9") { 588 status = true; 589 } 590 else { 591 status = false; 592 break; 593 } 594 } 595 return status; 596 } 597 } 598 599 class Menu {// 菜谱类:对应菜谱,包含饭店提供的所有菜的信息。 600 private Dish dishAll[] = new Dish[6]; 601 public Dish searthDish(String dishName){//根据菜名在菜谱中查找菜品信息,返回Dish对象。 602 for(int i = 0; i < 6; i++) { 603 if(dishAll[i] == null) { 604 dishAll[i] = new Dish();// 分配空间 605 } 606 if(dishName.equals(dishAll[i].getName())){ 607 return dishAll[i]; 608 } 609 } 610 return null;// 说明该菜并不存在 611 } 612 public void addDish(String dishName, int unit_price, boolean isT, int N){// 添加一道菜品信息 613 Dish Replace = new Dish(); 614 Replace.setName(dishName); 615 Replace.setUnit_price(unit_price); 616 Replace.setIsT(isT); 617 if(this.dishAll[N] == null) 618 this.dishAll[N] = new Dish();// 分配空间 619 this.dishAll[N] = Replace; 620 } 621 } 622 623 class Record {// 点菜记录类:保存订单上的一道菜品记录 624 private String recordTemp = new String();// 临时存储的记录 625 626 private boolean replaceTableNumExist = false;// 代点菜桌号存在情况 627 628 private boolean isMix = false;// 是否为夹杂的菜谱 629 630 private boolean isDeleteRecord = false;// 是否为删除记录 631 632 private int deleteNum;// 要删除的序号 633 634 private boolean deleteErrorStatus = false;// 是否删除错误 635 636 private boolean wrongStatus = false;// 是否输出wrong format 637 638 boolean existStatus = true;// 是否存在该菜品 639 640 private boolean replaceStatus = false;// 判断该记录是否为代点菜记录 641 642 private int replaceTableNum;// 代点单的桌号 643 644 private boolean deleteStatus = false;// 用于判断是否删除,删除了说明为true不可用,false则没有删除,可用 645 646 private boolean deleteRepeat = false;// 是否重复删除 647 648 private boolean numError = false;// 序号错误 649 650 private int orderNum;// 序号 651 652 private Dish dishThis = new Dish();// 本条记录的菜品 653 654 private int portion;// 份额 655 656 private int num;// 份数 657 658 private float recordPrice;// 本条记录的价格 659 660 public boolean getReplaceTableNumExist() { 661 return replaceTableNumExist; 662 } 663 664 public void setReplaceTableNumExist(boolean replaceTableNumExist) { 665 this.replaceTableNumExist = replaceTableNumExist; 666 } 667 668 public int getReplaceTableNum() { 669 return replaceTableNum; 670 } 671 672 public void setReplaceTableNum(int replaceTableNum) { 673 this.replaceTableNum = replaceTableNum; 674 } 675 676 public boolean getNumError() { 677 return numError; 678 } 679 680 public void setNumError(boolean numError) { 681 this.numError = numError; 682 } 683 684 public boolean getIsDeleteRecord() { 685 return isDeleteRecord; 686 } 687 688 public void setIsDeleteRecord(boolean isDeleteRecord) { 689 this.isDeleteRecord = isDeleteRecord; 690 } 691 692 public int getDeleteNum() { 693 return deleteNum; 694 } 695 696 public void setDeleteNum(int deleteNum) { 697 this.deleteNum = deleteNum; 698 } 699 700 public boolean getDeleteErrorStatus() { 701 return deleteErrorStatus; 702 } 703 704 public void setDeleteErrorStatus(boolean deleteErrorStatus) { 705 this.deleteErrorStatus = deleteErrorStatus; 706 } 707 708 public boolean getWrongStatus() { 709 return wrongStatus; 710 } 711 712 public void setWrongStatus(boolean wrongStatus) { 713 this.wrongStatus = wrongStatus; 714 } 715 716 public boolean getExistStatus() { 717 return existStatus; 718 } 719 720 public void setExistStatus(boolean existStatus) { 721 this.existStatus = existStatus; 722 } 723 724 public float getRecordPrice() { 725 return recordPrice; 726 } 727 728 public void setRecordPrice(float recordPrice) { 729 this.recordPrice = recordPrice; 730 } 731 732 public boolean getIsMix() { 733 return isMix; 734 } 735 736 public void setIsMix(boolean isMix) { 737 this.isMix = isMix; 738 } 739 740 public boolean getDeleteRepeat() { 741 return deleteRepeat; 742 } 743 744 public void setDeleteRepeat(boolean deleteRepeat) { 745 this.deleteRepeat = deleteRepeat; 746 } 747 748 public boolean getDeleteStatus() { 749 return deleteStatus; 750 } 751 752 public void setDeleteStatus(boolean deleteStatus) { 753 this.deleteStatus = deleteStatus; 754 } 755 756 public String getRecordTemp() { 757 return recordTemp; 758 } 759 760 public boolean getReplaceStatus() { 761 return replaceStatus; 762 } 763 764 public void setReplaceStatus(boolean replaceStatus) { 765 this.replaceStatus = replaceStatus; 766 } 767 768 public void setRecordTemp(String recordTemp) { 769 this.recordTemp = recordTemp; 770 } 771 772 public boolean getStatus() {// 得到是否删除的状态 773 return deleteStatus; 774 } 775 776 public void setStatus(boolean deleteStatus) {// 设置状态 777 this.deleteStatus = deleteStatus; 778 } 779 780 public int getPortion() {// 得到份额 781 return portion; 782 } 783 784 public void setPortion(int portion) {// 设置份额 785 this.portion = portion; 786 } 787 788 public int getNum() {// 得到份数 789 return num; 790 } 791 792 public void setNum(int num) {// 设置份数 793 this.num = num; 794 } 795 796 public int getOrderNum() {// 得到序号 797 return orderNum; 798 } 799 800 public void setOrderNum(int orderNum) {// 设置序号 801 this.orderNum = orderNum; 802 } 803 804 public Dish getDishThis() {// 得到菜品 805 return dishThis; 806 } 807 808 public void setDishThis(Dish dishThis) {// 设置菜品 809 this.dishThis = dishThis; 810 } 811 812 } 813 814 class Order {// 订单类:保存用户点的所有菜的信息。 815 private Record[] records = new Record[10];// 保存订单上每一道的记录 816 817 private int totalNum;// 记录数 818 819 private int orderPrice;// 本条订单菜价总和(未算入特定时间的折扣) 820 821 private int orderPriceOrigin;// 原始价 822 823 public void addOrderPriceOrigin(int recordPrice) { 824 this.orderPriceOrigin += recordPrice; 825 } 826 827 public int getOrderPriceOrigin() { 828 return orderPriceOrigin; 829 } 830 831 public void setOrderPriceOrigin(int orderPriceOrigin) { 832 this.orderPriceOrigin = orderPriceOrigin; 833 } 834 835 public void addOrderPrice(int recordPrice) { 836 this.orderPrice += recordPrice; 837 } 838 839 public int getOrderPrice() { 840 return orderPrice; 841 } 842 843 public void setOrderPrice(int orderPrice) { 844 this.orderPrice = orderPrice; 845 } 846 847 public int getTotalNum() {// 得到记录数 848 return this.totalNum; 849 } 850 851 public void addTotalNum() {// 给记录数+1 852 this.totalNum ++; 853 } 854 855 public float getTotalPrice(){// 计算订单的总价 856 float totalPrice = 0; 857 for(int i = 0; i < this.totalNum; i++) { 858 if(getRecords()[i].getStatus()) 859 totalPrice += getRecords()[i].getDishThis().getPrice(1); 860 } 861 return totalPrice; 862 } 863 864 public Record addARecord(int orderNum,Dish dish,int portion,int num){// 添加一条菜品信息到订单中。 865 Record records = new Record(); 866 records.setDishThis(dish); 867 records.setOrderNum(orderNum); 868 records.setNum(num); 869 records.setPortion(portion); 870 this.totalNum ++;// 订单内菜品数需要+1 871 return records; 872 } 873 874 public void delARecordByOrderNum(int orderNum){// 根据序号删除一条记录 875 boolean flag = false; 876 for(int i = 0; i < this.totalNum; i++) { 877 if(getRecords()[i].getOrderNum() == orderNum) { 878 flag = true; 879 if(getRecords()[i].getStatus()) {// 说明没有删除 880 getRecords()[i].setStatus(false); 881 } 882 else {// 说明重复删除了 883 System.out.println("deduplication " + orderNum); 884 } 885 } 886 } 887 if(flag == false) { 888 System.out.println("delete error"); 889 } 890 } 891 892 public boolean checkOrderNum() {// 判断序号是不是从小到大的 893 boolean flag = true; 894 for(int i = 0; i < this.totalNum; i++) {// 小于100防止越界 895 for(int j = i+1; j < this.totalNum; j++) {// 同上 896 if(getRecords()[j].getOrderNum() < getRecords()[i].getOrderNum()){ 897 flag = false; 898 break; 899 } 900 else 901 flag = true; 902 } 903 if(flag == false) 904 return flag; 905 } 906 return true; 907 } 908 909 public Record[] getRecords() {// 得到record 910 for(int i = 0; i < 10; i++) { 911 if(this.records[i] == null) 912 this.records[i] = new Record(); 913 } 914 return records; 915 } 916 917 public void setRecords(Record records, int N) {// 设置record[N] 918 if(N < 10)// 防止数组越界 919 this.records[N] = records; 920 } 921 } 922 923 class Table{ 924 private boolean wrongStatus = true;// 桌号是否为数字 925 926 private boolean time_isValid = true;// 时间是否合法 927 928 private boolean tableNumber_Out = true;// 是否超出桌号范围 929 930 private boolean tableStatus = true;// 该桌是否可用,是否连续输出错误记录 931 932 private boolean isOpeningtime = true;// 是否为营业时间 933 934 private String tableInfo = new String();// 桌子信息 935 936 private Time tableTime = new Time();// 该桌时间 937 938 private Dish dish[] = new Dish[4];// 用来存储用户输入的需要的菜品 939 940 private Order orderTable = new Order();// 本桌订单 941 942 private int tableNumber;// 桌号 943 944 public boolean getWrongStatus() { 945 return wrongStatus; 946 } 947 948 public void setWrongStatus(boolean wrongStatus) { 949 this.wrongStatus = wrongStatus; 950 } 951 952 public boolean getTime_isValid() { 953 return time_isValid; 954 } 955 956 public void setTime_isValid(boolean time_isValid) { 957 this.time_isValid = time_isValid; 958 } 959 960 public boolean getTableNumber_Out() { 961 return tableNumber_Out; 962 } 963 964 public void setTableNumber_Out(boolean tableNumber_Out) { 965 this.tableNumber_Out = tableNumber_Out; 966 } 967 968 public boolean getTableStatus() { 969 return tableStatus; 970 } 971 972 public void setTableStatus(boolean tableStatus) { 973 this.tableStatus = tableStatus; 974 } 975 976 public boolean getIsOpeningtime() { 977 return isOpeningtime; 978 } 979 980 public void setIsOpeningtime(boolean isOpeningtime) { 981 this.isOpeningtime = isOpeningtime; 982 } 983 984 public Time getTableTime() { 985 return tableTime; 986 } 987 988 public void setTableTime(Time tableTime) { 989 this.tableTime = tableTime; 990 } 991 992 public String getTableInfo() { 993 return tableInfo; 994 } 995 996 public void setTableInfo(String tableInfo) { 997 this.tableInfo = tableInfo; 998 } 999 1000 public Order getOrderTable() {// 得到本桌记录 1001 return orderTable; 1002 } 1003 1004 public void setOrderTable(Order orderTable) {// 设置本桌记录 1005 this.orderTable = orderTable; 1006 } 1007 1008 public boolean checkTableNumber(String tableNumber) {// 用于判断桌号输入是否合法 1009 boolean status = false; 1010 for(int i = 0; i < tableNumber.length(); i++) { 1011 if(tableNumber.charAt(i) >= "0" && tableNumber.charAt(i) <= "9") { 1012 status = true; 1013 } 1014 else { 1015 status = false; 1016 break; 1017 } 1018 } 1019 return status; 1020 } 1021 1022 public void setTableNumber(int tableNumber) {// 设置桌号 1023 this.tableNumber = tableNumber; 1024 } 1025 1026 public int getTableNumber() {// 得到桌号 1027 return this.tableNumber; 1028 } 1029 1030 public void setDishN(String name, int unit_price, int N) {// 设置dish[N] 1031 this.dish[N].setName(name); 1032 this.dish[N].setUnit_price(unit_price); 1033 } 1034 1035 public Dish[] getDish() {// 得到dish[] 1036 for(int i = 0; i < 4; i++) { 1037 if(this.dish[i] == null) 1038 dish[i] = new Dish(); 1039 } 1040 return this.dish; 1041 } 1042 1043 } 1044 1045 class Time{ 1046 private int year;// 年 1047 1048 private int month;// 月 1049 1050 private int day;// 日 1051 1052 private int hour;// 小时 1053 1054 private int minute;// 分钟 1055 1056 private int second;// 秒 1057 1058 //设置当前下单时间 1059 public void setTableTime(int year, int month, int day, int hour, int minute, int second) { 1060 this.year = year; 1061 this.month = month; 1062 this.day = day; 1063 this.hour = hour; 1064 this.minute = minute; 1065 this.second = second; 1066 } 1067 1068 public int getYear() { 1069 return year; 1070 } 1071 1072 public int getMonth() { 1073 return month; 1074 } 1075 1076 public int getDay() { 1077 return day; 1078 } 1079 1080 public int getHour() { 1081 return hour; 1082 } 1083 1084 public int getMinute() { 1085 return minute; 1086 } 1087 1088 public int getSecond() { 1089 return second; 1090 } 1091 1092 public int week(int year, int month, int day) { 1093 Calendar calendar = Calendar.getInstance(); 1094 calendar.set(Calendar.YEAR, year);// 设置年 1095 calendar.set(Calendar.MONTH, month-1);// 设置月 1096 calendar.set(Calendar.DATE, day);// 设置日 1097 int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; 1098 if(week == 0){// 说明是星期日 1099 week = 7; 1100 } 1101 return week; 1102 } 1103 1104 public boolean validDate(String year, String month, String day) {// 判断日期是否合法 1105 if(year.length() == 4) { 1106 if(month.length() <= 2 && day.length() <= 2) { 1107 for(int i = 0; i < month.length(); i++) { 1108 if(month.charAt(i) < "0" || month.charAt(i) > "9") {
1109 return false;
1110 }
1111 }
1112 for(int i = 0; i < day.length(); i++) { 1113 if(day.charAt(i) < "0" || day.charAt(i) > "9") {
1114 return false;
1115 }
1116 }
1117 return true;
1118 }
1119 else {
1120 return false;
1121 }
1122 }
1123 else {
1124 return false;
1125 }
1126 }
1127
1128 public boolean validTime(String hour, String minute, String second) {// 判断时间是否合法
1129 if(hour.length() == 2 && minute.length() == 2 && second.length() == 2) {
1130 for(int i = 0; i < 2; i++) { 1131 if(hour.charAt(i) < "0" || hour.charAt(i) > "9" || minute.charAt(i) < "0" || minute.charAt(i) > "9" || second.charAt(i) < "0" || second.charAt(i) > "9") {
1132 return false;
1133 }
1134 }
1135 return true;
1136 }
1137 else{
1138 return false;
1139 }
1140 }
1141
1142 public boolean checkDate(int year, int month, int day) {// 判断日期是否输入合法
1143 int month_maxnum[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// 当月最大天数
1144 if(year >= 2022 && year <= 2023) { 1145 if(month >= 1 && month <= 12) { 1146 if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) 1147 month_maxnum[2] = 29; 1148 else 1149 month_maxnum[2] = 28; 1150 if(day >= 1 && day <= month_maxnum[month]) 1151 return true; 1152 else 1153 return false; 1154 } 1155 else 1156 return false; 1157 } 1158 else 1159 return false; 1160 } 1161 1162 public boolean checkTime(int hour, int minute, int second) {// 检查该天时间是否合法 1163 if(hour >= 0 && hour <= 24) { 1164 if(minute >= 0 && minute <= 60) { 1165 if(second >=0 && second <= 60) 1166 return true; 1167 else 1168 return false; 1169 } 1170 return false; 1171 } 1172 else 1173 return false; 1174 } 1175 // 判断是否和折扣情况 1176 public float account(){ 1177 int now = hour * 10000 + minute * 100 + second; 1178 int time1 = 170000;// 17点整 1179 int time2 = 203000;// 20点半 1180 int time3 = 103000;// 10点半 1181 int time4 = 143000;// 14点半 1182 int time5 = 93000;// 9点半 1183 int time6 = 213000;// 21点半 1184 if(week(year, month, day) >= 1 && week(year, month, day) <= 5) {// 周一到周五 1185 if(now >= time1 && now <= time2) 1186 return (float)0.8; 1187 else if(now >= time3 && now <= time4) 1188 return (float)0.6; 1189 else 1190 return 0;// 不在营业 1191 } 1192 else { 1193 if(now >= time5 && now <= time6) 1194 return 1; 1195 else 1196 return 0;// 不在营业 1197 } 1198 } 1199 1200 // 是否在营业 1201 public boolean isOpen(int year, int month, int day, int hour, int minute, int second){ 1202 int now = hour * 10000 + minute * 100 + second; 1203 int time1 = 170000;// 17点整 1204 int time2 = 203000;// 20点半 1205 int time3 = 103000;// 10点半 1206 int time4 = 143000;// 14点半 1207 int time5 = 93000;// 9点半 1208 int time6 = 213000;// 21点半 1209 if(week(year, month, day) >= 1 && week(year, month, day) <= 5) {// 周一到周五 1210 if(now >= time1 && now <= time2) 1211 return true; 1212 else if(now >= time3 && now <= time4) 1213 return true; 1214 else 1215 return false; 1216 } 1217 else { 1218 if(now >= time5 && now <= time6) 1219 return true; 1220 else 1221 return false; 1222 } 1223 } 1224 1225 // 判断特色菜是否打折 1226 public boolean checkT(){ 1227 if(week(this.year, this.month, this.day) >= 1 && week(this.year, this.month, this.day) <= 5) {
1228 return true;
1229 }
1230 else
1231 return false;
1232 }
1233 }
一开始我全部都是根据输入的信息生硬的使用正则表达式进行编程,半路发现功能无法实现,只能重新换方法,我意识到编程前首先是要把题目要求弄清,多读几遍题目,再一个就是要想好编程中用的方法再动手实现。

(3)踩坑心得:

  在题目集5中:在正则表达式中数字的范围不能变为两位数,比如说:[0-9]是正确的,而[10-19]就不正确。

  日期7-6中,将之前的代码拿过来也可以通过所有测试点,这时说明通过所有测试点并不是意味着写的有多好,只是大体功能上可以使用。

  在菜单7-1中:一开始我全部都是根据输入的信息生硬的使用正则表达式进行编程,半路发现功能无法实现,只能重新换方法,我意识到编程前首先是要把题目要求弄清,多读几遍题目,再一个就是要想好编程中用的方法再动手实现。

(4)改进意见:

  1.对于像类与类之间关系的使用练习,多使用之前做过的题目进行格式上的变化,一方面可以降低题目难度,另一方面可以节约时间,做其他新的题目。

  2.像菜单这种难度较大的题目,可以从一开始先给一个基础版,以后再加进阶版,比如:我们做过的菜单有3、4,而明显之前有1、2,可是从没见过。

(5)总结:

  1:面对题目较长时一定要多读题,画一画类图,再下手,不要一股脑瞎做。

  2:程序员一定要具有不怕困难的精神,不能遇到困难就退缩。

  3:现在一个最大的问题就是上课听的时候似懂非懂,一下课就不记得讲过什么,尤其是模式。