oop第二次博客作业

发布时间 2023-06-29 23:08:44作者: 人世宇、歆蒂

前言:

第五次题目集知识点涉及:

正则表达式、字符串合法性校验、类的封装与设计、类的无参构造、类的有参构造、类的继承、重写父类。此次题目及需要完成的内容和功能较多,比较复杂,第一题难度偏大。

第四次题目集知识点涉及:

类的设计与封装、类的无参构造、类的有参构造、正则表达式、排序算法(冒泡排序、插入排序、选择排序)。此次题目一共有四题,其中第二道需要用到正则表达式来判断关键字的出现次数,需要掌握正则表达式的使用。正则表达式、排序算法、类的封装与设计、类的继承、类的无参构造、类的有参构造、类的多态。其中前四题主要是对正则表达式的考察,难度并不大。后两题需要设计的内容较多,难度适中。

期中考试题目:

主要还是考了面向对象、封装、继承、多类与接口的有关题目,还有比较接口出了一道题目,难度不是太大,但是还是最后没有做全对。

设计与分析:

类图:

 

 

 

第一道题的源码:

import java.util.Scanner;

 

class Day{

   int value;

   Month month;

   int mon_maxnum[] = {31,28,31,30,31,30,31,31,30,31,30,31};

  

   Day(){

      //默认构造方法

   }

  

   Day(int yearValue, int monthValue, int dayValue){

      //带参构造方法

      this.month = new Month(yearValue, monthValue);

      this.value = dayValue;

   }

  

   public int getValue() {

      return value;

   }

   public void setValue(int value) {

      this.value = value;

   }

  

   public Month getMonth() {

      return month;

   }

  

   public void setMonth(Month value) {

      this.month = value;

   }

  

   public void resetMin() {

      //日期复位(1)

      value = 1;

   }

  

   public void resetMax() {

      //日期设为该月最大值

      value = mon_maxnum[month.getValue()-1];

   }

  

   public boolean validate() {

      //校验数据合法性

      if(this.getMonth().getYear().isLeapYear())

          mon_maxnum[1]=29;

        if(value>=1&&value<=mon_maxnum[month.getValue()-1])

            return true;

        else

            return false;

 

   }

  

   public void dayIncrement() {

      //日期增1

      value++;

   }

  

   public void dayReduction() {

      //日期减1

      value--;

   }

}

 

class Month{

   int value;

   Year year;

  

   Month(){

     

   }

  

   Month(int yearValue,int monthValue){

      this.year = new Year(yearValue);

      this.value = monthValue;

   }

  

   public int getValue() {

      return value;

   }

  

   public void setValue(int value) {

      this.value = value;

   }

  

   public Year getYear() {

      return year;

   }

  

   public void setYear(Year year) {

      this.year = year;

   }

  

   public void resetMin() {

      //月份复位1

      value = 1;

   }

  

   public void resetMax() {

      //月份设置为12

      value = 12;

   }

  

   public boolean validate() {

      //校验数据合法性

      if(value>=1&&value<=12)

            return true;

        else

            return false;

   }

  

   public void monthIncrement() {

      //月份增1

      value++;

     

   }

  

   public void monthReduction() {

      //月份减1

      value--;

   }

}

 

class Year{

   int value;

  

   Year(){

     

   }

  

   Year(int value){

      this.value = value;

   }

  

   public int getValue() {

      return value;

   }

  

   public void setValue(int value) {

      this.value = value;

   }

  

   public boolean isLeapYear() {

      //判断是否为闰年

       if((value%4==0&&value%100!=0)||value%400==0)

               return true;

           else

               return false;

   }

   public boolean validate() {

      //校验数据合法性

      if(value<=2050&&value>=1900)

            return true;

        else

            return false;

     

   }

   public void yearIncrement() {

      //年份增1

      value++;

   }

  

   public void yearReduction() {

      //年份减一

      value--;

   }

}//class Year

 

class DateUtil {

   Day day;

   DateUtil(){

     

   }

  

   DateUtil(int d, int m, int y){

       this.day=new Day(d,m,y);

   }

  

   public Day getDay() {

      //day setter

      return day;

   }

  

   public void setDay(Day d) {

      this.day = d;

   }

  

   public boolean checkInputValidity() {

      //校验数据合法性

   if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())//检验年月日

            return true;

        else

