Java pta作业4-6次总结

tongkai / 2023-05-03 / 原文

一、前言

(1)pta第四次作业题目列表如下:

7-1 菜单计价程序-3 

7-2 有重复的数据

7-3 去掉重复的数据 

7-4 单词统计与排序 

7-5 面向对象编程(封装性)

7-6 GPS测绘中度分秒转换

7-7 判断两个日期的先后,计算间隔天数、周数 

总结:这次的作业题目量适中,题目难度除第一题的菜单外基本难度不是很大,很多题目前面的作业都有涉及到。主要涉及的内容就是让我们学习到一些类中方法的使用,就比如第七题中让我们了解ChronoUnit类中的一些方法等等。

(2)pta第五次作业题目列表如下:

7-1 正则表达式训练-QQ号校验

7-2 字符串训练-字符排序

7-3 正则表达式训练-验证码校验

7-4 正则表达式训练-学号校验

7-5 日期问题面向对象设计(聚合一)

7-6 日期问题面向对象设计(聚合二)

总结:这次的作业题目量适中,题目难度也一般,所涉及的新的知识点就是正则表达式和聚和的使用,也是将前面做过的日期类改成聚合的形式,总体来说难度不高。

(3)pta第六次作业题目列表如下:

7-1 菜单计价程序-4

总结:这次作业的题目量很少,就一道题,但是是第四次作业菜单的升级版,本来前面的都还没写出来,所以这次的作业就感觉有点难以解决了,也是写了好久也没写出来。

二、设计与分析

日期问题面向对象设计(聚合一)

类图如下:

 

  这题是属于之前日期类的修改,将其改成如类图一样的聚和的形式,由月聚合年,天聚合月,然后DateUtill聚合天来完成之前对于日期类的操作。其余的方法都和之前差不多,但是在我提交之后,计算两天之间相差的天数还是有一些测试点没过。我想到了之前的这个方法有一点小瑕疵,就是计算天数的循环会因为前面的日期是闰年但是月份在2月后会产生的误差。修改代码如下:

 

public int getDaysofDates(DateUtil date) {
        int days = 0;
        if (equalTwoDates(date)) {
            return days;
        }else {
            if (compareDates(date)) {
                if (this.day.month.value == date.day.month.value) {
                    days = days + this.day.value - date.day.value;
                }else {
                    while (this.day.month.value != date.day.month.value) {
                        if(date.day.month.year.isLeapYear()) {
                            date.day.mon_maxNum[1] = 29;
                        }else {
                            date.day.mon_maxNum[1] = 28;
                        }
                        days += date.day.mon_maxNum[date.day.month.value-1] - date.day.value +1;
                        date.day.month.monthIncrement();
                        date.day.resetMin();
                        if (date.day.month.value == 13) {
                            date.day.month.year.yearIncrement();
                            date.day.month.resetMin();
                        }
                    }
                    days = days + this.day.value - date.day.value;
                }
                while (this.day.month.year.value != date.day.month.year.value) {
                    if (date.day.month.year.isLeapYear()) {
                        if ((this.day.month.value == 2 && this.day.value < 29) || this.day.month.value == 1) {
                            days += 366;
                        }else {
                            days += 365;
                        }
                    }else {
                        date.day.month.year.yearIncrement();
                        if (date.day.month.year.isLeapYear()) {
                            if ((this.day.month.value == 2 && this.day.value == 29) || this.day.month.value > 2) {
                                days +=366;
                            }else {
                                days += 365;
                            }
                        }else {
                            days += 365;
                        }
                        date.day.month.year.yearReduction();
                    }
                    date.day.month.year.yearIncrement();
                }
            }else {
                if (this.day.month.value == date.day.month.value) {
                    days = date.day.value - this.day.value;
                }else {
                    while (date.day.month.value != this.day.month.value) {
                        if(this.day.month.year.isLeapYear()) {
                            this.day.mon_maxNum[1] = 29;
                        }else {
                            this.day.mon_maxNum[1] = 28;
                        }
                        days += this.day.mon_maxNum[this.day.month.value-1] - this.day.value +1;
                        this.day.month.monthIncrement();
                        this.day.resetMin();
                        if (this.day.month.value == 13) {
                            this.day.month.year.yearIncrement();
                            this.day.month.resetMin();
                        }
                    }
                    days = days + date.day.value - this.day.value;
                }
                while (this.day.month.year.value != date.day.month.year.value) {
                    if (this.day.month.year.isLeapYear()) {
                        if ((date.day.month.value == 2 && date.day.value < 29) || date.day.month.value == 1) {
                            days += 366;
                        }else {
                            days += 365;
                        }
                    }else {
                        this.day.month.year.yearIncrement();
                        if (this.day.month.year.isLeapYear()) {
                            if ((date.day.month.value == 2 && date.day.value == 29) || date.day.month.value > 2) {
                                days +=366;
                            }else {
                                days += 365;
                            }
                        }else {
                            days += 365;
                        }
                        this.day.month.year.yearReduction();
                    }
                    this.day.month.year.yearIncrement();
                }
            }
        }
        return days;
    }
}

 

