OPP第二次作业

how-to-ak / 2023-05-03 / 原文

1.前言

  • 习题四的整体难度不大,但第一题难度较大,需要对整体输入输出有一个很好的设计,对设计者提出了很高的要求,尽管本人很快的写完了其他的题目,但是在对第一题的设计中频频碰壁,难以达到题目预期效果,导致第一题未能成功做出。

     

  • 习题五整体难度适中,主要考查了正则表达式和聚合,需要对正则表达式有较高的熟练度,清楚聚合的使用,由于之前对日期类题目有过较多的训练,也使得对日期类题型有较清晰的思路。

  • 习题六整体难度较高,前两题考察了Java的继承于多态,后三题需要有非常严谨的逻辑,学会合理的分割输入数据,需要创建多个类,建立多个类之间的关系,也对阅读和理解能力提出了较高的要求。


    2.设计与分析

    OPP训练集05

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

    • 题目如下

       

    分析

    本题其实难度较高,但是由于这题已经写过多次,并且类图给出了很多的提示,他引导着我们用month类聚合year类,day类聚合month类,DataUtil类聚合day类,类似于套娃的模式,使得到DataUtil类时已经可以调用day,month,year的类,然后用Main函数去调用DataUtil类去进行相关的数据的传输。

    代码如下

    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;
           long 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 = input.nextLong();

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

               if (!date.checkInoutValidity()) {
                   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
               long  n = 0;
               year = Integer.parseInt(input.next());
               month = Integer.parseInt(input.next());
               day = input.nextLong();

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

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

               n = input.nextLong();

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

    //           System.out.print(
    //                   date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().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());

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

               if (fromDate.checkInoutValidity() && toDate.checkInoutValidity()) {
                   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 Year{
       private int value;
       public int getValue() {
           return value;
      }

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

       public Year getYear() {
           return year;
      }

       public void setYear(Year year) {

           this.year = year;
      }


       public int getValue() {
           return value;
      }

       public void setValue(int value) {
           if(value>=1&&value<=12)
               this.value = value;
           else {
               System.out.println("Wrong Format");
               System.exit(0);
          }
      }

       public Month(){};
       public Month(int yearValue,int monthValue){
           this.year = new Year(yearValue);
           if(monthValue<=12&&monthValue>=1)
               this.value = monthValue;
           else{
               System.out.println("Wrong Format");
               System.exit(0);

          }
      }
       public void resetMin(){
           value=1;
      }
       public void resetMax(){
           value=12;
      }
       public boolean validate(){
           if(value>=1&&value<=12)
               return true;
           else return false;
      }
       public void monthIncrement(){
           value++;
      }
       public void monthReduction(){
           value--;
      }

    }
    class Day{
       private long value;
       private Month month;
       public long getValue() {
           return value;
      }

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



       public Month getMonth() {
           return month;
      }

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


       private int[]mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
       public Day(){};
       public Day(int yearValue,int monthValue,long dayValue){
           this.month =new Month(yearValue,monthValue);
           this.value = dayValue;
      }
       public void resetMin(){
           value=1;
      }
       public void resetMax(){
           if(this.getMonth().getYear().isLeapYear())
               mon_maxnum[1]=29;
           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(){

           value++;
      }
       public void dayReduction(){
           value--;
      }

    }
    class DateUtil{
       private Day day;
       private int[]mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
       public Day getDay() {
           return day;
      }

       public void setDay(Day day) {
           this.day = day;
      }
       public DateUtil(){};
       public DateUtil(long d,int m,int y){
           this.day=new Day(y,m,d);
      }
       public boolean checkInoutValidity(){
           if(this.getDay().validate()&&this.getDay().getMonth().validate()&&this.getDay().getMonth().getYear().validate())
               return true;
           else return false;
      }
       public boolean compareDates(DateUtil date){
           if(this.getDay().getMonth().getYear().getValue()>date.getDay().getMonth().getYear().getValue())return true;
           else if(this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()&&this.getDay().getMonth().getValue()>date.getDay().getMonth().getValue())
               return true;
           else if(this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&&this.getDay().getValue()>date.getDay().getValue())
               return true;
           else return 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 DateUtil getNextNDays(long n){
           if(this.getDay().getMonth().getYear().isLeapYear())
               mon_maxnum[1]=29;
           else mon_maxnum[1]=28;
           this.day.setValue(day.getValue()+n);
           while(day.getValue()>mon_maxnum[day.getMonth().getValue()-1]){
               if(this.getDay().getMonth().getYear().isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               day.setValue(day.getValue()-mon_maxnum[day.getMonth().getValue()-1]);
               day.getMonth().monthIncrement();
               if(day.getMonth().getValue()>12)
              {
                   day.getMonth().resetMin();
                   day.getMonth().getYear().yearIncrement();
              }

          }

           return this;

      }
       public DateUtil getPreviousNDays(long n){

           this.day.setValue(day.getValue()-n);

           while(day.getValue()<1)
          {
               day.getMonth().monthReduction();

               if(day.getMonth().getValue()<=0)
              {
                   day.getMonth().resetMax();
                   day.getMonth().getYear().yearReduction();

              }
               if(this.getDay().getMonth().getYear().isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               day.setValue(day.getValue()+mon_maxnum[day.getMonth().getValue()-1]);
          }
           return this;
      }
       public int getDaysofDates(DateUtil date){

           int res = 0;
           //if(!equalTwoDates(date)) {
           while (compareDates(date)&&!equalTwoDates(date)) {

               if(this.getDay().getMonth().getYear().isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               day.dayReduction();
               res++;
               if (day.getValue()== 0) {
                   day.getMonth().monthReduction();

                   if (day.getMonth().getValue() == 0) {

                       day.getMonth().resetMax();
                       day.getMonth().getYear().yearReduction();
                  }
                   day.setValue(mon_maxnum[day.getMonth().getValue()-1]);

              }
          }

           //if(!equalTwoDates(date))
           while (!compareDates(date)&&!equalTwoDates(date)) {
               if(date.getDay().getMonth().getYear().isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               date.day.dayReduction();
               res++;
               if (date.day.getValue() == 0) {
                   date.day.getMonth().monthReduction();

                   if (date.day.getMonth().getValue() == 0) {
                       date.day.getMonth().resetMax();
                       date.day.getMonth().getYear().yearReduction();

                  }
                   date.day.setValue(mon_maxnum[date.day.getMonth().getValue()-1]);

              }

          }

           return res;
      }




    }

    心得

    本次练习是我第一次切实接触聚合,也使我对聚合有了更深刻的认知,聚合这种套娃的模式带来的便利也让我获益匪浅,我也是会学会多在今后的设计中学会使用聚合的模式去提高我代码的规范性和编程的效率。


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

    题目如下:

    分析

    本题使用了另外一种较为正常的聚合方法,使用一个类去调用其他的几个类,思路更为直接和清晰,调用起来也比上题会更加的方便(因为上题调用year的值要经过好几个类的过度,可能会略显繁琐)。

    代码如下

    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;
           long 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 = input.nextLong();

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

               if (!date.checkInoutValidity()) {
                   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
               long  n = 0;
               year = Integer.parseInt(input.next());
               month = Integer.parseInt(input.next());
               day = input.nextLong();

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

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

               n = input.nextLong();

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

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

               if (fromDate.checkInoutValidity() && toDate.checkInoutValidity()) {
                   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 Year{
       private int value;
       public int getValue() {
           return value;
      }

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





       public int getValue() {
           return value;
      }

       public void setValue(int value) {
           if(value<=12&&value>=1)
               this.value = value;
           else {
               System.out.println("Wrong Format");
               System.exit(0);
          }
      }

       public Month(){};
       public Month(int monthValue){
           if(monthValue<=12&&monthValue>=1)
               this.value = monthValue;
           else {
               System.out.println("Wrong Format");
               System.exit(0);
          }
      }
       public void resetMin(){
           value=1;
      }
       public void resetMax(){
           value=12;
      }
       public boolean validate(){
           if(value>=1&&value<=12)
               return true;
           else return false;
      }
       public void monthIncrement(){
           value++;
      }
       public void monthReduction(){
           value--;
      }

    }
    class Day{
       private long value;

       public long getValue() {
           return value;
      }

       public void setValue(long value) {
           this.value = value;
      }
       public Day(){};
       public Day(long dayValue){
           this.value = dayValue;
      }

       public void dayIncrement(){

           value++;
      }
       public void dayReduction(){
           value--;
      }

    }
    class DateUtil{
       private Day day;

       public Month getMonth() {
           return month;
      }

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

       private Month month;

       public Year getYear() {
           return year;
      }

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

       private Year year;
       private int[]mon_maxnum = {31,28,31,30,31,30,31,31,30,31,30,31};
       public Day getDay() {
           return day;
      }

       public void setDay(Day day) {
           this.day = day;
      }
       public DateUtil(){};
       public DateUtil(long d,int m,int y){
           this.year=new Year(y);
           this.month=new Month(m);
           this.day=new Day(d);

      }
       private void setDayMin(){
           this.day.setValue(1);
      }
       private void setDayMax(){
           if(this.year.isLeapYear())
               mon_maxnum[1]=29;
           else mon_maxnum[1]=28;
           this.day.setValue(mon_maxnum[this.month.getValue()-1]);
      }
       public boolean checkInoutValidity(){
           if(this.year.isLeapYear())
               mon_maxnum[1]=29;
           else mon_maxnum[1]=28;
           if(this.day.getValue()>=1&&this.day.getValue()<=mon_maxnum[this.month.getValue()-1]&&this.month.getValue()>=1&&this.month.getValue()<=12&&this.year.getValue()>=1820&&this.year.getValue()<=2020)
               return true;
           else return false;
      }
       public boolean compareDates(DateUtil date){
           if(this.year.getValue()>date.year.getValue())return true;
           else if(this.year.getValue()==date.year.getValue()&&this.month.getValue()>date.month.getValue())
               return true;
           else if(this.year.getValue()==date.year.getValue()&&this.month.getValue()==date.month.getValue()&&this.day.getValue()>date.day.getValue())
               return true;
           else return false;
      }
       public boolean equalTwoDates(DateUtil date){
           if(this.day.getValue()==date.day.getValue()&&this.month.getValue()==date.month.getValue()&&this.year.getValue()==date.year.getValue())
               return true;
           else
               return false;
      }
       public String showDate(){
           return this.year.getValue()+"-"+this.month.getValue()+"-"+this.day.getValue();
      }
       public DateUtil getNextNDays(long n){
           if(this.year.isLeapYear())
               mon_maxnum[1]=29;
           else mon_maxnum[1]=28;
           this.day.setValue(day.getValue()+n);
           while(day.getValue()>mon_maxnum[month.getValue()-1]){
               if(this.year.isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               day.setValue(day.getValue()-mon_maxnum[month.getValue()-1]);
               month.monthIncrement();
               //day.getMonth().monthIncrement();
               if(month.getValue()>12)
              {
                   month.resetMin();
                   year.yearIncrement();

              }

          }

           return this;

      }
       public DateUtil getPreviousNDays(long n){

           this.day.setValue(day.getValue()-n);

           while(day.getValue()<1)
          {
               month.monthReduction();;
               //day.getMonth().monthReduction();

               if(month.getValue()<=0)
              {
                   month.resetMax();
                   year.yearReduction();
                   //day.getMonth().resetMax();
                   //day.getMonth().getYear().yearReduction();

              }
               if(this.year.isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               day.setValue(day.getValue()+mon_maxnum[month.getValue()-1]);
          }
           return this;
      }
       public int getDaysofDates(DateUtil date){

           int res = 0;
           //if(!equalTwoDates(date)) {
           while (compareDates(date)&&!equalTwoDates(date)) {

               if(year.isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               day.dayReduction();
               res++;
               if (day.getValue()== 0) {
                   month.monthReduction();
                   //day.getMonth().monthReduction();

                   if (month.getValue() == 0) {
                       month.resetMax();
                       year.yearReduction();
                  }
                   day.setValue(mon_maxnum[month.getValue()-1]);

              }
               if (equalTwoDates(date)) break;
          }

           while (!compareDates(date)&&!equalTwoDates(date)) {
               if(date.year.isLeapYear())
                   mon_maxnum[1]=29;
               else mon_maxnum[1]=28;
               date.day.dayReduction();
               res++;
               if (date.day.getValue() == 0) {
                   date.month.monthReduction();

                   if (date.month.getValue() == 0) {
                       date.month.resetMax();
                       date.year.yearReduction();

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

              }
               if (equalTwoDates(date)) break;

          }

           return res;
      }




    }

    心得

    对聚合的使用有了更进一步的理解,也学到了更舒服便利的聚合方法,同时两次的聚合的练习也让我知道了聚合是表示两个对象之间是整体和部分的弱关系,要和组合进行区分。


    OPP训练集05

    7-4 ATM机类结构设计(一)

    题目如下:

    分析

    本题需要对每一个账户构建类,在类里面要包含账户的账号信息,银行信息,名字信息,密码信息,初始资金信息等,可以运用聚合的结构,一定要逻辑清晰,明白程序判断的顺序(比如是先判断哪个报错,再判断哪个报错,再正常输出账户信息等),对于卡的信息及账户的信息等要使用ArrayList去储存。

    代码如下

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Main {
       public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           String str = sc.nextLine();
           String[]nums={"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};

           ArrayList<String> cardList1 = new ArrayList<>();
           ArrayList<String> ATMList1 = new ArrayList<>();
           ATMList1.add("01");
           ATMList1.add("02");
           ATMList1.add("03");
           ATMList1.add("04");
           Bank jsyh = new Bank("中国建设银行", ATMList1);
           ArrayList<String> ATMList2 = new ArrayList<>();
           ATMList2.add("05");
           ATMList2.add("06");
           ArrayList<String> ATMList3 = new ArrayList<>();
           ATMList3.add("07");
           ATMList3.add("08");
           ATMList3.add("09");
           ATMList3.add("10");
           ATMList3.add("11");
           //ATMList3.add("06");
           Bank gsyh = new Bank("中国工商银行", ATMList2);
           Bank nyyh = new Bank("中国农业银行", ATMList3);
           cardList1.add("6217000010041315709");
           cardList1.add("6217000010041315715");
           Account account0 = new Account("杨过",  "88888888", 10000.00, jsyh, cardList1);
           ArrayList<String> cardList2 = new ArrayList<>();
           cardList2.add("6217000010041315718");
           Account account1 = new Account("杨过",  "88888888", 10000.00, jsyh, cardList2);
           ArrayList<String> cardList3 = new ArrayList<>();
           cardList3.add("6217000010051320007");
           Account account2 = new Account("郭靖",  "88888888", 10000.00, jsyh, cardList3);
           ArrayList<String> cardList4 = new ArrayList<>();
           cardList4.add("6222081502001312389");
           Account account3 = new Account("张无忌",  "88888888", 10000.00, gsyh, cardList4);
           ArrayList<String> cardList5 = new ArrayList<>();
           cardList5.add("6222081502001312390");
           Account account4 = new Account("张无忌",  "88888888", 10000.00, gsyh, cardList5);
           ArrayList<String> cardList6 = new ArrayList<>();
           cardList6.add("6222081502001312399");
           cardList6.add("6222081502001312400");
           Account account5 = new Account("张无忌",  "88888888", 10000.00, gsyh, cardList6);
           ArrayList<String> cardList7 = new ArrayList<>();
           cardList7.add("6222081502051320785");
           Account account6 = new Account("韦小宝",  "88888888", 10000.00, gsyh, cardList7);
           ArrayList<String> cardList8 = new ArrayList<>();
           cardList8.add("6222081502051320786");
           Account account7 = new Account("韦小宝",  "88888888", 10000.00, gsyh, cardList8);
           ArrayList<Account> accountList = new ArrayList<>();
           accountList.add(account0);
           accountList.add(account1);
           accountList.add(account2);
           accountList.add(account3);
           accountList.add(account4);
           accountList.add(account5);
           accountList.add(account6);
           accountList.add(account7);
           while (!str.equals("#")) {
               String[] data = str.split("\\s+");

               if (data.length != 1) {
                   boolean flag = false;
                   int panduan1 = 0;
                   for (int i = 0; i < nums.length; i++) {
                       if (data[0].equals(nums[i]))
                           flag = true;
                  }
                   if (!flag) System.out.println("Sorry,this card does not exist.");
                   for (int i = 0; i < accountList.size(); i++) {
                       boolean panduan = false;
                       for (int j = 0; j < accountList.get(i).getCardList().size(); j++)
                           if (data[0].equals(accountList.get(i).getCardList().get(j))) {
                               if (!accountList.get(i).valiATM(data[2])) {
                                   System.out.println("Sorry,the ATM's id is wrong.");
                                   panduan = true;
                              } else for (int k = 0; k < accountList.get(i).getBank().getATMList().size(); k++)
                                   if (data[2].matches(accountList.get(i).getBank().getATMList().get(k)))
                                       panduan = true;
                               if (!panduan) System.out.println("Sorry,cross-bank withdrawal is not supported.");
                               if (panduan && !data[1].equals("88888888"))
                                   System.out.println("Sorry,your password is wrong.");
                               else {
                                   if (Double.parseDouble(data[3]) > 0) {
                                       System.out.printf(accountList.get(i).getName() + "在" + accountList.get(i).bankName(data[2]) + "的" + data[2] + "号ATM机上取款¥%.2f\n", Double.parseDouble(data[3]));
                                       accountList.get(i).changeMoney(Double.parseDouble(data[3]));
                                  } else {
                                       System.out.printf(accountList.get(i).getName() + "在" + accountList.get(i).bankName(data[2]) + "的" + data[2] + "号ATM机上存款¥%.2f\n", Double.parseDouble(data[3]));
                                       accountList.get(i).changeMoney(Double.parseDouble(data[3]));
                                  }
                                   System.out.printf("当前余额为¥%.2f\n", accountList.get(i).getMoney());
                              }
                          }
                  }


                   str = sc.nextLine();
              }
               else {
                   boolean flag = false;
                   for(int i=0;i< nums.length;i++){
                       if(data[0].equals(nums[i]))
                           flag = true;
                  }
                   if(!flag) System.out.println("Sorry,this card does not exist.");
                   if (data[0].equals("6217000010041315709") || data[0].equals("6217000010041315715"))
                       System.out.printf("业务:查询余额 ¥%.2f\n",account0.getMoney());
                   else if (data[0].equals("6217000010041315718"))
                       System.out.printf("业务:查询余额 ¥%.2f\n",account1.getMoney());
                   else if (data[0].equals("6217000010051320007") )
                       System.out.printf("业务:查询余额 ¥%.2f\n",account2.getMoney());
                   else if (data[0].equals("6222081502001312389") )
                       System.out.printf("业务:查询余额 ¥%.2f\n",account3.getMoney());
                   else if (data[0].equals("6222081502001312390") )
                       System.out.printf("业务:查询余额 ¥%.2f\n",account4.getMoney());
                   else if (data[0].equals("6222081502001312399") ||data[0].equals("6222081502001312400"))
                       System.out.printf("业务:查询余额 ¥%.2f\n",account5.getMoney());
                   else if (data[0].equals("6222081502051320785") )
                       System.out.printf("业务:查询余额 ¥%.2f\n",account6.getMoney());
                   else if (data[0].equals("6222081502051320786") )
                       System.out.printf("业务:查询余额 ¥%.2f\n",account7.getMoney());
                   str = sc.nextLine();
              }


          }
      }
    }
    class Bank {
       String bankname;
       ArrayList<String> ATMList;
       //创建无参构造方法
       public Bank(){
      }
       //创建带参构造方法
       public Bank(String bankname,ArrayList<String> ATMList)
      {
           this.bankname=bankname;
           this.ATMList=ATMList;
      }
       //getter
       public String getBankname() {
           return bankname;
      }
       public ArrayList<String> getATMList() {
           return ATMList;
      }
       //setter
       public void setBankname(String bankname){
           this.bankname=bankname;
      }
       public void setATMList(ArrayList<String> tATMList){
           this.ATMList=ATMList;
      }

    }
    class Account{
       public String getName() {
           return name;
      }

       public void setName(String name) {
           this.name = name;
      }

       public String getAccount() {
           return account;
      }

       public void setAccount(String account) {
           this.account = account;
      }

       public double getMoney() {
           return money;
      }

       public void setMoney(double money) {
           this.money = money;
      }

       public String getPassword() {
           return password;
      }

       public void setPassword(String password) {
           this.password = password;
      }

       public Bank getBank() {
           return bank;
      }

       public void setBank(Bank bank) {
           this.bank = bank;
      }

       public ArrayList<String> getCardList() {
           return cardList;
      }

       public void setCardList(ArrayList<String> cardList) {
           this.cardList = cardList;
      }

       private String name;
       private String account;
       private String password;
       private double money;

       private Bank bank;
       private ArrayList<String>cardList;
       public Account(){};
       public Account(String name,String password,double money,Bank bank,ArrayList<String>cardList){
           this.name=name;
           this.password=password;
           this.money = money;
           this.bank =bank;
           this.cardList =cardList;
      }
       public double changeMoney(double money){
           if(money>0)
               this.money-=money;
           else this.money-=money;
           return this.money;
      }
       public boolean checkMoney(double money){
           if(money>0&&Math.abs(money)>=this.money)
               return true;
           return false;

      }
       public boolean valiATM(String num){
           if(num.matches("0[1-9]|1[01]"))
               return true;
           return false;
      }

       public String bankName(String str){
           String name="";
           if(str.matches("0[1-4]")){
               name = "中国建设银行";
          }
           if(str.matches("0[5-6]")){
               name = "中国工商银行";
          }
           if(str.matches("0[7-9]|1[01]")){
               name = "中国农业银行";
          }
           return name;
      }
    }

    心得

    本题让我对ArrayList有了更熟练的掌握,对输入的字符串数据处理有了更深刻的体会,对于大题量的题目的逻辑分析能力有所提升,聚合的使用也更加熟练。


    7-5 ATM机类结构设计(二)

    题目如下

分析

本题在上题的基础上增加了卡的类型,以及对于的卡的功能,增加了用户的数量以及跨行利息的功能,需要对题目给的所有信息及样例完全理解并且计算核实合适的计算方法。

代码如下

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String str = sc.nextLine();
       String[]nums={"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786","6640000010045442002","6640000010045442003","6640000010045441009","6630000010033431001","6630000010033431008"};

       ArrayList<String> cardList1 = new ArrayList<>();
       ArrayList<String> ATMList1 = new ArrayList<>();
       ATMList1.add("01");
       ATMList1.add("02");
       ATMList1.add("03");
       ATMList1.add("04");
       Bank jsyh = new Bank("中国建设银行", ATMList1);
       ArrayList<String> ATMList2 = new ArrayList<>();
       ATMList2.add("05");
       ATMList2.add("06");
       ArrayList<String> ATMList3 = new ArrayList<>();
       ATMList3.add("07");
       ATMList3.add("08");
       ATMList3.add("09");
       ATMList3.add("10");
       ATMList3.add("11");
       //ATMList3.add("06");
       Bank gsyh = new Bank("中国工商银行", ATMList2);
       Bank nyyh = new Bank("中国农业银行", ATMList3);
       cardList1.add("6217000010041315709");
       cardList1.add("6217000010041315715");
       Account account0 = new Account("杨过",  "88888888", 10000.00, jsyh, cardList1);
       ArrayList<String> cardList2 = new ArrayList<>();
       cardList2.add("6217000010041315718");
       Account account1 = new Account("杨过",  "88888888", 10000.00, jsyh, cardList2);
       ArrayList<String> cardList3 = new ArrayList<>();
       cardList3.add("6217000010051320007");
       Account account2 = new Account("郭靖",  "88888888", 10000.00, jsyh, cardList3);
       ArrayList<String> cardList4 = new ArrayList<>();
       cardList4.add("6222081502001312389");
       Account account3 = new Account("张无忌",  "88888888", 10000.00, gsyh, cardList4);
       ArrayList<String> cardList5 = new ArrayList<>();
       cardList5.add("6222081502001312390");
       Account account4 = new Account("张无忌",  "88888888", 10000.00, gsyh, cardList5);
       ArrayList<String> cardList6 = new ArrayList<>();
       cardList6.add("6222081502001312399");
       cardList6.add("6222081502001312400");
       Account account5 = new Account("张无忌",  "88888888", 10000.00, gsyh, cardList6);
       ArrayList<String> cardList7 = new ArrayList<>();
       cardList7.add("6222081502051320785");
       Account account6 = new Account("韦小宝",  "88888888", 10000.00, gsyh, cardList7);
       ArrayList<String> cardList8 = new ArrayList<>();
       cardList8.add("6222081502051320786");
       Account account7 = new Account("韦小宝",  "88888888", 10000.00, gsyh, cardList8);
       ArrayList<String> cardList9 = new ArrayList<>();
       cardList9.add("6640000010045442002");
       cardList9.add("6640000010045442003");
       Account account8 = new Account("张三丰",  "88888888", 10000.00, jsyh, cardList9);
       ArrayList<String> cardList10 = new ArrayList<>();
       cardList10.add("6640000010045441009");
       Account account9 = new Account("令狐冲",  "88888888", 10000.00, gsyh, cardList10);
       ArrayList<String> cardList11 = new ArrayList<>();
       cardList11.add("6630000010033431001");
       Account account10 = new Account("乔峰",  "88888888", 10000.00, nyyh, cardList11);
       ArrayList<String> cardList12 = new ArrayList<>();
       cardList12.add("6630000010033431008");
       Account account11 = new Account("洪七公",  "88888888", 10000.00, nyyh, cardList12);
       ArrayList<Account> accountList = new ArrayList<>();
       accountList.add(account0);
       accountList.add(account1);
       accountList.add(account2);
       accountList.add(account3);
       accountList.add(account4);
       accountList.add(account5);
       accountList.add(account6);
       accountList.add(account7);
       accountList.add(account8);
       accountList.add(account9);
       accountList.add(account10);
       accountList.add(account11);
       while (!str.equals("#")) {
           String[] data = str.split("\\s+");

           if (data.length != 1) {
               boolean flag = false;int panduan1=0;
               for(int i=0;i< nums.length;i++){
                   if(data[0].equals(nums[i]))
                       flag = true;
              }
               if(!flag) System.out.println("Sorry,this card does not exist.");
               for(int i=0;i<accountList.size();i++)
              {
                   boolean panduan = false;
                   for(int j=0;j< accountList.get(i).getCardList().size();j++)
                   if(data[0].equals(accountList.get(i).getCardList().get(j)))
                  {
                       if(!accountList.get(i).valiATM(data[2])){
                           System.out.println("Sorry,the ATM's id is wrong.");
                       panduan=true;}
                       else if (!data[1].equals("88888888")){
                           System.out.println("Sorry,your password is wrong.");
                       panduan=true;}

                       else for(int k=0;k<accountList.get(i).getBank().getATMList().size();k++)
                          {

                               if(!data[2].matches(accountList.get(i).getBank().getATMList().get(k))){panduan1++;}
                          }
                       if(panduan1==accountList.get(i).getBank().getATMList().size()){
                           if(accountList.get(i).checkMoney(Double.parseDouble(data[3])))
                          {
                               panduan = true;
                               if(i<=7&&i>=0)
                                   System.out.println("Sorry,your account balance is insufficient.");
                               else if(accountList.get(i).whetheroverDraft(Double.parseDouble(data[3]))&&i<=11&&i>=8){
                                   System.out.println("Sorry,your account balance is insufficient.");
                                   //accountList.get(i).overDraft(Double.parseDouble(data[3]));
                              }
                               else {
                                   System.out.printf("业务:取款 " + accountList.get(i).getName() + "在" + account0.bankName(data[2]) + "的" + data[2] + "号ATM机上取款¥%.2f\n", Double.parseDouble(data[3]));
                                   accountList.get(i).overDraft(Double.parseDouble(data[3]));
                                   accountList.get(i).crossBank(Double.parseDouble(data[3]), data[2]);
                                   System.out.printf("当前余额为¥%.2f\n", accountList.get(i).getMoney());
                              }
                          }
                           else {
                               panduan =true;
                               System.out.printf("业务:取款 "+accountList.get(i).getName()+"在"+accountList.get(i).bankName(data[2])+"的"+data[2]+"号ATM机上取款¥%.2f\n", Double.parseDouble(data[3]));

                               accountList.get(i).crossBank(Double.parseDouble(data[3]),data[2]);
                               System.out.printf("当前余额为¥%.2f\n", accountList.get(i).getMoney());
                          }

                      }
                       if(!panduan&&flag){
                           if(Double.parseDouble(data[3])>0){
                               if(i<=7&&i>=0)
                                   System.out.println("Sorry,your account balance is insufficient.");
                               else if(accountList.get(i).whetheroverDraft(Double.parseDouble(data[3]))&&i<=11&&i>=8){
                                   System.out.println("Sorry,your account balance is insufficient.");
                                   //accountList.get(i).overDraft(Double.parseDouble(data[3]));
                              }
                               else {
                                   System.out.printf("业务:取款 " + accountList.get(i).getName() + "在" + accountList.get(i).bankName(data[2]) + "的" + data[2] + "号ATM机上取款¥%.2f\n", Double.parseDouble(data[3]));

                                   if (accountList.get(i).checkMoney(Double.parseDouble(data[3])))
                                       accountList.get(i).overDraft(Double.parseDouble(data[3]));
                                   accountList.get(i).changeMoney(Double.parseDouble(data[3]));
                                   System.out.printf("当前余额为¥%.2f\n", accountList.get(i).getMoney());
                              }
                          }
                           else {
                               System.out.printf("业务:取款 "+ accountList.get(i).getName()+"在"+accountList.get(i).bankName(data[2])+"的"+ data[2] + "号ATM机上存款¥%.2f\n", Double.parseDouble(data[3]));
                               accountList.get(i).changeMoney(Double.parseDouble(data[3]));
                               System.out.printf("当前余额为¥%.2f\n", accountList.get(i).getMoney());
                          }


                      }

                  }
              }

               str = sc.nextLine();
          }
           else {
               boolean flag = false;
               for(int i=0;i< nums.length;i++){
                   if(data[0].equals(nums[i]))
                       flag = true;
              }
               if(!flag) System.out.println("Sorry,this card does not exist.");
               if (data[0].equals("6217000010041315709") || data[0].equals("6217000010041315715"))
                   System.out.printf("业务:查询余额 ¥%.2f\n",account0.getMoney());
               else if (data[0].equals("6217000010041315718"))
                   System.out.printf("业务:查询余额 ¥%.2f\n",account1.getMoney());
               else if (data[0].equals("6217000010051320007") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account2.getMoney());
               else if (data[0].equals("6222081502001312389") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account3.getMoney());
               else if (data[0].equals("6222081502001312390") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account4.getMoney());
               else if (data[0].equals("6222081502001312399") ||data[0].equals("6222081502001312400"))
                   System.out.printf("业务:查询余额 ¥%.2f\n",account5.getMoney());
               else if (data[0].equals("6222081502051320785") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account6.getMoney());
               else if (data[0].equals("6222081502051320786") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account7.getMoney());
               else if (data[0].equals("6640000010045442002")||data[0].equals("6640000010045442003") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account8.getMoney());
               else if (data[0].equals("6640000010045441009") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account9.getMoney());
               else if (data[0].equals("6630000010033431001") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account10.getMoney());
               else if (data[0].equals("6630000010033431008") )
                   System.out.printf("业务:查询余额 ¥%.2f\n",account11.getMoney());
               str = sc.nextLine();
          }


      }
  }
}
class Bank {
   String bankname;
   ArrayList<String> ATMList;
   //创建无参构造方法
   public Bank(){
  }
   //创建带参构造方法
   public Bank(String bankname,ArrayList<String> ATMList)
  {
       this.bankname=bankname;
       this.ATMList=ATMList;
  }
   //getter
   public String getBankname() {
       return bankname;
  }
   public ArrayList<String> getATMList() {
       return ATMList;
  }
   //setter
   public void setBankname(String bankname){
       this.bankname=bankname;
  }
   public void setATMList(ArrayList<String> tATMList){
       this.ATMList=ATMList;
  }

}
class Account{
   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   public String getAccount() {
       return account;
  }

   public void setAccount(String account) {
       this.account = account;
  }

   public double getMoney() {
       return money;
  }

   public void setMoney(double money) {
       this.money = money;
  }

   public String getPassword() {
       return password;
  }

   public void setPassword(String password) {
       this.password = password;
  }

   public Bank getBank() {
       return bank;
  }

   public void setBank(Bank bank) {
       this.bank = bank;
  }

   public ArrayList<String> getCardList() {
       return cardList;
  }

   public void setCardList(ArrayList<String> cardList) {
       this.cardList = cardList;
  }

   private String name;
   private String account;
   private String password;
   private double money;

   private Bank bank;
   private ArrayList<String>cardList;
   public Account(){};
   public Account(String name,String password,double money,Bank bank,ArrayList<String>cardList){
       this.name=name;
       this.password=password;
       this.money = money;
       this.bank =bank;
       this.cardList =cardList;
  }
   public double changeMoney(double money){
       if(money>0)
           this.money-=money;
       else this.money-=money;
       return this.money;
  }
   public boolean checkMoney(double money){
       if(money>0&&Math.abs(money)>=this.money)
           return true;
       return false;

  }
   public boolean valiATM(String num){
       if(num.matches("0[1-9]|1[01]"))
           return true;
       return false;
  }
   public void crossBank(double money,String data){
       if(data.matches("0[1-4]")){
           this.money-=money*1.02;
      }
       if(data.matches("0[5-6]")){
           this.money-=money*1.03;
      }
       if(data.matches("0[7-9]|1[01]")){
           this.money-=money*1.04;
      }
  }
   public boolean whetheroverDraft(double money){
       if(this.money>0) {
           if (this.money - money - Math.abs(this.money - money) * 0.05 < -50000) {

               return true;
          }
      }
       else {
           if (this.money - money - Math.abs(money) * 0.05 < -50000) {
               return true;
          }
      }
       return false;
  }
   public void overDraft(double money){
       if (this.money>0)  this.money=this.money-Math.abs(this.money-money)*0.05;
       else {
           this.money=this.money-Math.abs(money)*0.05;
      }
  }
   public String bankName(String str){
       String name="";
       if(str.matches("0[1-4]")){
           name = "中国建设银行";
      }
       if(str.matches("0[5-6]")){
           name = "中国工商银行";
      }
       if(str.matches("0[7-9]|1[01]")){
           name = "中国农业银行";
      }
       return name;
  }
}

心得

明白了借记账户和贷记账户的区别,学会了熟悉的遍历ArrayList以及如何用boolean变量去进行逻辑的特殊判断,也对正则表达式和聚合模式有了更进一步的理解。


3.踩坑心得

易踩坑点一:

7-5 ATM机类结构设计(二):刚开始没有理解借记账号和贷记账号的区别,没有进行特判分类,借记账号只能在余额范围内进行取款,而贷记账号才可以进行透支取款。

解决方法:与同学交流,并且反复阅读题面并上网查阅相关的资料。


易踩坑点二:

7-5 ATM机类结构设计(二):在测试借记卡跨行取所有的余额的测试点中,我是用checkmoney函数来判断是否余额为负数,但是没有特判是否刚好为零的情况。

解决方法:对着测试点进行特殊样例调试,找到对应出错的函数进行分析并仔细思考问题所在。


4.改进建议

改进见解

在OPP6训练集的题目,个人认为某些实体类在做题中没有作用便没有对其进行相关的创建,并且有些地方没有严格遵循单一职责原则,有些地方有些面向过程编程,以上问题可能使我的代码缺少了很多java的规范性,尽管麻烦,但为了养成良好的java的书写规范要尽快适应java的各种规则,对所有的实体类进行创建,严格遵循单一职责原则,且时刻要遵循面向对象编程的原则。


5.总结

  • 锻炼提示了我编程的逻辑思维能力

  • 更加熟悉了split,Double.parseDouble()这种常用重要函数的使用

  • 学会了如何应用聚合的模式

  • 对正则表达式有了进一步的理解

  • 对于输入的字符串数据进行处理传参的方法更见娴熟