1. 1.    前言

这三次的主要知识点涉及到了题目集7的类的继承与多态、接口的应用、ArrayList链表排序、题目集8的类的设计、字符串的处理以及题目集9在题目集8上面的叠加。

 

题目集7有两道题7-1 图形卡片排序游戏、7-2 图形卡片分组游戏,7-1主要考察了继承、多态的应用;ArrayList泛型的应用方法、Comparable接口及泛型的应用、单一职责原则的应用、开-闭”原则的应用;7-2主要考察了类的继承、多态性使用方法以及接口的应用。难度适中,花费时间为1:2。

 

题目集8有一道题为ATM机类结构设计(一)主要考察了类的设计、字符串的处理,这道题完成功能并不难,但是要合理且依照类与类之间的关系来完成类的设计。

 

题目集9在题目集8的基础上增加了功能但类的设计和题目集8相差不大,与题目集8相比花的时间较少。

 

  1. 2.    设计与分析

题目集7 7-1

问题在于如何对所有图形进行统一处理,定义统一接口或父类后,使用父类对象作为参数,在实际操作时会自动调用子类重写的方法,在创建对象的时候将子类对象的引用赋值给父类对象,就可以对所有的图形进行统一操作。

 

 

class DealCardList{

       protected ArrayList<Card> cardList = new ArrayList<Card>();

       DealCardList(){

             

       }

       DealCardList(ArrayList<Integer> list){

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

                     if(list.get(i)==1){

                            double radius = Main.input.nextDouble();

                            cardList.add(new Card(new Circle(radius)));

                     }

                     if(list.get(i)==2){

                            double width = Main.input.nextDouble();

                            double length = Main.input.nextDouble();

                            cardList.add(new Card(new Rectangle(width,length)));

                     }

                     if(list.get(i)==3){

                            double side1 = Main.input.nextDouble();

                            double side2 = Main.input.nextDouble();

                            double side3 = Main.input.nextDouble();

                            cardList.add( new Card(new Triangle(side1,side2,side3)));

                     }

                     if(list.get(i)==4){

                            double topSide = Main.input.nextDouble();

                            double bottomSide = Main.input.nextDouble();

                            double height = Main.input.nextDouble();

                            cardList.add(new Card(new Trapezoid(topSide,bottomSide,height)));

                     }

              }

       }

       public void cardSort(){

              Collections.sort(cardList);

       }

       public boolean validate(){

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

                     if(!this.cardList.get(i).getShape().validate()){

                            return false;

                     }

              }

              return true;

       }

       public double getAllArea(){

              double sum=0;

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

                     sum = sum + cardList.get(i).getShape().getArea();

              }

              return sum;

       }

       public void showResult(){

              System.out.println("The original list:");

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

              System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              }

              System.out.println();

              System.out.println("The sorted list:");

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

                     cardSort();

                  System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              }

              System.out.println();

              System.out.println("Sum of area:"+String.format("%.2f", getAllArea()));

       }

}

 

题目集7 7-2

 基本和7-1相同,增加了对总面积最大值的统计,只需要在流操作的时候,用提前的定义的最大值变量与计算出的面积进行比较,更新最大值即可。

 

class DealCardList{

       protected ArrayList<Card> cardList = new ArrayList<Card>();

       DealCardList(){

             

       }