虽然复杂了很多但是所以情况都考虑进去了。整个源代码如下:

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(day, month, year);

            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.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(day, month, year);

            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, year);
            DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

            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 Day getDay() {
        return day;
    }

    public void setDay(Day day) {
        this.day = day;
    }
    public boolean checkInputValidity() {
        if (this.day.month.validate() && this.day.month.year.validate() && this.day.validate()) {
            return true;
        }else {
            return false;
        }
    }
    public boolean compareDates(DateUtil date) {
        if (this.day.month.year.value > date.day.month.year.value) {
            return true;
        }else if (this.day.month.year.value == date.day.month.year.value) {
            if (this.day.month.value > date.day.month.value) {
                return true;
            }else if (this.day.month.value == date.day.month.value) {
                if (this.day.value > date.day.value) {
                    return true;
                }else if (this.day.value < date.day.value) {
                    return false;
                }
            }else {
                return false;
            }
        }else {
            return false;
        }
        return false;
    }
    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;
        }else {
            return false;
        }
    }
    public String showDate() {
        return this.day.month.year.value + "-" +
                this.day.month.value + "-" +
                this.day.value;
    }
    public DateUtil getNextNDays(int n) {
        if (this.day.month.year.isLeapYear()) {
            this.day.mon_maxNum[1] = 29;
        }else {
            this.day.mon_maxNum[1] = 28;
        }
        if(n > this.day.mon_maxNum[this.day.month.value-1] - this.day.value) {
            n -= (this.day.mon_maxNum[this.day.month.value-1] - this.day.value);
            this.day.month.monthIncrement();
            while (n > this.day.mon_maxNum[this.day.month.value-1]) {
                if(this.day.month.year.isLeapYear()) {
                        this.day.mon_maxNum[1]=29;
                }else {
                    this.day.mon_maxNum[1]=28;
                }
                n = n - this.day.mon_maxNum[this.day.month.value-1];
                this.day.month.monthIncrement();
                if(this.day.month.value == 13) {
                    this.day.month.resetMin();
                    this.day.month.year.yearIncrement();
                }
            }
            this.day.value = n;
        }else {
                this.day.value += n;
            }
        return this;
    }
    public DateUtil getPreviousNDays(int n) {
        this.day.value -= n;
        while (this.day.value <= 0) {
            if(this.day.month.year.isLeapYear()) {
                this.day.mon_maxNum[1] = 29;
            }else {
                this.day.mon_maxNum[1] = 28;
            }
            this.day.month.monthReduction();
            if(this.day.month.value == 0) {
                this.day.month.resetMax();
                this.day.month.year.yearReduction();
            }
            this.day.value += this.day.mon_maxNum[this.day.month.value-1];
        }
        return this;
    }
    public int getDaysofDates(DateUtil date) {
        int days = 0;
        if (equalTwoDates(date)) {
            return days;
        }else {
            if (compareDates(date)) {
                if (this.day.month.value == date.day.month.value) {
                    days = days + this.day.value - date.day.value;
                }else {
                    while (this.day.month.value != date.day.month.value) {
                        if(date.day.month.year.isLeapYear()) {
                            date.day.mon_maxNum[1] = 29;
                        }else {
                            date.day.mon_maxNum[1] = 28;
                        }
                        days += date.day.mon_maxNum[date.day.month.value-1] - date.day.value +1;
                        date.day.month.monthIncrement();
                        date.day.resetMin();
                        if (date.day.month.value == 13) {
                            date.day.month.year.yearIncrement();
                            date.day.month.resetMin();
                        }
                    }
                    days = days + this.day.value - date.day.value;
                }
                while (this.day.month.year.value != date.day.month.year.value) {
                    if (date.day.month.year.isLeapYear()) {
                        if ((this.day.month.value == 2 && this.day.value < 29) || this.day.month.value == 1) {
                            days += 366;
                        }else {
                            days += 365;
                        }
                    }else {
                        date.day.month.year.yearIncrement();
                        if (date.day.month.year.isLeapYear()) {
                            if ((this.day.month.value == 2 && this.day.value == 29) || this.day.month.value > 2) {
                                days +=366;
                            }else {
                                days += 365;
                            }
                        }else {
                            days += 365;
                        }
                        date.day.month.year.yearReduction();
                    }
                    date.day.month.year.yearIncrement();
                }
            }else {
                if (this.day.month.value == date.day.month.value) {
                    days = date.day.value - this.day.value;
                }else {
                    while (date.day.month.value != this.day.month.value) {
                        if(this.day.month.year.isLeapYear()) {
                            this.day.mon_maxNum[1] = 29;
                        }else {
                            this.day.mon_maxNum[1] = 28;
                        }
                        days += this.day.mon_maxNum[this.day.month.value-1] - this.day.value +1;
                        this.day.month.monthIncrement();
                        this.day.resetMin();
                        if (this.day.month.value == 13) {
                            this.day.month.year.yearIncrement();
                            this.day.month.resetMin();
                        }
                    }
                    days = days + date.day.value - this.day.value;
                }
                while (this.day.month.year.value != date.day.month.year.value) {
                    if (this.day.month.year.isLeapYear()) {
                        if ((date.day.month.value == 2 && date.day.value < 29) || date.day.month.value == 1) {
                            days += 366;
                        }else {
                            days += 365;
                        }
                    }else {
                        this.day.month.year.yearIncrement();
                        if (this.day.month.year.isLeapYear()) {
                            if ((date.day.month.value == 2 && date.day.value == 29) || date.day.month.value > 2) {
                                days +=366;
                            }else {
                                days += 365;
                            }
                        }else {
                            days += 365;
                        }
                        this.day.month.year.yearReduction();
                    }
                    this.day.month.year.yearIncrement();
                }
            }
        }
        return days;
    }
}
class Year {
    int value;
    public Year() {

    }
    public Year(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
    public boolean isLeapYear() {
        if ((this.value % 4 == 0 && this.value % 100 != 0) || this.value % 400 == 0) {
            return true;
        }else {
            return false;
        }
    }
    public boolean validate() {
        if (this.value >= 1900 && this.value <= 2050) {
            return true;
        }else {
            return false;
        }
    }
    public void yearIncrement() {
        this.value ++;
    }
    public void yearReduction() {
        this.value --;
    }
}
class Month {
    int value;
    Year year = new Year();
    public Month() {

    }
    public Month(int yearValue,int monthValue) {
        this.value = monthValue;
        this.year.value = yearValue;
    }

    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 resetMin() {
        this.value = 1;
    }
    public void resetMax() {
        this.value = 12;
    }
    public boolean validate() {
        if (this.value >= 1 && this.value <= 12) {
            return true;
        }else {
            return false;
        }
    }
    public void monthIncrement() {
        this.value ++;
    }
    public void monthReduction() {
        this.value --;
    }
}
class Day {
    int value;
    Month month = new Month();
    int[] mon_maxNum = {31,28,31,30,31,30,31,31,30,31,30,31};
    public Day() {

    }
    public Day(int yearValue,int monthValue,int dayValue) {
        this.value = dayValue;
        this.month.value = monthValue;
        this.month.year.value = yearValue;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Month getMonth() {
        return month;
    }

    public void setMonth(Month month) {
        this.month = month;
    }
    public void resetMin() {
        this.value = 1;
    }
    public void resetMax() {
        this.value = mon_maxNum[this.month.value-1];
    }
    public boolean validate() {
        if (this.month.year.isLeapYear()) {
            this.mon_maxNum[1] = 29;
        }else {
            this.mon_maxNum[1] = 28;
        }
        if (this.value >= 1 && this.value <= mon_maxNum[this.month.value-1]) {
            return true;
        }else {
            return false;
        }
    }
    public void dayIncrement() {
        this.value ++;
    }
    public void dayReduction() {
        this.value --;
    }
}

日期问题面向对象设计(聚合二)

 

