面向对象程序设计-第三次题目集博客作业
1.前言
这三次题目集的难度在线,但是又不算很难;就本人来说这比正则表达式好做得多!本次题目集主要考察类的继承、多态性使用方法以及接口的应用。都比较好掌握,因为前面的作业集也出现过类似的题目,所以做起来也是得心应手。自我感觉已经能够熟练地去利用类的继承和多态来达到自己的目的,接口类感觉和普通的父类区别不大,也许是我还没发现他的闪光点吧,废话不多说,马上进入正篇。
2.具体题目分析
7-1 图形卡片排序游戏
在之前的题目集中,也出现过类似这种题目,先输入各图形卡片代表的数字 ,然后根据这个顺序来填写对应图形的半径,长宽,高等,输出对应的图形类型和面积;再对他们进行排序。唯一不同的是多加了一种梯形类型的图形。

按照类图指引,我们可以看到除了常规三角形类,梯形类,圆形类,矩形类作为子类以及图形类的父类,还有一个接口。(虽然接口的内容什么都没有),根据题目要求,我们只需要在父类中定义求面积的方法,然后在各子类中重写对应的求面积方法即可。对其进行同步排序也是只需要运用一个数组进行同步标记,然后根据类型和大小来进行排序,正常输出即可,没什么难度。下面是代码。
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); DecimalFormat df = new DecimalFormat("0.00"); int card; int flag=0; int circle_num=0; int retangle_num=0; int triangle_num=0; int trapezoid_num=0; int []record=new int[100]; int record_cir=0,record_tri=0,record_re=0,record_tr=0; int num=0; double cir_area,re_area,tri_area,tr_area; double radius,width,length,size1,size2,size3,height_size,low_size,height; ArrayList<Circle> a=new ArrayList(); ArrayList<Retangle> b=new ArrayList(); ArrayList<triangle> c=new ArrayList(); ArrayList<trapezoid> d=new ArrayList(); do { card=sc.nextInt(); if(card==1) { a.add(new Circle()); circle_num++; record[num++]=card; } else if(card==2) { b.add(new Retangle()); retangle_num++; record[num++]=card; } else if(card==3) { c.add(new triangle()); triangle_num++; record[num++]=card; } else if(card==4) { d.add(new trapezoid()); trapezoid_num++; record[num++]=card; } else if(card!=0) { flag=1; } } while(card!=0&&flag==0); if(flag==1) { System.out.print("Wrong Format"); } else { double []all=new double[circle_num+retangle_num+triangle_num+trapezoid_num]; int allnum=0; double allarea=0; double temp; int temp2; for(int i=0;i<num;i++) { switch(record[i]) { case 1: radius=sc.nextDouble(); if(radius<=0) { flag=1; } a.get(record_cir).setRadius(radius); record_cir++; break; case 2: width=sc.nextDouble(); length=sc.nextDouble(); if(width<=0||length<=0) { flag=1; } b.get(record_re).setWidth(width); b.get(record_re).setLength(length); record_re++; break; case 3: size1=sc.nextDouble(); size2=sc.nextDouble(); size3=sc.nextDouble(); if(size1<0||size2<0||size3<0) { flag=1; } if(size1>=(size1+size2)||size2>=(size1+size3)||size3>=(size1+size2)) { flag=1; } c.get(record_tri).setSize(size1, size2, size3); record_tri++; break; case 4: height_size=sc.nextDouble(); low_size=sc.nextDouble(); height=sc.nextDouble(); if(height_size<0||low_size<0||height<0) { flag=1; } d.get(record_tr).setSize(height_size, low_size, height); record_tr++; break; } } record_cir=0; record_tri=0; record_re=0; record_tr=0; if(flag==1) { System.out.print("Wrong Format"); } else { System.out.println("The original list:"); for(int i=0;i<num;i++) { switch(record[i]) { case 1: cir_area=a.get(record_cir).getArea(); record_cir++; System.out.print("Circle:"+df.format(cir_area)+" "); all[allnum++]=cir_area; allarea=allarea+cir_area; break; case 2: re_area=b.get(record_re).getArea(); record_re++; System.out.print("Rectangle:"+df.format(re_area)+" "); all[allnum++]=re_area; allarea=allarea+re_area; break; case 3: tri_area=c.get(record_tri).getArea(); record_tri++; System.out.print("Triangle:"+df.format(tri_area)+" "); all[allnum++]=tri_area; allarea=allarea+tri_area; break; case 4: tr_area=d.get(record_tr).getArea(); record_tr++; System.out.print("Trapezoid:"+df.format(tr_area)+" "); all[allnum++]=tr_area; allarea=allarea+tr_area; break; } } for(int i=0;i<allnum-1;i++) { for(int j=0;j<allnum-1;j++) { if(all[j]<all[j+1]) { temp=all[j]; all[j]=all[j+1]; all[j+1]=temp; temp2=record[j]; record[j]=record[j+1]; record[j+1]=temp2; } } } System.out.println(""); System.out.println("The sorted list:"); for(int i=0;i<num;i++) { switch(record[i]) { case 1: System.out.print("Circle:"+df.format(all[i])+" "); break; case 2: System.out.print("Rectangle:"+df.format(all[i])+" "); break; case 3: System.out.print("Triangle:"+df.format(all[i])+" "); break; case 4: System.out.print("Trapezoid:"+df.format(all[i])+" "); break; } } System.out.println(""); System.out.print("Sum of area:"+df.format(allarea)); } } } } class Circle{ private double radius; Circle(){ } Circle(double radius){ this.radius=radius; } void setRadius(double radius) { this.radius=radius; } double getRadius() { return this.radius; } double getArea() { double area; area=Math.PI*this.radius*this.radius; return area; } } class Retangle{ private double length; private double width; Retangle(){ } Retangle(double length,double width){ this.length=length; this.width=width; } void setLength(double length) { this.length=length; } double getLength() { return this.length; } void setWidth(double width) { this.width=width; } double getWidth() { return this.width; } double getArea() { double area; area=this.length*this.width; return area; } } class triangle{ double size1; double size2; double size3; triangle(){ } triangle(double size1,double size2,double size3){ this.size1=size1; this.size2=size2; this.size3=size3; } void setSize(double size1,double size2,double size3){ this.size1=size1; this.size2=size2; this.size3=size3; } double getArea() { double area; double p=(this.size1+this.size2+this.size3)/2; area=p*(p-this.size1)*(p-this.size2)*(p-this.size3); area=(double)Math.sqrt(area); return area; } } class trapezoid{ double height_size; double low_size; double height; trapezoid(){ } trapezoid(double height_size,double low_size,double height){ this.height_size=height_size; this.low_size=low_size; this.height=height; } void setSize(double height_size,double low_size,double height) { this.height_size=height_size; this.low_size=low_size; this.height=height; } double getArea() { double area; area=(this.height_size+this.low_size)*this.height/2; return area; } }
只要公式没有问题,以及对无法构造成对应图形的边和高进行分析并输入“Wrong Farmer”,其余没有什么大问题。对个人来说,排序是最麻烦的部分。需要处处做标记,然后运算总值再进行输出,稍有不慎就会弄错。这题的大概思路如此,欢迎各位发表更好的见解。
7-2 图形卡片分组游戏
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); DecimalFormat df = new DecimalFormat("0.00"); int card; int flag=0; int circle_num=0; int retangle_num=0; int triangle_num=0; int trapezoid_num=0; int []record=new int[100]; int record_cir=0,record_tri=0,record_re=0,record_tr=0; int num=0; double cir_area,re_area,tri_area,tr_area; double radius,width,length,size1,size2,size3,height_size,low_size,height; double maxarea=0; double maxarea_cir=0; double maxarea_re=0; double maxarea_tri=0; double maxarea_tr=0; ArrayList<Circle> a=new ArrayList(); ArrayList<Retangle> b=new ArrayList(); ArrayList<triangle> c=new ArrayList(); ArrayList<trapezoid> d=new ArrayList(); int count=0; do { card=sc.nextInt(); count++; if(card==1) { a.add(new Circle()); circle_num++; record[num++]=card; } else if(card==2) { b.add(new Retangle()); retangle_num++; record[num++]=card; } else if(card==3) { c.add(new triangle()); triangle_num++; record[num++]=card; } else if(card==4) { d.add(new trapezoid()); trapezoid_num++; record[num++]=card; } else if(card!=0) { flag=1; } } while(card!=0&&flag==0); if(flag==1||count<=1) { System.out.print("Wrong Format"); } else { double []all=new double[circle_num+retangle_num+triangle_num+trapezoid_num]; int allnum=0; double allarea=0; double temp; int temp2; for(int i=0;i<num;i++) { switch(record[i]) { case 1: radius=sc.nextDouble(); if(radius<=0) { flag=1; } a.get(record_cir).setRadius(radius); record_cir++; break; case 2: width=sc.nextDouble(); length=sc.nextDouble(); if(width<=0||length<=0) { flag=1; } b.get(record_re).setWidth(width); b.get(record_re).setLength(length); record_re++; break; case 3: size1=sc.nextDouble(); size2=sc.nextDouble(); size3=sc.nextDouble(); if(size1<0||size2<0||size3<0) { flag=1; } if(size1>=(size1+size2)||size2>=(size1+size3)||size3>=(size1+size2)) { flag=1; } c.get(record_tri).setSize(size1, size2, size3); record_tri++; break; case 4: height_size=sc.nextDouble(); low_size=sc.nextDouble(); height=sc.nextDouble(); if(height_size<0||low_size<0||height<0) { flag=1; } d.get(record_tr).setSize(height_size, low_size, height); record_tr++; break; } } record_cir=0; record_tri=0; record_re=0; record_tr=0; if(flag==1) { System.out.print("Wrong Format"); } else { System.out.println("The original list:"); System.out.print("["); for(int i=0;i<num;i++) { switch(record[i]) { case 1: cir_area=a.get(record_cir).getArea(); record_cir++; System.out.print("Circle:"+df.format(cir_area)+" "); all[allnum++]=cir_area; allarea=allarea+cir_area; maxarea_cir=maxarea_cir+cir_area; if(maxarea_cir>maxarea) { maxarea=maxarea_cir; } break; case 2: re_area=b.get(record_re).getArea(); record_re++; System.out.print("Rectangle:"+df.format(re_area)+" "); all[allnum++]=re_area; allarea=allarea+re_area; maxarea_re=maxarea_re+re_area; if(maxarea_re>maxarea) { maxarea=maxarea_re; } break; case 3: tri_area=c.get(record_tri).getArea(); record_tri++; System.out.print("Triangle:"+df.format(tri_area)+" "); all[allnum++]=tri_area; allarea=allarea+tri_area; maxarea_tri=maxarea_tri+tri_area; if(maxarea_tri>maxarea) { maxarea=maxarea_tri; } break; case 4: tr_area=d.get(record_tr).getArea(); record_tr++; System.out.print("Trapezoid:"+df.format(tr_area)+" "); all[allnum++]=tr_area; allarea=allarea+tr_area; maxarea_tr=maxarea_tr+tr_area; if(maxarea_tr>maxarea) { maxarea=maxarea_tr; } break; } } System.out.println("]"); /*for(int i=0;i<allnum-1;i++) { for(int j=0;j<allnum-1;j++) { if(all[j]<all[j+1]) { temp=all[j]; all[j]=all[j+1]; all[j+1]=temp; temp2=record[j]; record[j]=record[j+1]; record[j+1]=temp2; } } }*/ Iterator<Circle> A=a.iterator(); Iterator<Retangle> B=b.iterator(); Iterator<triangle> C=c.iterator(); Iterator<trapezoid> D=d.iterator(); System.out.println("The Separated List:"); System.out.print("["); while(A.hasNext()) { Circle circle=A.next(); System.out.print("Circle:"+df.format(circle.getArea())+" "); } System.out.print("]"); System.out.print("["); while(B.hasNext()) { Retangle retangle=B.next(); System.out.print("Rectangle:"+df.format(retangle.getArea())+" "); } System.out.print("]"); System.out.print("["); while(C.hasNext()) { triangle triangle=C.next(); System.out.print("Triangle:"+df.format(triangle.getArea())+" "); } System.out.print("]"); System.out.print("["); while(D.hasNext()) { trapezoid trapezoid=D.next(); System.out.print("Trapezoid:"+df.format(trapezoid.getArea())+" "); } System.out.print("]"); System.out.println(""); Collections.sort(a); Collections.sort(b); Collections.sort(c); Collections.sort(d); A=a.iterator(); B=b.iterator(); C=c.iterator(); D=d.iterator(); System.out.println("The Separated sorted List:"); System.out.print("["); while(A.hasNext()) { Circle circle=A.next(); System.out.print("Circle:"+df.format(circle.getArea())+" "); } System.out.print("]"); System.out.print("["); while(B.hasNext()) { Retangle retangle=B.next(); System.out.print("Rectangle:"+df.format(retangle.getArea())+" "); } System.out.print("]"); System.out.print("["); while(C.hasNext()) { triangle triangle=C.next(); System.out.print("Triangle:"+df.format(triangle.getArea())+" "); } System.out.print("]"); System.out.print("["); while(D.hasNext()) { trapezoid trapezoid=D.next(); System.out.print("Trapezoid:"+df.format(trapezoid.getArea())+" "); } System.out.print("]"); System.out.println(""); System.out.print("The max area:"+df.format(maxarea)); } } } } class Circle implements Comparable<Circle>{ private double radius; Circle(){ } Circle(double radius){ this.radius=radius; } void setRadius(double radius) { this.radius=radius; } double getRadius() { return this.radius; } double getArea() { double area; area=Math.PI*this.radius*this.radius; return area; } public int compareTo(Circle object) { if(this.getArea()>object.getArea()){ return -1; } else if(this.getArea()<object.getArea()){ return 1; } return 0; } } class Retangle implements Comparable<Retangle>{ private double length; private double width; Retangle(){ } Retangle(double length,double width){ this.length=length; this.width=width; } void setLength(double length) { this.length=length; } double getLength() { return this.length; } void setWidth(double width) { this.width=width; } double getWidth() { return this.width; } double getArea() { double area; area=this.length*this.width; return area; } public int compareTo(Retangle object) { if(this.getArea()>object.getArea()){ return -1; } else if(this.getArea()<object.getArea()){ return 1; } return 0; } } class triangle implements Comparable<triangle>{ double size1; double size2; double size3; triangle(){ } triangle(double size1,double size2,double size3){ this.size1=size1; this.size2=size2; this.size3=size3; } void setSize(double size1,double size2,double size3){ this.size1=size1; this.size2=size2; this.size3=size3; } double getArea() { double area; double p=(this.size1+this.size2+this.size3)/2; area=p*(p-this.size1)*(p-this.size2)*(p-this.size3); area=(double)Math.sqrt(area); return area; } public int compareTo(triangle object) { if(this.getArea()>object.getArea()){ return -1; } else if(this.getArea()<object.getArea()){ return 1; } return 0; } } class trapezoid implements Comparable<trapezoid>{ double height_size; double low_size; double height; trapezoid(){ } trapezoid(double height_size,double low_size,double height){ this.height_size=height_size; this.low_size=low_size; this.height=height; } void setSize(double height_size,double low_size,double height) { this.height_size=height_size; this.low_size=low_size; this.height=height; } double getArea() { double area; area=(this.height_size+this.low_size)*this.height/2; return area; } public int compareTo(trapezoid object) { if(this.getArea()>object.getArea()){ return -1; } else if(this.getArea()<object.getArea()){ return 1; } return 0; } }
7-3 ATM机类结构设计(一)
本题需要我们编写一个ATM机的模拟程序,能够完成用户的存款取款,以及查询余额功能。本题主要通过提前对数据的录入,然后根据输入的内容长度来判断需要执行的功能,然后输出对应的结果。需要注意的就只有几个点:1.判断输入的长度来决定要执行存款取款还是查询余额。2.如果是存款,就只需要判断卡号和密码的输入是否有问题。3.如果是取款,除了判断卡号和密码是否有误以外还要判断余额是否足够。这题的难点就体现在上述几点。4.是否进行了跨行操作,本题不允许跨行操作。根据以下规则来判断是否有误:
boolean check() { int flag=0; int i,j; int k=0; int p=0; //检查账号是否正确 for(i=0;i<checklist.size();i++){ for(j=0;j<checklist.get(i).getAccountlist().size();j++){ if (account.equals(checklist.get(i).getAccountlist().get(j))){ flag=1; k=i; break; } } if(flag==1){ break; } } //检查密码是否正确 if(flag==1){ if(password.equals(checklist.get(k).passwork)){ flag=2; } else{ System.out.println("Sorry,your password is wrong.");//银行卡密码错误 return false; } } else{ System.out.println("Sorry,this card does not exist.");//卡号不存在 return false; } //检查ATM机编号是否正确 if(flag==2){ for(i=0;i<checkbank_list.size();i++){ for(j=0;j<checkbank_list.get(i).getAtm().size();j++){ if(atm.equals(checkbank_list.get(i).getAtm().get(j))){ flag=3; p=i; break; } } if(flag==3) { break; } } } //检查金额是否正确 if(flag==3){ if(money<=checklist.get(k).account){ flag=4; } else{ System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额 return false; } } else{ System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在 return false; } //检查是否跨行 if(flag==4){ if(checklist.get(k).user_bank.equals(checkbank_list.get(p).bank_name)){ flag=5; } } if(flag!=5){ System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款 return false; } else return true; } boolean check1() { int i,j; int flag=0; for(i=0;i<checklist.size();i++){ for(j=0;j<checklist.get(i).getAccountlist().size();j++){ if (account.equals(checklist.get(i).getAccountlist().get(j))){ flag=1; break; } } if(flag==1){ break; } } if(flag==1){ return true; } else { return false; } }
然后根据以下代码来判断输入的长度来决定功能:
while(!date.equals("#")) { String[] shuju=date.split("\\s+"); if(shuju.length!=1){ Check check = new Check(list,list_bank,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); if (check.check()){ for(int i=0;i<list.size();i++){ for(int j=0;j<list.get(i).getAccountlist().size();j++){ if (shuju[0].equals(list.get(i).getAccountlist().get(j))){ flag=1; temp=i; break; } } if(flag==1){ break; } }
本题需要特别小心,因为数据有点多,在录入的时候不要出现错误,否则会有测试点过不去的。(本人就是因为在录入的过程中录入了错误的数据,导致出现了错误的结果但是一直找不到逻辑上的错误而浪费很多的时间)。
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); ArrayList <Account>list=new ArrayList <Account>(); ArrayList <Bank>list_bank=new ArrayList <Bank>(); //================================================================================== ArrayList <String>yangguo1=new ArrayList <String>();//杨过的信息 yangguo1.add("6217000010041315709"); yangguo1.add("6217000010041315715"); ArrayList <String>yangguo2=new ArrayList <String>(); yangguo2.add("6217000010041315718"); Account yangguo_1=new Account("杨过","中国建设银行","3217000010041315709",10000,yangguo1,"88888888"); Account yangguo_2=new Account("杨过","中国建设银行","3217000010041315715",10000,yangguo2,"88888888"); ArrayList <String>guojing1=new ArrayList <String>();//郭靖的信息 guojing1.add("6217000010051320007"); Account guojing_1=new Account("郭靖","中国建设银行","3217000010051320007",10000,guojing1,"88888888"); ArrayList <String>zhangwuji1=new ArrayList <String>();//张无忌的信息 zhangwuji1.add("6222081502001312389"); Account zhangwuji_1=new Account("张无忌","中国工商银行","3222081502001312389",10000,zhangwuji1,"88888888"); ArrayList <String>zhangwuji2=new ArrayList <String>(); zhangwuji2.add("6222081502001312390"); Account zhangwuji_2=new Account("张无忌","中国工商银行","3222081502001312390",10000,zhangwuji2,"88888888"); ArrayList <String>zhangwuji3=new ArrayList <String>(); zhangwuji3.add("6222081502001312399"); zhangwuji3.add("6222081502001312400"); Account zhangwuji_3=new Account("张无忌","中国工商银行","3222081502001312399",10000,zhangwuji3,"88888888"); ArrayList <String>weixiaobao1=new ArrayList <String>();//韦小宝的信息 weixiaobao1.add("6222081502051320785"); Account weixiaobao_1=new Account("韦小宝","中国工商银行","3222081502001312399",10000,weixiaobao1,"88888888"); ArrayList <String>weixiaobao2=new ArrayList <String>(); weixiaobao2.add("6222081502051320786"); Account weixiaobao_2=new Account("韦小宝","中国工商银行","3222081502001312399",10000,weixiaobao2,"88888888"); //====================================================================================================== list.add(yangguo_1); list.add(yangguo_2); list.add(guojing_1); list.add(zhangwuji_1); list.add(zhangwuji_2); list.add(zhangwuji_3); list.add(weixiaobao_1); list.add(weixiaobao_2); ArrayList <String>jianshe=new ArrayList <String>(); jianshe.add("01"); jianshe.add("02"); jianshe.add("03"); jianshe.add("04"); Bank jianshebank=new Bank("中国建设银行",jianshe);//中国建设银行信息 ArrayList <String>gongshang=new ArrayList <String>(); gongshang.add("05"); gongshang.add("06"); Bank gongshangbank=new Bank("中国工商银行",gongshang);//中国工商银行信息 list_bank.add(jianshebank); list_bank.add(gongshangbank); //====================================================================================================== String date; int temp=0; int flag=0; date=sc.nextLine(); DecimalFormat df = new DecimalFormat("0.00"); while(!date.equals("#")) { String[] shuju=date.split("\\s+"); if(shuju.length!=1){ Check check = new Check(list,list_bank,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); if (check.check()){ for(int i=0;i<list.size();i++){ for(int j=0;j<list.get(i).getAccountlist().size();j++){ if (shuju[0].equals(list.get(i).getAccountlist().get(j))){ flag=1; temp=i; break; } } if(flag==1){ break; } } list.get(temp).account=list.get(temp).account-Double.parseDouble(shuju[3]); if(Double.parseDouble(shuju[3])>0) System.out.println(list.get(temp).user_name+"在"+list.get(temp).user_bank+"的"+shuju[2]+"号ATM机上取款¥"+df.format(Double.parseDouble(shuju[3]))); else System.out.println(list.get(temp).user_name+"在"+list.get(temp).user_bank+"的"+shuju[2]+"号ATM机上存款¥"+df.format(-Double.parseDouble(shuju[3]))); System.out.println("当前余额为¥"+df.format(list.get(temp).account)); } } else { Check check1=new Check(list,shuju[0]); if(check1.check1()){ for(int i=0;i<list.size();i++){ for(int j=0;j<list.get(i).getAccountlist().size();j++){ if (shuju[0].equals(list.get(i).getAccountlist().get(j))){ flag=1; temp=i; break; } } if(flag==1){ break; } } System.out.println("¥"+df.format(list.get(temp).account)); } } date=sc.nextLine(); } } } class Bank{//存储银行名字和ATM编号 String bank_name; ArrayList <String> atm; Bank(String bank_name,ArrayList <String> atm){ this.bank_name=bank_name; this.atm=atm; } ArrayList <String> getAtm(){ return this.atm; } } class Account{ String user_name; String user_bank;//使用者隶属于哪个银行 double account; String bankaccount; ArrayList<String> accountlist; String passwork; Account(String user_name,String user_bank,String bankaccount,double account,ArrayList<String> accountlist,String passwork){ this.user_name=user_name; this.user_bank=user_bank; this.account=account; this.bankaccount=bankaccount; this.accountlist=accountlist; this.passwork=passwork; } ArrayList<String> getAccountlist(){ return this.accountlist; } } class Check{ ArrayList <Account>checklist; ArrayList <Bank>checkbank_list; String account; String password; String atm; double money; Check(ArrayList <Account>checklist,String account){ this.checklist=checklist; this.account=account; } Check(ArrayList <Account>checklist,ArrayList <Bank>checkbank_list,String account,String password,String atm,double money){ this.checklist=checklist; this.checkbank_list=checkbank_list; this.account=account; this.password=password; this.atm=atm; this.money=money; } boolean check() { int flag=0; int i,j; int k=0; int p=0; //检查账号是否正确 for(i=0;i<checklist.size();i++){ for(j=0;j<checklist.get(i).getAccountlist().size();j++){ if (account.equals(checklist.get(i).getAccountlist().get(j))){ flag=1; k=i; break; } } if(flag==1){ break; } } //检查密码是否正确 if(flag==1){ if(password.equals(checklist.get(k).passwork)){ flag=2; } else{ System.out.println("Sorry,your password is wrong.");//银行卡密码错误 return false; } } else{ System.out.println("Sorry,this card does not exist.");//卡号不存在 return false; } //检查ATM机编号是否正确 if(flag==2){ for(i=0;i<checkbank_list.size();i++){ for(j=0;j<checkbank_list.get(i).getAtm().size();j++){ if(atm.equals(checkbank_list.get(i).getAtm().get(j))){ flag=3; p=i; break; } } if(flag==3) { break; } } } //检查金额是否正确 if(flag==3){ if(money<=checklist.get(k).account){ flag=4; } else{ System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额 return false; } } else{ System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在 return false; } //检查是否跨行 if(flag==4){ if(checklist.get(k).user_bank.equals(checkbank_list.get(p).bank_name)){ flag=5; } } if(flag!=5){ System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款 return false; } else return true; } boolean check1() { int i,j; int flag=0; for(i=0;i<checklist.size();i++){ for(j=0;j<checklist.get(i).getAccountlist().size();j++){ if (account.equals(checklist.get(i).getAccountlist().get(j))){ flag=1; break; } } if(flag==1){ break; } } if(flag==1){ return true; } else { return false; } } }
7-1 ATM机类结构设计(二)
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Scanner sc=new Scanner(System.in); ArrayList <Account>list=new ArrayList <Account>(); ArrayList <Bank>list_bank=new ArrayList <Bank>(); //================================================================================== ArrayList <String>yangguo1=new ArrayList <String>();//杨过的信息 yangguo1.add("6217000010041315709"); yangguo1.add("6217000010041315715"); ArrayList <String>yangguo2=new ArrayList <String>(); yangguo2.add("6217000010041315718"); Account yangguo_1=new Account("杨过","中国建设银行","3217000010041315709",10000,yangguo1,"88888888","jieji"); Account yangguo_2=new Account("杨过","中国建设银行","3217000010041315715",10000,yangguo2,"88888888","jieji"); ArrayList <String>guojing1=new ArrayList <String>();//郭靖的信息 guojing1.add("6217000010051320007"); Account guojing_1=new Account("郭靖","中国建设银行","3217000010051320007",10000,guojing1,"88888888","jieji"); ArrayList <String>zhangwuji1=new ArrayList <String>();//张无忌的信息 zhangwuji1.add("6222081502001312389"); Account zhangwuji_1=new Account("张无忌","中国工商银行","3222081502001312389",10000,zhangwuji1,"88888888","jieji"); ArrayList <String>zhangwuji2=new ArrayList <String>(); zhangwuji2.add("6222081502001312390"); Account zhangwuji_2=new Account("张无忌","中国工商银行","3222081502001312390",10000,zhangwuji2,"88888888","jieji"); ArrayList <String>zhangwuji3=new ArrayList <String>(); zhangwuji3.add("6222081502001312399"); zhangwuji3.add("6222081502001312400"); Account zhangwuji_3=new Account("张无忌","中国工商银行","3222081502001312399",10000,zhangwuji3,"88888888","jieji"); ArrayList <String>weixiaobao1=new ArrayList <String>();//韦小宝的信息 weixiaobao1.add("6222081502051320785"); Account weixiaobao_1=new Account("韦小宝","中国工商银行","3222081502001312399",10000,weixiaobao1,"88888888","jieji"); ArrayList <String>weixiaobao2=new ArrayList <String>(); weixiaobao2.add("6222081502051320786"); Account weixiaobao_2=new Account("韦小宝","中国工商银行","3222081502001312399",10000,weixiaobao2,"88888888","jieji"); ArrayList <String>zhangsanfeng1=new ArrayList <String>();//张三丰的信息 zhangsanfeng1.add("6640000010045442002"); zhangsanfeng1.add("6640000010045442003"); Account zhangsanfeng_1=new Account("张三丰","中国建设银行","3640000010045442002",10000,zhangsanfeng1,"88888888","daiji"); ArrayList <String>linghuchong1=new ArrayList <String>();//令狐冲的信息 linghuchong1.add("6640000010045441009"); Account linghuchong_1=new Account("令狐冲","中国工商银行","3640000010045441009",10000,linghuchong1,"88888888","daiji"); ArrayList <String>qiaofeng1=new ArrayList <String>();//乔峰的信息 qiaofeng1.add("6630000010033431001"); Account qiaofeng_1=new Account("乔峰","中国农业银行","3630000010033431001",10000,qiaofeng1,"88888888","daiji"); ArrayList <String>hongqigong1=new ArrayList <String>();//洪七公的信息 hongqigong1.add("6630000010033431008"); Account hongqigong_1=new Account("洪七公","中国农业银行","3630000010033431008",10000,hongqigong1,"88888888","daiji"); list.add(yangguo_1); list.add(yangguo_2); list.add(guojing_1); list.add(zhangwuji_1); list.add(zhangwuji_2); list.add(zhangwuji_3); list.add(weixiaobao_1); list.add(weixiaobao_2); list.add(zhangsanfeng_1); list.add(linghuchong_1); list.add(qiaofeng_1); list.add(hongqigong_1); ArrayList <String>jianshe=new ArrayList <String>(); jianshe.add("01"); jianshe.add("02"); jianshe.add("03"); jianshe.add("04"); Bank jianshebank=new Bank("中国建设银行",jianshe);//中国建设银行信息 ArrayList <String>gongshang=new ArrayList <String>(); gongshang.add("05"); gongshang.add("06"); Bank gongshangbank=new Bank("中国工商银行",gongshang);//中国工商银行信息 ArrayList <String>nongye=new ArrayList <String>(); nongye.add("07"); nongye.add("08"); nongye.add("09"); nongye.add("10"); nongye.add("11"); Bank nongyebank=new Bank("中国农业银行",nongye);//中国农业银行信息 list_bank.add(jianshebank); list_bank.add(gongshangbank); list_bank.add(nongyebank); String date; date=sc.nextLine(); DecimalFormat df = new DecimalFormat("0.00"); while(!date.equals("#")) { int temp=0; int temp2=0; int flag=0; int flag2=0; int flag3=0; double money=0; String[] shuju=date.split("\\s+"); if(shuju.length!=1){ Check check = new Check(list,list_bank,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); if (check.check()){ money=Double.parseDouble(shuju[3]); for(int i=0;i<list.size();i++){ for(int j=0;j<list.get(i).getAccountlist().size();j++){ if (shuju[0].equals(list.get(i).getAccountlist().get(j))){ flag=1; temp=i; break; } } if(flag==1){ break; } } for(int i=0;i<list_bank.size();i++){ for(int j=0;j<list_bank.get(i).getAtm().size();j++){ if(shuju[2].equals(list_bank.get(i).getAtm().get(j))){ flag2=1; temp2=i; break; } } if(flag2==1) { break; } } if(list.get(temp).user_bank.equals(list_bank.get(temp2).bank_name)) { flag3=1; } if(Double.parseDouble(shuju[3])>0) { if(flag3!=1) {//跨行 //System.out.println("跨行"); if(list_bank.get(temp2).bank_name=="中国建设银行"&&list.get(temp).user_bank!="中国建设银行") { if(list.get(temp).cardtype=="jieji") {//跨行且为借记卡 if(money*(1+0.02)<=list.get(temp).account){//如果钱足够 list.get(temp).account=list.get(temp).account-(money*(1+0.02)); } } else {//贷记 if(list.get(temp).account<0) {//账户钱为负数 money=money*(1+0.02)+money*0.05; list.get(temp).account=list.get(temp).account-money; } else {//账户钱为正数 if(money*(1+0.02)<=list.get(temp).account){ list.get(temp).account=list.get(temp).account-money*(1+0.02); } else list.get(temp).account=list.get(temp).account-money*(1+0.02)-(money-list.get(temp).account)*0.05; } } } if(list_bank.get(temp2).bank_name=="中国工商银行"&&list.get(temp).user_bank!="中国工商银行") { if(list.get(temp).cardtype=="jieji") {//跨行且为借记卡 if(money*(1+0.03)<=list.get(temp).account){//如果钱足够 list.get(temp).account=list.get(temp).account-(money*(1+0.03)); } } else {//贷记 if(list.get(temp).account<0) {//账户钱为负数 money=money*(1+0.03)+money*0.05; list.get(temp).account=list.get(temp).account-money; } else {//账户钱为正数 if(money*(1+0.03)<=list.get(temp).account){ list.get(temp).account=list.get(temp).account-money*(1+0.03); } else list.get(temp).account=list.get(temp).account-money*(1+0.03)-(money-list.get(temp).account)*0.05; } } } if(list_bank.get(temp2).bank_name=="中国农业银行"&&list.get(temp).user_bank!="中国农业银行") { if(list.get(temp).cardtype=="jieji") {//跨行且为借记卡 if(money*(1+0.04)<=list.get(temp).account){//如果钱足够 list.get(temp).account=list.get(temp).account-(money*(1+0.04)); } } else {//贷记 if(list.get(temp).account<0) {//账户钱为负数 money=money*(1+0.04)+money*0.05; list.get(temp).account=list.get(temp).account-money; } else {//账户钱为正数 if(money*(1+0.04)<=list.get(temp).account){ list.get(temp).account=list.get(temp).account-money*(1+0.04); } else list.get(temp).account=list.get(temp).account-money*(1+0.04)-(money-list.get(temp).account)*0.05; } } } } else {//不跨行 //System.out.println("不跨行"); if(list.get(temp).cardtype=="jieji") {//跨行且为借记卡 if(money<=list.get(temp).account){//如果钱足够 list.get(temp).account=list.get(temp).account-money; } } else {//贷记 if(list.get(temp).account<0) {//账户钱为负数 money=money+money*0.05; list.get(temp).account=list.get(temp).account-money; } else {//账户钱为正数 if(money<=list.get(temp).account){ list.get(temp).account=list.get(temp).account-money; } else list.get(temp).account=list.get(temp).account-money-(money-list.get(temp).account)*0.05; } } } System.out.println("业务:取款 "+list.get(temp).user_name+"在"+list_bank.get(temp2).bank_name+"的"+shuju[2]+"号ATM机上取款¥"+df.format(Double.parseDouble(shuju[3]))); } else { list.get(temp).account=list.get(temp).account-Double.parseDouble(shuju[3]); System.out.println("业务:存款 "+list.get(temp).user_name+"在"+list_bank.get(temp2).bank_name+"的"+shuju[2]+"号ATM机上存款¥"+df.format(-Double.parseDouble(shuju[3]))); } System.out.println("当前余额为¥"+df.format(list.get(temp).account)); } } else { Check check1=new Check(list,shuju[0]); if(check1.check1()){ for(int i=0;i<list.size();i++){ for(int j=0;j<list.get(i).getAccountlist().size();j++){ if (shuju[0].equals(list.get(i).getAccountlist().get(j))){ flag=1; temp=i; break; } } if(flag==1){ break; } } System.out.println("业务:查询余额 "+"¥"+df.format(list.get(temp).account)); } else { System.out.println("Sorry,this card does not exist."); } } date=sc.nextLine(); // temp=0; // temp2=0; // flag=0; // flag2=0; // flag3=0; // money=0; } } } class Bank{//存储银行名字和ATM编号 String bank_name; ArrayList <String> atm; Bank(String bank_name,ArrayList <String> atm){ this.bank_name=bank_name; this.atm=atm; } ArrayList <String> getAtm(){ return this.atm; } } class Account{ String user_name; String user_bank;//使用者隶属于哪个银行 double account; String bankaccount; ArrayList<String> accountlist; String passwork; String cardtype; Account(String user_name,String user_bank,String bankaccount,double account,ArrayList<String> accountlist,String passwork,String cardtype){ this.user_name=user_name; this.user_bank=user_bank; this.account=account; this.bankaccount=bankaccount; this.accountlist=accountlist; this.passwork=passwork; this.cardtype=cardtype; } ArrayList<String> getAccountlist(){ return this.accountlist; } } class Check{ ArrayList <Account>checklist; ArrayList <Bank>checkbank_list; String account; String password; String atm; double money; double allneedmoney; Check(ArrayList <Account>checklist,String account){ this.checklist=checklist; this.account=account; } Check(ArrayList <Account>checklist,ArrayList <Bank>checkbank_list,String account,String password,String atm,double money){ this.checklist=checklist; this.checkbank_list=checkbank_list; this.account=account; this.password=password; this.atm=atm; this.money=money; } boolean check() { int flag=0; int flag_kuahang=0; int flag_noenough=0; int i,j; int k=0; int p=0; //检查账号是否正确 for(i=0;i<checklist.size();i++){ for(j=0;j<checklist.get(i).getAccountlist().size();j++){ if (account.equals(checklist.get(i).getAccountlist().get(j))){ flag=1; k=i; break; } } if(flag==1){ break; } } //检查密码是否正确 if(flag==1){ if(password.equals(checklist.get(k).passwork)){ flag=2; } else{ System.out.println("Sorry,your password is wrong.");//银行卡密码错误 System.exit(0); return false; } } else{ System.out.println("Sorry,this card does not exist.");//卡号不存在 System.exit(0); return false; } //检查ATM机编号是否正确 if(flag==2){ for(i=0;i<checkbank_list.size();i++){ for(j=0;j<checkbank_list.get(i).getAtm().size();j++){ if(atm.equals(checkbank_list.get(i).getAtm().get(j))){ flag=3; p=i; break; } } if(flag==3) { break; } } } //检查金额是否正确 if(flag==3){ if(money<0) {//存款 return true; } else {//取款,money大于等于0 if(checklist.get(k).user_bank.equals(checkbank_list.get(p).bank_name)){//检测是否跨行 flag_kuahang=1; } if(flag_kuahang!=1) {//跨行 if(checkbank_list.get(p).bank_name=="中国建设银行"&&checklist.get(k).user_bank!="中国建设银行") { if(checklist.get(k).cardtype=="jieji") {//跨行且为借记卡 if(money*(1+0.02)<=checklist.get(k).account){//如果钱足够 return true; } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } } else {//贷记 if(checklist.get(k).account<0) {//账户钱为负数 allneedmoney=money*(1+0.02)+money*0.05; if(allneedmoney-checklist.get(k).account>50000) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } else {//账户钱为正数 if(money*(1+0.02)<=checklist.get(k).account){//如果钱足够 return true; } else { allneedmoney=money*(1+0.02)+(money-checklist.get(k).account)*0.05; if(allneedmoney-checklist.get(k).account>50000) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } } } } if(checkbank_list.get(p).bank_name=="中国工商银行"&&checklist.get(k).user_bank!="中国工商银行") { if(checklist.get(k).cardtype=="jieji") {//跨行且为借记卡 if(money*(1+0.03)<=checklist.get(k).account){//如果钱足够 return true; } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } } else {//贷记 if(checklist.get(k).account<0) {//账户钱为负数 allneedmoney=money*(1+0.03)+money*0.05; if(allneedmoney-checklist.get(k).account>50000) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } else {//账户钱为正数 if(money*(1+0.03)<=checklist.get(k).account){//如果钱足够 return true; } else { allneedmoney=money*(1+0.03)+(money-checklist.get(k).account)*0.05; if(allneedmoney-checklist.get(k).account>50000) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } } } } if(checkbank_list.get(p).bank_name=="中国农业银行"&&checklist.get(k).user_bank!="中国农业银行") { if(checklist.get(k).cardtype=="jieji") {//跨行且为借记卡 if(money*(1+0.04)<=checklist.get(k).account){//如果钱足够 return true; } else { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } } else {//贷记 if(checklist.get(k).account<0) {//账户钱为负数 allneedmoney=money*(1+0.04)+money*0.05; if(allneedmoney-checklist.get(k).account>50000) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } else {//账户钱为正数 if(money*(1+0.04)<=checklist.get(k).account){//如果钱足够 return true; } else { allneedmoney=money*(1+0.04)+(money-checklist.get(k).account)*0.05; if(allneedmoney-checklist.get(k).account>50000) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } } } } } else {//不跨行 if(money<=checklist.get(k).account){//如果钱足够 return true; } else {//钱不够 if(checklist.get(k).cardtype=="jieji") {//不跨行且为借记卡 System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else {//不跨行且为贷记卡 if(checklist.get(k).account<0) {//账户钱为负数 allneedmoney=money*(1+0.05); if(allneedmoney-checklist.get(k).account>50000) {//透支金额大于5w System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else {//透支金额小于5w return true; } } else {//账户钱为正数 allneedmoney=(money-checklist.get(k).account)*0.05+money; if(allneedmoney-checklist.get(k).account>50000) {//透支金额大于5w System.out.println("Sorry,your account balance is insufficient."); System.exit(0); return false; } else { return true; } } } } } } } else{ System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在 System.exit(0); return false; } return false; } boolean check1() { int i,j; int flag=0; for(i=0;i<checklist.size();i++){ for(j=0;j<checklist.get(i).getAccountlist().size();j++){ if (account.equals(checklist.get(i).getAccountlist().get(j))){ flag=1; break; } } if(flag==1){ break; } } if(flag==1){ return true; } else { return false; } } }
本题对于是否能够正常提款的判断有稍许的复杂和繁琐,但是只要弄明白结算的顺序即可。本题还是要注意不要弄错数据的录入,否则还是会发现逻辑上没有错误但是输出的结果不对而导致测试点过不去。
踩坑心得:
改进建议:

浙公网安备 33010602011771号