       DealCardList(ArrayList<Integer> list){

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

                     if(list.get(i)==1){

                            double radius = Main.input.nextDouble();

                            cardList.add(new Card(new Circle(radius)));

                     }

                     if(list.get(i)==2){

                            double width = Main.input.nextDouble();

                            double length = Main.input.nextDouble();

                            cardList.add(new Card(new Rectangle(width,length)));

                     }

                     if(list.get(i)==3){

                            double side1 = Main.input.nextDouble();

                            double side2 = Main.input.nextDouble();

                            double side3 = Main.input.nextDouble();

                            cardList.add( new Card(new Triangle(side1,side2,side3)));

                     }

                     if(list.get(i)==4){

                            double topSide = Main.input.nextDouble();

                            double bottomSide = Main.input.nextDouble();

                            double height = Main.input.nextDouble();

                            cardList.add(new Card(new Trapezoid(topSide,bottomSide,height)));

                     }

              }

       }

       public void cardSort(){

              Collections.sort(cardList);

       }

       public boolean validate(){

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

                     if(!this.cardList.get(i).getShape().validate()){

                            return false;

                     }

              }

              return true;

       }

       public double getAllArea(){

              double sum1=0;

              double sum2=0;

              double sum3=0;

              double sum4=0;

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

                 if(cardList.get(i).getShape().shapeName=="Circle"){

                        sum1 = sum1 + cardList.get(i).getShape().getArea();

                }

                 if(cardList.get(i).getShape().shapeName=="Rectangle"){

                        sum2= sum2 + cardList.get(i).getShape().getArea();

                 }

                 if(cardList.get(i).getShape().shapeName=="Triangle"){

                        sum4= sum4 + cardList.get(i).getShape().getArea();

                 }

                 if(cardList.get(i).getShape().shapeName=="Trapezoid"){

                        sum4= sum4 + cardList.get(i).getShape().getArea();

                 }

              }

              Double sum5 = Math.max(sum1,Math.max(sum2, Math.max(sum3, sum4)));

              return sum5;

       }

       public void showResult(){

              System.out.println("The original list:");

              System.out.print("[");

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

              System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

              }

              System.out.print("]");

              System.out.println();

              System.out.println("The Separated List:");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Circle"){

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Rectangle"){

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Triangle"){

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Trapezoid"){

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.println("]");

             

              System.out.println("The Separated sorted List:");

              cardSort();

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Circle"){

                            cardSort();

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Rectangle"){

                            cardSort();

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Triangle"){

                            cardSort();

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

              System.out.print("[");

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

                     if(cardList.get(i).getShape().shapeName=="Trapezoid"){

                            cardSort();

                     System.out.print(cardList.get(i).getShape().shapeName+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");

                     }

              }

              System.out.print("]");

             

              System.out.println();

              System.out.println("The max area:"+String.format("%.2f", getAllArea()));

       }

}

 

题目集8

本题主要考查了类的设计

 

 

 

 

 

import java.util.ArrayList;

import java.util.Scanner;

 

public class Main {

       private static String [][]Bankinf = new String[][] {

              {"中国建设银行","01"},

              {"中国建设银行","02"},

              {"中国建设银行","03"},

              {"中国建设银行","04"},

              {"中国工商银行","05"},

              {"中国工商银行","06"},

              {"中国农业银行","07"},

              {"中国农业银行","08"},

              {"中国农业银行","09"},

              {"中国农业银行","10"},

              {"中国农业银行","11"},

       };

       private static String [][]Cardinf = new String[][] {

              {"杨过","中国建设银行","3217000010041315709","10000.00","6217000010041315709","借"},

              {"杨过","中国建设银行","3217000010041315709","10000.00","6217000010041315715","借"},

              {"杨过","中国建设银行","3217000010041315715","10000.00","6217000010041315718","借"},

              {"郭靖","中国建设银行","3217000010051320007","10000.00","6217000010051320007","借"},

              {"张无忌","中国工商银行","3222081502001312389","10000.00","6222081502001312389","借"},

              {"张无忌","中国工商银行","3222081502001312390","10000.00","6222081502001312390","借"},

              {"张无忌","中国工商银行","3222081502001312399","10000.00","6222081502001312399","借"},

              {"张无忌","中国工商银行","3222081502001312399","10000.00","6222081502001312400","借"},

              {"韦小宝","中国工商银行","3222081502051320785","10000.00","6222081502051320785","借"},

              {"韦小宝","中国工商银行","3222081502051320786","10000.00","6222081502051320786","借"},

              {"张三丰","中国建设银行","3640000010045442002","10000.00","6640000010045442002","贷"},

              {"张三丰","中国建设银行","3640000010045442002","10000.00","6640000010045442003","贷"},

              {"令狐冲","中国工商银行","3640000010045441009","10000.00","6640000010045441009","贷"},

              {"乔峰","中国农业银行","3630000010033431001","10000.00","6630000010033431001","贷"},

              {"洪七公","中国农业银行","3630000010033431008","10000.00","6630000010033431008","贷"},

       };