            return false;

 

   }

  

   public boolean compareDates(DateUtil date) {

      //比较两个日期的大小

   if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())//比较年

            return false;

        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())//年相等,月后者月更大

            return false;

        else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())

            return false;

        else

            return true;

      //传入的日期和这个类中的日期比较,如果类的日期大,返回false

 

   }

  

   public boolean equalTwoDates(DateUtil date) {

      //判定两个日期是否相等

       if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())

               return true;

           else

               return false;

 

   }

  

   public String showDate() {

      //日期格式化

      return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();

   }

  

   public int syts(DateUtil d){

       //计算该年的剩余天数

        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

        int b=0;

        int i;

        for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){

            b=b+a[i];

        }

        b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();

        if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)//闰年

            b++;

        return b;

    }

 

 

   public DateUtil getNextNDays(int n) {

      //求下n天

      int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

        int y=0,m=0,d=0;

        int i,j;

        int b=syts(this);//该年剩余天数

        if(b>n){//该年剩余天数大于n

            y=this.getDay().getMonth().getYear().getValue();

            if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年

                a[2]=29;

            }

            int e=a[this.getDay().getMonth().getValue()];//该月的天数

            e=e-this.getDay().getValue();//本月剩余的天数

            if(e>=n){//如果n天后在本月

                m=this.getDay().getMonth().getValue();

                d=n+this.getDay().getValue();

            }

            else{//如果n天后不在本月

                n=n-e;

                m=this.getDay().getMonth().getValue()+1;

                i=m;

                while(n-a[i]>0&&i<=12){//找到月

                    n=n-a[i];

                    m++;

                    i++;

                }

                d=n;//找到天

            }

        }

        else{//该年剩余天数小于n

            n=n-b;

            y=this.getDay().getMonth().getYear().getValue()+1;

            int c=365;//平年天数

            if(new Year(y).isLeapYear()){//闰年天数

                c++;

            }

            while(n-c>0){//找到年

                n=n-c;

                y++;

                c=365;

                if(new Year(y).isLeapYear())

                    c++;

            }

            i=1;

            while(n-a[i]>0&&i<=12){//找到月

                n=n-a[i];

                i++;

            }

            m=i;

            d=n;//找到天

        }

        return new DateUtil(y, m, d);

 

   }

   public DateUtil getPreviousNDays(int n) {

      //求前n天

      int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

        int y=0,m=0,d=0;

        int i,b;

        b=365-syts(this);//该日期所在年份已经过的天数

        if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年

           b++;

        }

        if (b>n){//如果前n天在该年

            y=this.getDay().getMonth().getYear().getValue();

            int e=this.getDay().getValue();//本月已经过的天数

            if(e>n){//如果前n天在本月

                m=this.getDay().getMonth().getValue();

                d=e-n;

            }

            else{//如果前n天不在本月

                n=n-e;

                m=this.getDay().getMonth().getValue()-1;

                i=m;

                while(n-a[i]>0&&i>=0){//找到月

                    n=n-a[i];

                    m--;

                    i--;

                }

                d=a[i]-n;//找到天

                if(new Year(y).isLeapYear()&&m==2){

                    d++;

                }

            }

        }

        else{//如果前n天不在该年

            n=n-b;

            y=this.getDay().getMonth().getYear().getValue()-1;

            int f=365;

            if(new Year(y).isLeapYear()){

                f++;

            }

            while(n-f>0){//找到年

                n=n-f;

                y--;

                f=365;

                if(new Year(y).isLeapYear())

                    f++;

            }

            i=12;

            while(n-a[i]>0&&i>=0){//找到月

                n=n-a[i];

                i--;

            }

            m=i;

            d=a[i]-n;//找到天

            if(new Year(f).isLeapYear()&&m==2){

                d++;

            }

        }

        return new DateUtil(y, m, d);

 

   }

  

   public int getDaysofDates(DateUtil date) {

      //求两个日期之间的天数

      DateUtil b1=this;

        DateUtil b2=date;

        if(this.equalTwoDates(date)){//如果两天的日期相等

            return 0;

        }

        else if(!this.compareDates(date)){//如果日期大小不对

            b1=date;

            b2=this;

        }

        int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

        int i,j,ts=0;

        for(i=b1.getDay().getMonth().getYear().getValue()+1;i<b2.getDay().getMonth().getYear().getValue();i++){//两个日期的年数之和

            ts=ts+365;

            if(new Year(i).isLeapYear())

                ts++;

        }

        if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()==b2.getDay().getMonth().getValue()){//年份相同,月份相同,日不同

            ts=b2.getDay().getValue()-b1.getDay().getValue();

        }

        else if(b1.getDay().getMonth().getYear().getValue()==b2.getDay().getMonth().getYear().getValue()&&b1.getDay().getMonth().getValue()!=b2.getDay().getMonth().getValue()){//年份相同,月份不同

            if(b1.getDay().getMonth().getYear().isLeapYear())//是闰年

                a[2]=29;

            ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期该月剩余的天数

            ts=ts+b2.getDay().getValue();//大日期的天数

            for(j=b1.getDay().getMonth().getValue()+1;j<=b2.getDay().getMonth().getValue()-1;j++)//月份天数和

                ts+=a[j];

        }

        else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){//年份不同

            ts=ts+a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数

            ts=ts+b2.getDay().getValue();//大日期在该月已经过的天数

            for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++)//小日期在该年剩余的天数

                ts=ts+a[j];

            for(j=b2.getDay().getMonth().getValue()-1;j>0;j--)//大日期在该年已经过的天数

                ts=ts+a[j];

            if(b1.getDay().getMonth().getYear().isLeapYear()&&b1.getDay().getMonth().getValue()<=2)//如果小日期该年为闰年且该天在1月或2月

                ts++;

            if(b2.getDay().getMonth().getYear().isLeapYear()&&b2.getDay().getMonth().getValue()>2)//如果大日期该年为闰年且该天在1月或2月后

                ts++;

        }

        return ts;

    }

 

  

}//class DateUtil

 

public class Main {

 

   public static void main(String[] args) {

      // TODO Auto-generated method stub

      Scanner x=new Scanner(System.in);

        int year=0,month=0,day=0,a,b;

        a=x.nextInt();//输入判断类型

        year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日

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

        if(a==1){//求下n天

            b=x.nextInt();//输入n

            if(!c.checkInputValidity()||b<0){//如果数据不合法

                System.out.println("Wrong Format");

                System.exit(0);

            }

            else

                System.out.println(c.getNextNDays(b).showDate());

        }

        else if(a==2){

            b=x.nextInt();//输入n

            if(!c.checkInputValidity()||b<0){//如果数据不合法

                System.out.println("Wrong Format");

                System.exit(0);

            }

            else

                System.out.println(c.getPreviousNDays(b).showDate());

        }

        else if(a==3){

            int y1,m1,d1;

            y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日

            DateUtil d=new DateUtil(y1,m1,d1);

            if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法

                System.out.println("Wrong Format");

                System.exit(0);

            }

            else

                System.out.println(c.getDaysofDates(d));

        }

        else

            System.out.println("Wrong Format");

 

   }

 

}

代码分析:

代码中的主要类包括Menu(菜单类)、Dish(菜品类)、Order(订单类)、Record(订单记录类)和Table(桌子类)等。

菜单类(Menu)用于保存餐厅提供的所有菜品信息,包括菜品名称、单价、是否为特殊菜等。菜单类提供了根据菜品名称查找菜品、判断菜品是否为特殊菜等功能。

菜品类(Dish)用于保存菜品的具体信息,包括菜品名称、单价、是否为特殊菜等。菜品类提供了计算菜品价格的方法。

订单类(Order)用于保存用户点的所有菜品信息,包括菜品名称、份数、份额、口味程度等。订单类提供了计算订单总价、添加菜品信息、删除菜品信息等功能。

订单记录类(Record)用于保存订单上的一道菜品记录,包括菜品、份数、份额、口味程度等。

桌子类(Table)用于保存每个桌子的订单信息,包括桌子序号、日期、时间等。桌子类提供了判断是否有重复桌号、判断是否为同一天、同一时段的订单等功能。

UD类用于对订单进行排序,按照用户名称进行排序。

Main类为程序的入口,实现了用户输入和输出的功能,并调用其他类的方法来实现相应的功能。

设计这个实验的过程:

在设计代码结构时,首先考虑了菜单类(Menu)和菜品类(Dish)。菜单类用于保存所有菜品的信息,包括菜品名称、单价和是否为特殊菜等。菜品类则用于表示每一道菜的具体信息,包括菜品名称、单价等。这两个类的设计使得菜单和菜品之间可以进行关联和查询,方便后续的订单处理。

接下来,设计了订单类(Order)和订单记录类(Record)。订单类用于表示用户点的所有菜品信息,包括菜品名称、份数、份额、口味程度等。订单记录类则用于保存订单上的一道菜品记录,包括菜品、份数、份额、口味程度等。通过这两个类的设计,可以方便地管理订单和菜品的关系,并进行订单的计算和处理。

