java三次作业题目集7、8、9分析
(1)前言:总结三次题目集的知识点、题量、难度等情况
题目集7:主要使用到java语言的输入输出语句,if-else语句,类的继承,运用简单的数学知识完成计算,例如计算圆的面积、矩形的面积、梯形的面积、三角形的面积等,以及动态数组的应用排序等。总共2题,题目难度较小;
题目集8:主要使用到java语言的输入输出语句,if-else语句,类的继承,动态数组的应用等,总共1题,题目难度中等。
题目集9:主要使用到java语言的输入输出语句,if-else语句,类的继承,类的聚合,类与类之间的双向关联,动态数组的应用等,要求类与类之间关联性强,进行各功能在不同部分单独实现,总共1题,题目难度中等。
(2)设计与分析:
7-1 图形卡片排序游戏 (40 分)
输入格式:
- 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
- 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format。
- 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
- 排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
- 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为Sum of area:总面积值。
源码:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static Scanner input = new Scanner(System.in); public static void main(String[] args) {// TODO Auto-generated method stub ArrayList<Integer> list = new ArrayList<Integer>(); int num = input.nextInt(); while(num != 0) { withDraw(num); if(num < 0 || num > 4) { System.out.println("Wrong Format"); System.exit(0); } list.add(num); num = input.nextInt(); } withDraw(1); withDraw(num); Kapianlist kapianlist = new Kapianlist(list); kapianlist.showResult(); input.close(); } public static int withDraw(int i) { int k=i+1; return k; } } class Shape{ String name; public Shape (){ } public Shape(String name){ this.name=name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public double getArea(){ return 0; } } class Circle extends Shape{ double r; public Circle(Double r){ this.r=r; } public double getArea(){ return Math.PI*r*r; } } class Rectangle extends Shape{ double w; double l; public Rectangle(double w,double l){ this.w=w; this.l=l; } public double getArea(){ return w*l; } } class Triangle extends Shape{ double a; double b; double c; public Triangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c; } public Boolean check() { if(a+b<=c||a+c<=b||b+c<=a||a<0||b<0||c<0) { return false; } else return true; } public double getArea(){ double p=(a+b+c)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } } class Trapezoid extends Shape{ double s; double x; double h; public Trapezoid(double s,double x,double h){ this.s=s; this.x=x; this.h=h; } public double getArea(){ return (s+x)*h/2; } } class Kapian{ Shape shape; Kapian(Shape shape) { this.shape=shape; } public Shape getshape() { return shape; } public void sethape(Shape shape) { this.shape=shape; } } class Kapianlist{ ArrayList<Kapian> KList=new ArrayList<Kapian>(); public Kapianlist(ArrayList<Integer> list){ for (int i = 0; i < list.size(); i++) { if(list.get(i)==1){ double r=Main.input.nextDouble(); if(r<0){ System.out.println("Wrong Format"); System.exit(0); } Circle circle=new Circle(r); Kapian kapian=new Kapian(circle); kapian.getshape().setName("Circle"); KList.add(kapian); } if(list.get(i)==2){ double w=Main.input.nextDouble(); double l=Main.input.nextDouble(); if(w<0||l<0){ System.out.println("Wrong Format"); System.exit(0); } Rectangle rectangle=new Rectangle(w,l); Kapian kapian=new Kapian(rectangle); kapian.getshape().setName("Rectangle"); KList.add(kapian); } if(list.get(i)==3){ double a=Main.input.nextDouble(); double b=Main.input.nextDouble(); double c=Main.input.nextDouble(); Triangle triangle=new Triangle(a,b,c); if(!triangle.check()){ System.out.println("Wrong Format"); System.exit(0); } Kapian kapian=new Kapian(triangle); kapian.getshape().setName("Triangle"); KList.add(kapian); } if(list.get(i)==4){ double s=Main.input.nextDouble(); double x=Main.input.nextDouble(); double h=Main.input.nextDouble(); Trapezoid trapezoid=new Trapezoid(s,x,h); if(s<0||x<0||h<0){ System.out.println("Wrong Format"); System.exit(0); } Kapian kapian=new Kapian(trapezoid); kapian.getshape().setName("Trapezoid"); KList.add(kapian); } } } public void showResult() { System.out.println("The original list:"); for(int i=0;i<KList.size();i++) { System.out.print(KList.get(i).getshape().getName()+":"); System.out.printf("%.2f"+" ",KList.get(i).getshape().getArea()); } System.out.printf("\n"); System.out.println("The sorted list:"); KapianSort(); for(int i=0;i<KList.size();i++) { System.out.print(KList.get(i).getshape().getName()+":"); System.out.printf("%.2f"+" ",KList.get(i).getshape().getArea()); } System.out.printf("\n"); System.out.printf("Sum of area:%.2f\n",AllArea()); } public double AllArea() { double s=0; int i; for(i=0;i<KList.size();i++) { s = s + KList.get(i).getshape().getArea(); } return s; } public void KapianSort() { for(int j=0;j<KList.size();j++) { for(int i=j+1;i<KList.size();i++) { if(KList.get(j).getshape().getArea()<KList.get(i).getshape().getArea()) Collections.swap(KList, j, i); } } } }
相应类图:

解释:要实现带有名字和面积的储存和排序,就需要有一个动态数组,储存形状明和对应形状类,这样就可以进行对面积的排序和输出,而且利用继承,可以直接用父类Shape做形参,使程序更高效。
7-2 图形卡片分组游戏 (60 分)
掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。
输入格式:
在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:1 3 4 2 1 3 4 2 1 3 0
根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出Wrong Format。
如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
排序前的各图形类型及面积,格式为[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
各组中面积之和的最大值输出,格式为The max area:面积值。
源码:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static Scanner input = new Scanner(System.in); public static void main(String[] args) {// TODO Auto-generated method stub ArrayList<Integer> list = new ArrayList<Integer>(); int num = input.nextInt(); while(num != 0) { withDraw(num); if(num < 0 || num > 4) { System.out.println("Wrong Format"); System.exit(0); } list.add(num); num = input.nextInt(); } if(list.isEmpty()){ System.out.println("Wrong Format"); System.exit(0); } withDraw(1); withDraw(num); Kapianlist kapianlist = new Kapianlist(list); kapianlist.showResult(); input.close(); } public static int withDraw(int i) { int k=i+1; return k; } } class Shape{ String name; public Shape (){ } public Shape(String name){ this.name=name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public double getArea(){ return 0; } } class Circle extends Shape{ double r; public Circle(Double r){ this.r=r; } public double getArea(){ return Math.PI*r*r; } } class Rectangle extends Shape{ double w; double l; public Rectangle(double w,double l){ this.w=w; this.l=l; } public double getArea(){ return w*l; } } class Triangle extends Shape{ double a; double b; double c; public Triangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c; } public Boolean check() { if(a+b<=c||a+c<=b||b+c<=a||a<0||b<0||c<0) { return false; } else return true; } public double getArea(){ double p=(a+b+c)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c)); } } class Trapezoid extends Shape{ double s; double x; double h; public Trapezoid(double s,double x,double h){ this.s=s; this.x=x; this.h=h; } public double getArea(){ return (s+x)*h/2; } } class Kapian{ Shape shape; Kapian(Shape shape) { this.shape=shape; } public Shape getshape() { return shape; } public void sethape(Shape shape) { this.shape=shape; } } class Kapianlist{ ArrayList<Kapian> KList=new ArrayList<Kapian>(); ArrayList<Kapian> KList1=new ArrayList<Kapian>(); ArrayList<Kapian> KList2=new ArrayList<Kapian>(); ArrayList<Kapian> KList3=new ArrayList<Kapian>(); ArrayList<Kapian> KList4=new ArrayList<Kapian>(); public Kapianlist(ArrayList<Integer> list){ for (int i = 0; i < list.size(); i++) { if(list.get(i)==1){ double r=Main.input.nextDouble(); if(r<0){ System.out.println("Wrong Format"); System.exit(0); } Circle circle=new Circle(r); Kapian kapian=new Kapian(circle); kapian.getshape().setName("Circle"); KList.add(kapian); KList1.add(kapian); } if(list.get(i)==2){ double w=Main.input.nextDouble(); double l=Main.input.nextDouble(); if(w<0||l<0){ System.out.println("Wrong Format"); System.exit(0); } Rectangle rectangle=new Rectangle(w,l); Kapian kapian=new Kapian(rectangle); kapian.getshape().setName("Rectangle"); KList.add(kapian); KList2.add(kapian); } if(list.get(i)==3){ double a=Main.input.nextDouble(); double b=Main.input.nextDouble(); double c=Main.input.nextDouble(); Triangle triangle=new Triangle(a,b,c); if(!triangle.check()){ System.out.println("Wrong Format"); System.exit(0); } Kapian kapian=new Kapian(triangle); kapian.getshape().setName("Triangle"); KList.add(kapian); KList3.add(kapian); } if(list.get(i)==4){ double s=Main.input.nextDouble(); double x=Main.input.nextDouble(); double h=Main.input.nextDouble(); Trapezoid trapezoid=new Trapezoid(s,x,h); if(s<0||x<0||h<0){ System.out.println("Wrong Format"); System.exit(0); } Kapian kapian=new Kapian(trapezoid); kapian.getshape().setName("Trapezoid"); KList.add(kapian); KList4.add(kapian); } } } public void showResult() { System.out.println("The original list:"); System.out.printf("["); for(int i=0;i<KList.size();i++) { System.out.print(KList.get(i).getshape().getName()+":"); System.out.printf("%.2f"+" ",KList.get(i).getshape().getArea()); } System.out.printf("]"); System.out.printf("\n"); System.out.println("The Separated List:"); showSeparated(KList1); showSeparated(KList2); showSeparated(KList3); showSeparated(KList4); System.out.printf("\n"); System.out.println("The Separated sorted List:"); KapianSort(KList1); KapianSort(KList2); KapianSort(KList3); KapianSort(KList4); showSeparated(KList1); showSeparated(KList2); showSeparated(KList3); showSeparated(KList4); System.out.printf("\n"); System.out.printf("The max area:"); double an[]=new double[4]; an[0]=AllArea(KList1); an[1]=AllArea(KList2); an[2]=AllArea(KList3); an[3]=AllArea(KList4); double MAX=-1; for (int i = 0; i < 4; i++) { if(an[i]>MAX){ MAX=an[i]; } } System.out.printf("%.2f",MAX); } public double AllArea(ArrayList<Kapian> KList) { double s=0; int i; for(i=0;i<KList.size();i++) { s = s + KList.get(i).getshape().getArea(); } return s; } public void KapianSort(ArrayList<Kapian> KList) { for(int j=0;j<KList.size();j++) { for(int i=j+1;i<KList.size();i++) { if(KList.get(j).getshape().getArea()<KList.get(i).getshape().getArea()) Collections.swap(KList, j, i); } } } public void showSeparated(ArrayList<Kapian> KList){ System.out.printf("["); for(int i=0;i<KList.size();i++) { System.out.print(KList.get(i).getshape().getName()+":"); System.out.printf("%.2f"+" ",KList.get(i).getshape().getArea()); } System.out.printf("]"); } }
相应类图:


解释:要实现带有名字和面积的储存和排序,就需要有一个动态数组,储存形状明和对应形状类,这样就可以进行对面积的排序和输出,而且利用继承,可以直接用父类Shape做形参,使程序更高效。对于同一个图形类型单独排序的话,我又单独使用了4个动态数组进行单独储存和排序。
7-1 ATM机类结构设计(一) (100 分)
设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
存款、取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。
查询余额功能输入数据格式: 卡号
输出格式:
①输入错误处理
如果输入卡号不存在,则输出Sorry,this card does not exist.。
如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.。
如果输入银行卡密码错误,则输出Sorry,your password is wrong.。
如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.。
如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.。
②取款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③存款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。
源码:
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =new Scanner(System.in); Person person[] = new Person[10]; person[0]=new Person("杨过","中国建设银行","3217000010041315709",10000.00,"6217000010041315709","88888888"); person[1]=new Person("杨过","中国建设银行","3217000010041315709",10000.00,"6217000010041315715","88888888"); person[2]=new Person("杨过","中国建设银行","3217000010041315715",10000.00,"6217000010041315718","88888888"); person[3]=new Person("郭靖","中国建设银行","3217000010051320007",10000.00,"6217000010051320007","88888888"); person[4]=new Person("张无忌","中国工商银行","3222081502001312389",10000.00,"6222081502001312389","88888888"); person[5]=new Person("张无忌","中国工商银行","3222081502001312390",10000.00,"6222081502001312390","88888888"); person[6]=new Person("张无忌","中国工商银行","3222081502001312399",10000.00,"6222081502001312399","88888888"); person[7]=new Person("张无忌","中国工商银行","3222081502001312399",10000.00,"6222081502001312400","88888888"); person[8]=new Person("韦小宝","中国工商银行","3222081502051320785",10000.00,"6222081502051320785","88888888"); person[9]=new Person("韦小宝","中国工商银行","3222081502051320786",10000.00,"6222081502051320786","88888888"); String card,Password; String atmID; double amount; String jian[]={"01","02","03","04"}; String gong[]={"05","06"}; ArrayList<String> list = new ArrayList<String>(); /*list.add(input.nextLine()); while (!input.nextLine().equals("#")){ list.add(input.nextLine()); }*/ int m=0; list.add(input.nextLine()); while(!list.get(m).equals("#")){ list.add(input.nextLine()); m++; } for (int i = 0; i < list.size()-1; i++) { String[] S=list.get(i).split("\\s+"); //System.out.println(S.length); int num=S.length; card = S[0]; int checkID=0; int k=-1; for (int j = 0; j <10; j++) { if(card.equals(person[j].ID)){ checkID=1; k=j; break; } } //System.out.println(k); //System.out.println(person[k].bank); if(checkID==1) { if (num>1){ Password = S[1]; atmID = S[2]; amount = (int) Double.parseDouble(S[3]); if (Password.equals(person[k].password)) { int find = 0; int findkua=0; if (person[k].bank.equals("中国建设银行")) { for (int j = 0; j < jian.length; j++) { if (jian[j].equals(atmID)) { find = 1; break; } } for (int l = 0; l < gong.length; l++) { if (gong[l].equals(atmID)) { findkua = 1; break; } } } else if (person[k].bank.equals("中国工商银行")) { for (int j = 0; j < gong.length; j++) { if (gong[j].equals(atmID)) { find = 1; break; } } for (int j = 0; j < jian.length; j++) { if (jian[j].equals(atmID)) { findkua = 1; break; } } } if(findkua ==1){ System.out.println("Sorry,cross-bank withdrawal is not supported."); System.exit(0); } if (find == 0) { System.out.println("Sorry,the ATM's id is wrong."); } else { if (person[k].balance - amount < 0) { System.out.println("Sorry,your account balance is insufficient."); } else { if (amount > 0) { System.out.printf(person[k].Name + "在" + person[k].bank + "的" + atmID + "号ATM机上取款¥" + "%.2f\n", amount); } else { System.out.printf(person[k].Name + "在" + person[k].bank + "的" + atmID + "号ATM机上存款¥" + "%.2f\n", Math.abs(amount)); } System.out.printf("当前余额为¥%.2f\n", person[k].balance - amount); Withdraw(k,person[k].account,amount,person); } } } else System.out.println("Sorry,your password is wrong."); } else if(num==1){ System.out.printf("¥%.2f\n",person[k].balance); } } else System.out.println("Sorry,this card does not exist."); } } public static void Withdraw(int k,String account,double amount,Person person[]){ for (int i = 0; i < 10; i++) { if(account.equals(person[i].account)){ person[i].balance-=amount; } } } } class Person{ String Name; String bank; String account; double balance; String ID; String password; public Person(String Name,String bank, String account,double balance,String ID,String password){ this.account=account; this.balance=balance; this.ID=ID; this.bank=bank; this.password=password; this.Name=Name; } }
相应类图:


解释:使用了一个简单的类来单独储存每个用户的信息,使用类的数组来进行初始化,查询或存取钱就对这个数组进行遍历,相较于类的聚合,虽然结构简单,但是各部分功能并不清晰,不利于修改和拓展。
7-1 ATM机类结构设计(二) (100 分)
设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔)
查询余额功能输入数据格式: 卡号
输出格式:
①输入错误处理
如果输入卡号不存在,则输出Sorry,this card does not exist.。
如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.。
如果输入银行卡密码错误,则输出Sorry,your password is wrong.。
如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.。
②取款业务输出
输出共两行,格式分别为:
业务:取款 [用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③查询余额业务输出
业务:查询余额 ¥[金额]
金额保留两位小数。
源码:
import java.util.ArrayList; import java.util.Objects; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String card, Password, atmID; double amount; Yinlian yinlian = new Yinlian(); yinlian.addBank(new Bank("中国建设银行", 0.02)); yinlian.addBank(new Bank("中国工商银行", 0.03)); yinlian.addBank(new Bank("中国农业银行", 0.04)); User a1 = new User("杨过"); User a2 = new User("郭靖"); User a3 = new User("张无忌"); User a4 = new User("韦小宝"); User a5 = new User("张三丰"); User a6 = new User("令狐冲"); User a7 = new User("乔峰"); User a8 = new User("洪七公"); yinlian.getBank("中国建设银行").addUser(a1); yinlian.getBank("中国建设银行").addUser(a2); yinlian.getBank("中国工商银行").addUser(a3); yinlian.getBank("中国工商银行").addUser(a4); yinlian.getBank("中国建设银行").addUser(a5); yinlian.getBank("中国工商银行").addUser(a6); yinlian.getBank("中国农业银行").addUser(a7); yinlian.getBank("中国农业银行").addUser(a8); Bank bank1 = yinlian.getBank("中国建设银行"); Bank bank2 = yinlian.getBank("中国工商银行"); Bank bank3 = yinlian.getBank("中国农业银行"); String at1[] = {"01", "02", "03", "04"}; String at2[] = {"05", "06"}; String at3[] = {"07", "08", "09", "10", "11"}; ATM atm1 = new ATM(at1); ATM atm2 = new ATM(at2); ATM atm3 = new ATM(at3); bank1.addATM(atm1); bank2.addATM(atm2); bank3.addATM(atm3); a1.addAccount(new Account(a1.username, "3217000010041315709", bank1, 10000, 1)); a1.addAccount(new Account(a1.username, "3217000010041315715", bank1, 10000, 1)); a2.addAccount(new Account(a2.username, "3217000010051320007", bank1, 10000, 1)); a3.addAccount(new Account(a3.username, "3222081502001312389", bank2, 10000, 1)); a3.addAccount(new Account(a3.username, "3222081502001312390", bank2, 10000, 1)); a3.addAccount(new Account(a3.username, "3222081502001312399", bank2, 10000, 1)); a4.addAccount(new Account(a4.username, "3222081502051320785", bank2, 10000, 1)); a4.addAccount(new Account(a4.username, "3222081502051320786", bank2, 10000, 1)); a5.addAccount(new Account(a5.username, "3640000010045442002", bank1, 10000, 2)); a6.addAccount(new Account(a6.username, "3640000010045441009", bank2, 10000, 2)); a7.addAccount(new Account(a7.username, "3630000010033431001", bank3, 10000, 2)); a8.addAccount(new Account(a8.username, "3630000010033431008", bank3, 10000, 2)); a1.getAccount("3217000010041315709").addCard(new Card("6217000010041315709", "88888888", a1.getAccount("3217000010041315709"))); a1.getAccount("3217000010041315709").addCard(new Card("6217000010041315715", "88888888", a1.getAccount("3217000010041315709"))); a1.getAccount("3217000010041315715").addCard(new Card("6217000010041315718", "88888888", a1.getAccount("3217000010041315715"))); a2.getAccount("3217000010051320007").addCard(new Card("6217000010051320007", "88888888", a2.getAccount("3217000010051320007"))); a3.getAccount("3222081502001312389").addCard(new Card("6222081502001312389", "88888888", a3.getAccount("3222081502001312389"))); a3.getAccount("3222081502001312390").addCard(new Card("6222081502001312390", "88888888", a3.getAccount("3222081502001312390"))); a3.getAccount("3222081502001312399").addCard(new Card("6222081502001312399", "88888888", a3.getAccount("3222081502001312399"))); a3.getAccount("3222081502001312399").addCard(new Card("6222081502001312400", "88888888", a3.getAccount("3222081502001312399"))); a4.getAccount("3222081502051320785").addCard(new Card("6222081502051320785", "88888888", a4.getAccount("3222081502051320785"))); a4.getAccount("3222081502051320786").addCard(new Card("6222081502051320786", "88888888", a4.getAccount("3222081502051320786"))); a5.getAccount("3640000010045442002").addCard(new Card("6640000010045442002", "88888888", a5.getAccount("3640000010045442002"))); a5.getAccount("3640000010045442002").addCard(new Card("6640000010045442003", "88888888", a5.getAccount("3640000010045442002"))); a6.getAccount("3640000010045441009").addCard(new Card("6640000010045441009", "88888888", a6.getAccount("3640000010045441009"))); a7.getAccount("3630000010033431001").addCard(new Card("6630000010033431001", "88888888", a7.getAccount("3630000010033431001"))); a8.getAccount("3630000010033431008").addCard(new Card("6630000010033431008", "88888888", a8.getAccount("3630000010033431008"))); ArrayList<String> list = new ArrayList<>(); int m = 0; list.add(input.nextLine()); while (!list.get(m).equals("#")) { list.add(input.nextLine()); m++; } for (int i = 0; i < list.size() - 1; i++) { String[] S = list.get(i).split("\\s+"); card = S[0]; if (yinlian.check(card) != null) { Card one = yinlian.check(card); if (S.length == 1) { { System.out.print("业务:查询余额 "); System.out.printf("¥%.2f", one.account.balance); } } else if (S.length > 1) { Password = S[1]; atmID = S[2]; amount = Double.parseDouble(S[3]); if (one.password.equals(Password)) { int type = one.account.type;//获取类型 if (getATMBank(atmID, yinlian) == null) { System.out.println("Sorry,the ATM's id is wrong."); System.exit(0); } double rate = Objects.requireNonNull(getATMBank(atmID, yinlian)).atm.getRate();//获取利率 if (one.account.bank.atm.checkATM(atmID)) {//同一银行 if (type == 1||type == 2 && one.account.balance - amount > 0) { one.account.setBalance(amount); } else if (type == 2 && one.account.balance - amount <= 0) {//透支取款 if (one.account.balance >= 0) { one.account.setBalance(amount + (amount - one.account.balance) * 0.05); } else { one.account.setBalance(amount * 1.05); } } /*else { System.out.printf(one.account.Name + "在" + one.account.bank.Bankname + "的" + atmID + "号ATM机上存款¥" + "%.2f\n", Math.abs(amount)); }*/ } else if (checkKua(one.account.bank, atmID, yinlian)) {//跨行取款 if (type == 1 || (type == 2 && one.account.balance - amount >= 0)) { one.account.setBalance(amount + amount * rate); } else if (type == 2 && one.account.balance - amount < 0) {//透支取款 if (one.account.balance >= 0) { one.account.setBalance(amount + amount * rate + (amount - one.account.balance) * 0.05); } else { one.account.setBalance(amount * rate + amount * 1.05); } } } if ((type == 1 && one.account.balance < 0) || (type == 2 && one.account.balance < -50000)) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } else { System.out.print("业务:取款 "); System.out.printf(one.account.Name + "在" + Objects.requireNonNull(getATMBank(atmID, yinlian)).Bankname + "的" + atmID + "号ATM机上取款¥" + "%.2f\n", amount); System.out.printf("当前余额为¥%.2f\n", one.account.balance); } } else{ System.out.println("Sorry,your password is wrong."); System.exit(0); } } } else{ System.out.println("Sorry,this card does not exist."); System.exit(0); } } } public static boolean checkKua(Bank bank, String num, Yinlian yinlian) { for (int i = 0; i < yinlian.Banklist.size(); i++) { if (yinlian.Banklist.get(i) != bank) { if (yinlian.Banklist.get(i).atm.checkATM(num)) { return true; } } } return false; } public static Bank getATMBank(String atm, Yinlian yinlian) { for (int i = 0; i < yinlian.Banklist.size(); i++) { if (yinlian.Banklist.get(i).atm.checkATM(atm)) { return yinlian.Banklist.get(i); } } return null; } } class Yinlian { ArrayList<Bank> Banklist = new ArrayList<Bank>(); public Yinlian() { } public void addBank(Bank bank) { Banklist.add(bank); } public Bank getBank(String name) { for (int i = 0; i < Banklist.size(); i++) { if (Banklist.get(i).Bankname.equals(name)) { return Banklist.get(i); } } return null; } public Card check(String num) { for (int i = 0; i < Banklist.size(); i++) { Bank a = Banklist.get(i); if (a.check(num) != null) { return a.check(num); } ; } return null; } } class Bank { public ArrayList<User> Userlist = new ArrayList<User>(); String Bankname; ATM atm; double rate; User user[]; public Bank(String bankname, double rate) { this.Bankname = bankname; this.rate = rate; } public void addATM(ATM atm) { this.atm = atm; atm.setBank(this); } public void addUser(User user) { Userlist.add(user); } public Card check(String num) { for (int i = 0; i < Userlist.size(); i++) { User a = Userlist.get(i); if (a.check(num) != null) { return a.check(num); } } return null; } } class ATM { public ArrayList<String> numlist = new ArrayList<String>(); Bank bank; ATM(String[] i) { for (int j = 0; j < i.length; j++) { numlist.add(i[j]); } } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } public boolean checkATM(String num) { for (int i = 0; i < numlist.size(); i++) { if (numlist.get(i).equals(num)) { return true; } } return false; } public double getRate() { return bank.rate; } } class User {//用户 public ArrayList<Account> Accountlist = new ArrayList<Account>(); String username; public User(String username) { this.username = username; } public void addAccount(Account account) { Accountlist.add(account); } public Account getAccount(String num) { for (int i = 0; i < Accountlist.size(); i++) { if (Accountlist.get(i).accountnum.equals(num)) { return Accountlist.get(i); } } return null; } public Card check(String num) { for (int i = 0; i < Accountlist.size(); i++) { Account a = Accountlist.get(i); if (a.check(num) != null) { return a.check(num); } } return null; } } class Account {//账户 public ArrayList<Card> Cardlist = new ArrayList<Card>(); String accountnum; Bank bank; double balance; String Name; int type; Account(String Name, String accountnum, Bank bank, double balance, int type) { this.Name = Name; this.accountnum = accountnum; this.bank = bank; this.balance = balance; this.type = type; } public void addCard(Card card) { Cardlist.add(card); } public void setBalance(double amount) { this.balance = balance - amount; } public Bank getBank() { return bank; } public Card check(String num) { for (int i = 0; i < Cardlist.size(); i++) { Card a = Cardlist.get(i); if (a.check(num) != null) { return a.check(num); } } return null; } } class Card {//银行卡号 String cardnum; String password; Account account; Card(String cardnum, String password, Account account) { this.cardnum = cardnum; this.password = password; this.account = account; } public String getCardnum() { return cardnum; } public String getPassword() { return password; } public Card check(String num) { if (num.equals(cardnum)) { return this; } return null; } }
相应类图:


解释:这次相较于第八次作业,严格按照了题目的要求,使用了聚合,类与类之间关联紧密,而且也使用了双向关联,Yinlian类中包含Bank类,Bank类中包含ATM类和User类,User类中包含Account类,Account类中包含Card类,每个类都实现了独立的功能,方便修改和添加功能。
(1)采坑心得:
对于数据初始化一定要细心准确,否则测试就很容易出错,要考虑到所有情况,比如贷记卡跟信用卡就都要考虑是否跨行取款,是否透支。
(2)改进建议:
对于题目集8可以改成题目集9的结构,其实当时作业截止后就是按要求重新写了一次,题目集9也是按这个代码进行修改和添加一些功能的。
(5)总结:
对于功能要区分清楚,设计出完整的独立的结构,这样也方便代码的修改和功能的添加。

浙公网安备 33010602011771号