       public static void main(String[] args) {

             

              Scanner sc = new Scanner(System.in);

              String a = sc.nextLine();

              String []strxx = new String[10];

              String []print = new String[10];

              int n=0;

              while(!a.equals("#")) {

                     strxx[n] = a;n++;

                     a = sc.nextLine();

              }

             

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

                    

                     String strxx1=strxx[i];

                    

                    

                    

                    

                     String print1="";

                     String[] y = strxx1.split("\\s+");

                     int flag=0;

                     for(int j=0;j<15;j++) {

                            if(y[0].equals(Cardinf[j][4])) {

                                   flag=1;

                            }

                     }

                     if(flag==0) {

                            print1="Sorry,this card does not exist.\n";

                     }

                     else {

                                   if(y.length==1) {

                                   for(int j=0;j<15;j++) {

                                          if(y[0].equals(Cardinf[j][4])) {

                                                 float balance =Float.parseFloat(Cardinf[j][3]);

                                                 print1="业务:查询余额 ¥"+String.format("%.2f", balance)+"\n";

                                                

                                          }

                                   }

                                }

                              else {

                                   if(y[1].equals("88888888")) {

                                   if(y[2].equals("01")||y[2].equals("02")||y[2].equals("03")||y[2].equals("04")||y[2].equals("05")||y[2].equals("06")||y[2].equals("07")||y[2].equals("08")||y[2].equals("09")||y[2].equals("10")||y[2].equals("11")) {

                                                 String bankname ="";

                                                 for(int m=0;m<11;m++) {

                                                        if(y[2].equals(Bankinf[m][1])) {

                                                               bankname = Bankinf[m][0];

                                                        }

                                                 }

                                                 float sum = 0;

                                                 for(int s=0;s<15;s++) {

                                                 if(y[0].equals(Cardinf[s][4])) {

                                                        if(Cardinf[s][1].equals(bankname)) {//同行

                                                               if(Cardinf[s][5].equals("借")) {

                                                                      if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])) {

                                                                             sum=Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3]);

                                                                             Cardinf[s][3]=sum+"";

                                                                             if(s==0) {

                                                                                   

                                                                                    Cardinf[1][3]=sum+"";

                                                                             }

                                                                             if(s==1) {

                                                                                    Cardinf[0][3]=sum+"";

                                                                             }

                                                                             if(s==6) {

                                                                                    Cardinf[7][3]=sum+"";

                                                                             }

                                                                             if(s==7) {

                                                                                    Cardinf[6][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==10) {

                                                                                    Cardinf[11][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==11) {

                                                                                    Cardinf[10][3]=sum+"";

                                                                                   

                                                                             }

                                                                            

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+Cardinf[s][1]+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                      }

                                                                      else {

                                                                             Cardinf[s][3]="-50000";

                                                                             print1="Sorry,your account balance is insufficient.\n";

                                                                      }

                                                               }

                                                               else if(Cardinf[s][5].equals("贷")) {

                                                                      if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])) {

                                                                             sum=Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3]);

                                                                             Cardinf[s][3]=sum+"";

                                                                             if(s==0) {

                                                                                   

                                                                                    Cardinf[1][3]=sum+"";

                                                                             }

                                                                             if(s==1) {

                                                                                    Cardinf[0][3]=sum+"";

                                                                             }

                                                                             if(s==6) {

                                                                                    Cardinf[7][3]=sum+"";

                                                                             }

                                                                             if(s==7) {

                                                                                    Cardinf[6][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==10) {

                                                                                    Cardinf[11][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==11) {

                                                                                    Cardinf[10][3]=sum+"";

                                                                                   

                                                                             }

                                                                             float money1 = Float.parseFloat(y[3]);

                                                                             print1="业务:取款 "+Cardinf[s][0]+"在"+Cardinf[s][1]+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                      }

                                                                      else if(Float.parseFloat(Cardinf[s][3])+50000/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])>0){

                                                                             sum=(float) ((Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3]))*1.05);