 类图如下:

 这题也是改成聚合模式,但是和上一题不一样的就是将年月日聚合为有关DateUtill类。DateUtill类里包含了所有的算法,操作都在里面进行。源代码如下:

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());

            DateUtil1 date = new DateUtil1(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().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " 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());

            DateUtil1 date = new DateUtil1(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.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
            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());

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

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() +
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }

        } else{
            System.out.println("Wrong Format");
            System.exit(0);
        }

    }
}
class Day1 {
    int value;
    public Day1() {

    }
    public Day1(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
    public void dayIncrement() {
        this.value ++;
    }
    public void dayReduction() {
        this.value --;
    }
}
class Month1 {
    int value;
    public Month1() {

    }
    public Month1(int value) {
        this.value = value;
    }

    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 void monthIncrement() {
        this.value ++;
    }
    public void monthReduction() {
        this.value --;
    }
    public boolean validate() {
        if (this.value >= 1 && this.value <= 12) {
            return true;
        }else {
            return false;
        }
    }
}
class DateUtil1 {
    Day1 day = new Day1();
    Month1 month = new Month1();
    Year1 year =new Year1();
    int[] mon_maxNum = {31,28,31,30,31,30,31,31,30,31,30,31};

    public DateUtil1() {

    }
    public DateUtil1(int y,int m,int d) {
        this.day.value = d;
        this.month.value = m;
        this.year.value = y;
    }

    public Day1 getDay() {
        return day;
    }

    public void setDay(Day1 day) {
        this.day = day;
    }

    public Month1 getMonth() {
        return month;
    }

    public void setMonth(Month1 month) {
        this.month = month;
    }

    public Year1 getYear() {
        return year;
    }

    public void setYear(Year1 year) {
        this.year = year;
    }

