JAVA第三次大作业Blog
前言
题目集7-9的题量是比较少的,重点考察类设计能力,难度适中。只要类设计的恰当,题目是不难做的。也就是说这三次的题目集都只是很单纯的考察我们面向对象的设计思维和类设计能力。
设计与分析
①题目集7中7-1、7-2两题的递进式设计分析总结
先分析题目7-1。在本题的指导书中给出了具体的类设计建议,根据该类设计完善自己的代码即可,基本不会出现什么问题。
先给出我自己的源码。
1 import java.util.*; 2 3 public class Main { 4 public static Scanner inp = new Scanner(System.in); 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 boolean flag = true; 10 11 flag = Input.inType(list); 12 if(!flag) { 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 17 DealCardList deal = new DealCardList(list); 18 flag = Input.inValues(deal.cardList,list); 19 if(!flag) { 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 24 deal.showResult(); 25 } 26 27 } 28 class Input { 29 30 static boolean inType(ArrayList<Integer> list) { 31 // Scanner in = new Scanner(System.in); 32 int valu; 33 boolean flag = true; 34 valu = Main.inp.nextInt(); 35 while(valu!=0) 36 { 37 if(type_validata(valu)) 38 list.add(valu); 39 else 40 flag = false; 41 valu = Main.inp.nextInt(); 42 } 43 return flag; 44 } 45 46 static boolean inValues(ArrayList<Card> cardList,ArrayList<Integer> list) { 47 // Scanner in = new Scanner(System.in); 48 int i; 49 boolean flag = true; 50 int errors = 0; 51 52 for(i = 0;i<cardList.size();i++) { 53 54 switch(list.get(i)) { 55 case 1: 56 double radius = Main.inp.nextDouble(); 57 ((Circle) cardList.get(i).getShape()).setRadius(radius); 58 flag = ((Circle) cardList.get(i).getShape()).validata(); 59 if(!flag) 60 errors++; 61 break; 62 case 2: 63 double width = Main.inp.nextDouble(); 64 double length = Main.inp.nextDouble(); 65 ((Rectangle) cardList.get(i).getShape()).setWidth(width); 66 ((Rectangle) cardList.get(i).getShape()).setLength(length); 67 flag = ((Rectangle) cardList.get(i).getShape()).validata(); 68 if(!flag) 69 errors++; 70 break; 71 case 3: 72 double side1 = Main.inp.nextDouble(); 73 double side2 = Main.inp.nextDouble(); 74 double side3 = Main.inp.nextDouble(); 75 ((Triangle) cardList.get(i).getShape()).setSide1(side1); 76 ((Triangle) cardList.get(i).getShape()).setSide2(side2); 77 ((Triangle) cardList.get(i).getShape()).setSide3(side3); 78 flag = ((Triangle) cardList.get(i).getShape()).validata(); 79 if(!flag) 80 errors++; 81 break; 82 case 4: 83 double topSide = Main.inp.nextDouble(); 84 double bottomSide = Main.inp.nextDouble(); 85 double height = Main.inp.nextDouble(); 86 ((Trapezoid) cardList.get(i).getShape()).setTopSide(topSide); 87 ((Trapezoid) cardList.get(i).getShape()).setBottomSide(bottomSide); 88 ((Trapezoid) cardList.get(i).getShape()).setHeight(height); 89 flag = ((Trapezoid) cardList.get(i).getShape()).validata(); 90 if(!flag) 91 errors++; 92 break; 93 default: 94 } 95 } 96 97 if(errors!=0) 98 return false; 99 else 100 return true; 101 } 102 103 static boolean type_validata(int i) { 104 if(i>=1&&i<=4) 105 return true; 106 else 107 return false; 108 } 109 } 110 class DealCardList { 111 112 ArrayList<Card> cardList = new ArrayList<Card>(); 113 114 DealCardList(){ 115 116 } 117 DealCardList(ArrayList<Integer> list){ 118 for(int i = 0;i<list.size();i++) { 119 trans(list.get(i)); 120 } 121 } 122 123 /* 124 * boolean validata() { 125 * 126 * } 127 */ 128 static int jugeType(String s) { 129 if(s.equals("Circle")) 130 return 1; 131 if(s.equals("Rectangle")) 132 return 2; 133 if(s.equals("Triangle")) 134 return 3; 135 if(s.equals("Trapezoid")) 136 return 4; 137 138 return 0; 139 } 140 141 void cardSort() { 142 143 Collections.sort(this.cardList); 144 } 145 146 double getAllArea() { 147 double area = 0; 148 int i; 149 for(i = 0;i<cardList.size();i++) 150 { 151 area += Card.result(jugeType(cardList.get(i).getShape().getShapeName()), cardList.get(i).getShape()); 152 } 153 return area; 154 } 155 156 void show() { 157 int i; 158 int type; 159 double area; 160 for(i = 0;i<cardList.size();i++) 161 { 162 System.out.print(cardList.get(i).getShape().getShapeName()+":"); 163 type = jugeType(cardList.get(i).getShape().getShapeName()); 164 switch(type) 165 { 166 case 1: 167 area = ((Circle) cardList.get(i).getShape()).getArea(); 168 System.out.print(String.format("%.2f", area)+" "); 169 break; 170 case 2: 171 area = ((Rectangle) cardList.get(i).getShape()).getArea(); 172 System.out.print(String.format("%.2f", area)+" "); 173 break; 174 case 3: 175 area = ((Triangle) cardList.get(i).getShape()).getArea(); 176 System.out.print(String.format("%.2f", area)+" "); 177 break; 178 case 4: 179 area = ((Trapezoid) cardList.get(i).getShape()).getArea(); 180 System.out.print(String.format("%.2f", area)+" "); 181 break; 182 } 183 } 184 } 185 186 void showResult() { 187 int i; 188 int type; 189 double area; 190 191 System.out.println("The original list:"); 192 show(); 193 System.out.println("\nThe sorted list:"); 194 cardSort(); 195 show(); 196 System.out.print("\nSum of area:"+String.format("%.2f",getAllArea())); 197 198 } 199 200 void trans(int i) { 201 switch(i) 202 { 203 case 1: 204 Card c1 = new Card(new Circle()); 205 this.cardList.add(c1); 206 break; 207 case 2: 208 Card c2 = new Card(new Rectangle()); 209 this.cardList.add(c2); 210 break; 211 case 3: 212 Card c3 = new Card(new Triangle()); 213 this.cardList.add(c3); 214 break; 215 case 4: 216 Card c4 = new Card(new Trapezoid()); 217 this.cardList.add(c4); 218 break; 219 default: 220 221 } 222 } 223 } 224 class Card implements Comparable<Card>{ 225 226 private Shape shape; 227 228 Card(){ 229 230 } 231 Card(Shape shape){ 232 this.shape = shape; 233 } 234 235 public Shape getShape() { 236 return shape; 237 } 238 public void setShape(Shape shape) { 239 this.shape = shape; 240 } 241 242 public int compareTo(Card card) { 243 int t1,t2; 244 double area1,area2; 245 area1 = this.shape.getArea(); 246 area2 = card.getShape().getArea(); 247 if(area1>=area2) 248 return -1; 249 else 250 return 1; 251 } 252 253 static double result(int i,Shape o) { 254 switch(i) { 255 case 1: 256 return ((Circle) o).getArea(); 257 case 2: 258 return ((Rectangle) o).getArea(); 259 case 3: 260 return ((Triangle) o).getArea(); 261 case 4: 262 return ((Trapezoid) o).getArea(); 263 default: 264 return 0; 265 } 266 } 267 268 } 269 class Triangle extends Shape{ 270 271 private double side1; 272 private double side2; 273 private double side3; 274 275 Triangle(){ 276 this.setShapeName("Triangle"); 277 } 278 Triangle(double side1,double side2,double side3){ 279 this.side1 = side1; 280 this.side2 = side2; 281 this.side3 = side3; 282 this.setShapeName("Triangle"); 283 } 284 285 public double getSide1() { 286 return side1; 287 } 288 public void setSide1(double side1) { 289 this.side1 = side1; 290 } 291 public double getSide2() { 292 return side2; 293 } 294 public void setSide2(double side2) { 295 this.side2 = side2; 296 } 297 public double getSide3() { 298 return side3; 299 } 300 public void setSide3(double side3) { 301 this.side3 = side3; 302 } 303 304 //@Override 305 double getArea() { //海伦公式 306 double p; 307 double S; 308 double area; 309 p = (this.side1+this.side2+this.side3)/2.0; //三角形半周长 310 S = p*(p-this.side1)*(p-this.side2)*(p-this.side3); 311 area = Math.sqrt(S); 312 return area; 313 } 314 315 //@Override 316 boolean validata() { 317 if(this.side1+this.side2>this.side3&&this.side1+this.side3>this.side2&&this.side2+this.side3>this.side1) { 318 return true; 319 } 320 else 321 return false; 322 } 323 } 324 class Trapezoid extends Shape{ 325 326 private double topSide; 327 private double bottomSide; 328 private double height; 329 330 Trapezoid(){ 331 this.setShapeName("Trapezoid"); 332 } 333 Trapezoid(double topSide,double bottomSide,double height){ 334 this.topSide = topSide; 335 this.bottomSide = bottomSide; 336 this.height = height; 337 this.setShapeName("Trapezoid"); 338 } 339 340 public double getTopSide() { 341 return topSide; 342 } 343 public void setTopSide(double topSide) { 344 345 this.topSide = topSide; 346 } 347 public double getBottomSide() { 348 return bottomSide; 349 } 350 public void setBottomSide(double bottomSide) { 351 352 this.bottomSide = bottomSide; 353 } 354 public double getHeight() { 355 return height; 356 } 357 public void setHeight(double height) { 358 this.height = height; 359 } 360 361 //@Override 362 double getArea() { 363 double area; 364 area = (this.topSide+this.bottomSide)*this.height/2.0; 365 return area; 366 } 367 368 //@Override 369 boolean validata() { 370 if(this.topSide<0||this.bottomSide<0||this.height<0) 371 return false; 372 else 373 return true; 374 } 375 } 376 class Rectangle extends Shape{ 377 378 private double width; 379 private double length; 380 381 Rectangle(){ 382 this.setShapeName("Rectangle"); 383 } 384 Rectangle(double width,double length){ 385 this.width = width; 386 this.length = length; 387 this.setShapeName("Rectangle"); 388 } 389 390 public double getWidth() { 391 return width; 392 } 393 public void setWidth(double width) { 394 395 this.width = width; 396 } 397 public double getLength() { 398 return length; 399 } 400 public void setLength(double length) { 401 402 this.length = length; 403 } 404 405 //@Override 406 double getArea() { 407 double area; 408 area = this.width*this.length; 409 return area; 410 } 411 412 //@Override 413 boolean validata() { 414 if(this.width<0||this.length<0) 415 return false; 416 else 417 return true; 418 } 419 } 420 class Circle extends Shape{ 421 422 private double radius; 423 424 Circle(){ 425 this.setShapeName("Circle"); 426 } 427 Circle(double radius){ 428 this.radius = radius; 429 this.setShapeName("Circle"); 430 } 431 432 public double getRadius() { 433 return radius; 434 } 435 public void setRadius(double radius) { 436 437 this.radius = radius; 438 } 439 440 //@Override 441 double getArea() { 442 double area; 443 area = Math.PI*this.radius*this.radius; 444 return area; 445 } 446 447 //@Override 448 boolean validata() { 449 if(this.radius<0) { 450 return false; 451 } 452 return true; 453 } 454 } 455 abstract class Shape { 456 457 private String shapeName; 458 459 Shape(){ 460 461 } 462 Shape(String shapeName){ 463 this.shapeName = shapeName; 464 } 465 466 public String getShapeName() { 467 return shapeName; 468 } 469 public void setShapeName(String shapeName) { 470 this.shapeName = shapeName; 471 } 472 473 double getArea() { 474 double s = 0.0; 475 return s; 476 } 477 478 boolean validata() { 479 return true; 480 } 481 482 public String toString() { 483 String a = "example"; 484 return a; 485 } 486 }
基于给出的类设计分析,思路主要是使用到继承和多态。对于题目卡片形状,抽象出一个Shape的抽象父类,里头定义了形状名称、形状的面积求法等(抽象定义,不实现)。又题目给出了明确的“卡片”定义,所以有一个卡片的类,当然卡片最主要的属性就是它的形状了,这都没什么好纠结的。题目给出了具体类设计,我们只需要按照类图去写代码就可以了,基本不会遇到什么问题。这里唯一需要注意的点就是在计算面积的时候,题目给出了“保留两位小数”的要求。当遇到这样的要求的时候一定要警惕,比如后面让你求和的时候,必须要注意到底是先保留两位再求和还是先求和再保留两位小数。
踩坑心得
这里唯一需要注意的点,就是 求和 、 保留两位小数 这两个点。一定要注意自己测试一下测试点到底对这个点是什么要求,有时候可能错的是保留两位小数和求和之间的矛盾点,但给出的测试点错误提示却让你没办法把这两点联系到一起,所以最好的做法就是自己先对疑惑点进行测试。
改进建议
先给出类图。

可以看出来这里面还使用了比较器。关于比较器,这个可以是默认的,也可以自己去修改。在这里根据题目的要求我是做了修改的。
因为具体比较的还是卡片,所以我的compare to 函数是在Card类里重写的。
1 public int compareTo(Card card) { 2 int t1,t2; 3 double area1,area2; 4 area1 = this.shape.getArea(); 5 area2 = card.getShape().getArea(); 6 if(area1>=area2) 7 return 1; 8 else 9 return -1; 10 }
这个Compare的接口是所有类默认会继承的。
下面给出我的SourceMonitor图分析。


从圈复杂度可以看出来平均来说每个类的圈复杂度都蛮低的,也就是说从设计方面来说是可以的(老师给的类图)。这个Input类是我自己添进去的,为了保持Main类的单一职责。现在可以看到最大圈复杂度出现在了Input类中。截出具体Input类的情况如下。

inValues是函数的功能是分类型得到输入的数据,这是基于inType函数的基础来的。所以在inValues函数中的判断会多一些,是为了更好的对输入的数据(半径之类的)进行分类存储,为之后的计算铺垫。
从上面的分析中可以看出来,Input的圈复杂度有点过高了,很容易出现bug,所以inValues可以再把职责划分的细一点,把职责分出去,进一步降低圈复杂度。
题目7-2.
题目7-2的要求基本和7-1重合一大部分,没有什么添加的点,主要就是给卡片按图形分个类输出,然后输出的格式做点改变。
先给出我的源码。
1 import java.util.*; 2 3 public class Main { 4 public static Scanner inp = new Scanner(System.in); 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 boolean flag = true; 10 11 flag = Input.inType(list); 12 if(!flag) { 13 System.out.print("Wrong Format"); 14 System.exit(0); 15 } 16 17 DealCardList deal = new DealCardList(list); 18 flag = Input.inValues(deal.cardList,list); 19 if(!flag) { 20 System.out.print("Wrong Format"); 21 System.exit(0); 22 } 23 24 deal.showResult2(); 25 } 26 27 } 28 class Input { 29 30 static boolean inType(ArrayList<Integer> list) { 31 // Scanner in = new Scanner(System.in); 32 int valu; 33 boolean flag = true; 34 valu = Main.inp.nextInt(); 35 int fflag = 0; 36 while(valu!=0) 37 { 38 fflag = 1; 39 if(type_validata(valu)) 40 list.add(valu); 41 else 42 flag = false; 43 valu = Main.inp.nextInt(); 44 } 45 if(fflag==0) 46 return false; 47 return flag; 48 } 49 static boolean inValues(ArrayList<Card> cardList,ArrayList<Integer> list) { 50 int i; 51 boolean flag = true; 52 int errors = 0; 53 54 for(i = 0;i<cardList.size();i++) { 55 56 switch(list.get(i)) { 57 case 1: 58 double radius = Main.inp.nextDouble(); 59 ((Circle) cardList.get(i).getShape()).setRadius(radius); 60 flag = ((Circle) cardList.get(i).getShape()).validata(); 61 if(!flag) 62 errors++; 63 break; 64 case 2: 65 double width = Main.inp.nextDouble(); 66 double length = Main.inp.nextDouble(); 67 ((Rectangle) cardList.get(i).getShape()).setWidth(width); 68 ((Rectangle) cardList.get(i).getShape()).setLength(length); 69 flag = ((Rectangle) cardList.get(i).getShape()).validata(); 70 if(!flag) 71 errors++; 72 break; 73 case 3: 74 double side1 = Main.inp.nextDouble(); 75 double side2 = Main.inp.nextDouble(); 76 double side3 = Main.inp.nextDouble(); 77 ((Triangle) cardList.get(i).getShape()).setSide1(side1); 78 ((Triangle) cardList.get(i).getShape()).setSide2(side2); 79 ((Triangle) cardList.get(i).getShape()).setSide3(side3); 80 flag = ((Triangle) cardList.get(i).getShape()).validata(); 81 if(!flag) 82 errors++; 83 break; 84 case 4: 85 double topSide = Main.inp.nextDouble(); 86 double bottomSide = Main.inp.nextDouble(); 87 double height = Main.inp.nextDouble(); 88 ((Trapezoid) cardList.get(i).getShape()).setTopSide(topSide); 89 ((Trapezoid) cardList.get(i).getShape()).setBottomSide(bottomSide); 90 ((Trapezoid) cardList.get(i).getShape()).setHeight(height); 91 flag = ((Trapezoid) cardList.get(i).getShape()).validata(); 92 if(!flag) 93 errors++; 94 break; 95 default: 96 } 97 } 98 99 if(errors!=0) 100 return false; 101 else 102 return true; 103 } 104 105 static boolean type_validata(int i) { 106 if(i>=1&&i<=4) 107 return true; 108 else 109 return false; 110 } 111 } 112 class DealCardList { 113 114 ArrayList<Card> cardList = new ArrayList<Card>(); 115 116 DealCardList(){ 117 118 } 119 DealCardList(ArrayList<Integer> list){ 120 for(int i = 0;i<list.size();i++) { 121 trans(list.get(i)); 122 } 123 } 124 static int jugeType(String s) { 125 if(s.equals("Circle")) 126 return 1; 127 if(s.equals("Rectangle")) 128 return 2; 129 if(s.equals("Triangle")) 130 return 3; 131 if(s.equals("Trapezoid")) 132 return 4; 133 134 return 0; 135 } 136 void cardSort() { 137 Collections.sort(this.cardList); 138 } 139 double getAllArea() { 140 double area = 0; 141 int i; 142 for(i = 0;i<cardList.size();i++) 143 { 144 area += Card.result(jugeType(cardList.get(i).getShape().getShapeName()), cardList.get(i).getShape()); 145 } 146 return area; 147 } 148 149 void show() { 150 int i; 151 int type; 152 double area; 153 for(i = 0;i<cardList.size();i++) 154 { 155 System.out.print(cardList.get(i).getShape().getShapeName()+":"); 156 type = jugeType(cardList.get(i).getShape().getShapeName()); 157 switch(type) 158 { 159 case 1: 160 area = ((Circle) cardList.get(i).getShape()).getArea(); 161 System.out.print(String.format("%.2f", area)+" "); 162 break; 163 case 2: 164 area = ((Rectangle) cardList.get(i).getShape()).getArea(); 165 System.out.print(String.format("%.2f", area)+" "); 166 break; 167 case 3: 168 area = ((Triangle) cardList.get(i).getShape()).getArea(); 169 System.out.print(String.format("%.2f", area)+" "); 170 break; 171 case 4: 172 area = ((Trapezoid) cardList.get(i).getShape()).getArea(); 173 System.out.print(String.format("%.2f", area)+" "); 174 break; 175 } 176 } 177 } 178 void show2() { 179 System.out.print("["); 180 show(); 181 System.out.print("]"); 182 } 183 double[] show3() { 184 int i; 185 double[] max = new double[4]; 186 for(i = 0;i<4;i++) 187 { 188 System.out.print("["); 189 max[i] = 0; 190 for(int j = 0;j<cardList.size();j++) 191 { 192 if(jugeType(cardList.get(j).getShape().getShapeName())==i+1) 193 { 194 //if(cardList.get(j).getShape().getArea()>max[i]) 195 max[i] += cardList.get(j).getShape().getArea(); 196 System.out.print(cardList.get(j).getShape().getShapeName()+":"+String.format("%.2f", Card.result(i+1, cardList.get(j).getShape()))+" "); 197 } 198 } 199 System.out.print("]"); 200 } 201 return max; 202 } 203 void showResult2() { 204 double[] max = new double[4]; 205 System.out.println("The original list:"); 206 show2(); 207 System.out.println("\nThe Separated List:"); 208 // cardSort(); 209 show3(); 210 System.out.println("\nThe Separated sorted List:"); 211 cardSort(); 212 max = show3(); 213 double maxnum = max[0]; 214 int i = 1; 215 while(i<4) { 216 if(maxnum<max[i]) 217 maxnum = max[i]; 218 i++; 219 } 220 System.out.print("\nThe max area:"+String.format("%.2f", maxnum)); 221 } 222 void trans(int i) { 223 switch(i) 224 { 225 case 1: 226 Card c1 = new Card(new Circle()); 227 this.cardList.add(c1); 228 break; 229 case 2: 230 Card c2 = new Card(new Rectangle()); 231 this.cardList.add(c2); 232 break; 233 case 3: 234 Card c3 = new Card(new Triangle()); 235 this.cardList.add(c3); 236 break; 237 case 4: 238 Card c4 = new Card(new Trapezoid()); 239 this.cardList.add(c4); 240 break; 241 default: 242 243 } 244 } 245 } 246 class Card implements Comparable<Card>{ 247 248 private Shape shape; 249 250 Card(){ 251 252 } 253 Card(Shape shape){ 254 this.shape = shape; 255 } 256 257 public Shape getShape() { 258 return shape; 259 } 260 public void setShape(Shape shape) { 261 this.shape = shape; 262 } 263 264 public int compareTo(Card card) { 265 int t1,t2; 266 double area1,area2; 267 area1 = this.shape.getArea(); 268 area2 = card.getShape().getArea(); 269 if(area1>=area2) 270 return -1; 271 else 272 return 1; 273 } 274 275 static double result(int i,Shape o) { 276 switch(i) { 277 case 1: 278 return ((Circle) o).getArea(); 279 case 2: 280 return ((Rectangle) o).getArea(); 281 case 3: 282 return ((Triangle) o).getArea(); 283 case 4: 284 return ((Trapezoid) o).getArea(); 285 default: 286 return 0; 287 } 288 } 289 290 } 291 class Triangle extends Shape{ 292 293 private double side1; 294 private double side2; 295 private double side3; 296 297 Triangle(){ 298 this.setShapeName("Triangle"); 299 } 300 Triangle(double side1,double side2,double side3){ 301 this.side1 = side1; 302 this.side2 = side2; 303 this.side3 = side3; 304 this.setShapeName("Triangle"); 305 } 306 307 public double getSide1() { 308 return side1; 309 } 310 public void setSide1(double side1) { 311 this.side1 = side1; 312 } 313 public double getSide2() { 314 return side2; 315 } 316 public void setSide2(double side2) { 317 this.side2 = side2; 318 } 319 public double getSide3() { 320 return side3; 321 } 322 public void setSide3(double side3) { 323 this.side3 = side3; 324 } 325 326 //@Override 327 double getArea() { //海伦公式 328 double p; 329 double S; 330 double area; 331 p = (this.side1+this.side2+this.side3)/2.0; //三角形半周长 332 S = p*(p-this.side1)*(p-this.side2)*(p-this.side3); 333 area = Math.sqrt(S); 334 return area; 335 } 336 337 //@Override 338 boolean validata() { 339 if(this.side1+this.side2>this.side3&&this.side1+this.side3>this.side2&&this.side2+this.side3>this.side1) { 340 return true; 341 } 342 else 343 return false; 344 } 345 } 346 class Trapezoid extends Shape{ 347 348 private double topSide; 349 private double bottomSide; 350 private double height; 351 352 Trapezoid(){ 353 this.setShapeName("Trapezoid"); 354 } 355 Trapezoid(double topSide,double bottomSide,double height){ 356 this.topSide = topSide; 357 this.bottomSide = bottomSide; 358 this.height = height; 359 this.setShapeName("Trapezoid"); 360 } 361 362 public double getTopSide() { 363 return topSide; 364 } 365 public void setTopSide(double topSide) { 366 367 this.topSide = topSide; 368 } 369 public double getBottomSide() { 370 return bottomSide; 371 } 372 public void setBottomSide(double bottomSide) { 373 374 this.bottomSide = bottomSide; 375 } 376 public double getHeight() { 377 return height; 378 } 379 public void setHeight(double height) { 380 this.height = height; 381 } 382 383 //@Override 384 double getArea() { 385 double area; 386 area = (this.topSide+this.bottomSide)*this.height/2.0; 387 return area; 388 } 389 390 //@Override 391 boolean validata() { 392 if(this.topSide<0||this.bottomSide<0||this.height<0) 393 return false; 394 else 395 return true; 396 } 397 } 398 class Rectangle extends Shape{ 399 400 private double width; 401 private double length; 402 403 Rectangle(){ 404 this.setShapeName("Rectangle"); 405 } 406 Rectangle(double width,double length){ 407 this.width = width; 408 this.length = length; 409 this.setShapeName("Rectangle"); 410 } 411 412 public double getWidth() { 413 return width; 414 } 415 public void setWidth(double width) { 416 417 this.width = width; 418 } 419 public double getLength() { 420 return length; 421 } 422 public void setLength(double length) { 423 424 this.length = length; 425 } 426 427 //@Override 428 double getArea() { 429 double area; 430 area = this.width*this.length; 431 return area; 432 } 433 434 //@Override 435 boolean validata() { 436 if(this.width<0||this.length<0) 437 return false; 438 else 439 return true; 440 } 441 } 442 class Circle extends Shape{ 443 444 private double radius; 445 446 Circle(){ 447 this.setShapeName("Circle"); 448 } 449 Circle(double radius){ 450 this.radius = radius; 451 this.setShapeName("Circle"); 452 } 453 454 public double getRadius() { 455 return radius; 456 } 457 public void setRadius(double radius) { 458 459 this.radius = radius; 460 } 461 462 //@Override 463 double getArea() { 464 double area; 465 area = Math.PI*this.radius*this.radius; 466 return area; 467 } 468 469 //@Override 470 boolean validata() { 471 if(this.radius<0) { 472 return false; 473 } 474 return true; 475 } 476 } 477 abstract class Shape { 478 479 private String shapeName; 480 481 Shape(){ 482 483 } 484 Shape(String shapeName){ 485 this.shapeName = shapeName; 486 } 487 488 public String getShapeName() { 489 return shapeName; 490 } 491 public void setShapeName(String shapeName) { 492 this.shapeName = shapeName; 493 } 494 495 double getArea() { 496 double s = 0.0; 497 return s; 498 } 499 500 boolean validata() { 501 return true; 502 } 503 504 public String toString() { 505 String a = "example"; 506 return a; 507 } 508 }
一些需要注意的点和7-1差不多。在7-2中,题目的输入还是一样的,改变的其实主要就是输出形式的问题。所以在7-2中,我重写了输出函数。添上了要求形式的输出部分,其他的都是没变的。需要明确的是两次代码从类设计到圈复杂度都没什么太大的变化。因为只是改了一个输出的形式罢了。
两次代码的递进设计分析总结
真要说什么“递进设计”其实我觉得还是说不太上的,因为也没添加什么别的功能之类的,改的地方还只是输出形式而已,所以我做出修改的部分挺普通的,就感觉不是什么很值得讲的东西。总的来说我重写了showResult函数,也就是说Main中输出结果的函数我直接写了另一个(这里还是值得提一提的,我把输入部分和输出部分都专门写了函数或者类,把职责分出去了,所以这里我才改的比较轻松,因为很明确自己要动的是哪个部分,然后直接按7-2的新思路写就行了。相当于我又写了个输出模块然后把所有的拼起来的感觉...)。这样讲来,其实最重要的还是“单一职责”。如果不是把输出的函数单独写了出去,分了另一个模块的话,我写起来就不是那么简单的事情了。因为一旦一个职责没分好牵扯的还是比较多的,首先“调用”就是一个问题。所以还是要做好“单一职责”,按这样写,如果改的地方是输入方式,那我也能消化的比较好,原因就在于我是分了输入模块的。这样我就只需要考虑输入模块内部就可以了,而不是在整个全部的代码里去考虑修改。
②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
题目集8
先贴上我自己的源码。
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 public class Main { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 BankFederation BF = Initiate.Init_bank(); 7 Deal.dealData(BF); 8 } 9 } 10 class BankFederation { 11 private ArrayList<Bank> bankList = new ArrayList<Bank>(); 12 public ArrayList<Bank> getBankList() { 13 return bankList; 14 } 15 public void addBankList(Bank bankListmen) { 16 this.bankList.add(bankListmen); 17 } 18 } 19 class Bank { 20 private String bankName; 21 private ArrayList<Account> accountList = new ArrayList<Account>(); 22 private ArrayList<ATM> atmList = new ArrayList<ATM>(); 23 Bank(String bankName){ 24 this.bankName = bankName; 25 } 26 public String getBankName() { 27 return bankName; 28 } 29 public void setBankName(String bankName) { 30 this.bankName = bankName; 31 } 32 public ArrayList<Account> getAccountList() { 33 return accountList; 34 } 35 public void addAccountList(Account accountListmen) { 36 this.accountList.add(accountListmen); 37 } 38 public ArrayList<ATM> getAtmList() { 39 return atmList; 40 } 41 public void addAtmList(ATM atmListmen) { 42 this.atmList.add(atmListmen); 43 } 44 } 45 class Account { 46 47 private String bankName; 48 private String accountNum; 49 private double accountBalance; 50 private ArrayList<BankCard> bankCardList = new ArrayList<BankCard>(); 51 private User user; 52 53 Account(String accountNum,double accountBalance,User user,String bank){ 54 this.accountBalance = accountBalance; 55 this.accountNum = accountNum; 56 this.user = user; 57 this.bankName = bank; 58 } 59 60 public User getUser() { 61 return user; 62 } 63 64 public void setUser(User user) { 65 this.user = user; 66 } 67 68 public String getAccountNum() { 69 return accountNum; 70 } 71 public void setAccountNum(String accountNum) { 72 this.accountNum = accountNum; 73 } 74 public double getAccountBalance() { 75 return accountBalance; 76 } 77 public void setAccountBalance(double accountBalance) { 78 this.accountBalance = accountBalance; 79 } 80 public ArrayList<BankCard> getBankCardList() { 81 return bankCardList; 82 } 83 public void addBankCardList(BankCard bankCardmen) { 84 this.bankCardList.add(bankCardmen); 85 } 86 87 public String getBankName() { 88 return bankName; 89 } 90 91 public void setBankName(String bankName) { 92 this.bankName = bankName; 93 } 94 } 95 class BankCard { 96 97 private String cardNum; 98 private String password; 99 100 BankCard(String cardNum,String password){ 101 this.cardNum = cardNum; 102 this.password = password; 103 } 104 105 public String getCardNum() { 106 return cardNum; 107 } 108 public void setCardNum(String cardNum) { 109 this.cardNum = cardNum; 110 } 111 public String getPassword() { 112 return password; 113 } 114 public void setPassword(String password) { 115 this.password = password; 116 } 117 118 119 } 120 class Initiate { 121 122 static BankFederation Init_bank() { 123 BankFederation BF = new BankFederation(); 124 Bank bank_1 = new Bank("中国建设银行"); 125 Bank bank_2 = new Bank("中国工商银行"); 126 BF.addBankList(bank_1); 127 BF.addBankList(bank_2); 128 129 //bank_1 130 User user_1_1 = new User("杨过"); 131 Account account_1_1 = new Account("3217000010041315709",10000,user_1_1,"中国建设银行"); 132 BankCard bankcard_1_1_1 = new BankCard("6217000010041315709","88888888"); 133 BankCard bankcard_1_1_2 = new BankCard("6217000010041315715","88888888"); 134 account_1_1.addBankCardList(bankcard_1_1_1); 135 account_1_1.addBankCardList(bankcard_1_1_2); 136 Account account_1_2 = new Account("3217000010041315715",10000,user_1_1,"中国建设银行"); 137 BankCard bankcard_1_2_1 = new BankCard("6217000010041315718","88888888"); 138 account_1_2.addBankCardList(bankcard_1_2_1); 139 User user_1_2 = new User("郭靖"); 140 Account account_1_3 = new Account("3217000010051320007",10000,user_1_2,"中国建设银行"); 141 BankCard bankcard_1_3_1 = new BankCard("6217000010051320007","88888888"); 142 account_1_3.addBankCardList(bankcard_1_3_1); 143 bank_1.addAccountList(account_1_1); 144 bank_1.addAccountList(account_1_2); 145 bank_1.addAccountList(account_1_3); 146 147 ATM atm_1_1 = new ATM(bank_1,"01"); 148 ATM atm_1_2 = new ATM(bank_1,"02"); 149 ATM atm_1_3 = new ATM(bank_1,"03"); 150 ATM atm_1_4 = new ATM(bank_1,"04"); 151 bank_1.addAtmList(atm_1_1); 152 bank_1.addAtmList(atm_1_2); 153 bank_1.addAtmList(atm_1_3); 154 bank_1.addAtmList(atm_1_4); 155 156 157 //bank_2 158 User user_2_1 = new User("张无忌"); 159 Account account_2_1 = new Account("3222081502001312389",10000,user_2_1,"中国工商银行"); 160 BankCard bankcard_2_1_1 = new BankCard("6222081502001312389","88888888"); 161 account_2_1.addBankCardList(bankcard_2_1_1); 162 Account account_2_2 = new Account("3222081502001312390",10000,user_2_1,"中国工商银行"); 163 BankCard bankcard_2_2_1 = new BankCard("6222081502001312390","88888888"); 164 account_2_2.addBankCardList(bankcard_2_2_1); 165 Account account_2_3 = new Account("3222081502001312399",10000,user_2_1,"中国工商银行"); 166 BankCard bankcard_2_3_1 = new BankCard("6222081502001312399","88888888"); 167 BankCard bankcard_2_3_2 = new BankCard("6222081502001312400","88888888"); 168 account_2_3.addBankCardList(bankcard_2_3_1); 169 account_2_3.addBankCardList(bankcard_2_3_2); 170 User user_2_2 = new User("韦小宝"); 171 Account account_2_4 = new Account("3222081502051320785",10000,user_2_2,"中国工商银行"); 172 BankCard bankcard_2_4_1 = new BankCard("6222081502051320785","88888888"); 173 account_2_4.addBankCardList(bankcard_2_4_1); 174 Account account_2_5 = new Account("3222081502051320786",10000,user_2_2,"中国工商银行"); 175 BankCard bankcard_2_5_1 = new BankCard("6222081502051320786","88888888"); 176 account_2_5.addBankCardList(bankcard_2_5_1); 177 bank_2.addAccountList(account_2_1); 178 bank_2.addAccountList(account_2_2); 179 bank_2.addAccountList(account_2_3); 180 bank_2.addAccountList(account_2_4); 181 bank_2.addAccountList(account_2_5); 182 183 ATM atm_2_1 = new ATM(bank_2,"05"); 184 ATM atm_2_2 = new ATM(bank_2,"06"); 185 bank_2.addAtmList(atm_2_1); 186 bank_2.addAtmList(atm_2_2); 187 188 return BF; 189 } 190 } 191 class ATM { 192 193 private Bank bank; 194 private String num; 195 196 ATM(Bank bank,String num){ 197 this.bank = bank; 198 this.num = num; 199 } 200 201 public Bank getBank() { 202 return bank; 203 } 204 public void setBank(Bank bank) { 205 this.bank = bank; 206 } 207 public String getNum() { 208 return num; 209 } 210 public void setNum(String num) { 211 this.num = num; 212 } 213 214 public boolean checkBalance(String cardNum) { 215 216 Account nowUse = findBankCard(cardNum,0); 217 if(nowUse==null) 218 {System.out.println("Sorry,this card does not exist.");return false;} 219 else 220 System.out.print("¥"+String.format("%.2f",nowUse.getAccountBalance())); 221 return true; 222 223 } 224 225 public boolean withdrawal(String cardNum,String password,double quantity) { 226 Account nowUse = findBankCard(cardNum,password); 227 double renew; 228 int flag = 0; 229 if(nowUse==null) 230 return false; 231 else 232 { 233 if(nowUse.getAccountBalance()>=quantity) 234 { 235 flag = 1; 236 renew = nowUse.getAccountBalance()-quantity; 237 nowUse.setAccountBalance(renew); 238 System.out.println(nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 239 System.out.print("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 240 } 241 else 242 System.out.println("Sorry,your account balance is insufficient."); 243 244 } 245 if(flag==0) 246 return false; 247 else 248 return true; 249 } 250 251 public boolean deposit(String cardNum,String password,double quantity) { 252 Account nowUse = findBankCard(cardNum,password); 253 double renew; 254 int flag = 0; 255 if(nowUse==null) 256 return false; 257 else 258 { 259 flag = 1; 260 renew = nowUse.getAccountBalance()+Math.abs(quantity); 261 nowUse.setAccountBalance(renew); 262 System.out.println(nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上存款¥"+String.format("%.2f",Math.abs(quantity))); 263 System.out.print("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 264 } 265 if(flag==0) 266 return false; 267 else 268 return true; 269 } 270 271 public Account findBankCard(String cardNum,int n) { 272 String s1 = cardNum.substring(3, 16); 273 int flag = 0; 274 275 for(Account i:bank.getAccountList()) { 276 String s2 = i.getAccountNum().substring(3, 16); 277 if(s1.equals(s2)) { 278 for(BankCard j:i.getBankCardList()) { 279 if(cardNum.equals(j.getCardNum())) 280 { 281 // System.out.println("¥"+i.getAccountBalance()); 282 return i; 283 284 } 285 } 286 } 287 } 288 289 return null; 290 } 291 292 public Account findBankCard(String cardNum,String pass) { 293 String s1 = cardNum.substring(3, 16); 294 int flag = 0; 295 296 cir1: for(Account i:bank.getAccountList()) { 297 String s2 = i.getAccountNum().substring(3, 16); 298 if(s1.equals(s2)) { 299 for(BankCard j:i.getBankCardList()) { 300 if(cardNum.equals(j.getCardNum())) 301 { 302 // System.out.println("¥"+i.getAccountBalance()); 303 if(pass.equals(j.getPassword())) 304 return i; 305 else 306 { 307 flag = 1; 308 System.out.println("Sorry,your password is wrong."); 309 break cir1; 310 } 311 312 } 313 } 314 } 315 } 316 if(flag == 0) 317 System.out.println("Sorry,this card does not exist."); 318 return null; 319 } 320 } 321 class User { 322 323 private String userName; 324 325 User(String userName){ 326 this.userName = userName; 327 } 328 329 public String getUserName() { 330 return userName; 331 } 332 333 public void setUserName(String userName) { 334 this.userName = userName; 335 } 336 337 } 338 class Deal { 339 340 static void dealData(BankFederation BF) { 341 ArrayList<String> cmd; 342 cmd = Input.in_cmd(); 343 boolean flag = true; 344 int ss = 0; 345 String[] temp; 346 347 for(String i:cmd) { 348 temp = i.split("\\s+"); 349 if(ss==1) 350 System.out.print("\n"); 351 ss = 1; 352 if(temp.length==1) 353 flag = deal_cmd_2(i,BF); 354 else if(temp.length==4) 355 flag = deal_cmd_1(i,BF); 356 if(flag == false) 357 return ; 358 } 359 360 361 } 362 363 protected static boolean deal_cmd_1(String temp,BankFederation BF) { 364 //卡号 密码 ATM机编号 金额 365 //当金额大于0时,代表取款,否则代表存款。 366 String[] cmd = temp.split("\\s+"); 367 String s1 = cmd[0].substring(0, 3); 368 int i = findATM(temp,BF); 369 boolean flag = false; 370 if(i>=1&&i<=4&&s1.equals("621")) 371 { 372 if(Double.valueOf(cmd[3])>0) 373 flag = BF.getBankList().get(0).getAtmList().get(i-1).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3])); 374 else 375 flag = BF.getBankList().get(0).getAtmList().get(i-1).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3])); 376 } 377 else if(i>=5&&s1.equals("622")) 378 { 379 if(Double.valueOf(cmd[3])>0) 380 flag = BF.getBankList().get(1).getAtmList().get(i-5).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3])); 381 else 382 flag = BF.getBankList().get(1).getAtmList().get(i-5).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3])); 383 } 384 else if(i!=-1) { 385 System.out.println("Sorry,cross-bank withdrawal is not supported."); 386 } 387 return flag; 388 } 389 390 public static int findATM(String temp,BankFederation BF) 391 { 392 String[] cmd = temp.split("\\s+"); 393 String s1 = cmd[0].substring(0, 3); 394 int num = 0; 395 396 for(ATM i:BF.getBankList().get(0).getAtmList()) 397 { 398 num++; 399 if(cmd[2].equals(i.getNum())) 400 { 401 return num; 402 } 403 404 } 405 406 for(ATM i:BF.getBankList().get(1).getAtmList()) 407 { 408 num++; 409 if(cmd[2].equals(i.getNum())) 410 { 411 return num; 412 } 413 414 } 415 416 System.out.println("Sorry,the ATM's id is wrong."); 417 return -1; 418 } 419 420 protected static boolean deal_cmd_2(String temp,BankFederation BF) { 421 422 String cardName = temp.substring(0, 3); 423 boolean flag = false; 424 if(cardName.equals("621")) 425 flag = BF.getBankList().get(0).getAtmList().get(0).checkBalance(temp); 426 else if (cardName.equals("622")) 427 flag = BF.getBankList().get(1).getAtmList().get(0).checkBalance(temp); 428 return flag; 429 } 430 } 431 class Input { 432 433 static ArrayList<String> in_cmd() { 434 Scanner input = new Scanner(System.in); 435 ArrayList<String> cmd = new ArrayList<String>(); 436 String temp; 437 438 temp = input.nextLine(); 439 while(!temp.equals("#")) { 440 cmd.add(temp); 441 temp = input.nextLine(); 442 } 443 444 return cmd; 445 } 446 }
关于我自己的设计思路:
第一步,我首先思考的是题目中给出的几个“实体”之间的关系。
题目明确给出的几个实体:银联、银行、账户、用户、银行卡、ATM机
这几个实体之间的关系,我的结论如下。

第二步,完善实体的逻辑结构。
1、
类:银联
属性:ArrayList<银行>
2、
类:银行
属性:String 名称、ArrayList<账户>、ArrayList<ATM>
3、
类:账户
属性:String 账号、double 账户余额、User 所属用户、ArrayList<银行卡>
4、
类:银行卡
属性:String 卡号、String 密码
5、
类:ATM
属性:String 编号、Bank 所属银行
行为:存、取、查询
第三步,基于实体的类设计,完成整个类图的设计。

踩坑心得
在本次实验中,要求不能跨行存取等等,根据这些要求的描述,有一系列需要判断的条件,需要自己理清一个先后顺序。
判断ATM编号是否正确----------->判断是否跨行--------------->卡号是否存在------------->密码是否错误------------->取款是否大于余额
还有一个就是关于程序数据的存储、初始化,这个我是专门搞了一个Initial来写的。
改进建议
先给出SourceMonitor截图。


可以看出来总体在类设计上圈复杂度还是比较可观的,也可以说明初步我的类设计应该是没有什么问题的。现在就是有一个类Deal的圈复杂度过高了。从名字就可以看出来,这是一个处理数据的类。下面我给出具体的类详情。

这里需要说明一下,我是将命令分为了两类存储,一类就是查询的,一类是存取的。显然搞存取的命令函数会复杂很多,这里还是需要进一步的分割职责才行,不然容易出现bug。
题目集9
先给出我的源码
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 public class Main { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 BankFederation BF = Initiate.Init_bank(); 7 Deal.dealData(BF); 8 } 9 } 10 class Deal { 11 static void dealData(BankFederation BF) { 12 ArrayList<String> cmd; 13 cmd = Input.in_cmd(); 14 boolean flag = true; 15 String[] temp; 16 for(String i:cmd) { 17 temp = i.split("\\s+"); 18 if(temp.length==1) 19 flag = deal_cmd_2(i,BF); 20 else if(temp.length==4) 21 flag = deal_cmd_1(i,BF); 22 if(flag == false) 23 return ; 24 } 25 } 26 27 protected static boolean deal_cmd_1(String temp,BankFederation BF) { 28 //卡号 密码 ATM机编号 金额 29 //当金额大于0时,代表取款,否则代表存款。 30 String[] cmd = temp.split("\\s+"); 31 String s1 = cmd[0].substring(0, 3); 32 int i = findATM(temp,BF); 33 boolean flag = false; 34 /* 35 if(i>=1&&i<=4&&s1.equals("621")) 36 { 37 if(Double.valueOf(cmd[3])>0) 38 flag = BF.getBankList().get(0).getAtmList().get(i-1).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3])); 39 else 40 flag = BF.getBankList().get(0).getAtmList().get(i-1).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3])); 41 } 42 else if(i>=5&&s1.equals("622")) 43 { 44 if(Double.valueOf(cmd[3])>0) 45 flag = BF.getBankList().get(1).getAtmList().get(i-5).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3])); 46 else 47 flag = BF.getBankList().get(1).getAtmList().get(i-5).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3])); 48 } 49 else if(i!=-1) { 50 System.out.println("Sorry,cross-bank withdrawal is not supported."); 51 } */ 52 if(i>=1&&i<=4) { 53 if(Double.valueOf(cmd[3])>0) 54 flag = BF.getBankList().get(0).getAtmList().get(i-1).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF); 55 // else 56 // flag = BF.getBankList().get(0).getAtmList().get(i-1).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF); 57 } 58 else if(i==5||i==6) { 59 if(Double.valueOf(cmd[3])>0) 60 flag = BF.getBankList().get(1).getAtmList().get(i-5).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF); 61 // else 62 // flag = BF.getBankList().get(1).getAtmList().get(i-5).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF); 63 } 64 else if(i>=7&&i<=11) { 65 if(Double.valueOf(cmd[3])>0) 66 flag = BF.getBankList().get(2).getAtmList().get(i-7).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF); 67 // else 68 // flag = BF.getBankList().get(2).getAtmList().get(i-7).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF); 69 } 70 71 return flag; 72 } 73 74 public static int findATM(String temp,BankFederation BF) 75 { 76 String[] cmd = temp.split("\\s+"); 77 String s1 = cmd[0].substring(0, 3); 78 int num = 0; 79 80 for(ATM i:BF.getBankList().get(0).getAtmList()) 81 { 82 num++; 83 if(cmd[2].equals(i.getNum())) 84 { 85 return num; 86 // return 0; 87 } 88 89 } 90 91 for(ATM i:BF.getBankList().get(1).getAtmList()) 92 { 93 num++; 94 if(cmd[2].equals(i.getNum())) 95 { 96 return num; 97 //return 1; 98 } 99 100 } 101 102 for(ATM i:BF.getBankList().get(2).getAtmList()) 103 { 104 num++; 105 if(cmd[2].equals(i.getNum())) 106 { 107 return num; 108 //return 2; 109 } 110 111 } 112 113 System.out.println("Sorry,the ATM's id is wrong."); 114 return -1; 115 } 116 117 protected static boolean deal_cmd_2(String temp,BankFederation BF) { 118 119 String cardName = temp.substring(0, 3); 120 boolean flag = false; 121 122 if(cardName.equals("621")) 123 flag = BF.getBankList().get(0).getAtmList().get(0).checkBalance(temp); 124 else if (cardName.equals("622")) 125 flag = BF.getBankList().get(1).getAtmList().get(0).checkBalance(temp); 126 else 127 flag = BF.getBankList().get(2).getAtmList().get(0).checkBalance(temp,BF); 128 129 return flag; 130 } 131 } 132 class Bank { 133 134 private String bankName; 135 private double rate; 136 private ArrayList<Account> accountList = new ArrayList<Account>(); 137 // private ArrayList<Account> accountDList = new ArrayList<Account>(); //贷记账户 138 private ArrayList<ATM> atmList = new ArrayList<ATM>(); 139 140 Bank(String bankName,double rate){ 141 this.bankName = bankName; 142 this.rate = rate; 143 } 144 145 public String getBankName() { 146 return bankName; 147 } 148 public void setBankName(String bankName) { 149 this.bankName = bankName; 150 } 151 public ArrayList<Account> getAccountList() { 152 return accountList; 153 } 154 public void addAccountList(Account accountListmen) { 155 this.accountList.add(accountListmen); 156 157 } 158 159 public ArrayList<ATM> getAtmList() { 160 return atmList; 161 } 162 163 public void addAtmList(ATM atmListmen) { 164 this.atmList.add(atmListmen); 165 } 166 167 public double getRate() { 168 return rate; 169 } 170 171 public void setRate(double rate) { 172 this.rate = rate; 173 } 174 175 /* 176 * public ArrayList<Account> getAccountDList() { return accountDList; } 177 * 178 * public void addAccountDList(Account accountDList) { 179 * this.accountDList.add(accountDList); } 180 */ 181 182 } 183 class ATM { 184 185 private Bank bank; 186 private String num; 187 188 ATM(Bank bank,String num){ 189 this.bank = bank; 190 this.num = num; 191 } 192 193 public Bank getBank() { 194 return bank; 195 } 196 public void setBank(Bank bank) { 197 this.bank = bank; 198 } 199 public String getNum() { 200 return num; 201 } 202 public void setNum(String num) { 203 this.num = num; 204 } 205 206 public boolean checkBalance(String cardNum,BankFederation BF) { 207 Bank k; 208 Account nowUse = findBankCard(cardNum,0); 209 boolean flag = false; 210 if(nowUse==null) 211 { 212 cir4: for(Bank i:BF.getBankList()) { 213 flag = i.getAtmList().get(0).checkBalance(cardNum); 214 if(flag==true) 215 break cir4; 216 } 217 if(flag==false) 218 System.out.println("Sorry,this card does not exist."); 219 return flag; 220 } 221 else 222 System.out.print("业务:查询余额 ¥"+String.format("%.2f",nowUse.getAccountBalance())); 223 return true; 224 225 } 226 227 public boolean checkBalance(String cardNum) { 228 // Bank k; 229 Account nowUse = findBankCard(cardNum,0); 230 if(nowUse==null) 231 return false; 232 else 233 System.out.print("业务:查询余额 ¥"+String.format("%.2f",nowUse.getAccountBalance())); 234 return true; 235 236 } 237 238 //外部进入,取钱 239 public boolean withdrawal(String cardNum,String password,double quantity,Bank bank,String num) { 240 Account nowUse = findBankCard(cardNum,password); 241 double renew; 242 int flag = 0; 243 if(nowUse==null) 244 return false; 245 else 246 { 247 if(nowUse.getAccountBalance()>=(quantity+(quantity*(bank.getRate())))) 248 { 249 flag = 1; 250 renew = nowUse.getAccountBalance()-quantity-(quantity*bank.getRate()); 251 nowUse.setAccountBalance(renew); 252 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+bank.getBankName()+"的"+num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 253 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 254 } 255 else if(nowUse.getType().equals("debit")) 256 System.out.print("Sorry,your account balance is insufficient."); 257 else if(nowUse.getType().equals("credits")) { 258 flag = 1; 259 260 if(nowUse.getAccountBalance()>=0) { 261 renew = nowUse.getAccountBalance()-quantity; 262 renew = renew+renew*0.05; 263 renew = renew-quantity*bank.getRate(); 264 }else { 265 renew = nowUse.getAccountBalance()-quantity; 266 renew = renew-quantity*0.05; 267 renew = renew-quantity*bank.getRate(); 268 } 269 if(renew<-50000) 270 {System.out.print("Sorry,your account balance is insufficient.");return false;} 271 nowUse.setAccountBalance(renew); 272 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+bank.getBankName()+"的"+num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 273 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 274 } 275 276 } 277 if(flag==0) 278 return false; 279 else 280 return true; 281 } 282 283 public boolean withdrawal(String cardNum,String password,double quantity,BankFederation BF) { 284 int ii = 0; 285 Account nowUse = findBankCard(cardNum,password); 286 Account external; 287 Bank k = null; 288 double renew; 289 int flag = 0; 290 if(nowUse==null) 291 { 292 cir2: for(Bank i:BF.getBankList()) { 293 if(!this.bank.getBankName().equals(i.getBankName())) { 294 external = i.getAtmList().get(0).findBankCard(cardNum, password); 295 if(external!=null) 296 { 297 ii = 1; 298 k = i; 299 break cir2; 300 } 301 else 302 { 303 if(i.getAtmList().get(0).findBankCard(cardNum,0)!=null) 304 ii = 2; 305 } 306 } 307 } 308 if(ii==0) { 309 System.out.print("Sorry,this card does not exist."); 310 return false; 311 }else if(ii==2) 312 return false; 313 return k.getAtmList().get(0).withdrawal(cardNum, password, quantity,this.bank,this.num); 314 } 315 else 316 { 317 if(nowUse.getAccountBalance()>=quantity) 318 { 319 flag = 1; 320 renew = nowUse.getAccountBalance()-quantity; 321 nowUse.setAccountBalance(renew); 322 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 323 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 324 } 325 else if(nowUse.getType().equals("debit")) 326 System.out.println("Sorry,your account balance is insufficient."); 327 else if(nowUse.getType().equals("credits")) { 328 flag = 1; 329 330 if(nowUse.getAccountBalance()>=0) { 331 renew = nowUse.getAccountBalance()-quantity; 332 renew = renew+renew*0.05; 333 // renew = renew-quantity*this.bank.getRate(); 334 }else { 335 renew = nowUse.getAccountBalance()-quantity; 336 renew = renew-quantity*0.05; 337 // renew = renew-quantity*this.bank.getRate(); 338 } 339 if(renew<-50000) 340 {System.out.print("Sorry,your account balance is insufficient.");return false;} 341 nowUse.setAccountBalance(renew); 342 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 343 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 344 } 345 } 346 if(flag==0) 347 return false; 348 else 349 return true; 350 } 351 /* 352 public boolean deposit(String cardNum,String password,double quantity,Bank bank,String num) { 353 Account nowUse = findBankCard(cardNum,password); 354 double renew; 355 int flag = 0; 356 if(nowUse==null) 357 return false; 358 else 359 { 360 flag = 1; 361 renew = nowUse.getAccountBalance()+Math.abs(quantity); 362 nowUse.setAccountBalance(renew); 363 System.out.println(nowUse.getUser().getUserName()+"在"+bank.getBankName()+"的"+num+"号ATM机上存款¥"+Math.abs(quantity)); 364 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 365 } 366 if(flag==0) 367 return false; 368 else 369 return true; 370 } 371 372 public boolean deposit(String cardNum,String password,double quantity,BankFederation BF) { 373 Account nowUse = findBankCard(cardNum,password); 374 Account external; 375 Bank k = null; 376 int ii = 0; 377 double renew; 378 int flag = 0; 379 if(nowUse==null) { 380 cir3: for(Bank i:BF.getBankList()) { 381 if(!this.bank.getBankName().equals(i.getBankName())) { 382 external = i.getAtmList().get(0).findBankCard(cardNum, password); 383 if(external!=null) 384 { 385 ii = 1; 386 k = i; 387 break cir3; 388 } 389 } 390 } 391 if(ii==0) { 392 System.out.println("Sorry,this card does not exist."); 393 return false; 394 } 395 k.getAtmList().get(0).deposit(cardNum, password, quantity,this.bank,this.num); 396 } 397 else 398 { 399 flag = 1; 400 renew = nowUse.getAccountBalance()+Math.abs(quantity); 401 nowUse.setAccountBalance(renew); 402 System.out.println(nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上存款¥"+Math.abs(quantity)); 403 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 404 } 405 if(flag==0) 406 return false; 407 else 408 return true; 409 } 410 */ 411 public Account findBankCard(String cardNum,int n) { 412 String s1 = cardNum.substring(3, 16); 413 int flag = 0; 414 415 for(Account i:bank.getAccountList()) { 416 String s2 = i.getAccountNum().substring(3, 16); 417 if(s1.equals(s2)) { 418 for(BankCard j:i.getBankCardList()) { 419 if(cardNum.equals(j.getCardNum())) 420 { 421 // System.out.println("¥"+i.getAccountBalance()); 422 return i; 423 424 } 425 } 426 } 427 } 428 429 return null; 430 } 431 432 public Account findBankCard(String cardNum,String pass) { 433 String s1 = cardNum.substring(3, 16); 434 int flag = 0; 435 436 cir1: for(Account i:bank.getAccountList()) { 437 String s2 = i.getAccountNum().substring(3, 16); 438 if(s1.equals(s2)) { 439 for(BankCard j:i.getBankCardList()) { 440 if(cardNum.equals(j.getCardNum())) 441 { 442 // System.out.println("¥"+i.getAccountBalance()); 443 if(pass.equals(j.getPassword())) 444 return i; 445 else 446 { 447 flag = 1; 448 System.out.println("Sorry,your password is wrong."); 449 break cir1; 450 } 451 452 } 453 } 454 } 455 } 456 /* 457 * if(flag == 0) System.out.println("Sorry,this card does not exist."); 458 */ 459 return null; 460 } 461 } 462 class BankFederation { 463 464 private ArrayList<Bank> bankList = new ArrayList<Bank>(); 465 466 public ArrayList<Bank> getBankList() { 467 return bankList; 468 } 469 470 public void addBankList(Bank bankListmen) { 471 this.bankList.add(bankListmen); 472 } 473 474 } 475 class Initiate { 476 477 static BankFederation Init_bank() { 478 BankFederation BF = new BankFederation(); 479 Bank bank_1 = new Bank("中国建设银行",0.02); 480 Bank bank_2 = new Bank("中国工商银行",0.03); 481 Bank bank_3 = new Bank("中国农业银行",0.04); 482 BF.addBankList(bank_1); 483 BF.addBankList(bank_2); 484 BF.addBankList(bank_3); 485 486 //bank_1 487 User user_1_1 = new User("杨过"); 488 Account account_1_1 = new Account("3217000010041315709",10000,user_1_1,"中国建设银行","debit"); 489 BankCard bankcard_1_1_1 = new BankCard("6217000010041315709","88888888"); 490 BankCard bankcard_1_1_2 = new BankCard("6217000010041315715","88888888"); 491 account_1_1.addBankCardList(bankcard_1_1_1); 492 account_1_1.addBankCardList(bankcard_1_1_2); 493 Account account_1_2 = new Account("3217000010041315715",10000,user_1_1,"中国建设银行","debit"); 494 BankCard bankcard_1_2_1 = new BankCard("6217000010041315718","88888888"); 495 account_1_2.addBankCardList(bankcard_1_2_1); 496 User user_1_2 = new User("郭靖"); 497 Account account_1_3 = new Account("3217000010051320007",10000,user_1_2,"中国建设银行","debit"); 498 BankCard bankcard_1_3_1 = new BankCard("6217000010051320007","88888888"); 499 account_1_3.addBankCardList(bankcard_1_3_1); 500 User user_1_3 = new User("张三丰"); 501 Account account_1_4 = new Account("3640000010045442002",10000,user_1_3,"中国建设银行","credits"); 502 BankCard bankcard_1_4_1 = new BankCard("6640000010045442002","88888888"); 503 BankCard bankcard_1_4_2 = new BankCard("6640000010045442003","88888888"); 504 account_1_4.addBankCardList(bankcard_1_4_1); 505 account_1_4.addBankCardList(bankcard_1_4_2); 506 bank_1.addAccountList(account_1_1); 507 bank_1.addAccountList(account_1_2); 508 bank_1.addAccountList(account_1_3); 509 bank_1.addAccountList(account_1_4); 510 511 ATM atm_1_1 = new ATM(bank_1,"01"); 512 ATM atm_1_2 = new ATM(bank_1,"02"); 513 ATM atm_1_3 = new ATM(bank_1,"03"); 514 ATM atm_1_4 = new ATM(bank_1,"04"); 515 bank_1.addAtmList(atm_1_1); 516 bank_1.addAtmList(atm_1_2); 517 bank_1.addAtmList(atm_1_3); 518 bank_1.addAtmList(atm_1_4); 519 520 521 //bank_2 522 User user_2_1 = new User("张无忌"); 523 Account account_2_1 = new Account("3222081502001312389",10000,user_2_1,"中国工商银行","debit"); 524 BankCard bankcard_2_1_1 = new BankCard("6222081502001312389","88888888"); 525 account_2_1.addBankCardList(bankcard_2_1_1); 526 Account account_2_2 = new Account("3222081502001312390",10000,user_2_1,"中国工商银行","debit"); 527 BankCard bankcard_2_2_1 = new BankCard("6222081502001312390","88888888"); 528 account_2_2.addBankCardList(bankcard_2_2_1); 529 Account account_2_3 = new Account("3222081502001312399",10000,user_2_1,"中国工商银行","debit"); 530 BankCard bankcard_2_3_1 = new BankCard("6222081502001312399","88888888"); 531 BankCard bankcard_2_3_2 = new BankCard("6222081502001312400","88888888"); 532 account_2_3.addBankCardList(bankcard_2_3_1); 533 account_2_3.addBankCardList(bankcard_2_3_2); 534 User user_2_2 = new User("韦小宝"); 535 Account account_2_4 = new Account("3222081502051320785",10000,user_2_2,"中国工商银行","debit"); 536 BankCard bankcard_2_4_1 = new BankCard("6222081502051320785","88888888"); 537 account_2_4.addBankCardList(bankcard_2_4_1); 538 Account account_2_5 = new Account("3222081502051320786",10000,user_2_2,"中国工商银行","debit"); 539 BankCard bankcard_2_5_1 = new BankCard("6222081502051320786","88888888"); 540 account_2_5.addBankCardList(bankcard_2_5_1); 541 User user_2_3 = new User("令狐冲"); 542 Account account_2_6 = new Account("3640000010045441009",10000,user_2_3,"中国工商银行","credits"); 543 BankCard bankcard_2_6_1 = new BankCard("6640000010045441009","88888888"); 544 account_2_6.addBankCardList(bankcard_2_6_1); 545 bank_2.addAccountList(account_2_1); 546 bank_2.addAccountList(account_2_2); 547 bank_2.addAccountList(account_2_3); 548 bank_2.addAccountList(account_2_4); 549 bank_2.addAccountList(account_2_5); 550 bank_2.addAccountList(account_2_6); 551 552 ATM atm_2_1 = new ATM(bank_2,"05"); 553 ATM atm_2_2 = new ATM(bank_2,"06"); 554 bank_2.addAtmList(atm_2_1); 555 bank_2.addAtmList(atm_2_2); 556 557 558 User user_3_1 = new User("乔峰"); 559 Account account_3_1 = new Account("3630000010033431001",10000,user_3_1,"中国农业银行","credits"); 560 BankCard bankcard_3_1_1 = new BankCard("6630000010033431001","88888888"); 561 account_3_1.addBankCardList(bankcard_3_1_1); 562 User user_3_2 = new User("洪七公"); 563 Account account_3_2 = new Account("3630000010033431008",10000,user_3_2,"中国农业银行","credits"); 564 BankCard bankcard_3_2_1 = new BankCard("6630000010033431008","88888888"); 565 account_3_2.addBankCardList(bankcard_3_2_1); 566 bank_3.addAccountList(account_3_1); 567 bank_3.addAccountList(account_3_2); 568 569 ATM atm_3_1 = new ATM(bank_3,"07"); 570 ATM atm_3_2 = new ATM(bank_3,"08"); 571 ATM atm_3_3 = new ATM(bank_3,"09"); 572 ATM atm_3_4 = new ATM(bank_3,"10"); 573 ATM atm_3_5 = new ATM(bank_3,"11"); 574 bank_3.addAtmList(atm_3_1); 575 bank_3.addAtmList(atm_3_2); 576 bank_3.addAtmList(atm_3_3); 577 bank_3.addAtmList(atm_3_4); 578 bank_3.addAtmList(atm_3_5); 579 580 581 return BF; 582 } 583 } 584 class Account { 585 586 private String bankName; 587 private String accountNum; 588 private double accountBalance; 589 private String type; //账户类型 590 private ArrayList<BankCard> bankCardList = new ArrayList<BankCard>(); 591 private User user; 592 593 Account(String accountNum,double accountBalance,User user,String bank,String type){ 594 this.accountBalance = accountBalance; 595 this.accountNum = accountNum; 596 this.user = user; 597 this.bankName = bank; 598 this.type = type; 599 600 } 601 602 public User getUser() { 603 return user; 604 } 605 606 public void setUser(User user) { 607 this.user = user; 608 } 609 610 public String getAccountNum() { 611 return accountNum; 612 } 613 public void setAccountNum(String accountNum) { 614 this.accountNum = accountNum; 615 } 616 public double getAccountBalance() { 617 return accountBalance; 618 } 619 public void setAccountBalance(double accountBalance) { 620 this.accountBalance = accountBalance; 621 } 622 public ArrayList<BankCard> getBankCardList() { 623 return bankCardList; 624 } 625 public void addBankCardList(BankCard bankCardmen) { 626 this.bankCardList.add(bankCardmen); 627 } 628 629 public String getBankName() { 630 return bankName; 631 } 632 633 public void setBankName(String bankName) { 634 this.bankName = bankName; 635 } 636 637 public String getType() { 638 return type; 639 } 640 641 public void setType(String type) { 642 this.type = type; 643 } 644 645 646 647 648 } 649 class User { 650 651 private String userName; 652 653 User(String userName){ 654 this.userName = userName; 655 } 656 657 public String getUserName() { 658 return userName; 659 } 660 661 public void setUserName(String userName) { 662 this.userName = userName; 663 } 664 665 } 666 class Input { 667 668 static ArrayList<String> in_cmd() { 669 Scanner input = new Scanner(System.in); 670 ArrayList<String> cmd = new ArrayList<String>(); 671 String temp; 672 673 temp = input.nextLine(); 674 while(!temp.equals("#")) { 675 cmd.add(temp); 676 temp = input.nextLine(); 677 } 678 679 return cmd; 680 } 681 } 682 class BankCard { 683 684 private String cardNum; 685 private String password; 686 687 BankCard(String cardNum,String password){ 688 this.cardNum = cardNum; 689 this.password = password; 690 } 691 692 public String getCardNum() { 693 return cardNum; 694 } 695 public void setCardNum(String cardNum) { 696 this.cardNum = cardNum; 697 } 698 public String getPassword() { 699 return password; 700 } 701 public void setPassword(String password) { 702 this.password = password; 703 } 704 705 706 }
在做这个题的时候遇到了bug,还搞了挺久的。
踩坑心得
我首先遇到的问题,也是最后才发现解决的问题-------格式问题。也许看起来觉得很不可思议,但确实就是这个。因为这里我是改动了代码的,在改动的地方输出的数据没有保留两位小数输出。在测试样例的很迷的事情就是,它并没有用红色的下划波浪线表明我的答案是错的,只给了一个蓝色的波浪线.....然后我以为它的作用是为了让我更好的对齐,以便于对比我的答案和标准答案....(大汗 = . =||)就是在这样的情况下,我并没有发现自己的格式错误...(最后是让舍友测出来的)
当然在这中途我发现了一个计算错误的问题。其实在题目九中的修改,算是一个比较大的改动了,就是在计算方面。先不说我是怎么改动的代码,这个我留到之后做比较分析的时候专门讲。在计算的时候需要注意到一个不同情况的计算问题。题目中给出的要求“可以跨行存取到需要收取手续费,有的账户可以超支但也要收取手续费”。


根据以上的要求,那么在取款时,就会出现几种不同的情况,必须先搞清楚所有可能发生的情况然后再去针对性的写代码。
1、如果就是普通的信用卡,不支持超支
①不跨行,直接扣钱
②跨行,将所取钱款加上它应该收取的手续费一同,与现有余额做比较。(这里会有一个余额可能不足的情况)
2、贷记
①不跨行
(1)没有超支,普通的扣款
(2)超支,计算超支部分的手续费,计算总金额,看是否超出了超支底线,若未超出可扣,若超出,显示余额不足
②跨行(加上跨行的手续费再看有没有超支)
(1)没有超支,所取钱款加上收取的跨行手续费,若未超支,直接扣款
(2)超支,先将所取钱款的跨行手续费算出来,加在一起计算一个阶段性总金额,然后再在其中超支部分计算超支的手续费加起来。如果超出了余额,则余额不足无法借款,若未 超出则可以借款。
改进建议
两题的类图是没有改动的,只其中的部分计算函数有增加,不影响整体代码。所以这里就不作赘了,一起放在后面做一个分析。
设计思路分析总结
这两题本质上就是在考验我们面向对象的设计原则。面对变化的要求,我们的代码是否可以灵活变化满足需求。在题目集8设计类图的时候,是没有考虑过贷记账户和普通账户的,但我们职责类划分的比较清晰,所以加一个账户的类型并不是很难的事情。在题目集9,最困难的事情其实是计算存取款的函数。因为加了条件之后,这个计算的事情就变得比较多种类了。我的代码的话,我原本考虑的代码里面是完全没有考虑过跨行的事情的,所以这个计算函数肯定是要换掉的。又从我上面写的判断步骤来看,其实有部分代码是可以利用的,比如原来如果我判断到不是同行的ATM机就直接输出了,但比较好的是,我的判断代码是单独拿出来写的。也就是说我在这里具有足够的灵活性改变之后的代码走向。
1 public static int findATM(String temp,BankFederation BF) 2 { 3 String[] cmd = temp.split("\\s+"); 4 String s1 = cmd[0].substring(0, 3); 5 int num = 0; 6 7 for(ATM i:BF.getBankList().get(0).getAtmList()) 8 { 9 num++; 10 if(cmd[2].equals(i.getNum())) 11 { 12 return num; 13 } 14 15 } 16 17 for(ATM i:BF.getBankList().get(1).getAtmList()) 18 { 19 num++; 20 if(cmd[2].equals(i.getNum())) 21 { 22 return num; 23 } 24 25 } 26 27 System.out.println("Sorry,the ATM's id is wrong."); 28 return -1; 29 }
这是我写的判断代码。我把判断的职责分给了其他的函数,意味着在这一点上我就有很大的文章可作了。所以判断是否跨行我的代码基本不用改动(因为我的银行卡也是有专门的判断代码的)。所以在这个代码中我做的就是多加一个计算函数,当发现跨行的时候跳入另一个我新写的代码内进行计算,如果不跨行就直接沿用原本的代码就行。这样就完全将我上次实验的代码和我这次实验要实现的功能隔离开了。我是一个很好的局面,因为只有分开来,我考虑的面才足够小,不至于出现联动反应让其他的代码由于我的改动产生bug。
1 public boolean withdrawal(String cardNum,String password,double quantity,BankFederation BF) { 2 int ii = 0; 3 Account nowUse = findBankCard(cardNum,password); 4 Account external; 5 Bank k = null; 6 double renew; 7 int flag = 0; 8 if(nowUse==null) 9 { 10 cir2: for(Bank i:BF.getBankList()) { 11 if(!this.bank.getBankName().equals(i.getBankName())) { 12 external = i.getAtmList().get(0).findBankCard(cardNum, password); 13 if(external!=null) 14 { 15 ii = 1; 16 k = i; 17 break cir2; 18 } 19 else 20 { 21 if(i.getAtmList().get(0).findBankCard(cardNum,0)!=null) 22 ii = 2; 23 } 24 } 25 } 26 if(ii==0) { 27 System.out.print("Sorry,this card does not exist."); 28 return false; 29 }else if(ii==2) 30 return false; 31 return k.getAtmList().get(0).withdrawal(cardNum, password, quantity,this.bank,this.num); 32 } 33 else 34 { 35 if(nowUse.getAccountBalance()>=quantity) 36 { 37 flag = 1; 38 renew = nowUse.getAccountBalance()-quantity; 39 nowUse.setAccountBalance(renew); 40 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 41 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 42 } 43 else if(nowUse.getType().equals("debit")) 44 System.out.println("Sorry,your account balance is insufficient."); 45 else if(nowUse.getType().equals("credits")) { 46 flag = 1; 47 48 if(nowUse.getAccountBalance()>=0) { 49 renew = nowUse.getAccountBalance()-quantity; 50 renew = renew+renew*0.05; 51 // renew = renew-quantity*this.bank.getRate(); 52 }else { 53 renew = nowUse.getAccountBalance()-quantity; 54 renew = renew-quantity*0.05; 55 // renew = renew-quantity*this.bank.getRate(); 56 } 57 if(renew<-50000) 58 {System.out.print("Sorry,your account balance is insufficient.");return false;} 59 nowUse.setAccountBalance(renew); 60 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+String.format("%.2f",Math.abs(quantity))); 61 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance())); 62 } 63 } 64 if(flag==0) 65 return false; 66 else 67 return true; 68 }
总结
这三次的题目集侧重点很明确,就是类设计,考验我们面向对象设计原则。在题目集7中给出了具体的类设计,像是一个例子吧,让我们自己照类图写出来代码,然后在后面添加功能的题目中自己感受类设计的好处,看代码是否满足灵活变化的要求。之后在题目集8和题目集9中就是自己实操进行类设计。这是第一次自己完全独立的进行类设计,其实看到题目之后开始在纸上自己写画思路,感觉设计起来还是挺快的,并没有想象中的阻塞感,就比较顺溜....可能是这是最基础的题目吧,总体来说设计方面并不是太难。再结合后面我做要求改变的代码的时候,其实可以感受出来,类设计的最终目的就是职责必须划分清楚,不能把职责杂糅在一起,这样不仅容易出bug而且代码的灵活性大大下降,不足以支撑更多的要求。把模块化想的更细一点大概就是“单一职责”需要做到的了。说是类设计,更倾向于搞出一个贴合题目的“框架”,在“框架”的里面有函数,把函数所承担的职责划分的细一点,绝不多做不在自己职责之内的事情。

浙公网安备 33010602011771号