22201134-于子昂第二次blog作业

joe-4- / 2023-05-03 / 原文

关于第四到第六次的pta题目集的总结与心得

前言:

这三次题目集用到了正则表达式以及各种类的调用,同时题量偏大,难度一般吧(虽然我没有写完)

设计与分析:

关于题目集4的T7-1:

我当时没写。。。。就和题目集6的T7-1一起讲吧,反正是迭代的题。

关于题目集5的T7-5:

题目:

就是给三种方法,要不算日期前多少天是啥日期,要不是后多少天,要不是两个日期之间隔了多少天。

它是先前日期题的迭代,主要区别在于使用了聚合的关系将不同类一一关联了。先看源码:

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if(i == 1) {
DateUtil dateutil = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt());
i = input.nextInt();
if(!dateutil.checkInputValidity() || i < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
else {
System.out.print(dateutil.showDate() + " next " + i + " days is:");
dateutil.getNextNDays(i);
System.out.println(dateutil.showDate());
}
}
else if(i == 2) {
DateUtil dateutil = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt());
i = input.nextInt();
if(!dateutil.checkInputValidity() || i < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
else {
System.out.print(dateutil.showDate() + " previous " + i + " days is:");
dateutil.getPreviousNDays(i);
System.out.println(dateutil.showDate());
}
}
else if(i == 3) {
DateUtil dateutil1 = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt()), dateutil2 = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt());
if(!dateutil1.checkInputValidity() || !dateutil2.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
else {
System.out.println("The days between " + dateutil1.showDate() + " and " + dateutil2.showDate() + " are:" + dateutil1.getDaysofDates(dateutil2));
}
}
else {
System.out.println("Wrong Format");
System.exit(0);
}

}

}
class DateUtil {
private Day day;
private Month month;
private Year year;
private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil(){

}
public DateUtil(int y, int m, int d){
day = new Day(d);
month = new Month(m);
year = new Year(y);
}
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;
}
public void setDayMin() {
day.setValue(1);
}
public void setDayMax() {

day.setValue(mon_maxnum[month.getValue() - 1]);

}
public boolean checkInputValidity() {
if(!year.validate()) {
return false;
}
if(!month.validate()) {
return false;
}
if(day.getValue() < 1 || day.getValue() > mon_maxnum[month.getValue() - 1]) {
return false;
}
return true;
}
public boolean compareDates(DateUtil date) {
if(year.getValue() > date.year.getValue()) {
return true;
}
else {
if(month.getValue() > date.month.getValue()){
return true;
}
else {
if(day.getValue() > date.day.getValue()){
return true;
}
}
}
return false;
}
public boolean equalTwoDates(DateUtil date){
if(day.getValue() == date.day.getValue()) {
if(month.getValue() == date.month.getValue()) {
if(year.getValue() == date.year.getValue()) {
return true;
}
}
}
return false;
}
public String showDate() {
return String.valueOf(year.getValue()) + "-" + String.valueOf(month.getValue()) + "-" + String.valueOf(day.getValue());
}
public DateUtil getNextNDays(int n) {
int model;
DateUtil day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.year.yearIncrement();
if((year.isLeapYear() == true && month.getValue() <= 2) || (day1.year.isLeapYear() == true && day1.month.getValue() >= 3))
model=366;
else
model=365;
while(n>=model){
year.yearIncrement();
n=n-model;
day1.year.yearIncrement();
if((year.isLeapYear() == true && month.getValue() <= 2) || (day1.year.isLeapYear() == true && day1.month.getValue() >= 3))
model=366;
else
model=365;
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
if(month.getValue() == 2 && year.isLeapYear() == true) {
model=29;
}
else {
model=day1.day.getValue();
}
while(n>=model) {
if(month.getValue() == 12) {
year.yearIncrement();
month.resetMin();
}
else {
month.monthIncrement();;
}
n = n - model;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
if(month.getValue() == 2 && year.isLeapYear() == true) {
model = 29;
}
else {
model = day1.day.getValue();
}
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
while(n>=1) {
if(month.getValue() == 2 && year.isLeapYear() == false && day.getValue() == 29) {
month.monthIncrement();
day.setValue(2);
}
else if(month.getValue() == 2 && year.isLeapYear()== true && day.getValue() == 28) {
day.setValue(29);
}
else if(month.getValue() == 12 && day.getValue() == 31) {
year.yearIncrement();
month.resetMin();
this.setDayMin();;
}
else if(day.getValue() >= day1.day.getValue()) {
month.monthIncrement();
day.setValue(day.getValue() + 1 - day1.day.getValue());
}
else {
day.dayIncrement();
}
n=n-1;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
}
if(month.getValue() == 2 && year.isLeapYear() == false && day.getValue() == 29) {
month.monthIncrement();
this.setDayMin();;
}
return this ;
}
public DateUtil getPreviousNDays(int n) {
int model ;
DateUtil day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.year.yearReduction();
if((month.getValue() <= 3 && day1.year.isLeapYear()) || (month.getValue() >= 3 && year.isLeapYear() && n >= 366))
model = 366 ;
else
model = 365 ;
while( n >= model) {
year.yearReduction();
n = n - model;
day1.year.yearReduction();
if((month.getValue() <= 3 && day1.year.isLeapYear()) || (month.getValue() >= 3 && year.isLeapYear() && n >= 366))
model = 366 ;
else
model = 365 ;
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
if(month.getValue() == 3 && year.isLeapYear() == true) {
model = 29;
}
else {
model = day1.day.getValue();
}
while(n>=model) {
if(month.getValue() == 1) {
year.yearReduction();
month.resetMax();
}
else {
month.monthReduction();
}
n=n-model;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
if(month.getValue() == 3 && year.isLeapYear() == true) {
model = 29;
}
else {
model = day1.day.getValue();
}
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
while(n>=1) {
if(month.getValue() == 3 && year.isLeapYear() == true && day.getValue() == 1) {
month.monthReduction();;
day.setValue(29);
}
if(month.getValue() == 1 && day.getValue() == 1) {
year.yearReduction();
month.resetMax();
this.setDayMax();
}
if(day.getValue() == 1) {
month.monthReduction();
this.setDayMax();
}
else {
day.dayReduction();
}
n=n-1;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
}
return this;
}
public int getDaysofDates(DateUtil day) {
DateUtil day1, day2, day3;
int model, num = 0;
if(this.equalTwoDates(day) == true) {
return 0;
}
else {
if(this.compareDates(day) == true) {
day1 = day;
day2 = this;
}
else {
day1 = this;
day2 = day;
}
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.year.yearIncrement();
while(day2.year.getValue() - day1.year.getValue() >= 2 || (day2.year.getValue() - day1.year.getValue() == 1 && (day1.month.getValue() < day2.month.getValue() || (day1.month.getValue() == day2.month.getValue() && day1.day.getValue() <= day2.day.getValue())))) {
if((day1.year.isLeapYear() == true && (day1.month.getValue() <= 2)) || (day3.year.isLeapYear() == true && day1.month.getValue() >= 3)) {
model=366;
}
else {
model=365;
}
day1.year.yearIncrement();
num=num+model;
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.year.yearIncrement();
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();
while((day2.year.getValue() - day1.year.getValue() == 1 && (day2.month.getValue() >= 2 || (day2.month.getValue() == 1 && (day1.month.getValue() <= 11 || (day2.month.getValue() == 12 && day1.day.getValue() <= day2.day.getValue())))))||(day2.month.getValue() - day1.month.getValue() >= 2 || (day2.month.getValue() - day1.month.getValue() == 1 && (day1.month.getValue() <= day2.month.getValue())))) {
if(day1.month.getValue() == 2 && day1.year.isLeapYear() == true) {
model = 29;
}
else {
model = day3.day.getValue();
}
if(day1.month.getValue() == 12) {
day1.year.yearIncrement();
day1.month.resetMin();
}
else {
day1.month.monthIncrement();;
}
num=num+model;
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();
}
if(day1.month.getValue() == 2 && day1.year.isLeapYear() == true && day1.day.getValue() >= 30) {
day1.month.monthIncrement();
day1.day.setValue(day1.day.getValue() - 29);
}
else if(day1.year.isLeapYear() == false && day1.day.getValue() > day3.day.getValue()) {
day1.day.setValue(day1.day.getValue() - day3.day.getValue());
day1.month.monthIncrement();
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();
while(day2.year.getValue() > day1.year.getValue() || day2.month.getValue() - day1.month.getValue() == 1 || day1.day.getValue() < day2.day.getValue()) {
if(day1.month.getValue() == 2 && day1.year.isLeapYear() == false && day1.day.getValue() == 29) {
day1.month.monthIncrement();
day1.day.setValue(2);
}
else if(day1.month.getValue() == 2 && day1.year.isLeapYear() == true && day1.day.getValue() == 28) {
day1.day.setValue(29);
}
else if(day1.month.getValue() == 12 && day1.day.getValue() == 31) {
day1.year.yearIncrement();
day1.month.resetMin();
day1.setDayMin();
}
else if(day1.day.getValue() >= day3.day.getValue()) {
day1.month.monthIncrement();
day1.day.setValue(1);
}
else {
day1.day.dayIncrement();
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();;
num=num+1;
}
return num;
}

}
class Day {
private int value;
public Day() {

}
public Day(int dayValue) {
this.setValue(dayValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void dayIncrement() {
this.value = this.value + 1;
}
public void dayReduction() {
this.value = this.value - 1;
}

}
class Month {
private int value;
public Month() {

}
public Month(int monthValue) {
this.setValue(monthValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void resetMin() {
this.value = 1;
}
public void resetMax() {
this.value = 12;
}
public boolean validate() {
if(value < 1 || value > 12) {
return false;
}
else {
return true;
}
}
public void monthIncrement() {
this.value = this.value + 1;
}
public void monthReduction() {
this.value = this.value - 1;
}

}
class Year {
private 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 % 100 == 0 && value % 400 == 0)
return true;
else if(value % 100 != 0 && value % 4 == 0)
return true;
else
return false;
}
public boolean validate() {
if(value < 1820 || value > 2020) {
return false;
}
return true;
}
public void yearIncrement() {
this.value = this.value + 1;
}
public void yearReduction() {
this.value = this.value - 1;
}

}

 

再者是Source Monitor生成的图:

这里主要放DateUtil的了,毕竟别的类都大同小异。

由以上数据可以得知,我在其中用了过多的if等判断语句,导致平均复杂度过高。

在做题过程中,发现用多种类写出的代码确实是更加容易看懂和修改的。

 

关于题目集5的T7-6:

它也是日期类的迭代,只不过其中的类选择了让一个DateUtil类作为中间的控制类聚合其他除了主类的所有类。

源码如下:

 

import java.util.Scanner;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if(i == 1) {
DateUtil dateutil = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt());
i = input.nextInt();
if(!dateutil.checkInputValidity() || i < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
else {
System.out.print(dateutil.showDate() + " next " + i + " days is:");
dateutil.getNextNDays(i);
System.out.println(dateutil.showDate());
}
}
else if(i == 2) {
DateUtil dateutil = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt());
i = input.nextInt();
if(!dateutil.checkInputValidity() || i < 0) {
System.out.println("Wrong Format");
System.exit(0);
}
else {
System.out.print(dateutil.showDate() + " previous " + i + " days is:");
dateutil.getPreviousNDays(i);
System.out.println(dateutil.showDate());
}
}
else if(i == 3) {
DateUtil dateutil1 = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt()), dateutil2 = new DateUtil(input.nextInt(), input.nextInt(), input.nextInt());
if(!dateutil1.checkInputValidity() || !dateutil2.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}
else {
System.out.println("The days between " + dateutil1.showDate() + " and " + dateutil2.showDate() + " are:" + dateutil1.getDaysofDates(dateutil2));
}
}
else {
System.out.println("Wrong Format");
System.exit(0);
}

}

}
class DateUtil {
private Day day;
private Month month;
private Year year;
private int[] mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil(){

}
public DateUtil(int y, int m, int d){
day = new Day(d);
month = new Month(m);
year = new Year(y);
}
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;
}
public void setDayMin() {
day.setValue(1);
}
public void setDayMax() {

day.setValue(mon_maxnum[month.getValue() - 1]);

}
public boolean checkInputValidity() {
if(!year.validate()) {
return false;
}
if(!month.validate()) {
return false;
}
if(year.isLeapYear() && month.getValue() == 2)
if(day.getValue() < 1 || day.getValue() > 29) {
return false;
}
else {
if(day.getValue() < 1 || day.getValue() > mon_maxnum[month.getValue() - 1]) {
return false;
}
}
return true;
}
public boolean compareDates(DateUtil date) {
if(year.getValue() > date.year.getValue()) {
return true;
}
else {
if(month.getValue() > date.month.getValue()){
return true;
}
else {
if(day.getValue() > date.day.getValue()){
return true;
}
}
}
return false;
}
public boolean equalTwoDates(DateUtil date){
if(day.getValue() == date.day.getValue()) {
if(month.getValue() == date.month.getValue()) {
if(year.getValue() == date.year.getValue()) {
return true;
}
}
}
return false;
}
public String showDate() {
return String.valueOf(year.getValue()) + "-" + String.valueOf(month.getValue()) + "-" + String.valueOf(day.getValue());
}
public DateUtil getNextNDays(int n) {
int model;
DateUtil day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.year.yearIncrement();
if((year.isLeapYear() == true && month.getValue() <= 2) || (day1.year.isLeapYear() == true && day1.month.getValue() >= 3))
model=366;
else
model=365;
while(n>=model){
year.yearIncrement();
n=n-model;
day1.year.yearIncrement();
if((year.isLeapYear() == true && month.getValue() <= 2) || (day1.year.isLeapYear() == true && day1.month.getValue() >= 3))
model=366;
else
model=365;
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
if(month.getValue() == 2 && year.isLeapYear() == true) {
model=29;
}
else {
model=day1.day.getValue();
}
while(n>=model) {
if(month.getValue() == 12) {
year.yearIncrement();
month.resetMin();
}
else {
month.monthIncrement();;
}
n = n - model;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
if(month.getValue() == 2 && year.isLeapYear() == true) {
model = 29;
}
else {
model = day1.day.getValue();
}
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
while(n>=1) {
if(month.getValue() == 2 && year.isLeapYear() == false && day.getValue() == 29) {
month.monthIncrement();
day.setValue(2);
}
else if(month.getValue() == 2 && year.isLeapYear()== true && day.getValue() == 28) {
day.setValue(29);
}
else if(month.getValue() == 12 && day.getValue() == 31) {
year.yearIncrement();
month.resetMin();
this.setDayMin();;
}
else if(day.getValue() >= day1.day.getValue()) {
month.monthIncrement();
day.setValue(day.getValue() + 1 - day1.day.getValue());
}
else {
day.dayIncrement();
}
n=n-1;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.setDayMax();
}
if(month.getValue() == 2 && year.isLeapYear() == false && day.getValue() == 29) {
month.monthIncrement();
this.setDayMin();;
}
return this ;
}
public DateUtil getPreviousNDays(int n) {
int model ;
DateUtil day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.year.yearReduction();
if((month.getValue() <= 3 && day1.year.isLeapYear()) || (month.getValue() >= 3 && year.isLeapYear() && n >= 366))
model = 366 ;
else
model = 365 ;
while( n >= model) {
year.yearReduction();
n = n - model;
day1.year.yearReduction();
if((month.getValue() <= 3 && day1.year.isLeapYear()) || (month.getValue() >= 3 && year.isLeapYear() && n >= 366))
model = 366 ;
else
model = 365 ;
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
if(month.getValue() == 3 && year.isLeapYear() == true) {
model = 29;
}
else {
model = day1.day.getValue();
}
while(n>=model) {
if(month.getValue() == 1) {
year.yearReduction();
month.resetMax();
}
else {
month.monthReduction();
}
n=n-model;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
if(month.getValue() == 3 && year.isLeapYear() == true) {
model = 29;
}
else {
model = day1.day.getValue();
}
}
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
while(n>=1) {
if(month.getValue() == 3 && year.isLeapYear() == true && day.getValue() == 1) {
month.monthReduction();;
day.setValue(29);
}
if(month.getValue() == 1 && day.getValue() == 1) {
year.yearReduction();
month.resetMax();
this.setDayMax();
}
if(day.getValue() == 1) {
month.monthReduction();
this.setDayMax();
}
else {
day.dayReduction();
}
n=n-1;
day1 = new DateUtil(year.getValue(), month.getValue(), day.getValue());
day1.month.monthReduction();
if(day1.month.getValue() == 0) {
day1.year.yearReduction();
day1.month.resetMax();
}
day1.setDayMax();
}
return this;
}
public int getDaysofDates(DateUtil day) {
DateUtil day1, day2, day3;
int model, num = 0;
if(this.equalTwoDates(day) == true) {
return 0;
}
else {
if(this.compareDates(day) == true) {
day1 = day;
day2 = this;
}
else {
day1 = this;
day2 = day;
}
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.year.yearIncrement();
while(day2.year.getValue() - day1.year.getValue() >= 2 || (day2.year.getValue() - day1.year.getValue() == 1 && (day1.month.getValue() < day2.month.getValue() || (day1.month.getValue() == day2.month.getValue() && day1.day.getValue() <= day2.day.getValue())))) {
if((day1.year.isLeapYear() == true && (day1.month.getValue() <= 2)) || (day3.year.isLeapYear() == true && day1.month.getValue() >= 3)) {
model=366;
}
else {
model=365;
}
day1.year.yearIncrement();
num=num+model;
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.year.yearIncrement();
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();
while((day2.year.getValue() - day1.year.getValue() == 1 && (day2.month.getValue() >= 2 || (day2.month.getValue() == 1 && (day1.month.getValue() <= 11 || (day2.month.getValue() == 12 && day1.day.getValue() <= day2.day.getValue())))))||(day2.month.getValue() - day1.month.getValue() >= 2 || (day2.month.getValue() - day1.month.getValue() == 1 && (day1.month.getValue() <= day2.month.getValue())))) {
if(day1.month.getValue() == 2 && day1.year.isLeapYear() == true) {
model = 29;
}
else {
model = day3.day.getValue();
}
if(day1.month.getValue() == 12) {
day1.year.yearIncrement();
day1.month.resetMin();
}
else {
day1.month.monthIncrement();;
}
num=num+model;
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();
}
if(day1.month.getValue() == 2 && day1.year.isLeapYear() == true && day1.day.getValue() >= 30) {
day1.month.monthIncrement();
day1.day.setValue(day1.day.getValue() - 29);
}
else if(day1.year.isLeapYear() == false && day1.day.getValue() > day3.day.getValue()) {
day1.day.setValue(day1.day.getValue() - day3.day.getValue());
day1.month.monthIncrement();
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();
while(day2.year.getValue() > day1.year.getValue() || day2.month.getValue() - day1.month.getValue() == 1 || day1.day.getValue() < day2.day.getValue()) {
if(day1.month.getValue() == 2 && day1.year.isLeapYear() == false && day1.day.getValue() == 29) {
day1.month.monthIncrement();
day1.day.setValue(2);
}
else if(day1.month.getValue() == 2 && day1.year.isLeapYear() == true && day1.day.getValue() == 28) {
day1.day.setValue(29);
}
else if(day1.month.getValue() == 12 && day1.day.getValue() == 31) {
day1.year.yearIncrement();
day1.month.resetMin();
day1.setDayMin();
}
else if(day1.day.getValue() >= day3.day.getValue()) {
day1.month.monthIncrement();
day1.day.setValue(1);
}
else {
day1.day.dayIncrement();
}
day3 = new DateUtil(day1.year.getValue(), day1.month.getValue(), day1.day.getValue());
day3.setDayMax();;
num=num+1;
}
return num;
}

}
class Day {
private int value;
public Day() {

}
public Day(int dayValue) {
this.setValue(dayValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void dayIncrement() {
this.value = this.value + 1;
}
public void dayReduction() {
this.value = this.value - 1;
}

}
class Month {
private int value;
public Month() {

}
public Month(int monthValue) {
this.setValue(monthValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void resetMin() {
this.value = 1;
}
public void resetMax() {
this.value = 12;
}
public boolean validate() {
if(value < 1 || value > 12) {
return false;
}
else {
return true;
}
}
public void monthIncrement() {
this.value = this.value + 1;
}
public void monthReduction() {
this.value = this.value - 1;
}

}
class Year {
private 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 % 100 == 0 && value % 400 == 0)
return true;
else if(value % 100 != 0 && value % 4 == 0)
return true;
else
return false;
}
public boolean validate() {
if(value < 1820 || value > 2020) {
return false;
}
return true;
}
public void yearIncrement() {
this.value = this.value + 1;
}
public void yearReduction() {
this.value = this.value - 1;
}

}

 

还有Source Monitor生成的图,依旧是只放DateUtil类的

 

可以看出这次的DateUtil类比上道题的类更加复杂了,可见这复杂度与与之相关的类的数量是有必然关系的。

 

这两道题向我展示了同一种题以不同形式类群解决的可能性。

 

关于题目集6的T7-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)//根据序号查找一条记录

}

本次课题比菜单计价系列-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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

我并没有写到满分,除去自己懒的因素,从效率上来看,也是有大问题的,之后再聊。

关于源码:

import java.util.Scanner;
import java.util.Calendar;
public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Menu menu = new Menu();
Record record = new Record();
Order order = new Order();
Table[] table = new Table[100];
Calendar calendar = Calendar.getInstance();
int[] max_day = {31,28,31,30,31,30,31,31,30,31,30,31};
String line = input.nextLine();
String[] information = line.split(" ");
int tabletimes = -1, recordtimes, n = 0;
while(!information[0].equals("table")) {
n = 0;
if(menu.checkPrice(information[0], information[1])) {
menu.addDishtimes();
menu.addDish(information[0], Integer.parseInt(information[1]));
n = 1;
}
if(information.length == 3 && n == 1) {
menu.getDishs()[menu.getDishtimes()].setT(1);
}
line = input.nextLine();
information = line.split(" ");
}
while(true) {
if(information[0].equals("table")) {
if(information.length >= 5) {
System.out.println("wrong format");
break;
}
recordtimes = 0;
tabletimes ++;
table[tabletimes] = new Table();
String[] date1 = information[2].split("/");
String[] date2 = information[3].split("/");
if(date1[0].length() != 4 || date1[1].length() > 2 || date1[2].length() > 2 || date2[0].length() > 2 || date2[1].length() > 2 || date2[2].length() > 2 ) {
System.out.println("wrong format");
break;
}
for(n = 0; n <= tabletimes - 1; n++) {
if(Integer.parseInt(information[1]) == table[n].getNum() && (Integer.parseInt(date1[0]) == (table[n].getCalendar().get(Calendar.YEAR))) && (Integer.parseInt(date1[1]) == (table[n].getCalendar().get(Calendar.MONTH))) && (Integer.parseInt(date1[2]) == (table[n].getCalendar().get(Calendar.DATE)))) {
if(table[n].getCalendar().get(Calendar.DAY_OF_WEEK) == 6 || table[n].getCalendar().get(Calendar.DAY_OF_WEEK) == 7) {
if((Integer.parseInt(date2[0]) == (table[n].getCalendar().get(Calendar.HOUR_OF_DAY))) || ((Integer.parseInt(date2[0]) - (table[n].getCalendar().get(Calendar.HOUR_OF_DAY)) == 1) && ((Integer.parseInt(date2[1]) - (table[n].getCalendar().get(Calendar.MINUTE)) > 0) || (Integer.parseInt(date2[1]) - (table[n].getCalendar().get(Calendar.MINUTE)) == 0) && (Integer.parseInt(date2[2]) - (table[n].getCalendar().get(Calendar.SECOND)) > 0)))
|| ((table[n].getCalendar().get(Calendar.HOUR_OF_DAY) - (Integer.parseInt(date2[0])) == 1) && ((table[n].getCalendar().get(Calendar.MINUTE)) - Integer.parseInt(date2[1]) > 0) || (table[n].getCalendar().get(Calendar.MINUTE) - Integer.parseInt(date2[1]) == 0 && Integer.parseInt(date2[2]) - (table[n].getCalendar().get(Calendar.SECOND)) > 0))) {
tabletimes --;
}
}
if(table[n].getCalendar().get(Calendar.DAY_OF_WEEK) <= 5 || table[n].getCalendar().get(Calendar.DAY_OF_WEEK) >= 1) {
if((table[n].getCalendar().get(Calendar.HOUR) == 10 && table[n].getCalendar().get(Calendar.MINUTE) >= 30) || (table[n].getCalendar().get(Calendar.HOUR) >= 11 && table[n].getCalendar().get(Calendar.HOUR) <= 13) || (table[n].getCalendar().get(Calendar.HOUR) == 14 && table[n].getCalendar().get(Calendar.MINUTE) <= 30)) {
if((Integer.parseInt(date2[0]) == 10 && Integer.parseInt(date2[1]) >= 30) || (Integer.parseInt(date2[0]) >= 11 && Integer.parseInt(date2[0]) <= 13) || (Integer.parseInt(date2[0]) == 14 && Integer.parseInt(date2[1]) <= 30)){
tabletimes --;
}
}
else if((table[n].getCalendar().get(Calendar.HOUR) >= 17 && table[n].getCalendar().get(Calendar.HOUR) <= 19) || (table[n].getCalendar().get(Calendar.HOUR) == 20 && table[n].getCalendar().get(Calendar.MINUTE) <= 30)) {
if((Integer.parseInt(date2[0]) >= 17 && Integer.parseInt(date2[0]) <= 19) || (Integer.parseInt(date2[0]) == 20 && Integer.parseInt(date2[1]) <= 30))
tabletimes --;
}
}
}
}
if(information[1].charAt(0) == 0) {
System.out.println("wrong format");
break;
}
else if(!information[1].replace("-", "").matches("^[0-9]+$")) {
System.out.println("wrong format");
break;
}
else if(Integer.parseInt(information[1]) < 1 || Integer.parseInt(information[1]) > 55) {
System.out.println(Integer.parseInt(information[1]) + " table num out of range");
break;
}
else if(Integer.parseInt(information[1]) >= 1 || Integer.parseInt(information[1]) <= 55){
table[tabletimes].setNum(Integer.parseInt(information[1]));
if(date1[0].length() != 4 || Integer.parseInt(date1[1]) < 1 || (Integer.parseInt(date1[1]) > 12) || (Integer.parseInt(date1[2]) < 0 || Integer.parseInt(date1[2]) > max_day[Integer.parseInt(date1[1]) - 1])) {
System.out.println(information[1] +" date error");
}
else if((Integer.parseInt(date2[0]) < 0 || Integer.parseInt(date2[0]) > 23) || (Integer.parseInt(date2[1]) < 0 || (Integer.parseInt(date2[1]) > 59) || (Integer.parseInt(date2[2]) < 0 || Integer.parseInt(date2[2]) > 59))){
System.out.println(information[1] +" date error");
}
else if((Integer.parseInt(date1[0]) < 2022 || Integer.parseInt(date1[0]) > 2023)) {
System.out.println("not a valid time period");
}
else {
table[tabletimes].setCalendar(Integer.parseInt(date1[0]), Integer.parseInt(date1[1]), Integer.parseInt(date1[2]), Integer.parseInt(date2[0]), Integer.parseInt(date2[1]), Integer.parseInt(date2[2]));
System.out.println("table " + table[tabletimes].getNum() + ":");
}
}
line = input.nextLine();
information = line.split(" ");
while(!information[0].equals("table")) {
if(information.length == 5 && information[0].replace("-", "").matches("^[0-9]+$")) {
for(n = 0; n <= tabletimes - 1; n++) {
if(Integer.valueOf(information[0]) == table[n].getNum()) {
if(table[n].getOrder().checkNum(Integer.parseInt(information[1]))) {
if(menu.checkName(information[2])) {
record = new Record(Integer.parseInt(information[1]), menu.searchDish(information[2]), Integer.parseInt(information[3]), Integer.parseInt(information[4]));
if(record.checkPortion()) {
if(information[4].charAt(0) == 0) {
System.out.println("wrong format");
}
else if(record.checkNum()) {
table[n].getOrder().addARecord(record.getOrderNum(), record.getD(), record.getPortion(), record.getNum());
table[n].getOrder().setRecordtimes(table[n].getOrder().getRecordtimes() + 1);
}
}
}
}
}
break;
}
}
else if(information[1].equals("delete")) {
if(table[tabletimes].getOrder().delARecordByOrderNum(Integer.parseInt(information[0])) == 1) {
System.out.println("delete error");
}
if(table[tabletimes].getOrder().delARecordByOrderNum(Integer.parseInt(information[0])) == 2) {
System.out.println("deduplication " + Integer.parseInt(information[0]));
}
}
else if(information.length == 2 || information.length == 3) {
System.out.println("invalid dish");
}
else if(information.length == 4 ){
if(table[tabletimes].getOrder().getRecordtimes() == 0) {
if(menu.checkName(information[1])) {
record = new Record(Integer.parseInt(information[0]), menu.searchDish(information[1]), Integer.parseInt(information[2]), Integer.parseInt(information[3]));
if(record.checkPortion()) {
if(information[3].charAt(0) == 0) {
System.out.println("wrong format");
}
else if(record.checkNum()) {
System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + record.getPrice());
table[tabletimes].getOrder().addARecord(record.getOrderNum(), record.getD(), record.getPortion(), record.getNum());
recordtimes ++;
table[tabletimes].getOrder().setRecordtimes(recordtimes);
}
}
}
}
else {
if(table[tabletimes].getOrder().checkNum(Integer.parseInt(information[0]))){
if(menu.checkName(information[1])) {
record = new Record(Integer.parseInt(information[0]), menu.searchDish(information[1]), Integer.parseInt(information[2]), Integer.parseInt(information[3]));
if(record.checkPortion()) {
if(information[3].charAt(0) == 0) {
System.out.println("wrong format");
}
else if(record.checkNum()) {
System.out.println(record.getOrderNum() + " " + record.getD().getName() + " " + record.getPrice());
table[tabletimes].getOrder().addARecord(record.getOrderNum(), record.getD(), record.getPortion(), record.getNum());
recordtimes ++;
table[tabletimes].getOrder().setRecordtimes(recordtimes);
}
}
}
}
}
}
else {
System.out.println("wrong format");
}
line = input.nextLine();
information = line.split(" ");
if(information[0].equals("end")) {
break;
}
}
}
if(information[0].equals("end")) {
break;
}
}
for(n = 0; n <= tabletimes; n++) {
if(table[n].getNum() != 0)
System.out.println("table " + table[n].getNum() + ": " + table[n].sum() + " " + table[n].sumAfter());
}
}
}
class Menu {
private Dish[] dishs = new Dish[100];
private int dishtimes = -1;

public Menu() {
super();
// TODO Auto-generated constructor stub
}

public Menu(Dish[] dishs) {
super();
this.dishs = dishs;
}

public Dish[] getDishs() {
return dishs;
}

public void setDishs(Dish[] dishs) {
this.dishs = dishs;
}

public int getDishtimes() {
return dishtimes;
}

public void setDishtimes(int dishtimes) {
this.dishtimes = dishtimes;
}

public void addDishtimes() {
this.dishtimes ++;
}
public Dish searchDish(String dishName) {
int i;
Dish wrong = new Dish("wrong",0);
for(i = this.dishtimes; i >= 0; i--) {
if(dishName.equals(dishs[i].getName())) {
return dishs[i];
}
}
return wrong;
}

public Dish addDish(String dishName,int unit_price) {
this.dishs[this.dishtimes] = new Dish(dishName, unit_price);
return dishs[this.dishtimes];
}

public boolean checkPrice(String dishname, String unit_price) {
if(!unit_price.equalsIgnoreCase("0")) {
if(unit_price.charAt(0) == 0) {
System.out.println("wrong format");
return false;
}
}
else {
if(Integer.parseInt(unit_price) <= 0 || Integer.parseInt(unit_price) >= 300) {
System.out.println(dishname + " price out of range " + unit_price);
return false;
}
}
return true;
}

public boolean checkName(String dishname) {
int i = 0;
for(;i <= dishs.length - 1; i ++) {
if(dishs[i].getName().equals(dishname)) {
return true;
}
}
System.out.println(dishname + "does not exist");
return false;
}
}
class Dish {
private String name ="";
private int unit_price;
private int T;


public Dish() {
super();
// TODO Auto-generated constructor stub
}


public Dish(String name, int unit_price) {
super();
this.name = name;
this.unit_price = unit_price;
}


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 getT() {
return T;
}


public void setT(int t) {
T = t;
}


public int getPrice(int portion) {
switch(portion) {
case 1: return this.unit_price;
case 2: {
float price = this.unit_price * 1.5f ;
return Math.round(price);
}
case 3: return this.unit_price * 2;
}
return 0;
}
}
class Record {
private int orderNum;
private Dish d;
private int portion;
private int num;

public Record() {
super();
// TODO Auto-generated constructor stub
}

public Record(int orderNum, Dish d, int portion, int num) {
super();
this.orderNum = orderNum;
this.d = d;
this.portion = portion;
this.num = 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 int getPrice() {
return num * d.getPrice(portion);
}

public boolean checkPortion() {
if(this.d.getT() == 0 && (portion >= 1 || portion <= 3)){
return true;
}
if(this.d.getT() == 1 && (portion == 1 || portion <= 3)){
return true;
}
if(this.d.getT() >= 10) {
System.out.println("not a valid time period");
return false;
}
System.out.println(this.orderNum + " num out of range " + this.portion);
return false;
}

public boolean checkNum() {
if(this.num < 0 || this.num > 15) {
System.out.println(this.orderNum + " portion out of range " + this.num);
return false;
}
return true;
}
}
class Order {
private Record[] records = new Record[100];
private int recordtimes = 0;

public Order() {
super();
// TODO Auto-generated constructor stub
}

public Order(Record[] records, int recordtimes) {
super();
this.records = records;
this.recordtimes = recordtimes;
}

public Record[] getRecords() {
return records;
}

public void setRecords(Record[] records) {
this.records = records;
}

public int getRecordtimes() {
return recordtimes;
}

public void setRecordtimes(int recordtimes) {
this.recordtimes = recordtimes;
}

public int getTotalPrice() {
int i, sum = 0;
for(i = 0; i < records.length; i++) {
sum = sum + records[i].getPrice();
}
return sum;
}

public void addARecord(int orderNum, Dish dish, int portion,int num) {
records[recordtimes] = new Record(orderNum, dish, portion, num);
recordtimes ++;
}

public int delARecordByOrderNum(int orderNum) {
int i;
for(i = 0; i <= records.length - 1; i ++) {
if(orderNum == records[i].getOrderNum()) {
if(records[i].getNum() == 0) {
return 2;
}
else {
records[i].setNum(0);
return 1;
}
}
}
return 3;
}

public Record RecordByNum(int orderNum) {
int i;
for(i = 0; i <= records.length - 1; i ++) {
if(orderNum == records[i].getOrderNum()) {
return records[i];
}
}
System.out.println("wrong format");
return new Record();
}

public boolean checkNum(int orderNum) {
int i;
for(i = 0; i <= this.recordtimes - 1; i++) {
if(orderNum <= this.records[i].getOrderNum()) {
System.out.println("record serial number sequence error");
return false;
}
}
return true;
}
}
class Table {

private int num;
private Calendar calendar =Calendar.getInstance();
private Order order = new Order();
public Table() {
// TODO Auto-generated constructor stub
}

public Table(int num, Calendar calendar, Order order) {
super();
this.num = num;
this.calendar = calendar;
this.order = order;
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public Calendar getCalendar() {
return calendar;
}

public void setCalendar(int year, int month, int day, int hour, int minute, int second) {
this.calendar.set(Calendar.YEAR, year);
this.calendar.set(Calendar.MONTH, month);
this.calendar.set(Calendar.DATE, day);
this.calendar.set(Calendar.HOUR_OF_DAY, hour);
this.calendar.set(Calendar.MINUTE, minute);
this.calendar.set(Calendar.SECOND, second);
}

public Order getOrder() {
return order;
}

public void setOrder(Order order) {
this.order = order;
}

public boolean checkDate() {
if(this.calendar.get(Calendar.DAY_OF_WEEK) >= 1 && this.calendar.get(Calendar.DAY_OF_WEEK) <= 5) {
if((this.calendar.get(Calendar.HOUR) == 10 && this.calendar.get(Calendar.MINUTE) >= 30) || (this.calendar.get(Calendar.HOUR) >= 11 && this.calendar.get(Calendar.HOUR) <= 13) || (this.calendar.get(Calendar.HOUR) == 14 && this.calendar.get(Calendar.MINUTE) <= 30)) {
return true;
}
else if((this.calendar.get(Calendar.HOUR) >= 17 && this.calendar.get(Calendar.HOUR) <= 19) || (this.calendar.get(Calendar.HOUR) == 20 && this.calendar.get(Calendar.MINUTE) <= 30)) {
return true;
}
}
else if(this.calendar.get(Calendar.DAY_OF_WEEK) >= 6 && this.calendar.get(Calendar.DAY_OF_WEEK) <= 7) {
if((this.calendar.get(Calendar.HOUR) ==9 && this.calendar.get(Calendar.MINUTE) >= 30) && (this.calendar.get(Calendar.HOUR) >= 10 && this.calendar.get(Calendar.HOUR) <= 20) && (this.calendar.get(Calendar.HOUR) == 21 && this.calendar.get(Calendar.MINUTE) <= 30)) {
return true;
}
}
return false;
}

public int sum() {
int i,sum = 0;
for(i = 0; i <= this.order.getRecordtimes() - 1; i ++) {
sum = sum +order.getRecords()[i].getPrice();
}
return sum;
}

public int sumAfter() {
int i,sum = 0;
for(i = 0; i <= this.order.getRecordtimes() - 1; i ++) {
if(this.order.getRecords()[i].getD().getT() == 0) {
if(this.calendar.get(Calendar.DAY_OF_WEEK) >= 1 && this.calendar.get(Calendar.DAY_OF_WEEK) <= 5) {
if((this.calendar.get(Calendar.HOUR) == 10 && this.calendar.get(Calendar.MINUTE) >= 30) || (this.calendar.get(Calendar.HOUR) >= 11 && this.calendar.get(Calendar.HOUR) <= 13) || (this.calendar.get(Calendar.HOUR) == 14 && this.calendar.get(Calendar.MINUTE) <= 30)) {
float price = this.order.getRecords()[i].getPrice() * 0.6f ;
sum = sum + Math.round(price);
}
else if((this.calendar.get(Calendar.HOUR) >= 17 && this.calendar.get(Calendar.HOUR) <= 19) || (this.calendar.get(Calendar.HOUR) == 20 && this.calendar.get(Calendar.MINUTE) <= 30)) {
float price = this.order.getRecords()[i].getPrice() * 0.8f ;
sum = sum + Math.round(price);
}
}
else {
sum = sum +order.getRecords()[i].getPrice();
}
}
else {
if(this.calendar.get(Calendar.DAY_OF_WEEK) >= 1 && this.calendar.get(Calendar.DAY_OF_WEEK) <= 5) {
float price = this.order.getRecords()[i].getPrice() * 0.7f ;
sum = sum + Math.round(price);
}
else {
sum = sum + order.getRecords()[i].getPrice();
}
}
}
return sum;
}

}

 

再看类图:

自己在原题目的基础上加了一个table的类,但是感觉不太够,一大堆情况没考虑完全。总之就是很糟糕,上课讲的知识也没用到多少,下次绝对不这样了。

 

踩坑心得:

这几次作业日期类的由于是迭代所以没啥好说的,之前做对了这次也不会有大问题。

主要是点餐题

我只是搭了个框架,里面的内容也来不及细细填充,所以硬要说有坑的话,我是直接掉了下去,没什么心得的。

还是给个例子吧:

 因为没考虑到菜全都不通过的情况,所以答案少了table的最终价格。

 改进建议:

1,实在想不出就借鉴一下别人的方法,会了就行。

2,多用上老师上课讲的各种模式,多用。

3,争分夺秒写作业。

4,合理提高效率。

 

总结:

通过这三次作业我认识到了个人力量的有限,明白了有时为了完成任务借助他人力量的重要性。再者就是得努力完成自己分内的事,别荒废了时间。

希望老师保持现在的风格,再严一点也是没事的。