    public boolean checkInputValidity() {
        if (this.month.validate() && this.year.validate()) {
            if (this.year.isLeapYear()) {
                this.mon_maxNum[1] = 29;
            }else {
                this.mon_maxNum[1] = 28;
            }
            if (this.day.value >= 1 && this.day.value <= this.mon_maxNum[this.month.value-1]) {
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }
    public boolean compareDates(DateUtil1 date) {
        if (this.year.value > date.year.value) {
            return true;
        }else if (this.year.value == date.year.value) {
            if (this.month.value > date.month.value) {
                return true;
            }else if (this.month.value == date.month.value) {
                if (this.day.value > date.day.value) {
                    return true;
                }else if (this.day.value < date.day.value) {
                    return false;
                }
            }else {
                return false;
            }
        }else {
            return false;
        }
        return false;
    }
    public boolean equalTwoDates(DateUtil1 date) {
        if (this.year.value == date.year.value
                && this.month.value == date.month.value
                && this.day.value == date.day.value) {
            return true;
        }else {
            return false;
        }
    }
    public String showDate() {
        return this.year.value + "-" +
                this.month.value + "-" +
                this.day.value;
    }
    public DateUtil1 getNextNDays(int n) {
        if (this.year.isLeapYear()) {
            this.mon_maxNum[1] = 29;
        }else {
            this.mon_maxNum[1] = 28;
        }
        if(n > this.mon_maxNum[this.month.value-1] - this.day.value) {
            n -= (this.mon_maxNum[this.month.value-1] - this.day.value);
            this.month.monthIncrement();
            while (n > this.mon_maxNum[this.month.value-1]) {
                if(this.year.isLeapYear()) {
                    this.mon_maxNum[1]=29;
                }else {
                    this.mon_maxNum[1]=28;
                }
                n = n - this.mon_maxNum[this.month.value-1];
                this.month.monthIncrement();
                if(this.month.value == 13) {
                    this.month.resetMin();
                    this.year.yearIncrement();
                }
            }
            this.day.value = n;
        }else {
            this.day.value += n;
        }
        return this;
    }
    public DateUtil1 getPreviousNDays(int n) {
        this.day.value -= n;
        while (this.day.value <= 0) {
            if(this.year.isLeapYear()) {
                this.mon_maxNum[1] = 29;
            }else {
                this.mon_maxNum[1] = 28;
            }
            this.month.monthReduction();
            if(this.month.value == 0) {
                this.month.resetMax();
                this.year.yearReduction();
            }
            this.day.value += this.mon_maxNum[this.month.value-1];
        }
        return this;
    }
    public int getDaysofDates(DateUtil1 date) {
        int days = 0;
        if (equalTwoDates(date)) {
            return days;
        }else {
            if (compareDates(date)) {
                if (this.month.value == date.month.value) {
                    days = this.day.value - date.day.value;
                }else {
                    while (this.month.value != date.month.value) {
                        if(date.year.isLeapYear()) {
                            date.mon_maxNum[1] = 29;
                        }else {
                            date.mon_maxNum[1] = 28;
                        }
                        days += date.mon_maxNum[date.month.value-1] - date.day.value +1;
                        date.month.monthIncrement();
                        date.day.value = 1;
                        if (date.month.value == 13) {
                            date.year.yearIncrement();
                            date.month.resetMin();
                        }
                    }
                    days = days + this.day.value - date.day.value;
                }
                for (int i = date.year.value;i < this.year.value;i ++) {
                    date.year.yearIncrement();
                    if(date.year.isLeapYear()) {
                        days += 366;
                    }else {
                        days += 365;
                    }
                }
                if ((this.month.value == 2 && this.day.value < 29) || this.month.value == 1) {
                    days --;
                }
            }else {
                if (this.month.value == date.month.value) {
                    days = date.day.value - this.day.value;
                }else {
                    while (date.month.value != this.month.value) {
                        if(this.year.isLeapYear()) {
                            this.mon_maxNum[1] = 29;
                        }else {
                            this.mon_maxNum[1] = 28;
                        }
                        days += this.mon_maxNum[this.month.value-1] - this.day.value +1;
                        this.month.monthIncrement();
                        this.day.value = 1;
                        if (this.month.value == 13) {
                            this.year.yearIncrement();
                            this.month.resetMin();
                        }
                    }
                    days = days + date.day.value - this.day.value;
                }
                for (int i = this.year.value;i < date.year.value;i ++) {
                    this.year.yearIncrement();
                    if(this.year.isLeapYear()) {
                        days += 366;
                    }else {
                        days += 365;
                    }
                }
                if ((date.month.value == 2 && date.day.value < 29) || date.month.value == 1) {
                    days --;
                }
            }
        }
        return days;
    }
}
class Year1 {
    int value;
    public Year1() {

    }
    public Year1(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
    public boolean isLeapYear() {
        if ((this.value % 4 == 0 && this.value % 100 != 0) || this.value % 400 == 0) {
            return true;
        }else {
            return false;
        }
    }
    public boolean validate() {
        if (this.value >= 1820 && this.value <= 2020) {
            return true;
        }else {
            return false;
        }
    }
    public void yearIncrement() {
        this.value ++;
    }
    public void yearReduction() {
        this.value --;
    }
}

对于这两题的心得就是:对于给出类图的题目,可以先摸懂类图后再下手,这样自己的思路也会变得很清晰。

菜单计价程序-3

类图如下:

 其实这个题目并没有写出来,整个代码是有问题,当时还没搞懂如何判断输出。

菜单计价程序-4

类图如下:

 要想做对这道题,那肯定是先要将上一题写对。这道题也只是在上一题的基础上加上了一些判断错误的样例,所以基本的方法是没有变的。由于要判断时间以及日期的关系,所以我创建了一个Table类用来判断时间以及折扣,代码如下:

class Table   {
    int num;
    Calendar calendar = Calendar.getInstance();
    LocalTime localTime = LocalTime.now();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ;
    Order order;
    private int orderDish = 0;

    public int getOrderDish() {
        return orderDish;
    }

    public void setOrderDish(int orderDish) {
        this.orderDish = orderDish;
    }

    public Table() {

    }
    public Table (int num) {
        this.num = num;
    }
    public Table(Order order,int num,Calendar calendar,LocalTime localTime) {
        this.order = order;
        this.num = num;
        this.calendar = calendar;
        this.localTime = localTime;
    }
    public boolean isOpeningHours() {
        if (this.calendar.get(Calendar.DAY_OF_WEEK) == 3 ||
        this.calendar.get(Calendar.DAY_OF_WEEK) == 4) {
            if (localTime.getHour() >9 &&
                    localTime.getHour() <= 21) {
                return true;
            } else if ((localTime.getHour() == 9 &&
                    localTime.getMinute() >= 30) ||
                    (localTime.getHour() == 21 &&
                            localTime.getMinute() < 30)) {
                return true;
            } else if (localTime.getHour() == 21 &&
                    localTime.getMinute() == 30 &&
                    localTime.getSecond() == 0) {
                return true;
            }else {
                return false;
            }
        }else {
            if ((localTime.getHour() > 10 &&
                    localTime.getHour() <= 14) ||
                    (localTime.getHour() >= 17 &&
                            localTime.getHour() <= 20)) {
                return true;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() >= 30) ||
                    (localTime.getHour() == 14 &&
                            localTime.getMinute() < 30) ||
                    (localTime.getHour() == 20 &&
                            localTime.getSecond() < 30)) {
                return true;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() == 30 &&
                    localTime.getSecond() == 0) ||
                    (localTime.getHour() == 20 &&
                            localTime.getMinute() == 30 &&
                            localTime.getSecond() == 0)) {
                return true;
            }else {
                return false;
            }
        }
    }
    public double discount() {
        if (this.calendar.get(Calendar.DAY_OF_WEEK) == 3 ||
                this.calendar.get(Calendar.DAY_OF_WEEK) == 4) {
            return 1;
        }else {
            if (localTime.getHour() >= 17 &&
                    localTime.getHour() <= 20) {
                return 0.8;
            } else if (localTime.getHour() == 20 &&
                            localTime.getSecond() < 30) {
                return 0.8;
            } else if (localTime.getHour() == 20 &&
                            localTime.getMinute() == 30 &&
                            localTime.getSecond() == 0) {
                return 0.8;
            } else if ((localTime.getHour() > 10 &&
                    localTime.getHour() <= 14)) {
                return 0.6;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() >= 30) ||
                    (localTime.getHour() == 14 &&
                            localTime.getMinute() < 30)) {
                return 0.6;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() == 30 &&
                    localTime.getSecond() == 0)) {
                return 0.6;
            }
        }
        return 0;
    }
    public double discountNew() {
        if (this.calendar.get(Calendar.DAY_OF_WEEK) == 3 ||
                this.calendar.get(Calendar.DAY_OF_WEEK) == 4) {
            return 1;
        }else {
            return 0.7;
        }
    }
    public void getTotalPrice() {
        double sum = 0;
        if (isOpeningHours()) {
            for (Record record:this.order.getRecords()
                 ) {
                int price = record.getPrice();
                if (record.getD().specialDish.equals("T")) {
                    if (!record.isDelete()) {
                        sum += Math.round(price) * discountNew();
                    }
                }else {
                    if (!record.isDelete()) {
                        sum += Math.round(price) * discount();
                    }
                }
            }
            System.out.println("table " + this.num + ": " +
                    order.getTotalPrice() + " " +
                    Math.round(sum));
        }else {
            System.out.println("table " + this.num + " out of opening hours");
        }
    }
}