最后,设计了桌子类(Table)来管理每个桌子的订单信息。桌子类保存了桌子的序号、日期、时间等信息,并提供了一些方法来判断订单的合法性和进行订单的排序等操作。

在代码的实现过程中,使用了适当的数据结构和算法来实现功能,如数组、循环和条件判断等。同时,为了保证代码的可读性和可维护性,采用了合理的命名和注释,并尽量遵循面向对象的设计原则。

第二次pta作业:

源码:

import java.util.Scanner;

import java.util.regex.Pattern;

import java.time.LocalDate;

import java.time.LocalTime;

import java.time.temporal.ChronoUnit;

 

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        Menu menu = new Menu();

        Order order = new Order();

        Table table = new Table();

        for (int i = 0; i < table.order.length; i++) {

            table.order[i] = new Order();

            for (int j = 0; j < order.records.length; j++) {

                table.order[i].records[j] = new Record();

                table.order[i].s[j] = new SpecialDish();

            }

        }

        for (int i = 0; i < menu.dishes.length; i++) {

            menu.dishes[i] = new Dish();

            menu.dishes[i].name = " ";

        }

        int  tablei = 0;

        int tableFlag = 0;

        int j = 0; //j用来看订单的序号

        int stringFlag = 0;

        int combineFlag = 0; //判断是否要合并桌

        while (true) {

            String s= sc.nextLine();

            if (s.equals("end")) {

                break;

            }

            String str[] = s.split(" ");

 

 

            //菜单的输入、订单的输入和删除序号

            if (str.length == 2 || str.length == 4) {

 

                //菜单的输入

                if (str.length==4) {

 

                    //此时是订单的输入

                    if (isInteger(str[0])) {

                        if (tableFlag == 1) {

                            j = 0;

                            tableFlag = 0;

                        }

                        if (tablei - 1 > -1) {

                            if(menu.searchDish(str[1]) !=null){

                                table.order[tablei - 1].records[j].d = menu.searchDish(str[1]);

                                table.order[tablei - 1].addARecord(Integer.parseInt(str[0]), str[1], Integer.parseInt(str[2]), Integer.parseInt(str[3]));

                                System.out.println(str[0] + " " + str[1] + " " + table.order[tablei - 1].records[j].getPrice());

                            }

                            else{

                                System.out.println(str[1] + " does not exist");      //记录错误菜品名

                                continue;

                            }

                        }

                        j++;

                    }

 

                    //特殊菜

                    if (s.matches("(\\S+)( )(川菜|晋菜|浙菜)( )([^0])(\\d*)( )(T?)")) {

                        menu.addSpecial(true);

                        menu.addType(str[1]);

                        menu.addDish(str[0], Integer.parseInt(str[2]));

                    }

                } else if (str[0].length() != 1) {

                    menu.addDish(str[0], Integer.parseInt(str[1]));

                }

 

                //删除

                else {

                    if (tablei - 1 < 0) {

                        continue;

                    }

                    if (menu.findSpecial(str[0], table.order[tablei - 1])) {

                        SpecialDish s1 = table.order[tablei - 1].findSpecialDish(Integer.parseInt(str[0]));

                        table.order[tablei - 1].preNumber(Integer.parseInt(str[0]), s1);

                        int recordNumber = table.order[tablei - 1].findRecordNumber(Integer.parseInt(str[0]));

                        table.order[tablei - 1].deleteTaste(menu, table.order[tablei - 1].records[recordNumber].d.name, table.order[tablei - 1].records[recordNumber].degree, table.order[tablei - 1].records[recordNumber].count);

                        int specialNumber = table.order[tablei - 1].findNumber(Integer.parseInt(str[0]));

                        table.order[tablei - 1].s[specialNumber].ifDelete = true;

                    } else {

                        table.order[tablei - 1].delNumber(Integer.parseInt(str[0]));

                    }

                }

            }

 

            //订单的输入(给自己点菜, 并且此时只能是特殊菜)

            else if (str.length == 5) {

 

                //订单的输入

                if (isInteger(str[0])) {

                    if (stringFlag == 1) {

                        continue;

                    }

 

                    if (stringFlag == 1) {

                        continue;

                    }

                    if (menu.searchDish(str[1]) != null) {

                        if (tableFlag == 1) {

                            j = 0;

                            tableFlag = 0;

                        }

                        if (tablei - 1 > -1) {

                            if (table.order[tablei - 1].wheatherOrder(menu, str[1], Integer.parseInt(str[2]))) {

                                table.order[tablei - 1].s[j].addDish(menu, str[1], str[3], str[4]);

                                table.order[tablei - 1].s[j].num = Integer.parseInt(str[0]);

                                System.out.println(str[0] + " " + str[1] + " " + table.order[tablei - 1].s[j].searchPrice(str[1]));

                                table.order[tablei - 1].records[j].d = menu.searchDish(str[1]);

                                table.order[tablei - 1].addARecord(Integer.parseInt(str[0]), str[1], Integer.parseInt(str[3]), Integer.parseInt(str[4]));

                                table.order[tablei - 1].addwei(menu, str[1], Integer.parseInt(str[2]), table.order[tablei - 1].records[j].count);

                                table.order[tablei - 1].records[j].degree = Integer.parseInt(str[2]);

                                table.order[tablei - 1].pretendToDelete(Integer.parseInt(str[0]));

                                j++;

                            } else {

                                table.order[tablei - 1].printTasteOutOf(menu, str[1], Integer.parseInt(str[2]));

                            }

                        }

                    }

                    else {

                        System.out.println(str[1] + " does not exist");      //记录错误菜品名

                        continue;

                    }

                }

            }

            else if (str.length == 6) {

                if (!isInteger(str[0])) {

                    System.out.println("wrong format");

                }

                //代点菜存在

                if (menu.searchDish(str[2]) != null) {

                    if (tableFlag == 1) {

                        j = 0;

                        tableFlag = 0;

                    }

                    if (table.num[tablei - 1] != Integer.parseInt(str[0]) && table.ifExistTableNum(Integer.parseInt(str[0]))) {

                        if (menu.searchSpecial(str[2])) {

                            if (tablei - 1 > -1) {

                                table.order[tablei - 1].s[j].addDish(menu, str[2], str[4], str[5]);

                                table.order[tablei - 1].s[j].num = Integer.parseInt(str[1]);

                                System.out.println(str[1] + " table " + table.num[tablei - 1] + " pay for table " + str[0] + " " + Math.round(Integer.parseInt(str[5]) * menu.searchDish(str[2]).getPrice(Integer.parseInt(str[4]))));

                                table.order[tablei - 1].records[j].d = menu.searchDish(str[2]);

                                table.order[tablei - 1].addARecord(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[4]), Integer.parseInt(str[5]));

                                table.order[tablei - 1].pretendToDelete(Integer.parseInt(str[1]));

                                int number = table.searchOrder(Integer.parseInt(str[0]));

                                if (number != -1) {

                                    Dish d = menu.searchDish(str[2]);

                                    if (d.type.equals("川菜")) {

                                        table.order[number].spicyCount += Integer.parseInt(str[5]);

                                    } else if (d.type.equals("晋菜")) {

                                        table.order[number].acidCount += Integer.parseInt(str[5]);

                                    } else if (d.type.equals("浙菜")) {

                                        table.order[number].sweetCount += Integer.parseInt(str[5]);

                                    }

                                    table.order[number].addwei(menu, str[2], Integer.parseInt(str[3]), Integer.parseInt(str[5]));

                                }

                                j++;

                            }

                        } else {

                            System.out.println(str[1] + " table " + table.num[tablei - 1] + " pay for table " + str[0] + " " + Math.round(Integer.parseInt(str[4]) * menu.searchDish(str[2]).getPrice(Integer.parseInt(str[3]))));

                            table.order[tablei - 1].records[j].d = menu.searchDish(str[2]);

                            j++;

                            table.order[tablei - 1].addARecord(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Integer.parseInt(str[4]));

                        }

                    } else {

                        System.out.println("Table number :" + str[0] + " does not exist");

                    }

                }

 

                //代点菜不存在

                else {

                    System.out.println(str[2] + " does not exist");

                    continue;

                }

            }

            //输入桌号

            else if (str.length == 7) {

                if (str[0].equals("table")) {

                    if (!table.ifLegal(s)) {

                        System.out.println("wrong format");

                        stringFlag = 1;

                        continue;

                    }

                    table.num[tablei] = Integer.parseInt(str[1]);

                    table.date[tablei] = str[5];

                    table.time[tablei] = str[6];

                    table.order[tablei].workFlag = 0;

                    if (!table.isDate(tablei)) {

                        System.out.println(str[1] + " date error");

                        stringFlag = 1;

                        continue;

                    }

                    if (!table.judgeDiscount(tablei)) {

                        System.out.println("wrong format");

                        stringFlag = 1;

                        continue;

                    }

                    if (table.order[tablei].workFlag != 0) {

                        System.out.println("table " + table.num[tablei] + " out of opening hours");

                        stringFlag = 1;

                        continue;

                    }

                    table.order[tablei].owner = str[3];

                    table.order[tablei].phoneNumber = str[4];

                    stringFlag = 0;

                    table.specialDiscount(tablei);

                    if (table.ifSameOwner(table.order[tablei].owner, tablei)) {

                        combineFlag = 1;

                    }

                    System.out.println("table " + table.num[tablei] + ": ");

                    table.order[tablei].tableNum = Integer.parseInt(str[1]);

                    tablei++;

                    tableFlag = 1;

                } else {

                    System.out.println("wrong format");

                }

            } else {

                System.out.println("wrong format");

                stringFlag = 1;

                continue;

            }

        }

        int sum1 = 0, sum2 = 0;

        for (int i = 0; i < tablei; i++) {

            sum1 = 0;

            sum2 = 0;

            System.out.print("table " + table.num[i] + ": ");

            table.order[i].getTotalPrice();

            table.order[i].getTotalSpecialPrice();

            sum1 = table.order[i].totalPrice; // 正常菜的价格

            sum2 = table.order[i].specialTotalPrice; //特殊菜的价格

            System.out.print(sum1 + sum2 + " ");

            table.order[i].getAfterTotalPrice(menu);

            table.order[i].getAfterSpecialTotalPrice();

            int sum = table.order[i].afeterPrice + table.order[i].afterDiscountSpecialTotalPrice;

            System.out.print(sum);

            if (sum2 != 0) {

                table.order[i].getTotalSpecialCount(menu);

                table.order[i].getConcreteDegree();

                int degreeFlag = 0;

                System.out.print(" ");

                if (table.order[i].spicyLength != 0) {

                    System.out.print("川菜" + " " + table.order[i].spicyCount + " " + table.order[i].spicyString);

                    degreeFlag = 1;

                }

                if (table.order[i].acidLength != 0) {

                    if (degreeFlag == 1) {

                        System.out.print(" ");

                    }

                    degreeFlag = 1;

                    System.out.print("晋菜" + " " + table.order[i].acidCount + " " + table.order[i].aString);

                }

                if (table.order[i].sweetLength != 0) {

                    if (degreeFlag == 1) {

                        System.out.print(" ");

                    }

                    System.out.print("浙菜" + " " + table.order[i].sweetCount + " " + table.order[i].sweetString);

                }

            } else {

                System.out.print(" ");

            }

            System.out.println();

        }

        UD u = new UD();

        u.changeOrder(table);

        Inmation UI = new Inmation();

        for (int i = 0; i < tablei; i++) {

            table.order[i].userTo();

        }

        UI.combineUserInformation(table);

        for (int i = 0; i < UI.orderLength; i++) {

            System.out.println(UI.order[i].owner + " " + UI.order[i].phoneNumber + " " + UI.order[i].uPrice);

        }

    }

 

    public static boolean isInteger(String str) {

        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");

        return pattern.matcher(str).matches();

    }

}

 

