第三次Blog作业
(1)前言
- 抽象类的使用
- 考虑面向对象设计的“单一职责原则”
- 继承、多态性使用方法以及接口的应用
- 封装性
(2)设计与分析
①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf
输入格式:
- 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0 - 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
代码如下:
1 import java.util.ArrayList; 2 import java.util.Random; 3 import java.util.Scanner; 4 public class Main { 5 public static Scanner input = new Scanner(System.in); 6 7 public static void main(String args[]){ 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 int num = input.nextInt(); 10 while(num != 0){ 11 if(num < 0 || num > 4){ 12 System.out.println("Wrong Format"); 13 System.exit(0); 14 } 15 list.add(num); 16 num = input.nextInt(); 17 } 18 DealCardList dealCardList = new DealCardList(list); 19 if(!dealCardList.validate()){ 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 dealCardList.showResult(); 24 input.close(); 25 } 26 } 27 class DealCardList{ 28 private ArrayList<Card> cardList=new ArrayList<Card>(); 29 public DealCardList(){} 30 public DealCardList(ArrayList<Integer> list){ 31 for(int i=0;i<list.size();i++) 32 { 33 if(list.get(i)==0) 34 { 35 break; 36 } 37 else 38 { 39 switch(list.get(i)){ 40 case 1://circle 41 Card card1=new Card(new Circle(Main.input.nextDouble())); 42 card1.getShape().setShapeName("Circle"); 43 cardList.add(card1); 44 break; 45 case 2://rectangle 46 Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 47 card2.getShape().setShapeName("Rectangle"); 48 cardList.add(card2); 49 break; 50 case 3://triangle 51 Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 52 card3.getShape().setShapeName("Triangle"); 53 cardList.add(card3); 54 break; 55 case 4://trapezoid 56 Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 57 card4.getShape().setShapeName("Trapezoid"); 58 cardList.add(card4); 59 break; 60 } 61 } 62 } 63 } 64 public boolean validate(){ 65 boolean flag=true; 66 for(int i=0;i<cardList.size();i++){ 67 if(!cardList.get(i).getShape().validate()) 68 { 69 flag=false; 70 break; 71 } 72 } 73 return flag; 74 } 75 public void cardSort(){ 76 int i,j; 77 Card temp; 78 for(i=0;i<cardList.size();i++) 79 { 80 int mindex=i; 81 for(j=i+1;j< cardList.size();j++) 82 { 83 if(cardList.get(j).getShape().getArea()>cardList.get(mindex).getShape().getArea()) 84 { 85 mindex=j; 86 } 87 } 88 temp=cardList.get(i); 89 cardList.set(i,cardList.get(mindex)); 90 cardList.set(mindex,temp); 91 } 92 for (int k=0;k<cardList.size();k++) { 93 System.out.print(cardList.get(k).getShape()+":"); 94 System.out.printf("%.2f ",cardList.get(k).getShape().getArea()); 95 } 96 } 97 public double getAllArea(){ 98 double sum=0; 99 for(int i=0;i<cardList.size();i++) 100 { 101 sum+=cardList.get(i).getShape().getArea(); 102 } 103 return sum; 104 } 105 public void showResult(){ 106 System.out.println("The original list:"); 107 for (int i=0;i<cardList.size();i++) { 108 System.out.printf(cardList.get(i).getShape()+":"); 109 System.out.printf("%.2f ",cardList.get(i).getShape().getArea()); 110 } 111 System.out.println(); 112 System.out.println("The sorted list:"); 113 cardSort(); 114 System.out.println(); 115 System.out.printf("Sum of area:%.2f\n",getAllArea()); 116 } 117 118 } 119 class Card implements Comparable<Card>{ 120 private Shape shape; 121 public Card(){} 122 public Card(Shape shape){this.shape=shape;} 123 124 public Shape getShape() { 125 return shape; 126 } 127 128 public void setShape(Shape shape) { 129 this.shape = shape; 130 } 131 public int compareTo(Card card){ 132 return (int)(shape.getArea()-card.getShape().getArea()); 133 } 134 } 135 136 abstract class Shape{ 137 private String shapeName; 138 public Shape(){ 139 140 } 141 public Shape(String shapeName) { 142 this.shapeName=shapeName; 143 } 144 145 public String getShapeName() { 146 return shapeName; 147 } 148 149 public void setShapeName(String shapeName) { 150 this.shapeName = shapeName; 151 } 152 abstract public double getArea(); 153 abstract public boolean validate(); 154 public String toString(){ 155 return getShapeName(); 156 } 157 } 158 159 class Circle extends Shape 160 { 161 private double radius; 162 public Circle(){} 163 public Circle(double radius){ 164 this.radius=radius; 165 } 166 167 public void setRadius(double radius) { 168 this.radius = radius; 169 } 170 171 public double getRadius() { 172 return radius; 173 } 174 175 public double getArea() { 176 return Math.PI* Math.pow(radius,2); 177 } 178 public boolean validate() 179 { 180 if(radius<=0) 181 { 182 return false; 183 } 184 else 185 { 186 return true; 187 } 188 } 189 } 190 class Rectangle extends Shape 191 { 192 private double width; 193 private double length; 194 public Rectangle(){} 195 public Rectangle(double width,double length){ 196 this.length=length; 197 this.width=width; 198 } 199 public void setLength(double length) { 200 this.length = length; 201 } 202 203 public double getLength() { 204 return length; 205 } 206 207 public double getWidth() { 208 return width; 209 } 210 211 public void setWidth(double width) { 212 this.width = width; 213 } 214 215 public double getArea() { 216 return width*length; 217 } 218 public boolean validate() 219 { 220 if(width<0||length<0) 221 { 222 return false; 223 } 224 else 225 { 226 return true; 227 } 228 } 229 } 230 class Triangle extends Shape 231 { 232 private double side1; 233 private double side2; 234 private double side3; 235 public Triangle(){} 236 public Triangle(double side1,double side2,double side3) 237 { 238 this.side1=side1; 239 this.side2=side2; 240 this.side3=side3; 241 } 242 243 public double getSide2() { 244 return side2; 245 } 246 247 public void setSide2(double side1) { 248 this.side2 = side2; 249 } 250 251 public double getSide3() { 252 return side3; 253 } 254 255 public void setSide3(double side3) { 256 this.side3 = side3; 257 } 258 public double getArea() 259 { 260 double p=(side1+side2+side3)/2; 261 return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 262 } 263 public boolean validate() 264 { 265 if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) 266 { 267 return false; 268 } 269 if(side1-side2>=side3||side1-side3>=side2||side2-side3>=side1) 270 { 271 return false; 272 } 273 else 274 { 275 return true; 276 } 277 } 278 } 279 class Trapezoid extends Shape{ 280 private double topSide; 281 private double bottomSide; 282 private double height; 283 public Trapezoid(){} 284 public Trapezoid(double topSide,double bottomSide,double height) 285 { 286 this.topSide=topSide; 287 this.bottomSide=bottomSide; 288 this.height=height; 289 } 290 public double getArea() 291 { 292 return (topSide+bottomSide)/2*height; 293 } 294 public boolean validate() 295 { 296 if(topSide<0||bottomSide<0||height<0) 297 { 298 return false; 299 } 300 else 301 { 302 return true; 303 } 304 } 305 }
报表内容如下:

类图如下:

本次题目采用了ArrayList数组,数组中存入图形类元素,卡片分为四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)和梯形(Trapezoid)。
利用 Comparable 接口在 Card 类当中实现 Comparable 接口中的 CompareTo()方法,CompareTo()方法关系到数组当中排序的大小,根据接口在ArrayList数组中将卡片
面积按从大到小的顺序进行排序。由报表内容看到圈复杂度还是有点大,主要原因还是使用了过多的循环和判断,导致阅读性很差。采用ArrayList数组可以很方便的存储
各种图形对象,这也为写代码提供了很大的方便。
掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。
输入格式:
- 在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0 - 根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
代码如下:
1 import java.util.TreeSet; 2 import java.util.ArrayList; 3 import java.util.Random; 4 import java.util.Scanner; 5 public class Main { 6 public static Scanner input = new Scanner(System.in); 7 public static void main(String args[]){ 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 int num = input.nextInt(); 10 if(num==0) 11 { 12 System.out.println("Wrong Format"); 13 System.exit(0); 14 } 15 while(num != 0){ 16 if(num < 0 || num > 4){ 17 System.out.println("Wrong Format"); 18 System.exit(0); 19 } 20 list.add(num); 21 num = input.nextInt(); 22 } 23 DealCardList dealCardList = new DealCardList(list); 24 if(!dealCardList.validate()){ 25 System.out.println("Wrong Format"); 26 System.exit(0); 27 } 28 dealCardList.showResult(); 29 input.close(); 30 } 31 32 } 33 class DealCardList{ 34 double[] zs=new double[4]; 35 private ArrayList<Card> cardList=new ArrayList<Card>(); 36 public DealCardList(){} 37 public DealCardList(ArrayList<Integer> list){ 38 for(int i=0;i<list.size();i++) 39 { 40 if(list.get(i)==0) 41 { 42 break; 43 } 44 else 45 { 46 switch(list.get(i)){ 47 case 1://circle 48 Card card1=new Card(new Circle(Main.input.nextDouble())); 49 card1.getShape().setShapeName("Circle"); 50 cardList.add(card1); 51 break; 52 case 2://rectangle 53 Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 54 card2.getShape().setShapeName("Rectangle"); 55 cardList.add(card2); 56 break; 57 case 3://triangle 58 Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 59 card3.getShape().setShapeName("Triangle"); 60 cardList.add(card3); 61 break; 62 case 4://trapezoid 63 Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 64 card4.getShape().setShapeName("Trapezoid"); 65 cardList.add(card4); 66 break; 67 } 68 } 69 } 70 } 71 public boolean validate(){ 72 boolean flag=true; 73 for(int i=0;i<cardList.size();i++){ 74 if(!cardList.get(i).getShape().validate()) 75 { 76 flag=false; 77 break; 78 } 79 } 80 return flag; 81 } 82 public void circle(){ 83 zs[0]=0; 84 System.out.print("["); 85 for (Card a:cardList){ 86 if(a.getShape().getShapeName().equals("Circle")){ 87 System.out.print(a.getShape()+":"); 88 System.out.printf("%.2f ",a.getShape().getArea()); 89 zs[0]=zs[0]+a.getShape().getArea(); 90 } 91 } 92 System.out.print("]"); 93 } 94 //输出矩形的数据并计算总面积 95 public void rectangle(){ 96 zs[1]=0; 97 System.out.print("["); 98 for (Card a:cardList){ 99 if(a.getShape().getShapeName().equals("Rectangle")){ 100 System.out.print(a.getShape()+":"); 101 System.out.printf("%.2f ",a.getShape().getArea()); 102 zs[1]=zs[1]+a.getShape().getArea(); 103 } 104 } 105 System.out.print("]"); 106 } 107 //输出三角形的数据并计算总面积 108 public void triangle(){ 109 zs[2]=0; 110 System.out.print("["); 111 for (Card a:cardList){ 112 if(a.getShape().getShapeName().equals("Triangle")){ 113 System.out.print(a.getShape()+":"); 114 System.out.printf("%.2f ",a.getShape().getArea()); 115 zs[2]=zs[2]+a.getShape().getArea(); 116 } 117 } 118 System.out.print("]"); 119 } 120 //输出梯形的数据并计算总面积 121 public void trapezoid(){ 122 zs[3]=0; 123 System.out.print("["); 124 for (Card a:cardList){ 125 if(a.getShape().getShapeName().equals("Trapezoid")){ 126 System.out.print(a.getShape()+":"); 127 System.out.printf("%.2f ",a.getShape().getArea()); 128 zs[3]=zs[3]+a.getShape().getArea(); 129 } 130 } 131 System.out.print("]"); 132 } 133 //从大到小输出圆的数据 134 public void circleSort(){ 135 TreeSet<Card> kp = new TreeSet<>(cardList); 136 System.out.print("["); 137 for (Card a:kp){ 138 if(a.getShape().getShapeName().equals("Circle")){ 139 System.out.print(a.getShape()+":"); 140 System.out.printf("%.2f ",a.getShape().getArea()); 141 } 142 } 143 System.out.print("]"); 144 } 145 //从大到小输出矩形的数据 146 public void rectangleSort() { 147 TreeSet<Card> kp = new TreeSet<>(cardList); 148 System.out.print("["); 149 for (Card a : kp) { 150 if (a.getShape().getShapeName().equals("Rectangle")) { 151 System.out.print(a.getShape()+":"); 152 System.out.printf("%.2f ",a.getShape().getArea()); 153 } 154 } 155 System.out.print("]"); 156 } 157 //从大到小输出三角形的数据 158 public void triangleSort() { 159 TreeSet<Card> kp = new TreeSet<>(cardList); 160 System.out.print("["); 161 for (Card a : kp) { 162 if (a.getShape().getShapeName().equals("Triangle")) { 163 System.out.print(a.getShape()+":"); 164 System.out.printf("%.2f ",a.getShape().getArea()); 165 } 166 } 167 System.out.print("]"); 168 } 169 //从大到小输出梯形的数据 170 public void trapezoidSort() { 171 TreeSet<Card> kp = new TreeSet<>(cardList); 172 System.out.print("["); 173 for (Card a : kp) { 174 if (a.getShape().getShapeName().equals("Trapezoid")) { 175 System.out.print(a.getShape()+":"); 176 System.out.printf("%.2f ",a.getShape().getArea()); 177 } 178 } 179 System.out.print("]"); 180 } 181 //找出最大总面积 182 public double getMax() { 183 double max = 0; 184 int i; 185 for (i = 0; i < 4; i++) { 186 if (max < zs[i]) { 187 max = zs[i]; 188 } 189 } 190 return max; 191 } 192 public void showResult() { 193 System.out.println("The original list:"); 194 System.out.print("["); 195 for (int i=0;i<cardList.size();i++) { 196 System.out.printf(cardList.get(i).getShape()+":"); 197 System.out.printf("%.2f ",cardList.get(i).getShape().getArea()); 198 } 199 System.out.print("]"); 200 System.out.println("\nThe Separated List:"); 201 circle();rectangle();triangle();trapezoid(); 202 System.out.println("\nThe Separated sorted List:"); 203 circleSort();rectangleSort();triangleSort();trapezoidSort(); 204 System.out.printf("\nThe max area:%.2f\n",getMax()); 205 206 } 207 } 208 209 class Card implements Comparable<Card>{ 210 private Shape shape; 211 public Card(){} 212 public Card(Shape shape){this.shape=shape;} 213 214 public Shape getShape() { 215 return shape; 216 } 217 public void setShape(Shape shape) { 218 this.shape = shape; 219 } 220 public int compareTo(Card card){ 221 return (int)(card.getShape().getArea()-shape.getArea()); 222 } 223 } 224 225 abstract class Shape{ 226 private String shapeName; 227 public Shape(){ 228 229 } 230 public Shape(String shapeName) { 231 this.shapeName=shapeName; 232 } 233 234 public String getShapeName() { 235 return shapeName; 236 } 237 238 public void setShapeName(String shapeName) { 239 this.shapeName = shapeName; 240 } 241 abstract public double getArea(); 242 abstract public boolean validate(); 243 public String toString(){ 244 return getShapeName(); 245 } 246 } 247 248 class Circle extends Shape 249 { 250 private double radius; 251 public Circle(){} 252 public Circle(double radius){ 253 this.radius=radius; 254 } 255 public double getArea() { 256 return Math.PI* Math.pow(radius,2); 257 } 258 public boolean validate() 259 { 260 if(radius<=0) 261 { 262 return false; 263 } 264 else 265 { 266 return true; 267 } 268 } 269 } 270 class Rectangle extends Shape 271 { 272 private double width; 273 private double length; 274 public Rectangle(){} 275 public Rectangle(double width,double length){ 276 this.length=length; 277 this.width=width; 278 } 279 public double getArea() { 280 return width*length; 281 } 282 public boolean validate() 283 { 284 if(width<0||length<0) 285 { 286 return false; 287 } 288 else 289 { 290 return true; 291 } 292 } 293 } 294 class Triangle extends Shape 295 { 296 private double side1; 297 private double side2; 298 private double side3; 299 public Triangle(){} 300 public Triangle(double side1,double side2,double side3) 301 { 302 this.side1=side1; 303 this.side2=side2; 304 this.side3=side3; 305 } 306 public double getArea() 307 { 308 double p=(side1+side2+side3)/2; 309 return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 310 } 311 public boolean validate() 312 { 313 if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) 314 { 315 return false; 316 } 317 if(side1-side2>=side3||side1-side3>=side2||side2-side3>=side1) 318 { 319 return false; 320 } 321 else 322 { 323 return true; 324 } 325 } 326 } 327 class Trapezoid extends Shape{ 328 private double topSide; 329 private double bottomSide; 330 private double height; 331 public Trapezoid(){} 332 public Trapezoid(double topSide,double bottomSide,double height) 333 { 334 this.topSide=topSide; 335 this.bottomSide=bottomSide; 336 this.height=height; 337 } 338 public double getArea() 339 { 340 return (topSide+bottomSide)/2*height; 341 } 342 public boolean validate() 343 { 344 if(topSide<0||bottomSide<0||height<0) 345 { 346 return false; 347 } 348 else 349 { 350 return true; 351 } 352 } 353 }
报表内容如下:

类图如下:

这题和 7-1类似,主要是对卡片(Card)进行分组,同样, 卡片分为四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid)。
这次需要将每组的卡片根据面积值从大到小进行排序,并且求出卡片面积之和,然后根据卡片面积之和中的最大值排序。将根据卡片类型将所有卡片进行分组,对每组的
卡片根据面积值从大到小进行排序,同时求出该组内所有卡片 的面积之和,最后求出每组卡片面积之和中的最大值。同样使用了ArrayList数组存储各种图形对象,对高
效解题有很大帮助。
②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。 - 查询余额功能输入数据格式:
卡号
代码如下:
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4 5 public class Main { 6 public static void main(String[] args) { 7 ATM atm1 = new ATM("01","中国建设银行"); 8 ATM atm2 = new ATM("02","中国建设银行"); 9 ATM atm3 = new ATM("03","中国建设银行"); 10 ATM atm4 = new ATM("04","中国建设银行"); 11 ATM atm5 = new ATM("05","中国工商银行"); 12 ATM atm6 = new ATM("06","中国工商银行"); 13 //ATM组1 01,02,03,04 14 ArrayList<ATM> AtmList1 = new ArrayList<>(); 15 AtmList1.add(atm1); 16 AtmList1.add(atm2); 17 AtmList1.add(atm3); 18 AtmList1.add(atm4); 19 //ATM组2 05,06 20 ArrayList<ATM> AtmList2 = new ArrayList<>(); 21 AtmList2.add(atm5); 22 AtmList2.add(atm6); 23 //总atm组,共六台 24 ArrayList<ATM> atmList=new ArrayList<ATM>(); 25 atmList.add(atm1); 26 atmList.add(atm2); 27 atmList.add(atm3); 28 atmList.add(atm4); 29 atmList.add(atm5); 30 atmList.add(atm6); 31 32 Bank bank1 = new Bank("中国建设银行", AtmList1); 33 Bank bank2 = new Bank("中国工商银行", AtmList2); 34 //银行组 2个银行 35 ArrayList<Bank> bankList = new ArrayList<Bank>(); 36 bankList.add(bank1); 37 bankList.add(bank2); 38 //银联 39 UnionPay unionPay = new UnionPay(bankList); 40 //卡号组 分8组,10个卡号,含卡号,密码 41 Card card1 = new Card("6217000010041315709", "88888888");//杨过 42 Card card2 = new Card("6217000010041315715", "88888888"); 43 ArrayList<Card> cardList1 = new ArrayList<Card>(); 44 cardList1.add(card1); 45 cardList1.add(card2); 46 Card card3 = new Card("6217000010041315718", "88888888"); 47 ArrayList<Card> cardList2 = new ArrayList<Card>(); 48 cardList2.add(card3); 49 Card card4 = new Card("6217000010051320007", "88888888");//郭靖 50 ArrayList<Card> cardList3 = new ArrayList<Card>(); 51 cardList3.add(card4); 52 Card card5 = new Card("6222081502001312389", "88888888");//张无忌 53 ArrayList<Card> cardList4 = new ArrayList<Card>(); 54 cardList4.add(card5); 55 Card card6 = new Card("6222081502001312390", "88888888"); 56 ArrayList<Card> cardList5 = new ArrayList<Card>(); 57 cardList5.add(card6); 58 Card card7 = new Card("6222081502001312399", "88888888"); 59 Card card8 = new Card("6222081502001312400", "88888888"); 60 ArrayList<Card> cardList6 = new ArrayList<Card>(); 61 cardList6.add(card7); 62 cardList6.add(card8); 63 Card card9 = new Card("6222081502051320785", "88888888");//韦小宝 64 ArrayList<Card> cardList7 = new ArrayList<Card>(); 65 cardList7.add(card9); 66 Card card10 = new Card("6222081502051320786", "88888888"); 67 ArrayList<Card> cardList8 = new ArrayList<Card>(); 68 cardList8.add(card10); 69 //账号组 分4组 8个账号,含账号,卡号和余额 70 Account account1 = new Account("3217000010041315709", 10000.00, cardList1); 71 Account account2 = new Account("3217000010041315715", 10000.00, cardList2); 72 ArrayList<Account> accountList1 = new ArrayList<Account>(); 73 accountList1.add(account1); 74 accountList1.add(account2); 75 Account account3 = new Account("3217000010051320007", 10000.00, cardList3); 76 ArrayList<Account> accountList2 = new ArrayList<Account>(); 77 accountList2.add(account3); 78 Account account4 = new Account("3222081502001312389", 10000.00, cardList4); 79 Account account5 = new Account("3222081502001312390", 10000.00, cardList5); 80 Account account6 = new Account("3222081502001312399", 10000.00, cardList6); 81 ArrayList<Account> accountList3 = new ArrayList<Account>(); 82 accountList3.add(account4); 83 accountList3.add(account5); 84 accountList3.add(account6); 85 Account account7 = new Account("3222081502051320785", 10000.00, cardList7); 86 Account account8 = new Account("3222081502051320786", 10000.00, cardList8); 87 ArrayList<Account> accountList4 = new ArrayList<Account>(); 88 accountList4.add(account7); 89 accountList4.add(account8); 90 91 ArrayList<Account> accountList = new ArrayList<Account>(); 92 accountList.add(account1); 93 accountList.add(account2); 94 accountList.add(account3); 95 accountList.add(account4); 96 accountList.add(account5); 97 accountList.add(account6); 98 accountList.add(account7); 99 accountList.add(account8); 100 //4个用户 含用户名,银行和账号 101 User user1 = new User("杨过", "中国建设银行", accountList1,bank1); 102 User user2 = new User("郭靖", "中国建设银行", accountList2,bank1); 103 User user3 = new User("张无忌", "中国工商银行", accountList3,bank2); 104 User user4 = new User("韦小宝", "中国工商银行", accountList4,bank2); 105 //总用户组 含4个用户,包含姓名,银行,账号,余额,卡号,密码 106 ArrayList<User> userList = new ArrayList<User>(); 107 userList.add(user1); 108 userList.add(user2); 109 userList.add(user3); 110 userList.add(user4); 111 112 Scanner input=new Scanner(System.in); 113 String s; 114 Check check; 115 Check1 check1; 116 s=input.nextLine(); 117 while(!s.equals("#")){ 118 int i,j; 119 String[] shuju=s.split("\\s+");//输入的每行数据用空格隔开,存为数组 120 if(shuju.length!=1){ 121 check = new Check(userList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 122 if (check.check()){ 123 Access access = new Access(userList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); 124 access.access(); 125 126 Show show = new Show(userList,shuju[0], shuju[1],shuju[2],Double.parseDouble(shuju[3])); 127 show.show(); 128 } 129 } 130 else { 131 check1=new Check1(userList,shuju[0]); 132 if(check1.check1()){ 133 Show1 show1= new Show1(userList,shuju[0]); 134 show1.show1(); 135 } 136 } 137 s=input.nextLine(); 138 } 139 } 140 } 141 142 class Check { 143 ArrayList<User> userList; 144 //ArrayList<Account> accountList; 145 //ArrayList<Bank> bankList; 146 ArrayList<ATM> atmList; 147 String cardID; 148 String password; 149 String atmID; 150 double money; 151 152 public Check(ArrayList<User> userList, String cardID, String password, String atmID, double money) { 153 this.userList = userList; 154 this.atmID = atmID; 155 this.password = password; 156 this.cardID = cardID; 157 this.money = money; 158 } 159 160 public boolean check() { 161 int flag = 0; 162 int i, j = 0,m=0; 163 int k=0, l = 0,n=0; 164 //检查账号是否正确 165 for (i = 0; i < userList.size(); i++) { 166 for (j = 0; j < userList.get(i).accountList.size(); j++) { 167 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 168 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 169 flag = 1; 170 k = i; 171 l = j; 172 n = m; 173 break; 174 } 175 } 176 if (flag == 1) { 177 break; 178 } 179 } 180 if (flag == 1) { 181 break; 182 } 183 } 184 //检查密码是否正确 185 if (flag == 1) { 186 if (password.equals(userList.get(k).accountList.get(l).cardList.get(n).password)) { 187 flag = 2; 188 } else { 189 System.out.println("Sorry,your password is wrong.");//银行卡密码错误 190 return false; 191 } 192 } else { 193 System.out.println("Sorry,this card does not exist.");//卡号不存在 194 return false; 195 } 196 //检查ATM机编号是否正确 197 if (flag == 2) { 198 /* for (i = 0; i < atmList.size(); i++) { 199 200 if(atmID.equals(atmList.get(i).atmID)) 201 { 202 flag = 3; 203 break; 204 } 205 206 }*/ 207 if(atmID.equals("01")||atmID.equals("02")||atmID.equals("03")||atmID.equals("04")||atmID.equals("05")||atmID.equals("06")) 208 { 209 flag = 3; 210 211 } 212 } 213 //检查金额是否正确 214 if (flag == 3) { 215 if (money <= userList.get(k).accountList.get(l).getBalance()) { 216 flag = 4; 217 } else { 218 System.out.println("Sorry,your account balance is insufficient.");//取款金额大于账户余额 219 return false; 220 } 221 } else { 222 System.out.println("Sorry,the ATM's id is wrong.");//ATM机编号不存在 223 return false; 224 } 225 //检查是否跨行 226 if (flag == 4) { 227 for (i = 0; i < userList.size(); i++) { 228 for (j=0;j<userList.get(k).bank.AtmList.size();j++) { 229 if(atmID.equals(userList.get(k).bank.AtmList.get(j).atmID)) 230 { 231 flag =5; 232 break; 233 } 234 } 235 } 236 } 237 if (flag != 5) { 238 System.out.println("Sorry,cross-bank withdrawal is not supported.");//跨行存取款 239 return false; 240 } else 241 return true; 242 } 243 } 244 245 //余额查询的检查类 246 class Check1{ 247 ArrayList<User> userList; 248 // ArrayList<Account> accountList; 249 String cardID; 250 251 public Check1(ArrayList<User> userList, String cardID){ 252 this.userList = userList; 253 this.cardID = cardID; 254 } 255 public boolean check1(){ 256 int i,j,m; 257 int flag=0; 258 //卡号校验 259 for (i = 0; i < userList.size(); i++) { 260 for (j = 0; j < userList.get(i).accountList.size(); j++) { 261 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 262 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 263 flag = 1; 264 break; 265 } 266 } 267 if (flag == 1) { 268 break; 269 } 270 } 271 if (flag == 1) { 272 break; 273 } 274 } 275 if(flag==1) 276 return true; 277 else{ 278 System.out.println("Sorry,this card does not exist.");//卡号不存在 279 return false; 280 } 281 } 282 } 283 284 class Access{ 285 ArrayList<User> userList; 286 //ArrayList<Account> accountList; 287 String cardID; 288 String password; 289 String atmID; 290 double money; 291 public Access(ArrayList<User> userList, String cardID, String password, String atmID, double money){ 292 this.password=password; 293 this.atmID=atmID; 294 this.cardID=cardID; 295 this.userList=userList; 296 this.money=money; 297 } 298 public void access(){ 299 int i,j,m; 300 int t=0,k=0; 301 //卡号校验 302 for (i = 0; i < userList.size(); i++) { 303 for (j = 0; j < userList.get(i).accountList.size(); j++) { 304 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 305 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 306 t=i; 307 k=j; 308 break; 309 } 310 } 311 } 312 } 313 userList.get(t).accountList.get(k).setBalance(userList.get(t).accountList.get(k).getBalance()-money); 314 } 315 } 316 317 //存取款的展示类 318 class Show{ 319 //ArrayList<Account> accountList; 320 ArrayList<User> userList; 321 String cardID; 322 String password; 323 String atmID; 324 double money; 325 public Show( ArrayList<User> userList, String cardID, String password, String atmID, double money){ 326 this.password=password; 327 this.atmID=atmID; 328 this.cardID=cardID; 329 this.userList=userList; 330 this.money=money; 331 } 332 public void show() { 333 int i, j, k=0; 334 int t = 0, l = 0,m; 335 //卡号校验 336 for (i = 0; i < userList.size(); i++) { 337 for (j = 0; j < userList.get(i).accountList.size(); j++) { 338 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 339 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 340 t=i; 341 k=j; 342 break; 343 } 344 } 345 } 346 } 347 //取款 348 if (money >= 0) { 349 System.out.printf(userList.get(t).getUserName() + "在" + userList.get(t).getBankName() + "的" + atmID + "号ATM机上取款¥%.2f\n", money); 350 } else { 351 money = -money; 352 System.out.printf(userList.get(t).getUserName() + "在" + userList.get(t).getBankName() + "的" + atmID + "号ATM机上存款¥%.2f\n", money); 353 } 354 System.out.printf("当前余额为¥%.2f\n", userList.get(t).accountList.get(k).getBalance()); 355 } 356 } 357 358 //余额查询的展示类 359 class Show1{ 360 //ArrayList<Account> accountList; 361 ArrayList<User> userList; 362 String cardID; 363 public Show1(ArrayList<User> userList, String cardID){ 364 this.userList=userList; 365 this.cardID=cardID; 366 } 367 public void show1(){ 368 int i,j,m; 369 int t=0,k=0; 370 //卡号校验 371 for (i = 0; i < userList.size(); i++) { 372 for (j = 0; j < userList.get(i).accountList.size(); j++) { 373 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 374 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 375 t=i; 376 k=j; 377 break; 378 } 379 } 380 } 381 } 382 System.out.printf("¥%.2f\n",userList.get(t).accountList.get(k).getBalance()); 383 } 384 } 385 386 class UnionPay{ 387 ArrayList<Bank> BankList; 388 public UnionPay(){} 389 public UnionPay(ArrayList<Bank> BankList){ 390 this.BankList=BankList; 391 } 392 public ArrayList<Bank> getBankList() { 393 return BankList; 394 } 395 396 public void setBankList(ArrayList<Bank> bankList) { 397 BankList = bankList; 398 } 399 } 400 401 class Bank{ 402 String BankName; 403 ArrayList<ATM> AtmList; 404 public Bank(){} 405 public Bank(String BankName,ArrayList<ATM> AtmList){ 406 this.BankName=BankName; 407 this.AtmList=AtmList; 408 } 409 public String getBankName() { 410 return BankName; 411 } 412 public ArrayList<ATM> getAtmList() { 413 return AtmList; 414 } 415 public void setBankName(String BankName){ 416 this.BankName=BankName; 417 } 418 public void setAtmList(ArrayList<ATM> AtmList){ 419 this.AtmList=AtmList; 420 } 421 } 422 class ATM{ 423 String atmID; 424 String bankName; 425 public ATM(){} 426 public ATM(String atmID,String bankName){ 427 this.atmID=atmID; 428 this.bankName=bankName; 429 } 430 431 public String getAtmID() { 432 return atmID; 433 } 434 435 public void setAtmID(String atmID) { 436 this.atmID = atmID; 437 } 438 439 public String getBankName() { 440 return bankName; 441 } 442 443 public void setBankName(String bankName) { 444 this.bankName = bankName; 445 } 446 } 447 448 class User{ 449 String UserName; 450 String bankName; 451 Bank bank; 452 ArrayList<Account> accountList=new ArrayList<Account>(); 453 public User(){} 454 public User(String UserName,String bankName,ArrayList<Account> accountList, Bank bank){ 455 this.accountList=accountList; 456 this.bankName=bankName; 457 this.UserName=UserName; 458 this.bank=bank; 459 } 460 461 public void setBankName(String bankName) { 462 this.bankName = bankName; 463 } 464 465 public String getBankName() { 466 return bankName; 467 } 468 469 public ArrayList<Account> getAccountList() { 470 return accountList; 471 } 472 473 public String getUserName() { 474 return UserName; 475 } 476 477 public void setAccountList(ArrayList<Account> accountList) { 478 this.accountList = accountList; 479 } 480 481 public void setUserName(String userName) { 482 UserName = userName; 483 } 484 } 485 class Account{ 486 String accountID; 487 double balance; 488 ArrayList<Card> cardList=new ArrayList<Card>(); 489 public Account(){} 490 public Account(String accountID,double balance,ArrayList<Card> cardList){ 491 this.balance=balance; 492 this.accountID=accountID; 493 this.cardList=cardList; 494 } 495 496 public double getBalance() { 497 return balance; 498 } 499 500 public void setBalance(double balance) { 501 this.balance = balance; 502 } 503 504 public String getAccountID() { 505 return accountID; 506 } 507 508 public void setAccountID(String accountID) { 509 this.accountID = accountID; 510 } 511 512 public ArrayList<Card> getCardList() { 513 return cardList; 514 } 515 516 public void setCardList(ArrayList<Card> cardList) { 517 this.cardList = cardList; 518 } 519 } 520 class Card{ 521 String cardID; 522 String password; 523 public Card(){} 524 public Card(String cardID,String password){ 525 this.cardID=cardID; 526 this.password=password; 527 } 528 529 public void setPassword(String password) { 530 this.password = "88888888"; 531 } 532 533 public String getPassword() { 534 return password; 535 } 536 537 public String getCardID() { 538 return cardID; 539 } 540 541 public void setCardID(String cardID) { 542 this.cardID = cardID; 543 } 544 }
报表内容如下:

类图如下:

本次作业未按照标准格式写,代码中运用了大量的ArrayList数组初始化个用户的数据,总初始化代码量就占据了100行作业,数量非常多,且所用的诸如校验和计算类当
中有许多重复的代码,可用性比较差,只能通过User去查找,由此产生了很多循环,每次查找都需要使用3次for循环才能找到卡号,最大圈复杂度很大,可读性很差。所
有的查找都是基于卡号去查询的,而查询都必须经过User去查找,所有的信息最后都存储在了User类中。银行类并没有实现太大的功能,所以初始化当中也有很多冗余
的代码。主要也是这次作业难度实在有点大,各类当中的关系很难理清,Account类不仅包含账户信息,还必须能够通过Card类查找到与之相连的卡号等信息,所以最后
干脆都存储在了User类当中,所有信息都可通过User类去寻找。
设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔) - 查询余额功能输入数据格式:
卡号
代码如下:
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main{ 5 public static void main(String[] args) { 6 String a="中国建设银行",b1="中国工商银行",c="中国农业银行"; 7 ATM atm1 = new ATM("01",a); 8 ATM atm2 = new ATM("02",a); 9 ATM atm3 = new ATM("03",a); 10 ATM atm4 = new ATM("04",a); 11 ATM atm5 = new ATM("05",b1); 12 ATM atm6 = new ATM("06",b1); 13 ATM atm7 = new ATM("07",c); 14 ATM atm8 = new ATM("08",c); 15 ATM atm9 = new ATM("09",c); 16 ATM atm10= new ATM("10",c); 17 ATM atm11= new ATM("11",c); 18 ArrayList<ATM> AtmList1 = new ArrayList<>(); 19 AtmList1.add(atm1); 20 AtmList1.add(atm2); 21 AtmList1.add(atm3); 22 AtmList1.add(atm4); 23 ArrayList<ATM> AtmList2 = new ArrayList<>(); 24 AtmList2.add(atm5); 25 AtmList2.add(atm6); 26 ArrayList<ATM> AtmList3 = new ArrayList<>(); 27 AtmList3.add(atm7); 28 AtmList3.add(atm8); 29 AtmList3.add(atm9); 30 AtmList3.add(atm10); 31 AtmList3.add(atm11); 32 ATM atms[] ={atm1,atm2,atm3,atm4,atm5,atm6,atm7,atm8,atm9,atm10,atm11}; 33 ArrayList<ATM> atmList=new ArrayList<>(); 34 for (int i = 0; i < atms.length;i ++){ 35 atmList.add(atms[i]); 36 } 37 38 Bank bank1 = new Bank(a, AtmList1); 39 Bank bank2 = new Bank(b1, AtmList2); 40 Bank bank3 = new Bank(c, AtmList3); 41 ArrayList<Bank> bankList = new ArrayList<>(); 42 bankList.add(bank1); 43 bankList.add(bank2); 44 bankList.add(bank3); 45 46 String p="88888888"; 47 Card card1 = new Card("6217000010041315709",p); 48 Card card2 = new Card("6217000010041315715",p); 49 ArrayList<Card> cardList1 = new ArrayList<>(); 50 cardList1.add(card1); 51 cardList1.add(card2); 52 Card card3 = new Card("6217000010041315718",p); 53 ArrayList<Card> cardList2 = new ArrayList<>(); 54 cardList2.add(card3); 55 Card card4 = new Card("6217000010051320007",p); 56 ArrayList<Card> cardList3 = new ArrayList<>(); 57 cardList3.add(card4); 58 Card card5 = new Card("6222081502001312389",p); 59 ArrayList<Card> cardList4 = new ArrayList<>(); 60 cardList4.add(card5); 61 Card card6 = new Card("6222081502001312390",p); 62 ArrayList<Card> cardList5 = new ArrayList<>(); 63 cardList5.add(card6); 64 Card card7 = new Card("6222081502001312399",p); 65 Card card8 = new Card("6222081502001312400",p); 66 ArrayList<Card> cardList6 = new ArrayList<>(); 67 cardList6.add(card7); 68 cardList6.add(card8); 69 Card card9 = new Card("6222081502051320785",p); 70 ArrayList<Card> cardList7 = new ArrayList<>(); 71 cardList7.add(card9); 72 Card card10 = new Card("6222081502051320786",p); 73 ArrayList<Card> cardList8 = new ArrayList<>(); 74 cardList8.add(card10); 75 Card card11 = new Card("6640000010045442002",p); 76 Card card12 = new Card("6640000010045442003",p); 77 ArrayList<Card> cardList9 = new ArrayList<>(); 78 cardList9.add(card11); 79 cardList9.add(card12); 80 Card card13 = new Card("6640000010045441009",p); 81 ArrayList<Card> cardList10 = new ArrayList<>(); 82 cardList10.add(card13); 83 Card card14 = new Card("6630000010033431001",p); 84 ArrayList<Card> cardList11 = new ArrayList<>(); 85 cardList11.add(card14); 86 Card card15 = new Card("6630000010033431008",p); 87 ArrayList<Card> cardList12 = new ArrayList<>(); 88 cardList12.add(card15); 89 double b=10000.00; 90 Account account1 = new Account("3217000010041315709", b, cardList1); 91 Account account2 = new Account("3217000010041315715", b, cardList2); 92 ArrayList<Account> accountList1 = new ArrayList<>(); 93 accountList1.add(account1); 94 accountList1.add(account2); 95 Account account3 = new Account("3217000010051320007", b, cardList3); 96 ArrayList<Account> accountList2 = new ArrayList<>(); 97 accountList2.add(account3); 98 Account account4 = new Account("3222081502001312389", b, cardList4); 99 Account account5 = new Account("3222081502001312390", b, cardList5); 100 Account account6 = new Account("3222081502001312399", b, cardList6); 101 ArrayList<Account> accountList3 = new ArrayList<>(); 102 accountList3.add(account4); 103 accountList3.add(account5); 104 accountList3.add(account6); 105 Account account7 = new Account("3222081502051320785", b, cardList7); 106 Account account8 = new Account("3222081502051320786", b, cardList8); 107 ArrayList<Account> accountList4 = new ArrayList<>(); 108 accountList4.add(account7); 109 accountList4.add(account8); 110 Account account9 = new Account("3640000010045442002", b, cardList9); 111 ArrayList<Account> accountList5 = new ArrayList<>(); 112 accountList5.add(account9); 113 Account account10 = new Account("3640000010045441009", b, cardList10); 114 ArrayList<Account> accountList6 = new ArrayList<>(); 115 accountList6.add(account10); 116 Account account11 = new Account("3630000010033431001", b, cardList11); 117 ArrayList<Account> accountList7 = new ArrayList<>(); 118 accountList7.add(account11); 119 Account account12 = new Account("3630000010033431008", b, cardList12); 120 ArrayList<Account> accountList8 = new ArrayList<>(); 121 accountList8.add(account12); 122 123 ArrayList<Account> accountList = new ArrayList<>(); 124 Account accountss[] = {account1,account2,account3,account4,account5,account6,account7,account8,account9,account10,account11,account12}; 125 for (int i = 0; i< accountss.length;i ++){ 126 accountList.add(accountss[i]); 127 } 128 129 User user1 = new User("杨过", a, accountList1,bank1); 130 User user2 = new User("郭靖", a, accountList2,bank1); 131 User user3 = new User("张无忌", b1, accountList3,bank2); 132 User user4 = new User("韦小宝", b1, accountList4,bank2); 133 User user5 = new User("张三丰", a, accountList5,bank1); 134 User user6 = new User("令狐冲", b1, accountList6,bank2); 135 User user7 = new User("乔峰", c, accountList7,bank3); 136 User user8 = new User("洪七公", c, accountList8,bank3); 137 138 ArrayList<User> userList = new ArrayList<>(); 139 User users[]={user1,user2,user3,user4,user5,user6,user7,user8}; 140 for (int i = 0; i< users.length;i ++){ 141 userList.add(users[i]); 142 } 143 144 ArrayList<User> userList1 = new ArrayList<>(); 145 userList1.add(user5); 146 userList1.add(user6); 147 userList1.add(user7); 148 userList1.add(user8); 149 150 Scanner input=new Scanner(System.in); 151 String s; 152 Check check; 153 Check check1; 154 155 Access access; 156 Show show; 157 158 s=input.nextLine(); 159 while(!s.equals("#")){ 160 String[] shuju=s.split("\\s+"); 161 if(shuju.length!=1){ 162 access =new Access(userList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 163 check = new Check(userList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 164 check1=new Check(userList,userList1,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 165 show=new Show(userList,shuju[0],shuju[1],shuju[2],Double.parseDouble(shuju[3])); 166 if (check.check()){ 167 if(check1.check3()) { 168 169 if (check.check2()) { 170 access.access3(); 171 show.show2(); 172 } else { 173 access.access2(); 174 show.show2(); 175 } 176 } 177 else { 178 if(check.check4()){ 179 if(check.check2()){ 180 access.access1(); 181 show.show(); 182 } 183 else { 184 access.access(); 185 show.show(); 186 } 187 } 188 } 189 } 190 } 191 else { 192 check=new Check(userList,shuju[0]); 193 if(check.check1()){ 194 show=new Show(userList,shuju[0]); 195 show.show1(); 196 } 197 } 198 s=input.nextLine(); 199 } 200 } 201 } 202 203 class Check { 204 ArrayList<User> userList; 205 ArrayList<User> userList1; 206 String cardID; 207 String password; 208 String atmID; 209 double money; 210 int k = 0, l = 0, n = 0; 211 public Check(ArrayList<User> userList, String cardID, String password, String atmID, double money) { 212 this.userList = userList; 213 this.atmID = atmID; 214 this.password = password; 215 this.cardID = cardID; 216 this.money = money; 217 } 218 219 public boolean check() { 220 int flag = 0; 221 int i, j , m ; 222 for (i = 0; i < userList.size(); i++) { 223 for (j = 0; j < userList.get(i).accountList.size(); j++) { 224 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 225 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 226 flag = 1; 227 this.k = i; 228 this.l = j; 229 this.n = m; 230 break; 231 } 232 } 233 if (flag == 1) { 234 break; 235 } 236 } 237 if (flag == 1) { 238 break; 239 } 240 } 241 242 if (flag == 1) { 243 if (password.equals(userList.get(k).accountList.get(l).cardList.get(n).password)) { 244 flag = 2; 245 } else { 246 System.out.println("Sorry,your password is wrong."); 247 return false; 248 } 249 } else { 250 System.out.println("Sorry,this card does not exist."); 251 return false; 252 } 253 254 if (flag == 2) { 255 if (atmID.equals("01") || atmID.equals("02") || atmID.equals("03") || atmID.equals("04") || atmID.equals("05") || atmID.equals("06") || atmID.equals("07") || atmID.equals("08") || atmID.equals("09") || atmID.equals("10") || atmID.equals("11")) { 256 flag = 3; 257 258 } 259 } 260 if (flag == 3) { 261 return true; 262 } 263 else{ 264 System.out.println("Sorry,the ATM's id is wrong."); 265 return false; 266 } 267 } 268 public Check(ArrayList<User> userList, String cardID) 269 { 270 this.userList = userList; 271 this.cardID = cardID; 272 } 273 274 public boolean check1(){ 275 int i,j,m; 276 int flag=0; 277 278 for (i = 0; i < userList.size(); i++) { 279 for (j = 0; j < userList.get(i).accountList.size(); j++) { 280 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 281 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 282 flag = 1; 283 break; 284 } 285 } 286 if (flag == 1) { 287 break; 288 } 289 } 290 if (flag == 1) { 291 break; 292 } 293 } 294 if(flag==1) 295 return true; 296 else{ 297 System.out.println("Sorry,this card does not exist."); 298 return false; 299 } 300 } 301 public boolean check2(){ 302 check(); 303 int j,flag=0; 304 for (j=0;j<userList.get(this.k).bank.AtmList.size();j++) { 305 if(atmID.equals(userList.get(this.k).bank.AtmList.get(j).atmID)) 306 { 307 flag =1; 308 break; 309 } 310 } 311 if(flag==1){ 312 return false; 313 } 314 else { 315 return true; 316 } 317 } 318 public Check(ArrayList<User> userList,ArrayList<User> userList1,String cardID,String password,String atmID,double money){ 319 this.userList = userList; 320 this.userList1=userList1; 321 this.atmID = atmID; 322 this.password = password; 323 this.cardID = cardID; 324 this.money = money; 325 } 326 public boolean check3(){ 327 int i, j, m; 328 int flag = 0; 329 330 for (i = 0; i < userList1.size(); i++) { 331 for (j = 0; j < userList1.get(i).accountList.size(); j++) { 332 for (m = 0; m < userList1.get(i).accountList.get(j).cardList.size(); m++) { 333 if (cardID.equals(userList1.get(i).accountList.get(j).cardList.get(m).cardID)) { 334 flag=1; 335 break; 336 } 337 } 338 } 339 } 340 if(flag==1){ 341 return true; 342 } 343 else { 344 return false; 345 } 346 } 347 public boolean check4(){ 348 check(); 349 if (money <= userList.get(k).accountList.get(l).getBalance()) { 350 return true; 351 } else { 352 System.out.println("Sorry,your account balance is insufficient."); 353 System.exit(0); 354 return false; 355 } 356 } 357 } 358 359 360 361 class Access{ 362 ArrayList<User> userList; 363 String cardID; 364 String password; 365 String atmID; 366 double money; 367 int t=0,k=0; 368 public Access(ArrayList<User> userList, String cardID, String password, String atmID, double money){ 369 this.password=password; 370 this.atmID=atmID; 371 this.cardID=cardID; 372 this.userList=userList; 373 this.money=money; 374 } 375 public void Access(){ 376 int i,j,m; 377 for (i = 0; i < userList.size(); i++) { 378 for (j = 0; j < userList.get(i).accountList.size(); j++) { 379 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 380 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 381 this.t=i; 382 this.k=j; 383 break; 384 } 385 } 386 } 387 } 388 } 389 public void access(){ 390 Access(); 391 userList.get(t).accountList.get(k).setBalance(userList.get(t).accountList.get(k).getBalance()-money); 392 } 393 public void access1(){ 394 Access(); 395 double p=0; 396 for (int j = 0; j < userList.get(t).bank.AtmList.size(); j++) { 397 if (atmID.equals("01") || atmID.equals("02") || atmID.equals("03") || atmID.equals("04")) { 398 p = money * 0.02; 399 break; 400 } else if (atmID.equals("05") || atmID.equals("06")) { 401 p = money * 0.03; 402 break; 403 } else if (atmID.equals("07") || atmID.equals("08") || atmID.equals("09") || atmID.equals("10") || atmID.equals("11")) { 404 p = money * 0.04; 405 break; 406 } 407 } 408 userList.get(t).accountList.get(k).setBalance(userList.get(t).accountList.get(k).getBalance()-money-p); 409 } 410 public void access2(){ 411 double p; 412 Access(); 413 414 if(userList.get(t).accountList.get(k).getBalance()>0&&userList.get(t).accountList.get(k).getBalance()>money) 415 { 416 userList.get(t).accountList.get(k).setBalance(userList.get(t).accountList.get(k).getBalance()-money); 417 } 418 else if(userList.get(t).accountList.get(k).getBalance()>0&&userList.get(t).accountList.get(k).getBalance()<money) 419 { 420 421 p=(money-userList.get(t).accountList.get(k).getBalance())*0.05; 422 userList.get(t).accountList.get(k).setBalance(userList.get(t).accountList.get(k).getBalance()-money-p); 423 } 424 else if(userList.get(t).accountList.get(k).getBalance()<=0&&userList.get(t).accountList.get(k).getBalance()<money){ 425 p=money*0.05; 426 userList.get(t).accountList.get(k).setBalance(userList.get(t).accountList.get(k).getBalance()-money-p); 427 } 428 } 429 public void access3(){ 430 Access(); 431 int j; 432 double p = 0,q; 433 for (j = 0; j < userList.get(k).bank.AtmList.size(); j++) { 434 if (atmID.equals("01") || atmID.equals("02") || atmID.equals("03") || atmID.equals("04")) { 435 p = money * 0.02; 436 break; 437 } else if (atmID.equals("05") || atmID.equals("06")) { 438 p = money * 0.03; 439 break; 440 } else if (atmID.equals("07") || atmID.equals("08") || atmID.equals("09") || atmID.equals("10") || atmID.equals("11")) { 441 p = money * 0.04; 442 break; 443 } 444 } 445 if(userList.get(t).accountList.get(k).getBalance()>0&&userList.get(t).accountList.get(this.k).getBalance()>=money){ 446 userList.get(t).accountList.get(k).setBalance(userList.get(this.t).accountList.get(this.k).getBalance()-money-p); 447 } 448 else if(userList.get(t).accountList.get(k).getBalance()>0&&userList.get(this.t).accountList.get(this.k).getBalance()<money){ 449 q=(money-userList.get(t).accountList.get(this.k).getBalance())*0.05; 450 userList.get(t).accountList.get(k).setBalance(userList.get(this.t).accountList.get(this.k).getBalance()-money-p-q); 451 } 452 else if(userList.get(this.t).accountList.get(this.k).getBalance()<=0&&userList.get(this.t).accountList.get(this.k).getBalance()<money) 453 { 454 q=money*0.05; 455 userList.get(this.t).accountList.get(this.k).setBalance(userList.get(this.t).accountList.get(this.k).getBalance()-money-p-q); 456 } 457 } 458 } 459 460 class Show { 461 int t=0,k=0; 462 String bankName; 463 ArrayList<User> userList; 464 String cardID; 465 String password; 466 String atmID; 467 double money; 468 469 public Show(ArrayList<User> userList, String cardID, String password, String atmID, double money) { 470 this.password = password; 471 this.atmID = atmID; 472 this.cardID = cardID; 473 this.userList = userList; 474 this.money = money; 475 } 476 public void Show(){ 477 int i, j,m; 478 for (i = 0; i < userList.size(); i++) { 479 for (j = 0; j < userList.get(i).accountList.size(); j++) { 480 for (m = 0; m < userList.get(i).accountList.get(j).cardList.size(); m++) { 481 if (cardID.equals(userList.get(i).accountList.get(j).cardList.get(m).cardID)) { 482 this.t = i; 483 this.k = j; 484 break; 485 } 486 } 487 } 488 } 489 } 490 public void Show1(){ 491 492 for (int j = 0; j < userList.get(this.k).bank.AtmList.size(); j++) { 493 if (atmID.equals("01") || atmID.equals("02") || atmID.equals("03") || atmID.equals("04")) { 494 this.bankName = "中国建设银行"; 495 break; 496 } else if (atmID.equals("05") || atmID.equals("06")) { 497 this.bankName = "中国工商银行"; 498 break; 499 } else if (atmID.equals("07") || atmID.equals("08") || atmID.equals("09") || atmID.equals("10") || atmID.equals("11")) { 500 this.bankName = "中国农业银行"; 501 break; 502 } 503 } 504 } 505 public void show() { 506 Show(); 507 Show1(); 508 if (money >= 0) { 509 if (userList.get(this.t).accountList.get(this.k).getBalance() > 0) { 510 System.out.printf("业务:取款 " + userList.get(this.t).getUserName() + "在" + bankName + "的" + atmID + "号ATM机上取款¥%.2f\n", money); 511 System.out.printf("当前余额为¥%.2f\n", userList.get(this.t).accountList.get(this.k).getBalance()); 512 } else { 513 System.out.println("Sorry,your account balance is insufficient."); 514 System.exit(0); 515 } 516 } else { 517 money = -money; 518 System.out.printf("业务:存款 " + userList.get(this.t).getUserName() + "在" + bankName + "的" + atmID + "号ATM机上存款¥%.2f\n", money); 519 System.out.printf("当前余额为¥%.2f\n", userList.get(this.t).accountList.get(this.k).getBalance()); 520 } 521 } 522 public Show(ArrayList<User> userList, String cardID){ 523 this.userList=userList; 524 this.cardID=cardID; 525 } 526 public void show1(){ 527 Show(); 528 System.out.printf("业务:查询余额 ¥%.2f\n",userList.get(this.t).accountList.get(this.k).getBalance()); 529 } 530 public void show2(){ 531 Show(); 532 Show1(); 533 if (money > 0) { 534 if (userList.get(t).accountList.get(k).getBalance() >=(-50000)) { 535 System.out.printf("业务:取款 " + userList.get(this.t).getUserName() + "在" + bankName + "的" + atmID + "号ATM机上取款¥%.2f\n", money); 536 System.out.printf("当前余额为¥%.2f\n", userList.get(this.t).accountList.get(this.k).getBalance()); 537 } else { 538 System.out.println("Sorry,your account balance is insufficient."); 539 System.exit(0); 540 } 541 } else { 542 money = -money; 543 System.out.printf("业务:存款 " + userList.get(this.t).getUserName() + "在" + bankName + "的" + atmID + "号ATM机上存款¥%.2f\n", money); 544 System.out.printf("当前余额为¥%.2f\n", userList.get(this.t).accountList.get(this.k).getBalance()); 545 } 546 } 547 } 548 class Bank{ 549 String BankName; 550 ArrayList<ATM> AtmList; 551 public Bank(String BankName,ArrayList<ATM> AtmList){ 552 this.BankName=BankName; 553 this.AtmList=AtmList; 554 } 555 } 556 class ATM{ 557 String atmID; 558 String bankName; 559 public ATM(String atmID,String bankName){ 560 this.atmID=atmID; 561 this.bankName=bankName; 562 } 563 } 564 565 class User{ 566 String UserName; 567 String bankName; 568 Bank bank; 569 ArrayList<Account> accountList; 570 public User(String UserName,String bankName,ArrayList<Account> accountList, Bank bank){ 571 this.accountList=accountList; 572 this.bankName=bankName; 573 this.UserName=UserName; 574 this.bank=bank; 575 } 576 public String getUserName() { 577 return UserName; 578 } 579 } 580 class Account{ 581 String accountID; 582 double balance; 583 ArrayList<Card> cardList; 584 public Account(String accountID,double balance,ArrayList<Card> cardList){ 585 this.balance=balance; 586 this.accountID=accountID; 587 this.cardList=cardList; 588 } 589 public double getBalance() { 590 return balance; 591 } 592 593 public void setBalance(double balance) { 594 this.balance = balance; 595 } 596 } 597 class Card{ 598 String cardID; 599 String password; 600 public Card(String cardID,String password){ 601 this.cardID=cardID; 602 this.password=password; 603 } 604 }
报表内容如下:

类图如下:

这次题目和上次的ATM机从总体上来看大同小异,仅仅增加了一种新的信用卡。但从结构上来说,却要发生很大的改变,所有的包括atm机以及Account、User类等结构
都需要新增加信用卡类别。对于初始化又有了很大的负担。且新增了check类和count类来计算信用卡用户,由于要分借记卡和信用卡,所以要另外写check类和count类,
还有show类等,主要结构没有发生太大变化,直接在主函数里面加入判断,从而实现借记卡和信用卡的存取款。
(3)采坑心得
1.主要问题是关于7-3 ATM机类结构设计(一)的提交过程中,出现了代码长度超长,导致提交不了,于是删了许多基础类中没有用到的函数。由此可以知道代码切忌冗余,
最好简介清晰。
2.在7-1 ATM机类结构设计(二)中也遇到了和7-3 ATM机类结构设计(一)同样的问题,代码超出限定长度大概200多行。于是将check类、show类、account类进行了简写,
将代码中相同的部分全部整合到一个类当中去,在类里面进行各种借记卡和信用卡的分类使用。最后再初始化也进行了简写处理,将数组利用循环存储。
3.提交7-1 ATM机类结构设计(二)答案之后,只有60分,期间一直在测试bug,发现例子的数据都对得上,但就是拿不到满分。然后发现是少了一名成员洪七公(只能怪他
单独出现在下一页,没看到。)本以为问题解决了,结果一提交还是60分,但是测试的所有数据都没有问题,所以才想可能是有些小细节出现了差错,比如打错字什么的,最后,
在我的室友的共同努力下,终于找出了错误。原来是在初始化时,新增的信用卡用户没有放到新数组里面,还是放在了原来的借记卡用户数组里,所以导致有四个测试点过不去。
粗心害死人啊,改了整整两天才看到!
(4)改进建议
主要问题还是出现在ATM机类结构设计那两道题目中,从代码超长就可以看出写的代码并不简洁,代码中有许多重复的过程,比如初始化数据,我是一个一个添加进去的,有多
少用户就有多少个数组,所以初始化就占了100多行。有其是在这基础上加了新类型卡和新用户,初始化更复杂了。(二)中的check和show类等也是结构没有设计好,没实现单
一职责,所以添加新方法的时候会增加很多不必要的重复代码。所以在设计类的时候要先分析各类之间的联系以及类的结构设计,这在简化代码上会有很大帮助。
(5)总结
开闭原则是最基础的一个原则,抽象化是开闭原则的关键。是通过接口或者抽象类对扩展进行约束,不允许出现在接口或者抽象类中不存在的public方法。参数类型,引用对
象尽量使用接口或者抽象类,而不是具体的实现类。抽象层尽量保持稳定,一旦确定就不允许修改。写代码的时候要严格按照题目给的输入输出格式,不能完全按照自己所想
的,所想的想法要在规则之内。看完题目以及题目要求之后,按照自己想去敲代码,敲了一定的时间后,就敲不下去了,有的地方完全不是按照我自己脑子里想的那样去做。
这三次作业中基本上都运用到了面向对象的封装性、继承性与多态性三大技术特性,这让我们进一步加深了对它们的理解。其中的封装性基本上每道题都有用到,将每个类
中的属性设为私有,且每个类中都含有getter、setter方法和构造方法。继承性在三次作业中也是基本上都用上了,但难度却是逐步提升,首先是简单的使用extends来体现
继承性,然后开始增加难度,要使用子类继承并重写父类的方法(@Override)。在本阶段的后期学习了多态化的概念(不同的对象,接收到同一个消息的时候,产生不同的
操作),还学习了解了compareTo()接口等。

浙公网安备 33010602011771号