并且在这道题中还增加了一个特色菜的内容,为此我就给Dish这个类增加了一个属性specialDish用“T”来表示特色菜“F”则表示普通菜。由与在Menu类以及在Order类中都要使用到对象数组,为此我用到了ArrayList,毕竟它可以用来增减以及迭代。这样基本的都设计好了,现在只要开始判断错误了,但是对于题目给出的17种错误我也就写对了几种,就比如对于一些超出范围的数据,我都是用正则表达式来判断的。整个源代码如下:

import java.time.LocalTime;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Menu menu = new Menu();
        Scanner input = new Scanner(System.in);
        Order[] orders = new Order[10000];
        Table[] tables = new Table[10000];
        int i = 0;
        String nextLine = input.nextLine();
        while (!nextLine.equals("end")) {

            String[] lineArray = nextLine.split(" ");

            if ("table".equals(lineArray[0])) {
                orders[i] = new Order(menu);
                int[] delete = new int[10];
                int del = 0;
                int orderDish = 0;
                if (lineArray.length != 4) {
                    System.out.println("wrong format");
                    nextLine = input.nextLine();
                    while (!nextLine.equals("end")) {
                        String[] lineArray1 = nextLine.split(" ");
                        if ("table".equals(lineArray1[0])) {
                            break;
                        }else {
                            nextLine = input.nextLine();
                        }
                    }
                    continue;
                }
                if (!lineArray[0].equals("table")) {
                    System.out.println("wrong format");
                    nextLine = input.nextLine();
                    while (!nextLine.equals("end")) {
                        String[] lineArray1 = nextLine.split(" ");
                        if ("table".equals(lineArray1[0])) {
                            break;
                        }else {
                            nextLine = input.nextLine();
                        }
                    }
                    continue;
                }
                if (!(lineArray[1].matches("[1-9]|[1-4][0-9]|5[0-5]"))) {
                    System.out.println("wrong format");
                    nextLine = input.nextLine();
                    while (!nextLine.equals("end")) {
                        String[] lineArray1 = nextLine.split(" ");
                        if ("table".equals(lineArray1[0])) {
                            break;
                        }else {
                            nextLine = input.nextLine();
                        }
                    }
                    continue;
                }
                int tableNum= Integer.parseInt(lineArray[1]);
                String[] date = lineArray[2].split("/");
                String[] time = lineArray[3].split("/");
                if (!(date[0].matches("[0-9]{4}") &&
                        date[1].matches("[0-9]{1,2}") &&
                        date[2].matches("[0-9]{1,2}") &&
                        time[0].matches("[0-9]{1,2}") &&
                        time[1].matches("[0-9]{1,2}") &&
                        time[2].matches("[0-9]{1,2}"))) {
                    System.out.println("wrong format");
                    nextLine = input.nextLine();
                    while (!nextLine.equals("end")) {
                        String[] lineArray1 = nextLine.split(" ");
                        if ("table".equals(lineArray1[0])) {
                            break;
                        }else {
                            nextLine = input.nextLine();
                        }
                    }
                    continue;
                }
                if (!(date[1].matches("[1-9]|1[0-2]|0[1-9]") &&
                        time[0].matches("[0-9]|0[0-9]|1[0-9]|2[0-4]") &&
                        time[1].matches("[0-9]|0[0-9]|[1-5][0-9]") &&
                        time[2].matches("[0-9]|0[0-9]|[1-5][0-9]"))) {
                    System.out.println(tableNum + " date error");
                    nextLine = input.nextLine();
                    while (!nextLine.equals("end")) {
                        String[] lineArray1 = nextLine.split(" ");
                        if ("table".equals(lineArray1[0])) {
                            break;
                        }else {
                            nextLine = input.nextLine();
                        }
                    }
                    continue;
                }
                Calendar calendar= Calendar.getInstance();
                calendar.set(Integer.parseInt(date[0]),
                        Integer.parseInt(date[1]),
                        Integer.parseInt(date[2]));;
                LocalTime localTime = LocalTime.of(Integer.parseInt(time[0]),
                        Integer.parseInt(time[1]),
                        Integer.parseInt(time[2]));

                if (!isDate(calendar)) {
                    System.out.println("not a valid time period");
                    nextLine = input.nextLine();
                    while (!nextLine.equals("end")) {
                        String[] lineArray1 = nextLine.split(" ");
                        if ("table".equals(lineArray1[0])) {
                            break;
                        }else {
                            nextLine = input.nextLine();
                        }
                    }
                    continue;
                }
                System.out.println("table " + tableNum + ": ");
                nextLine = input.nextLine();
                while (!nextLine.equals("end")) {
                    String[] lineArray1 = nextLine.split(" ");
                    if ("table".equals(lineArray1[0])) {
                        break;
                    } else if ("table".equals(lineArray1[0] + lineArray1[1])) {
                        System.out.println("wrong format");
                    } else if (lineArray1.length == 5) {
                        Dish dish = new Dish();
                        dish = menu.searthDish(lineArray1[2]);
                        orderDish = dish.getPrice(Integer.parseInt(lineArray1[3])) *
                                Integer.parseInt(lineArray1[4]);
                        System.out.println(lineArray1[1] + " table " + tableNum +
                                " pay for table " + lineArray1[0] + " " + orderDish);
                    } else if (lineArray1.length == 4) {

                        int orderNum;
                        String dishName = lineArray1[1];
                        int portion;
                        int num;
                        if (lineArray1[0].matches("[1-9]|[1-9][0-9]")) {
                            orderNum = Integer.parseInt(lineArray1[0]);
                        }else {
                            orderNum = 0;
                        }
                        if (lineArray1[2].matches("[1-9]")) {
                            portion = Integer.parseInt(lineArray1[2]);
                        }else {
                            portion = 0;
                        }
                        if (lineArray1[3].matches("[1-9]|[1-9][0-9]")) {
                            num = Integer.parseInt(lineArray1[3]);
                        }else {
                            num = 0;
                        }

                        orders[i].addARecord(orderNum,dishName,portion,num);
                    } else if ("delete".equals(lineArray1[1])) {
                        int flag = 0;
                        delete[del] = Integer.parseInt(lineArray1[0]);
                        for (int j = del;j >= 1;j --) {
                            if (delete[j] == delete[j-1]) {
                                System.out.println("deduplication " + delete[del]);
                                flag = 1;
                            }
                        }
                        if (flag == 0) {
                            orders[i].delARecordByOrderNum(Integer.parseInt(lineArray1[0]));
                        }
                        del ++;
                    } else if (lineArray1.length == 2) {
                        System.out.println("invalid dish");
                    }else {
                        System.out.println("wrong format");
                    }
                    nextLine = input.nextLine();
                }
                tables[i] = new Table(orders[i],tableNum,calendar,localTime);
                tables[i].setOrderDish(orderDish);

                i ++;
            } else if (lineArray.length == 2) {
                if (lineArray[1].matches("[1-9]|[1-2][0-9]{1,2}")) {
                    menu.addDish(lineArray[0], Integer.parseInt(lineArray[1]));
                } else if (lineArray[1].matches("0|[3-9][0-9]{1,2}")) {
                    System.out.println(lineArray[0] + " price out of range " +
                            Integer.parseInt(lineArray[1]));
                }else {
                    System.out.println("wrong format");
                }
                nextLine = input.nextLine();
            } else if (lineArray.length == 3) {
                if (lineArray[1].matches("[1-9]|[1-2][0-9]{2}")) {
                    menu.addDish(lineArray[0],Integer.parseInt(lineArray[1]),lineArray[2]);

                } else if (lineArray[1].matches("0|[3-9][0-9]{2}")) {
                    System.out.println(lineArray[0] + " price out of range " +
                            Integer.parseInt(lineArray[1]));
                }else {
                    System.out.println("wrong format");
                }
                nextLine = input.nextLine();
            }else {
                System.out.println("wrong format");
                nextLine =input.nextLine();
            }
        }
        for (int j = 0;j < i;j ++) {
            tables[j].getTotalPrice();
        }
    }
    public static boolean isDate(Calendar calendar) {
        int[] maxNum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        if ((calendar.get(Calendar.YEAR) == 2022 ||
        calendar.get(Calendar.YEAR) == 2023) &&
                (calendar.get(Calendar.MONTH) >=1 &&
                        calendar.get(Calendar.MONTH) <= 12) &&
                (calendar.get(Calendar.DATE) >= 1 &&
                        calendar.get(Calendar.DATE) <= maxNum[calendar.get(Calendar.MONTH)])) {
            return true;
        }else {
            return false;
        }
    }
}