class Inmation {

    Order order[] = new Order[100];

    int orderLength = 0;

 

    public void outSetOrder() {

        for (int i = 0; i < order.length; i++) {

            order[i] = new Order();

            order[i].owner = "";

        }

    }

 

    public void combineUserInformation(Table table) {

        outSetOrder();

        for (int i = 0; i < table.order.length; i++) {

            int f = 0;

            for (int i1 = 0; i1 < order.length; i1++) {

                if (order[i1].owner.equals(table.order[i].owner)) {

                    f = 1;

                    order[i1].uPrice += table.order[i].uPrice;

                    break;

                }

            }

            if (f == 0 && table.order[i].owner != null) {

                order[orderLength++] = table.order[i];

            }

        }

    }

}

 

class Dish { //对应菜谱上一道菜的信息。

    String name;  //菜品名称

    int unit_price; //单价

    boolean special; //是否是特殊菜

    String type;//判断是川菜、浙菜、晋菜

 

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

        double price = 0;

        if (portion == 1) {

            price = unit_price;

        } else if (portion == 2) {

            price = unit_price * 1.5;

        } else if (portion == 3) {

            price = unit_price * 2;

        }

        int price1 = (int) (running(price));

        return price1;

    }

 

    double running(double f) {

        double a = Math.signum(f); //判断是正数负数还是0,负数返回-1.0,正数返回1.0

        if (a < 0.0)

            return 0.0 - Math.round(Math.abs(f));

        return Math.round(f);

    }

}

 

class SpecialDish {

    Dish d[] = new Dish[100];

    int length = 0;