                                                                             Cardinf[s][3]=sum+"";

                                                                             if(s==0) {

                                                                                   

                                                                                    Cardinf[1][3]=sum+"";

                                                                             }

                                                                             if(s==1) {

                                                                                    Cardinf[0][3]=sum+"";

                                                                             }

                                                                             if(s==6) {

                                                                                    Cardinf[7][3]=sum+"";

                                                                             }

                                                                             if(s==7) {

                                                                                    Cardinf[6][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==10) {

                                                                                    Cardinf[11][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==11) {

                                                                                    Cardinf[10][3]=sum+"";

                                                                                   

                                                                             }

                                                                             float money1 = Float.parseFloat(y[3]);

                                                                             print1="业务:取款 "+Cardinf[s][0]+"在"+Cardinf[s][1]+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                     

                                                                      }

                                                                      else if(Float.parseFloat(Cardinf[s][3])+50000/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])<=0) {

                                                                             sum=Float.parseFloat(Cardinf[s][3])-(float) ((Float.parseFloat(y[3]))*1.05);

                                                                             Cardinf[s][3]=sum+"";

                                                                             if(s==0) {

                                                                                   

                                                                                    Cardinf[1][3]=sum+"";

                                                                             }

                                                                             if(s==1) {

                                                                                    Cardinf[0][3]=sum+"";

                                                                             }

                                                                             if(s==6) {

                                                                                    Cardinf[7][3]=sum+"";

                                                                             }

                                                                             if(s==7) {

                                                                                    Cardinf[6][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==10) {

                                                                                    Cardinf[11][3]=sum+"";

                                                                                   

                                                                             }

                                                                             if(s==11) {

                                                                                    Cardinf[10][3]=sum+"";

                                                                                   

                                                                             }

                                                                             float money1 = Float.parseFloat(y[3]);

                                                                             print1="业务:取款 "+Cardinf[s][0]+"在"+Cardinf[s][1]+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                     

                                                                      }

                                                                      else {

                                                                             Cardinf[s][3]="-50000";

                                                                             print1="Sorry,your account balance is insufficient.\n";

                                                                            

                                                                      }

                                                               }

                                                              

                                                        }//同行结束

                                                 else {//跨行

                                                               if(bankname.equals("中国建设银行")) {//跨到建行

                                                                      if(Cardinf[s][5].equals("借")) {

                                                                             if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])*1.02) {

                                                                                    sum=(float) (Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3])*1.02);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   

                                                                                           float money1 = Float.parseFloat(y[3]);

                                                                                           print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                                   

                                                                             }

                                                                             else {

                                                                                    Cardinf[s][3]="-50000";

                                                                                    print1="Sorry,your account balance is insufficient.\n";

                                                                             }

                                                                      }

                                                                      else if(Cardinf[s][5].equals("贷")) {

                                                                             if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])*1.02) {

                                                                                    sum=(float) (Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3])*1.02);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                             }

                                                                             else if(Float.parseFloat(Cardinf[s][3])/1.02+50000/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])>0){

                                                                                    sum=(float) ((Float.parseFloat(Cardinf[s][3])/1.02-Float.parseFloat(y[3]))*1.05);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                             }

                                                                             else if((Float.parseFloat(Cardinf[s][3])+50000)/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])<=0) {

                                                                                    sum=Float.parseFloat(Cardinf[s][3])-(float) ((Float.parseFloat(y[3]))*1.05);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                             }

                                                                             else {

                                                                                    Cardinf[s][3]="-50000";

                                                                                    print1="Sorry,your account balance is insufficient.\n";

                                                                                   

                                                                             }

                                                                      }

                                                               }//跨到建行结束

                                                               if(bankname.equals("中国工商银行")) {//跨到工行

                                                                      if(Cardinf[s][5].equals("借")) {

                                                                             if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])*1.03) {

                                                                                    sum=(float) (Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3])*1.03);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   

                                                                                           float money1 = Float.parseFloat(y[3]);

                                                                                           print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                                   

                                                                             }

                                                                             else {

                                                                                    Cardinf[s][3]="-50000";

                                                                                    print1="Sorry,your account balance is insufficient.\n";

                                                                             }

                                                                      }

                                                                      else if(Cardinf[s][5].equals("贷")) {

                                                                             if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])*1.03) {

                                                                                    sum=(float) (Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3])*1.03);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                             }

                                                                             else if(Float.parseFloat(Cardinf[s][3])/1.03+50000/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])>0){

                                                                                    sum=(float) ((Float.parseFloat(Cardinf[s][3])/1.03-Float.parseFloat(y[3]))*1.05);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                             }

                                                                             else if((Float.parseFloat(Cardinf[s][3])+50000)/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])<=0) {

                                                                                    sum=Float.parseFloat(Cardinf[s][3])-(float) ((Float.parseFloat(y[3]))*1.05);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                             }

                                                                             else {

                                                                                    Cardinf[s][3]="-50000";

                                                                                    print1="Sorry,your account balance is insufficient.\n";

                                                                                   

                                                                             }

                                                                      }

                                                               }//跨到工行结束

                                                               if(bankname.equals("中国农业银行")) {//跨到农行

                                                                      if(Cardinf[s][5].equals("借")) {

                                                                             if(Float.parseFloat(Cardinf[s][3])>=Float.parseFloat(y[3])*1.04) {

                                                                                    sum=(float) (Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3])*1.04);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   

                                                                                           float money1 = Float.parseFloat(y[3]);

                                                                                           print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                                   

                                                                             }

                                                                             else {

                                                                                    Cardinf[s][3]="-50000";

                                                                                    print1="Sorry,your account balance is insufficient.\n";

                                                                             }

                                                                      }

                                                                      else if(Cardinf[s][5].equals("贷")) {

                                                                             if(Float.parseFloat(Cardinf[s][3])/1.04>=Float.parseFloat(y[3])) {

                                                                                   

                                                                                    sum=(float) (Float.parseFloat(Cardinf[s][3])-Float.parseFloat(y[3])*1.04);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                             }

                                                                             else if(Float.parseFloat(Cardinf[s][3])/1.04+50000/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])>0){

                                                                                    sum=(float) ((Float.parseFloat(Cardinf[s][3])/1.04-Float.parseFloat(y[3]))*1.05);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                             }

                                                                             else if((Float.parseFloat(Cardinf[s][3])+50000)/1.05>=Float.parseFloat(y[3])&&Float.parseFloat(Cardinf[s][3])<=0) {

                                                                                    sum=Float.parseFloat(Cardinf[s][3])-(float) ((Float.parseFloat(y[3]))*1.05);

                                                                                    Cardinf[s][3]=sum+"";

                                                                                    if(s==0) {

                                                                                          

                                                                                           Cardinf[1][3]=sum+"";

                                                                                    }

                                                                                    if(s==1) {

                                                                                           Cardinf[0][3]=sum+"";

                                                                                    }

                                                                                    if(s==6) {

                                                                                           Cardinf[7][3]=sum+"";

                                                                                    }

                                                                                    if(s==7) {

                                                                                           Cardinf[6][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==10) {

                                                                                           Cardinf[11][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                    if(s==11) {

                                                                                           Cardinf[10][3]=sum+"";

                                                                                          

                                                                                    }

                                                                                   float money1 = Float.parseFloat(y[3]);

                                                                                    print1="业务:取款 "+Cardinf[s][0]+"在"+bankname+"的"+y[2]+"号ATM机上取款¥"+String.format("%.2f", money1)+"\n当前余额为¥"+String.format("%.2f", sum)+"\n";

                                                                            

                                                                             }

                                                                             else {

                                                                                    Cardinf[s][3]="-50000";

                                                                                    print1="Sorry,your account balance is insufficient.\n";

                                                                                   

                                                                             }

                                                                      }

                                                               }//跨到农行结束

                                                       

                                                        }//跨行结束

                                                 }

                                              }

                                          }

                                          else {

                                                 print1="Sorry,the ATM's id is wrong.\n";

                                          }

                                         

                                   }

                                   else {

                                          print1="Sorry,your password is wrong.\n";

                                   }

                               }

                     }

                    

                    

                     print[i]=print1;

              }

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

                     System.out.print(print[i]);

              }

       }

      

}

 

 