class Table   {
    int num;
    Calendar calendar = Calendar.getInstance();
    LocalTime localTime = LocalTime.now();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ;
    Order order;
    private int orderDish = 0;

    public int getOrderDish() {
        return orderDish;
    }

    public void setOrderDish(int orderDish) {
        this.orderDish = orderDish;
    }

    public Table() {

    }
    public Table (int num) {
        this.num = num;
    }
    public Table(Order order,int num,Calendar calendar,LocalTime localTime) {
        this.order = order;
        this.num = num;
        this.calendar = calendar;
        this.localTime = localTime;
    }
    public boolean isOpeningHours() {
        if (this.calendar.get(Calendar.DAY_OF_WEEK) == 3 ||
        this.calendar.get(Calendar.DAY_OF_WEEK) == 4) {
            if (localTime.getHour() >9 &&
                    localTime.getHour() <= 21) {
                return true;
            } else if ((localTime.getHour() == 9 &&
                    localTime.getMinute() >= 30) ||
                    (localTime.getHour() == 21 &&
                            localTime.getMinute() < 30)) {
                return true;
            } else if (localTime.getHour() == 21 &&
                    localTime.getMinute() == 30 &&
                    localTime.getSecond() == 0) {
                return true;
            }else {
                return false;
            }
        }else {
            if ((localTime.getHour() > 10 &&
                    localTime.getHour() <= 14) ||
                    (localTime.getHour() >= 17 &&
                            localTime.getHour() <= 20)) {
                return true;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() >= 30) ||
                    (localTime.getHour() == 14 &&
                            localTime.getMinute() < 30) ||
                    (localTime.getHour() == 20 &&
                            localTime.getSecond() < 30)) {
                return true;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() == 30 &&
                    localTime.getSecond() == 0) ||
                    (localTime.getHour() == 20 &&
                            localTime.getMinute() == 30 &&
                            localTime.getSecond() == 0)) {
                return true;
            }else {
                return false;
            }
        }
    }
    public double discount() {
        if (this.calendar.get(Calendar.DAY_OF_WEEK) == 3 ||
                this.calendar.get(Calendar.DAY_OF_WEEK) == 4) {
            return 1;
        }else {
            if (localTime.getHour() >= 17 &&
                    localTime.getHour() <= 20) {
                return 0.8;
            } else if (localTime.getHour() == 20 &&
                            localTime.getSecond() < 30) {
                return 0.8;
            } else if (localTime.getHour() == 20 &&
                            localTime.getMinute() == 30 &&
                            localTime.getSecond() == 0) {
                return 0.8;
            } else if ((localTime.getHour() > 10 &&
                    localTime.getHour() <= 14)) {
                return 0.6;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() >= 30) ||
                    (localTime.getHour() == 14 &&
                            localTime.getMinute() < 30)) {
                return 0.6;
            } else if ((localTime.getHour() == 10 &&
                    localTime.getMinute() == 30 &&
                    localTime.getSecond() == 0)) {
                return 0.6;
            }
        }
        return 0;
    }
    public double discountNew() {
        if (this.calendar.get(Calendar.DAY_OF_WEEK) == 3 ||
                this.calendar.get(Calendar.DAY_OF_WEEK) == 4) {
            return 1;
        }else {
            return 0.7;
        }
    }
    public void getTotalPrice() {
        double sum = 0;
        if (isOpeningHours()) {
            for (Record record:this.order.getRecords()
                 ) {
                int price = record.getPrice();
                if (record.getD().specialDish.equals("T")) {
                    if (!record.isDelete()) {
                        sum += Math.round(price) * discountNew();
                    }
                }else {
                    if (!record.isDelete()) {
                        sum += Math.round(price) * discount();
                    }
                }
            }
            System.out.println("table " + this.num + ": " +
                    order.getTotalPrice() + " " +
                    Math.round(sum));
        }else {
            System.out.println("table " + this.num + " out of opening hours");
        }
    }
}
class Menu {
    private List<Dish> dishes = new ArrayList<>();//菜品数组,保存所有菜品信息