    int totalSpecialPrice = 0;

    int num = 0;

    boolean ifDelete = false;

 

 

    public void addDish(Menu m, String dishName, String portion, String count) {

        d[length] = new Dish();

        d[length].name = dishName;

        d[length].unit_price = m.searchDish(dishName).getPrice(Integer.parseInt(portion)) * Integer.parseInt(count);

        length++;

    }

 

    public int searchPrice(String dishName) {

        for (int i = 0; i < d.length; i++) {

            if (d[i].name.equals(dishName)) {

                return d[i].unit_price;

            }

        }

        return 0;

    }

 

    public int getSpecialPrice() {

        int price = 0;

        for (int i = 0; d[i] != null; i++) {

            price += d[i].unit_price;

        }

        return price;

    }

}

 

 

class Menu {  //对应菜谱,包含饭店提供的所有菜的信息。

    Dish[] dishes = new Dish[100];//菜品数组,保存所有菜品信息

    int length = 0;

 

 

    Dish searchDish(String dishName) {

        for (int i = dishes.length - 1; i >= 0; i--) {

            if (dishes[i].name.equals(dishName)) {

                return dishes[i];

            }

            continue;

        }

        return null; //未找到

    }

 

   protected boolean searchSpecial(String dishName) {

        if (searchDish(dishName).special) {

            return true;

        }

        return false;

    }

 

   protected boolean findSpecial(String orderNum, Order j) {

        for (int i = 0; i < j.records.length && j.records[i] != null; i++) {

            if (j.records[i].orderNum == Integer.parseInt(orderNum)) {

                if (searchSpecial(j.records[i].d.name)) {

                    return true;

                }

            }

        }

        return false;

    }

 

    public void addDish(String dishName, int unit_price) {

        dishes[length].name = dishName;

        dishes[length].unit_price = unit_price;

        length++;

    }

 

    public void addType(String type){

        dishes[length].type = type;

    }

 

    public void addSpecial(boolean special) {

        dishes[length].special = special;

    }

}

 

class Order {  //保存用户点的所有菜的信息

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

    SpecialDish[] s = new SpecialDish[100]; //特殊菜

    int tableNum = 0;

    int length = 0;

    double discount;

    int workFlag = 0;

    int totalPrice = 0;

    int uPrice = 0; //输出人名时的价格

    int specialTotalPrice = 0;

    int afeterPrice = 0;

    int afterDiscountSpecialTotalPrice = 0;

    int delete[] = new int[100];

    int deleteFlag = 0;

    double specialDiscount;

    int workOrRelaxFlag = 0; //周一到周五的中午为1,晚上为2,周日为3

 

    int spicyDegree[] = new int[100];

    int spicyDegreeCount[] = new int[100];

    int spicyLength = 0;

    int acidityDegree[] = new int[100];

    int acidDegreeCount[] = new int[100];

    int acidLength = 0;

    int sweetnessDegree[] = new int[100];

    int sweetDegreeCount[] = new int[100];

    int sweetLength = 0;

    int sAver = 0;

    int aAver = 0;

    int sweetAver = 0;

    String spicyString;

    String aString;

    String sweetString;

    String owner;

    String phoneNumber;

    int spicyCount = 0;

    int acidCount = 0;

    int sweetCount = 0;

 

    public void printTasteOutOf(Menu menu, String dishName, int degree) {

        Dish d = menu.searchDish(dishName);

        if (d != null) {

            if (d.type.equals("川菜")) {

                if (degree > 5) {

                    System.out.println("spicy num out of range :" + degree);

                }

            } else if (d.type.equals("晋菜")) {

                if (degree > 4) {

                    System.out.println("acidity num out of range :" + degree);

                }

            } else if (d.type.equals("浙菜")) {

                if (degree > 3) {

                    System.out.println("sweetness num out of range :" + degree);

                }

            }

        }

    }

 

    public boolean wheatherOrder(Menu menu, String dishName, int degree) {

        Dish d = menu.searchDish(dishName);

        if (d != null) {

            if (d.type.equals("川菜")) {

                if (degree <= 5) {

                    return true;

                }

                return false;

            } else if (d.type.equals("晋菜")) {

                if (degree <= 4) {

                    return true;

                }

                return false;

            } else if (d.type.equals("浙菜")) {

                if (degree <= 3) {

                    return true;

                }

                return false;

            }

        }

        return false;

    }

    public boolean addwei(Menu menu, String dishName, int degree, int count) {

        Dish d = menu.searchDish(dishName);

        if (d != null) {

            switch (d.type) {

                case "川菜":

                    return addSpicy(degree, count);

                case "晋菜":

                    return addAcid(degree, count);

                case "浙菜":

                    return addSweet(degree, count);

                default:

                    return false;

            }

        }

        return false;

    }

 

    public void getAverage() {

        double sumSpicy = 0, sumAcid = 0, sumSweet = 0;

        for (int i = 0; i < spicyLength; i++) {

            sumSpicy += spicyDegree[i] * spicyDegreeCount[i];

        }

        for (int i = 0; i < acidLength; i++) {

            sumAcid += acidityDegree[i] * acidDegreeCount[i];

        }

        for (int i = 0; i < sweetLength; i++) {

            sumSweet += sweetnessDegree[i] * sweetDegreeCount[i];

        }

        sAver = (int) Math.round(sumSpicy / spicyCount);

        aAver = (int) Math.round(sumAcid / acidCount);

        sweetAver = (int) Math.round(sumSweet / sweetCount);

    }

 

 

//    public void getConcreteDegree() {

//        getAverage();

//        if (sAver == 0) {

//            spicyString = "不辣";

//        } else if (sAver == 1) {

//            spicyString = "微辣";

//        } else if (sAver == 2) {

//            spicyString = "稍辣";

//        } else if (sAver == 3) {

//            spicyString = "辣";

//        } else if (sAver == 4) {

//            spicyString = "很辣";

//        } else if (sAver == 5) {

//            spicyString = "爆辣";

//        }

//        if (aAver == 0) {

//            aString = "不酸";

//        } else if (aAver == 1) {

//            aString = "微酸";

//        } else if (aAver == 2) {

//            aString = "稍酸";

//        } else if (aAver == 3) {

//            aString = "酸";

//        } else if (aAver == 4) {

//            aString = "很酸";

//        }

//        if (sweetAver == 0) {

//            sweetString = "不甜";

//        } else if (sweetAver == 1) {

//            sweetString = "微甜";

//        } else if (sweetAver == 2) {

//            sweetString = "稍甜";

//        } else if (sweetAver == 3) {

//            sweetString = "甜";

//        }

//    }

   