题目集9

从老师的代码中可以看到很明显的对应关系,利用老师的代码在上一题的基础上,多了一个贷款账户,只需要在操作类中新增两个方法。

 

 

/**

        * 校验取款金额是否大于余额

        */

       if (amount > balance-amount*aTM.getBank().getRate()&&account.getBankKind().equals("借记账号")) {

                     System.out.println("Sorry,your account balance is insufficient.");

                     System.exit(0);

              }

              if (amount+amount*aTM.getBank().getRate()-balance*0.05 > balance+50000&&account.getBankKind().equals("贷记账号")&&balance<=0) {

                     System.out.println("Sorry,your account balance is insufficient.");

                     System.exit(0);

              }

              if (amount+amount*aTM.getBank().getRate()-(amount - balance)*0.05 > balance+50000&&account.getBankKind().equals("贷记账号")&&balance>0) {

                     System.out.println("Sorry,your account balance is insufficient.");

                     System.exit(0);

              }

             

 

              /**

  * 跨行以及借记

  */

       if (account.getBank().getBankNO() !=      aTM.getBank().getBankNO()&&account.getBankKind().equals("借记账号")) {

                     account.setBalance(balance - amount-amount*aTM.getBank().getRate());

              }     

       /**

        * 不跨行以及借记

        */

       if (account.getBank().getBankNO() == aTM.getBank().getBankNO()&&account.getBankKind().equals("借记账号")) {

                     account.setBalance(balance - amount);

              }

       /**

       * 不跨行以及贷记

       */

              if (account.getBank().getBankNO() == aTM.getBank().getBankNO()&&account.getBankKind().equals("贷记账号")) {

                     if(balance>=amount) {

                     account.setBalance(balance - amount);

                     }else if(amount>balance&&balance>0) {

                            account.setBalance(balance - amount - (amount - balance)*0.05);

                     }else if(amount>balance&&balance<=0) {

                            account.setBalance(balance - amount - amount *0.05);

                     }

              }

       /**

       * 跨行以及贷记

        */

              if (account.getBank().getBankNO() != aTM.getBank().getBankNO()&&account.getBankKind().equals("贷记账号")) {

                     if(balance>=amount) {

                            account.setBalance(balance - amount - amount*aTM.getBank().getRate());

                            }else if(amount>balance&&balance>0) {

                                   account.setBalance(balance - amount - (amount - balance)*0.05-amount*aTM.getBank().getRate());

                            }else if(amount>balance&&balance<=0) {

                                   account.setBalance(balance - amount - amount *0.05-amount*aTM.getBank().getRate());

                            }

}

 

  1. 3.    踩坑心得

提交的public class类名不为Main,比如,提交的类名如果是public class Main1就会出错。

提交的代码中有多个public class。注意:提交的代码中只能有一个public class。

复制的时候将程序第一行package也复制进来。

最新版的PTA使用Open JDK 8,已经支持Java 8。估计还不支持Java 9请不要用Java 9中的新语法。

显示格式错误,一般是因为你的输出最后一行少了回车换行,或者每行的行尾多了空格之类的错误.

 

  1. 4.    改进建议

作业难度的话建议还是可以呈现梯形,这样做起来不会觉得那么困难。题量3~5题比较合理。

  1. 5.    总结

这几次OO编程训练以来,逐渐养成面向对象的编程思维,一次次的加深我对其思想和设计的理解和体会。解决一个个问题实现一个个功能时,会想到一个个模块地去解决,同时也会遇到一点困难,逐步认识自己的不足,但解决之后就会有收获的,所谓”需求推动进步“。通过本阶段的作业,抛开基本的继承、多态,还学习到单一职责原则和“开-闭”原则,这样大大提高代码的质量,收获很大。

 

Posted on 2021-06-20 17:24  满洲里有海  阅读(33)  评论(0编辑  收藏  举报