    public List<Dish> getDishes() {
        return dishes;
    }

    Dish searthDish(String dishName) {
        for (Dish dish : dishes) {
            if (dish.getDishName().equals(dishName)) {
                return dish;
            }
        }
        return null;
    }

    //添加一道菜品信息
    Dish addDish(String dishName, int unit_price) {
        for (Dish dish : dishes) {
            if (dish.getDishName().equals(dishName)) {
                dish.setUnit_price(unit_price);
                return dish;
            }
        }
        Dish dish = new Dish(dishName, unit_price);
        dishes.add(dish);
        return dish;
    }
    Dish addDish(String dishName,int unit_price,String orderDish) {
        for (Dish dish : dishes) {
            if (dish.getDishName().equals(dishName)) {
                dish.setUnit_price(unit_price);
                return dish;
            }
        }
        Dish dish = new Dish(dishName,unit_price,orderDish);
        dishes.add(dish);
        return dish;
    }
}

class Dish {
    String dishName;//菜品名称
    int unit_price; //单价
    String specialDish;

    public String getDishName() {
        return dishName;
    }
    public int getUnit_price() {
        return unit_price;
    }
    public void setDishName(String dishName) {
        this.dishName = dishName;
    }

    public void setUnit_price(int unit_price) {
        this.unit_price = unit_price;
    }

    public Dish(String name, int unit_price) {
        this.dishName = name;
        this.unit_price = unit_price;
        this.specialDish = "F";
    }

    public Dish(String dishName, int unit_price, String specialDish) {
        this.dishName = dishName;
        this.unit_price = unit_price;
        this.specialDish = specialDish;
    }

    public Dish() {
    }