    public void getConcreteDegree() {

           getAverage();

           switch(sAver) {

           case 0:

           spicyString = "不辣";

           break;

           case 1:

           spicyString = "微辣";

           break;

           case 2:

           spicyString = "稍辣";

           break;

           case 3:

           spicyString = "辣";

           break;

           case 4:

           spicyString = "很辣";

           break;

           case 5:

           spicyString = "爆辣";

           break;

           }

           switch(aAver) {

           case 0:

           aString = "不酸";

           break;

           case 1:

           aString = "微酸";

           break;

           case 2:

           aString = "稍酸";

           break;

           case 3:

           aString = "酸";

           break;

           case 4:

           aString = "很酸";

           break;

           }

           switch(sweetAver) {

           case 0:

           sweetString = "不甜";

           break;

           case 1:

           sweetString = "微甜";

           break;

           case 2:

           sweetString = "稍甜";

           break;

           case 3:

           sweetString = "甜";

           break;

           }

           }

 

 

 

       public boolean addSpicy(int spicDegree, int count) {

       if (spicDegree <= 5) {

       spicyDegree[spicyLength] = spicDegree;

       spicyDegreeCount[spicyLength] = count;

       spicyLength++;

       return true;

       } else {

       return false;

       }

       }

    public boolean addAcid(int acidDegree, int count) {

        if (acidDegree <= 4) {

            acidityDegree[acidLength] = acidDegree;

            acidDegreeCount[acidLength++] = count;

            return true;

        }

        return false;

    }

 

    public boolean addSweet(int sweetDegree, int count) {

        if (sweetDegree <= 3) {

            sweetnessDegree[sweetLength] = sweetDegree;

            sweetDegreeCount[sweetLength++] = count;

            return true;

        }

        return false;

    }

 

    protected void getTotalPrice() {  //计算订单的总价

        for (Record record : records) {

            totalPrice += record.getPrice();

        }

    }

 

    public void getTotalSpecialPrice() {

        for (SpecialDish specialDish : s) {

            specialTotalPrice += specialDish.getSpecialPrice();

        }

    }

 // 计算订单的总价

 // 定义一个计算订单总价的方法

 

 

 

    //添加一条菜品信息到订单中

    public void addARecord(int orderNum, String dishName, int portion, int count) {

        records[length].d.name = dishName;

        records[length].portion = portion;

        records[length].count = count;

        records[length].orderNum = orderNum;

        length++;

    }

 

   protected void pretendToDelete(int orderNum) {

        for (int i = 0; i < length; i++) {

            if (records[i].orderNum == orderNum) {

                totalPrice -= Math.toIntExact(Math.round(records[i].getPrice()));

            }

        }

    }

 

    //根据序号找到一个特殊菜

    SpecialDish findSpecialDish(int Num) {

        for (int i = 0; s[i] != null; i++) {

            if (s[i].num == Num) {

                return s[i];

            }

        }

        return null;

    }

 

  public void preNumber(int orderNum, SpecialDish s) {

        int flag = 0;

        for (int i = 0; i < deleteFlag; i++) {

            if (delete[i] == orderNum) {

                //System.out.println("deduplication " + orderNum);

                return;

            }

        }

        for (int i = 0; i < length; i++) {

            if (records[i].orderNum == orderNum) {

                flag = 1;

                delete[deleteFlag++] = orderNum;

                afterDiscountSpecialTotalPrice -= s.searchPrice(records[i].d.name);

                specialTotalPrice -= s.searchPrice(records[i].d.name);

            }

        }

        if (flag == 0) {

            System.out.println("");

               System.out.println("delete error;");

        }

    }

 

  protected void delNumber(int orderNum) {

        int flag = 0;

        for (int i = 0; i < deleteFlag; i++) {

            if (delete[i] == orderNum) {

                System.out.println("deduplication " + orderNum);

                System.out.println("");

                return;

            }

        }

        for (int i = 0; i < length; i++) {

            if (records[i].orderNum == orderNum) {

                flag = 1;

                delete[deleteFlag++] = orderNum;

                totalPrice -= Math.toIntExact(Math.round(records[i].getPrice()));

            }

        }

        if (flag == 0) {

            System.out.println("delete error;");

            System.out.println("");

        }

    }

 

    public void getTotalSpecialCount(Menu m) {

        for (int i = 0; i < records.length; i++) {

            Dish d = m.searchDish(records[i].d.name);

            if (d != null && d.special) {

                if (d.type.equals("川菜")) {

                    spicyCount += records[i].count;

                } else if (d.type.equals("晋菜")) {

                    acidCount += records[i].count;

                } else if (d.type.equals("浙菜")) {

                    sweetCount += records[i].count;

                }

            }

        }

    }

 

    public void getAfterTotalPrice(Menu menu) {

        for (int i = 0; i < records.length; i++) {

            Dish d = menu.searchDish(records[i].d.name);

            if (d != null && !d.special) {

                afeterPrice += Math.round(records[i].getPrice() * discount);

            }

        }

        if (afeterPrice < 0) {

            afeterPrice = 0;

        }

    }

 

    public void getAfterSpecialTotalPrice() {

        for (SpecialDish specialDish : s) {

            if (specialDish.ifDelete) {

                afterDiscountSpecialTotalPrice += specialDish.getSpecialPrice();

            } else {

                afterDiscountSpecialTotalPrice += Math.round(specialDish.getSpecialPrice() * specialDiscount);

            }

 

        }

    }

 

 

  

    public void deleteTaste(Menu menu, String dishName, int degree, int count) {

        Dish d = menu.searchDish(dishName);

        if (d != null) {

            if (d.type.equals("川菜")) {

                for (int i = 0; i < spicyDegree.length; i++) {

                    if (spicyDegree[i] == degree && spicyDegreeCount[i] == count) {

                        spicyDegree[i] -= degree;

                        spicyDegreeCount[i] -= count;

                        spicyCount -= count;

                        spicyLength--;

                        break;

                    }

                }

            } else if (d.type.equals("晋菜")) {

                for (int i = 0; i < acidityDegree.length; i++) {

                    if (acidityDegree[i] == degree && acidDegreeCount[i] == count) {

                        acidityDegree[i] -= degree;

                        acidDegreeCount[i] -= count;

                        acidCount -= count;

                        acidLength--;

                        break;

                    }

                }

            } else if (d.type.equals("浙菜")) {

                for (int i = 0; i < sweetnessDegree.length; i++) {

                    if (sweetnessDegree[i] == degree && sweetDegreeCount[i] == count) {

                        sweetnessDegree[i] -= degree;

                        sweetDegreeCount[i] -= count;

                        sweetCount -= count;

                        sweetLength--;

                        break;

                    }

                }

            }

        }

    }

 

    public int findRecordNumber(int number) {

        for (int i = 0; i < records.length; i++) {

            if (records[i].orderNum == number) {

                return i;

            }

        }

        return -1;

    }

 