    //计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    int getPrice(int portion) {
        float botSum[] = {1, 1.5f, 2};
        return Math.round((unit_price * botSum[portion - 1]));
    }
}

class Record {
    private int numOrder;//序号\
    private Dish d;//菜品\
    private int portion;//份额(1/2/3代表小/中/大份)\
    private int num;
    private boolean isDelete = false;

    public boolean isNotFound() {
        return notFound;
    }

    public void setNotFound(boolean notFound) {
        this.notFound = notFound;
    }

    private boolean notFound = false;



    public Record(int orderNum, Dish d, int portion, int num) {
        this.numOrder = orderNum;
        this.d = d;
        this.portion = portion;
        this.num = num;
    }
    public Record(Dish d, int portion) {
        this.d = d;
        this.portion = portion;
    }

    //计价,计算本条记录的价格
    int getPrice() {
        return d.getPrice(portion) * this.num;
    }

    public void setNumOrder(int numOrder) {
        this.numOrder = numOrder;
    }
    public int getNumOrder() {
        return numOrder;
    }

    public Dish getD() {
        return d;
    }

    public void setD(Dish d) {
        this.d = d;
    }


    public void setPortion(int portion) {
        this.portion = portion;
    }

    public int getPortion() {
        return portion;
    }


    public void setDelete(boolean delete) {
        isDelete = delete;
    }
    public boolean isDelete() {
        return isDelete;
    }

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

class Order {
    private Menu menu;
    private List<Record> records = new ArrayList<>();//保存订单上每一道的记录
    public Order(Menu menu) {
        this.menu = menu;
    }

    public List<Record> getRecords() {
        return records;
    }

    public Menu getMenu() {
        return menu;
    }

    //计算订单的总价
    int getTotalPrice() {
        int sum = 0;
        for (Record record : records) {
            int price = record.getPrice();
            if (!record.isDelete()) {
                sum = sum + price;
            }
        }
        return sum;
    }
    //添加一条菜品信息到订单中
    Record addARecord(int orderNum, String dishName, int portion, int num) {
        if (orderNum == 0) {
            System.out.println("wrong format");
            return null;
        }
        Dish dish = menu.searthDish(dishName);
        if (dish == null) {
            System.out.println(dishName + " does not exist");
            return null;
        }
        if (portion == 0) {
            System.out.println("wrong format");
            return null;
        }
        if (dish.specialDish.equals("T")) {
            if (portion != 1 && portion != 2 && portion != 3) {
                System.out.println(orderNum + " portion out of range " + portion);
                return null;
            }
        } else if (dish.specialDish.equals("F")) {
            if (portion != 1 && portion !=2 && portion != 3) {
                System.out.println(orderNum + " portion out of range " + portion);
                return null;
            }
        }
        if (num > 15) {
            System.out.println(orderNum + " num out of range " + num);
            return null;
        } else if (num == 0) {
            System.out.println("wrong format");
            return null;
        }
        Record record = new Record(orderNum, dish, portion, num);

        for (int i = 0;i < records.size();i ++) {
            if (records.get(i).getNumOrder() >= record.getNumOrder()) {
                System.out.println("record serial number sequence error");
                return null;
            }
        }
        records.add(record);
        int price = record.getPrice();
        System.out.println(record.getNumOrder() + " " + record.getD().getDishName() + " " + price);
        return record;
    }
    public boolean delARecordByOrderNum(int orderNum) {
        for (Record record : records) {
            if (!record.isNotFound() && !record.isDelete() && record.getNumOrder() == orderNum) {
                record.setDelete(true);
                return true;
            }
        }
        System.out.println("delete error");
        return false;
    }
}

心得:对于这两道题,我感觉就是要多学点Java里的一些类的用法,就比如题目中有需要判断是否为周末的要求,那我们自己写那肯定是非常难的,但是Calendar里就有这样的方法。

三、踩坑新得

这个阶段做作业以来我踩的一个坑就是第五次作业日期类的那个聚合一的最大年份的测试点老是过不去,我去测了一下也始终是“Wrong Format”的输出,那肯定是判断的时候出了问题,看了一下主函数,原来是输入参数时有问题。我主函数写的是:

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

 

但我DateUtill中带参的构造方法写的是:

public DateUtil(int d,int m,int y) {
        this.day.value = d;
        this.day.month.value = m;
        this.day.month.year.value = y;
    }

这确实让我踩了一个大坑,我一开始一直不知道哪里错了。虽然这个问题非常容易解决,但就是很容易犯这个错误,所以写代码也要细心。

四、改进建议

  对于菜单系列的这两道题,我一直觉得自己做得太烂了,但由于时间缘故,所以就只能这么写了。因为对于菜单计价-4中题目给出的很多错误我都只是在主函数完成的,这样就显得我的主函数的算法太多而且很繁杂。而且对于案例的输出我是直接采用了对输入处理之后直接输出的模式,我感觉如果用最近学的MVC模式的化话会更好些。

 

 

五、总结

  在这一阶段的学习中,我是真正开始感受到java的难,毕竟最近java的难度也是上来了。但是这个阶段也让我学到了很多东西,就比如类与类之间的四种关系:关联,依赖,聚集,继承。相应的pta中也也有相应的练习,与此同时,也发现了自己很多的不足:自己并不是什么都会了这一阶段有两次pta作业也都没有圆满完成,这也让我意思到自己还有很多的不足。特别是在类与类之间耦合性的这一知识点,一直是一知半解,所以在之后的学习中也要在这方面下功夫。总之,学得越多越能发现自己的不足,继续加油吧!!!