    public int findNumber(int number) {

        for (int i = 0; i < s.length; i++) {

            if (s[i].num == number) {

                return i;

            }

        }

        return -1;

    }

 

    public void userTo() {

        uPrice += afeterPrice + afterDiscountSpecialTotalPrice;

    }

}

 

 

class Record {  //保存订单上的一道菜品记录

    Dish d = new Dish();  //菜品

    int orderNum;

    int count;//订单上的份数

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

    int degree; //口味的程度

    int getPrice(){

        return count * d.getPrice(portion);

    }

}

class Table {

    Order order[] = new Order[100];

    int num[] = new int[100];//桌子的序号

    String date[] = new String[100];

    String time[] = new String[100];

 

 

    public void specialDiscount(int i) {

        String d[] = date[i].split("/");

        LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));

        int day = d1.getDayOfWeek().getValue();

        if (day != 6 && day != 7) {

            order[i].specialDiscount = 0.7;

        } else {

            order[i].specialDiscount = 1;

        }

    }

    boolean judgeDiscount(int i) {

        String d[] = date[i].split("/");

        String t[] = time[i].split("/");

        LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));

        LocalTime d2 = LocalTime.of(Integer.parseInt(t[0]), Integer.parseInt(t[1]), Integer.parseInt(t[2]));

        LocalTime dRelaxStart = LocalTime.of(9, 30);

        LocalTime dRelaxEnd = LocalTime.of(21, 30);

        LocalTime dWorkStart1 = LocalTime.of(10, 30);

        LocalTime dWorkStart2 = LocalTime.of(17, 0);

        LocalTime dWorkEnd1 = LocalTime.of(14, 30);

        LocalTime dWorkEnd2 = LocalTime.of(20, 30);

        int day = d1.getDayOfWeek().getValue();

        if (day == 6 || day == 7) {

            if (d2.isAfter(dRelaxEnd) || d2.isBefore(dRelaxStart)) {

                order[i].workFlag = 1;

            } else {

                order[i].workOrRelaxFlag = 3;

                order[i].discount = 1;

            }

        } else {

            if ((d2.isAfter(dWorkStart1) && d2.isBefore(dWorkEnd1)) || (d2.until(dWorkStart1, ChronoUnit.SECONDS) == 0 || d2.until(dWorkEnd1, ChronoUnit.SECONDS) == 0)) {

                order[i].discount = 0.6;

                order[i].workOrRelaxFlag = 1;

            } else if ((d2.isAfter(dWorkStart2) && d2.isBefore(dWorkEnd2)) || (d2.until(dWorkStart2, ChronoUnit.SECONDS) == 0 || d2.until(dWorkEnd2, ChronoUnit.SECONDS) == 0)) {

                order[i].workOrRelaxFlag = 2;

                order[i].discount = 0.8;

            } else {

                order[i].workFlag = 1;

            }

        }

        String pattern = "([0-1]?[0-9]|2[0-3])/([0-5][0-9])/([0-5][0-9])";

        String timeString = "";

        char[] c = time[i].toCharArray();

        for (int i1 = 0; i1 < c.length; i1++) {

            timeString += c[i1];

        }

        if (timeString.matches(pattern)) {

            return true;

        } else {

            return false;

        }

    }

 

    boolean isDate(int i) {

        String str[] = date[i].split("/");

 

 

        // 接下来需要检查年月日是否都是数字,并且是否分别在合法的范围内

        try {

            int year = Integer.parseInt(str[0]);

            int month = Integer.parseInt(str[1]);

            int day = Integer.parseInt(str[2]);

 

            if (year < 0 || month < 0 || month > 12) {

                return false;

            }

 

            if (day < 1 || day > getDaysInMonth(year, month)) {

                return false;

            }

        } catch (NumberFormatException e) {

            return false;

        }

        return true;

    }

    public static int getDaysInMonth(int year, int month) {

        switch (month) {

            case 2:

                return isLeapYear(year) ? 29 : 28;

            case 4:

            case 6:

            case 9:

            case 11:

                return 30;

            default:

                return 31;

        }

    }

    public static boolean isLeapYear(int year) {

        if (year % 4 == 0 && year % 100 != 0) {

            return true;

        } else if (year % 400 == 0) {

            return true;

        } else {

            return false;

        }

    }

 

    public boolean ifLegal(String str) {

        if (str.matches("(table)( )([^0])(\\d*)( )(:)( )(\\S{1,10})( )(180|181|189|133|135|136)(\\d{8})( )(\\d{4})(/)([1-9]|0[1-9]|\\d{2})(/)(\\d{1,2})( )(\\d{1,2})(/)(\\d{1,2})(/)(\\d{1,2})")) {

            return true;

        }

        return false;

    }

 

    public boolean ifSameDay(int tablei) {

        String d1[] = date[tablei].split("/");

        LocalDate day = LocalDate.of(Integer.parseInt(d1[0]), Integer.parseInt(d1[1]), Integer.parseInt(d1[2]));

        for (int i1 = tablei - 1; i1 >= 0; i1--) {

            String d[] = date[i1].split("/");

            LocalDate d2 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));

            if (d2.getDayOfWeek().getValue() == day.getDayOfWeek().getValue()) {

                return true;

            }

        }

        return false;

    }

 

    public boolean ifSameTime(int tablei) {

        if (!ifSameDay(tablei)) {

            return false;

        }

        for (int i1 = tablei - 1; i1 >= 0; i1--) {

            if (order[i1].workOrRelaxFlag == order[tablei].workOrRelaxFlag) {

                return true;

            }

        }

        return false;

    }

 

    public boolean ifExistTableNum(int tableNum) {

        for (int i = 0; i < num.length; i++) {

            if (num[i] == tableNum) {

                return true;

            }

        }

        return false;

    }

 

    public boolean ifSameOwner(String name, int tablei) {

        for (int i = tablei - 1; i >= 0; i--) {

            if (order[i].owner.equals(name)) {

                return true;

            }

        }

        return false;

    }

    public int searchOrder(int number){

        for (int i = 0; i < order.length; i++) {

            if(order[i].tableNum == number){

                return i;

            }

        }

        return -1;

    }

}

 

class UD {

    public void changeOrder(Table table) {

        for (int i = 0; table.order[i].owner != null && i < table.order.length - 1; i++) {

            for (int j = 0; table.order[j].owner != null && j < table.order.length - 1 - i; j++) {

                if (table.order[j + 1].owner != null) {

                    if (table.order[j].owner.compareTo(table.order[j + 1].owner) > 0) {

                        Order u;

                        u = table.order[j];

                        table.order[j] = table.order[j + 1];

                        table.order[j + 1] = u;

                    }

                }

            }

        }

    }

}

代码分析:

以上的题用Date包来把实现日期的书写,而且还写了一些table等的类让代码更加的完整与连贯,入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。

第三题的类图:

 

 

 

 

import java.util.ArrayList;

import java.util.Comparator;

import java.util.Scanner;

 

public class Main {

 

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);

  ArrayList<Shape> list = new ArrayList<>();

 

  int choice = input.nextInt();

 

  while (choice != 0) {

   switch (choice) {

   case 1:// Circle

    double radiums = input.nextDouble();

    Shape circle = new Circle(radiums);

    list.add(circle);

    break;

   case 2:// Rectangle

    double x1 = input.nextDouble();

    double y1 = input.nextDouble();

    double x2 = input.nextDouble();

    double y2 = input.nextDouble();

    Point leftTopPoint = new Point(x1, y1);

    Point lowerRightPoint = new Point(x2, y2);

    Rectangle rectangle = new Rectangle(leftTopPoint, lowerRightPoint);

    list.add(rectangle);

    break;

   }

   choice = input.nextInt();

  }

 

  list.sort(Comparator.naturalOrder());// 正向排序

 

  for (int i = 0; i < list.size(); i++) {

   System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");

  }

 }

 static void printArea(Shape shape) {

  double f = shape.getArea();

  if (shape instanceof Circle) {

   if (f <= 0)

    System.out.print("Wrong Format");

   else {

    String tmp = String.format("%.2f", f);

    System.out.print(tmp);

   }

  } else {

   String tmp = String.format("%.2f", f);

   System.out.print(tmp);

  }

 }

 

}

 

class Point {

 Point(double x, double y) {

  this.x = x;

  this.y = y;

 }

 

 public double getX() {

  return x;

 }

 

 public void setX(double x) {

  this.x = x;

 }

 

 public double getY() {

  return y;

 }

 

 public void setY(double y) {

  this.y = y;

 }

 

 double x, y;

 

}

 

class Shape extends implements Comparable<Shape> {

 public Shape() {

 

 }

 

 public double getArea() {

  return 0;

 }

 

 public int compareTo(Shape shape) {

  double s1, s2;

  s1 = getArea();

  s2 = shape.getArea();

  if (s1 > s2)

   return 1;

  else if (s1 < s2)

   return -1;

  return 0;

 }

}

 

class Circle extends Shape {

 double radius;

 

 Circle(double r) {

  radius = r;

 }

 

 public double getArea() {

  return (double) (Math.PI * radius * radius);

 }

 

}

 

class Rectangle extends Shape {

 public Rectangle(Point topLeftPoint, Point lowerRightPoint) {

  super();

  this.topLeftPoint = topLeftPoint;

  this.lowerRightPoint = lowerRightPoint;

 }

 

 public Point getTopLeftPoint() {

  return topLeftPoint;

 }

 

 public void setTopLeftPoint(Point topLeftPoint) {

  this.topLeftPoint = topLeftPoint;

 }

 

 public Point getLowerRightPoint() {

  return lowerRightPoint;

 }

 

 public void setLowerRightPoint(Point lowerRightPoint) {

  this.lowerRightPoint = lowerRightPoint;

 }

 

 Point topLeftPoint;

 Point lowerRightPoint;

 

 double getLength() {

  return Math.abs(topLeftPoint.x - lowerRightPoint.x);

 

 }

 

 double getHeight() {

  return Math.abs(topLeftPoint.y - lowerRightPoint.y);

 }

 

 public double getArea() {

  return getLength() * getHeight();

 }

 

}

以上是期中考试的第三题;

 

代码分析:

Main类为程序的入口,通过输入不同的图形类型和参数,创建对应的图形对象,并将其添加到一个ArrayList中。然后对ArrayList进行排序,按照图形的面积从小到大进行排序。最后,遍历ArrayList中的图形对象,计算并输出每个图形的面积。

Shape类为图形的基类,定义了一个抽象方法getArea()用于计算图形的面积,并实现了Comparable接口,用于对图形进行排序。

Circle类表示圆形,继承自Shape类,具有半径属性和计算面积的方法。

Rectangle类表示矩形,继承自Shape类,具有左上角和右下角两个点的属性,以及计算面积的方法。

Point类表示一个点,具有横坐标和纵坐标属性。

 

踩坑心得:

代码的初期设计存在问题,导致写完以后无法对其进行更进一步的优化。例如第五次作业的第一题日期聚合类二:

 

 对于前两次PTA大作业我主要问题是编程一些细节和内容并没有做好,导致一些测试点过不去就像第二次的5-2中的俩个都为异常输入的情况没有处理好导致测试点过不去,以及第一次的大作业有很多东西没有很好的编程,自己写的代码过于复杂,所有东西都往一个类里面放没有做到本学期的课程面向对象的思考方式还是像上个学期一样只考虑面向过程的C语言,这是我这个学期必须要改进的方面,处理数据的时候没有真正认证思考怎样才能写的简洁明了。其中格式错误一开始全部测试点过去,反复思考与带入数据才知道输入的判读合法的语句成了最大问题。以及点数的判断是很重要的要看清楚题目中选项的输入点的数量,否则你的代码无法运行。还有就是学好数学这一个大类这三次题目中一尤其是4,5俩次的大作业算法是编程实现的核心简便的算法不仅可以优化你的代码行数,还可以减少很多编程时间像携带原理来计算依次输入的点围成的图形的面积,是相当的好用和简便,以及图型判断的方式也可以让你在后面代码的编程上大大的缩短时间。

主要困难以及改进建议:

题目给出的类图,可以根据自己的理解进行适当的修改,但是容易造成类间联系过于复杂,不利于后期debug的维护。因此更加需要在写程序初期进行类的设计,要详细地考虑到继承与多态,便于后期进行测试与维护。

总结:

经过这么多次的学习,了解到了面向对象的好处

系统稳定好

面向对象方法用对象模拟问题域中的实体,以对象间的联系刻画实体间联系。当系统的功能需求变化时,不会引起软件结构的整体变化,仅需做一些局部的修改。

由于现实世界中的实体是相对稳定的,因此,以对象为中心构造的软件系统也会比较稳定。可维护好

由于面向对象的软件稳定性比较好,容易修改、容易理解、易于测试和调试,因而软件的可维护性就会比较好。

信息的查找速度和传播速度

因为面向对象方式是以对象为核心的。数据在内存中是无序存放的,当有许多数据时,需要找到某个数据可能需要找遍所有数据才能找到 ,而面向对象方法可以通过对象的”键”快速找到某个数据。当某个函数需要多个数据进行传参时,不再需要一个个传参,而是将这些数据放入一个对象中进行传参,这样就只相对于传了一个数据。