第三次Blog
一、前言
知识点:这三次作业主要考察的是我们对于接口、继承、组合的掌握,同时考察了我们对于题目给出的需求,要如何去面向对象地设计类。这三次作业,可以看成两次作业,日期和存款的第二个题目都只是在第一个题目上增加需求,所以这就考验我们要如何去设计类能够便于我们进行代码的后期维护。
题量:这三次作业的题量算是相当少了,三次作业四个题目,确实不算多。
难度:日期问题的难度一般,因为已经给出了类图,所以第一题就按照类图来就很容易了。第二次也只是在第一题的基础上增加需求,增加几个函数也很容易解决;存款问题的难度较大,首先要自己设计合理便于后期维护的类图,就考验我们面向对象的思想,要自己独立设计解决一个问题,是有一定难度的。
二、设计与分析
题目集七


7-1 图形卡片排序游戏 (40 分) 源码:
1 import java.util.*; 2 3 public class Main { 4 public static Scanner input=new Scanner(System.in); 5 public static void main(String[] args) { 6 7 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 10 int num=input.nextInt(); 11 12 while(num!=0) { 13 if(num<0||num>4) { 14 System.out.println("Wrong Format"); 15 System.exit(0); 16 } 17 list.add(num); 18 num=input.nextInt(); 19 } 20 21 DealCardList dealCardList =new DealCardList(list); 22 23 if(!dealCardList.validate()) { 24 System.out.println("Wrong Format"); 25 System.exit(0); 26 } 27 28 dealCardList.showResult(); 29 30 } 31 } 32 33 34 abstract class Shape{ 35 private String shapeName; 36 37 public String getShapeName() { 38 return this.shapeName; 39 } 40 41 public void setShapeName(String shapeName) { 42 this.shapeName=shapeName; 43 } 44 45 abstract public double getArea(); 46 abstract public boolean validate(); 47 abstract public String toString(); 48 49 } 50 51 52 //圆 53 class Circle extends Shape{ 54 private double radius; 55 56 public Circle(double radius){ 57 this.radius=radius; 58 } 59 60 public double getRadius() { 61 return this.radius; 62 } 63 64 public void setRadius(double radius) { 65 this.radius=radius; 66 } 67 68 @Override 69 public double getArea() { 70 // TODO Auto-generated method stub 71 return Math.PI*this.radius*this.radius; 72 } 73 74 @Override 75 public boolean validate() { 76 // TODO Auto-generated method stub 77 if(this.radius<0) 78 return false; 79 else 80 return true; 81 } 82 83 @Override 84 public String toString() { 85 // TODO Auto-generated method stub 86 return "Circle:"+String.format("%.2f",this.getArea())+" "; 87 } 88 89 } 90 91 //矩形 92 class Rectangle extends Shape{ 93 private double width; 94 private double length; 95 96 public Rectangle(double width2, double length2) { 97 // TODO Auto-generated constructor stub 98 this.length=length2; 99 this.width=width2; 100 } 101 102 public double getWidth() { 103 return this.width; 104 } 105 106 public void setWidth(double width) { 107 this.width=width; 108 } 109 110 public double getlength() { 111 return this.length; 112 } 113 114 public void setLength(double length) { 115 this.length=length; 116 } 117 118 @Override 119 public double getArea() { 120 // TODO Auto-generated method stub 121 return this.length*this.width; 122 } 123 124 @Override 125 public boolean validate() { 126 // TODO Auto-generated method stub 127 if(this.width>=0&&this.length>=0) 128 return true; 129 else 130 return false; 131 } 132 133 @Override 134 public String toString() { 135 // TODO Auto-generated method stub 136 return "Rectangle:"+String.format("%.2f",this.getArea())+" "; 137 } 138 139 } 140 141 //三角形 142 class Triangle extends Shape{ 143 private double side1; 144 private double side2; 145 private double side3; 146 147 public Triangle(double s1, double s2, double s3) { 148 // TODO Auto-generated constructor stub 149 this.side1=s1; 150 this.side2=s2; 151 this.side3=s3; 152 } 153 154 @Override 155 public double getArea() { 156 // TODO Auto-generated method stub 157 double p=(side1+side2+side3)/2; 158 double s=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 159 return s; 160 } 161 162 @Override 163 public boolean validate() { 164 // TODO Auto-generated method stub 165 if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) 166 return false; 167 else if(side1-side2>=side3||side1-side3>=side2||side2-side3>=side1) 168 return false; 169 else 170 return true; 171 172 } 173 174 @Override 175 public String toString() { 176 // TODO Auto-generated method stub 177 return "Triangle:"+String.format("%.2f",this.getArea())+" "; 178 } 179 180 } 181 182 //梯形 183 class Trapezoid extends Shape{ 184 private double topSide; 185 private double bottomSide; 186 private double height; 187 188 public Trapezoid(double tops, double bottoms, double h) { 189 // TODO Auto-generated constructor stub 190 this.bottomSide=bottoms; 191 this.height=h; 192 this.topSide=tops; 193 } 194 195 @Override 196 public double getArea() { 197 // TODO Auto-generated method stub 198 return (this.topSide+this.bottomSide)*this.height/2; 199 } 200 201 @Override 202 public boolean validate() { 203 // TODO Auto-generated method stub 204 if(topSide>=0&&bottomSide>=0&&height>=0) 205 return true; 206 else 207 return false; 208 } 209 210 @Override 211 public String toString() { 212 // TODO Auto-generated method stub 213 return "Trapezoid:"+String.format("%.2f",this.getArea())+" "; 214 } 215 216 } 217 218 class Card implements Comparable{ 219 private Shape shape; 220 221 public Card(Shape shape) { 222 this.shape=shape; 223 } 224 225 public Shape getShape() { 226 return this.shape; 227 } 228 229 public void setShape(Shape shape) { 230 this.shape=shape; 231 } 232 233 @Override 234 public int compareTo(Card card) { 235 // TODO Auto-generated method stub 236 if(this.shape.getArea()>card.shape.getArea())//从大到小 237 return 1; 238 else if(this.shape.getArea()<card.shape.getArea())//从小到大 239 return -1; 240 else 241 return 0; 242 } 243 244 } 245 246 class DealCardList{ 247 248 ArrayList<Card> cardList=new ArrayList<Card>(); 249 250 public DealCardList(ArrayList<Integer> list) { 251 // TODO Auto-generated constructor stub 252 for(int i=0;i<list.size();i++) { 253 int j=list.get(i); 254 255 switch(j) { 256 case 1:{ 257 double radius=Main.input.nextDouble(); 258 Circle c=new Circle(radius); 259 this.cardList.add(i,new Card(c)); 260 break; 261 } 262 case 2:{ 263 double width=Main.input.nextDouble(); 264 double length=Main.input.nextDouble(); 265 Rectangle rec=new Rectangle(width,length); 266 this.cardList.add(i, new Card(rec)); 267 break; 268 } 269 case 3:{ 270 double s1=Main.input.nextDouble(); 271 double s2=Main.input.nextDouble(); 272 double s3=Main.input.nextDouble(); 273 Triangle tri=new Triangle(s1,s2,s3); 274 this.cardList.add(i, new Card(tri)); 275 break; 276 } 277 case 4: 278 double tops=Main.input.nextDouble(); 279 double bottoms=Main.input.nextDouble(); 280 double h=Main.input.nextDouble(); 281 Trapezoid tra=new Trapezoid(tops,bottoms,h); 282 this.cardList.add(i, new Card(tra)); 283 break; 284 } 285 286 } 287 288 } 289 290 public boolean validate() { 291 for(Card c:cardList) { 292 if(!c.getShape().validate()) 293 return false; 294 } 295 return true; 296 } 297 298 public void cardSort() { 299 int i,j; 300 for(i=0;i<cardList.size();i++) { 301 for(j=0;j<cardList.size()-1;j++) { 302 Card card1=cardList.get(j); 303 Card card2=cardList.get(j+1); 304 if(card1.compareTo(card2)!=1) { 305 cardList.set(j, card2); 306 cardList.set(j+1, card1); 307 } 308 } 309 } 310 } 311 312 public String getAllArea() { 313 double sum=0; 314 for(Card c:cardList) 315 sum=sum+c.getShape().getArea(); 316 return String.format("%.2f",sum); 317 } 318 319 public void showResult() { 320 321 System.out.println("The original list:"); 322 for(Card c:cardList) 323 System.out.print(c.getShape().toString()); 324 System.out.println(""); 325 326 System.out.println("The sorted list:"); 327 this.cardSort(); 328 for(Card c:cardList) 329 System.out.print(c.getShape().toString()); 330 System.out.println(""); 331 332 System.out.println("Sum of area:"+this.getAllArea()); 333 } 334 } 335 336 interface Comparable{ 337 abstract public int compareTo(Card card); 338 }
SourceMonitor:

思路:
按照题目给出的类图进行编码就好,主函数也给了代码,没什么好说的。
踩坑心得:
对于这道题目,一开始按照类图写完代码后,一直显示数组越界,后来发现是 ArrayList.set 和 ArrayList.add 这两个搞混了,ArrayList.set 必须要动态数组不为空,当动态数组为空时,就无法set 会报错数组越界,数组为空时只能使用 ArrayList.add 对动态数组添加数据。然后还有一个特别傻x的问题,就是我一开始给构造方法的时候,忘记写构造方法的方法体了,导致构造方法一直是空的,所以一直无法输出,导致错误。
改进建议:
对于这道题目没有什么改进建议,按照题目给出的类图写代码就好,但是在编码的过程的时候要特别注意一些小细节,因为一些小错误导致我后面找bug的时候花费了一些时间,所谓细节决定成败,不要忽视任何一个小地方。
7-2 图形卡片分组游戏 (60 分) 源码:
1 import java.util.*; 2 3 public class Main { 4 public static Scanner input=new Scanner(System.in); 5 public static void main(String[] args) { 6 7 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 10 int num=input.nextInt(); 11 12 if(num==0) { 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 17 while(num!=0) { 18 if(num<0||num>4) { 19 System.out.println("Wrong Format"); 20 System.exit(0); 21 } 22 list.add(num); 23 num=input.nextInt(); 24 } 25 26 DealCardList dealCardList =new DealCardList(list); 27 28 if(!dealCardList.validate()) { 29 System.out.println("Wrong Format"); 30 System.exit(0); 31 } 32 33 dealCardList.showResult(); 34 35 } 36 } 37 38 39 abstract class Shape{ 40 private String shapeName; 41 42 public String getShapeName() { 43 return this.shapeName; 44 } 45 46 public void setShapeName(String shapeName) { 47 this.shapeName=shapeName; 48 } 49 50 abstract public double getArea(); 51 abstract public boolean validate(); 52 abstract public String toString(); 53 54 } 55 56 57 //圆 58 class Circle extends Shape{ 59 private double radius; 60 61 public Circle(double radius){ 62 this.radius=radius; 63 } 64 65 public double getRadius() { 66 return this.radius; 67 } 68 69 public void setRadius(double radius) { 70 this.radius=radius; 71 } 72 73 @Override 74 public double getArea() { 75 // TODO Auto-generated method stub 76 return Math.PI*this.radius*this.radius; 77 } 78 79 @Override 80 public boolean validate() { 81 // TODO Auto-generated method stub 82 if(this.radius<0) 83 return false; 84 else 85 return true; 86 } 87 88 @Override 89 public String toString() { 90 // TODO Auto-generated method stub 91 return "Circle:"+String.format("%.2f",this.getArea())+" "; 92 } 93 public String getShapeName() { 94 return "Circle"; 95 } 96 } 97 98 //矩形 99 class Rectangle extends Shape{ 100 private double width; 101 private double length; 102 103 public Rectangle(double width2, double length2) { 104 // TODO Auto-generated constructor stub 105 this.length=length2; 106 this.width=width2; 107 } 108 109 public double getWidth() { 110 return this.width; 111 } 112 113 public void setWidth(double width) { 114 this.width=width; 115 } 116 117 public double getlength() { 118 return this.length; 119 } 120 121 public void setLength(double length) { 122 this.length=length; 123 } 124 125 @Override 126 public double getArea() { 127 // TODO Auto-generated method stub 128 return this.length*this.width; 129 } 130 131 @Override 132 public boolean validate() { 133 // TODO Auto-generated method stub 134 if(this.width>=0&&this.length>=0) 135 return true; 136 else 137 return false; 138 } 139 140 @Override 141 public String toString() { 142 // TODO Auto-generated method stub 143 return "Rectangle:"+String.format("%.2f",this.getArea())+" "; 144 } 145 public String getShapeName() { 146 return "Rectangle"; 147 } 148 } 149 150 //三角形 151 class Triangle extends Shape{ 152 private double side1; 153 private double side2; 154 private double side3; 155 156 public Triangle(double s1, double s2, double s3) { 157 // TODO Auto-generated constructor stub 158 this.side1=s1; 159 this.side2=s2; 160 this.side3=s3; 161 } 162 163 @Override 164 public double getArea() { 165 // TODO Auto-generated method stub 166 double p=(side1+side2+side3)/2; 167 double s=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 168 return s; 169 } 170 171 @Override 172 public boolean validate() { 173 // TODO Auto-generated method stub 174 if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) 175 return false; 176 else if(side1-side2>=side3||side1-side3>=side2||side2-side3>=side1) 177 return false; 178 else 179 return true; 180 181 } 182 183 @Override 184 public String toString() { 185 // TODO Auto-generated method stub 186 return "Triangle:"+String.format("%.2f",this.getArea())+" "; 187 } 188 public String getShapeName() { 189 return "Triangle"; 190 } 191 } 192 193 //梯形 194 class Trapezoid extends Shape{ 195 private double topSide; 196 private double bottomSide; 197 private double height; 198 199 public Trapezoid(double tops, double bottoms, double h) { 200 // TODO Auto-generated constructor stub 201 this.bottomSide=bottoms; 202 this.height=h; 203 this.topSide=tops; 204 } 205 206 @Override 207 public double getArea() { 208 // TODO Auto-generated method stub 209 return (this.topSide+this.bottomSide)*this.height/2; 210 } 211 212 @Override 213 public boolean validate() { 214 // TODO Auto-generated method stub 215 if(topSide>=0&&bottomSide>=0&&height>=0) 216 return true; 217 else 218 return false; 219 } 220 221 @Override 222 public String toString() { 223 // TODO Auto-generated method stub 224 return "Trapezoid:"+String.format("%.2f",this.getArea())+" "; 225 } 226 public String getShapeName() { 227 return "Trapezoid"; 228 } 229 } 230 231 class Card implements Comparable{ 232 private Shape shape; 233 234 public Card(Shape shape) { 235 this.shape=shape; 236 } 237 238 public Shape getShape() { 239 return this.shape; 240 } 241 242 public void setShape(Shape shape) { 243 this.shape=shape; 244 } 245 246 @Override 247 public int compareTo(Card card) { 248 // TODO Auto-generated method stub 249 if(this.shape.getArea()>card.shape.getArea())//从大到小 250 return 1; 251 else if(this.shape.getArea()<card.shape.getArea())//从小到大 252 return -1; 253 else 254 return 0; 255 } 256 257 } 258 259 class DealCardList extends Group{ 260 261 ArrayList<Card> cardList=new ArrayList<Card>(); 262 263 public DealCardList(ArrayList<Integer> list) { 264 // TODO Auto-generated constructor stub 265 for(int i=0;i<list.size();i++) { 266 int j=list.get(i); 267 268 switch(j) { 269 case 1:{ 270 double radius=Main.input.nextDouble(); 271 Circle c=new Circle(radius); 272 this.cardList.add(i,new Card(c)); 273 break; 274 } 275 case 2:{ 276 double width=Main.input.nextDouble(); 277 double length=Main.input.nextDouble(); 278 Rectangle rec=new Rectangle(width,length); 279 this.cardList.add(i, new Card(rec)); 280 break; 281 } 282 case 3:{ 283 double s1=Main.input.nextDouble(); 284 double s2=Main.input.nextDouble(); 285 double s3=Main.input.nextDouble(); 286 Triangle tri=new Triangle(s1,s2,s3); 287 this.cardList.add(i, new Card(tri)); 288 break; 289 } 290 case 4: 291 double tops=Main.input.nextDouble(); 292 double bottoms=Main.input.nextDouble(); 293 double h=Main.input.nextDouble(); 294 Trapezoid tra=new Trapezoid(tops,bottoms,h); 295 this.cardList.add(i, new Card(tra)); 296 break; 297 } 298 299 } 300 301 } 302 303 public boolean validate() { 304 for(Card c:cardList) { 305 if(!c.getShape().validate()) 306 return false; 307 } 308 return true; 309 } 310 311 public void cardSort() { 312 int i,j; 313 for(i=0;i<cardList.size();i++) { 314 for(j=0;j<cardList.size()-1;j++) { 315 Card card1=cardList.get(j); 316 Card card2=cardList.get(j+1); 317 if(card1.compareTo(card2)!=1) { 318 cardList.set(j, card2); 319 cardList.set(j+1, card1); 320 } 321 } 322 } 323 } 324 325 public String getAllArea() { 326 double sum=0; 327 for(Card c:cardList) 328 sum=sum+c.getShape().getArea(); 329 return String.format("%.2f",sum); 330 } 331 332 public void showResult() { 333 334 System.out.println("The original list:"); 335 System.out.print("["); 336 for(Card c:cardList) 337 System.out.print(c.getShape().toString()); 338 System.out.print("]"); 339 System.out.println(""); 340 341 342 System.out.println("The Separated List:"); 343 this.groupby(); 344 System.out.print("["); 345 for(Shape c:this.cir) 346 System.out.print(c.toString()); 347 System.out.print("]"); 348 349 System.out.print("["); 350 for(Shape c:this.rec) 351 System.out.print(c.toString()); 352 System.out.print("]"); 353 354 System.out.print("["); 355 for(Shape c:this.tra) 356 System.out.print(c.toString()); 357 System.out.print("]"); 358 359 System.out.print("["); 360 for(Shape c:this.tri) 361 System.out.print(c.toString()); 362 System.out.print("]"); 363 System.out.println(""); 364 365 System.out.println("The Separated sorted List:"); 366 this.cardSort(); 367 this.cir.clear(); 368 this.rec.clear(); 369 this.tra.clear(); 370 this.tri.clear(); 371 this.groupby(); 372 System.out.print("["); 373 for(Shape c:this.cir) 374 System.out.print(c.toString()); 375 System.out.print("]"); 376 377 System.out.print("["); 378 for(Shape c:this.rec) 379 System.out.print(c.toString()); 380 System.out.print("]"); 381 382 System.out.print("["); 383 for(Shape c:this.tra) 384 System.out.print(c.toString()); 385 System.out.print("]"); 386 387 System.out.print("["); 388 for(Shape c:this.tri) 389 System.out.print(c.toString()); 390 System.out.print("]"); 391 System.out.println(""); 392 393 System.out.println("The max area:"+this.getMax()); 394 395 /* 396 System.out.println("The sorted list:"); 397 this.cardSort(); 398 for(Card c:cardList) 399 System.out.print(c.getShape().toString()); 400 System.out.println(""); 401 402 System.out.println("Sum of area:"+this.getAllArea()); 403 */ 404 } 405 406 @Override 407 public void groupby() { 408 // TODO Auto-generated method stub 409 410 for(int i=0;i<this.cardList.size();i++) { 411 String s=this.cardList.get(i).getShape().getShapeName(); 412 if(s.equals("Circle")) { 413 this.cir.add(this.cardList.get(i).getShape()); 414 } 415 if(s.equals("Rectangle")) { 416 this.rec.add(this.cardList.get(i).getShape()); 417 } 418 if(s.equals("Triangle")) { 419 this.tra.add(this.cardList.get(i).getShape()); 420 } 421 if(s.equals("Trapezoid")) { 422 this.tri.add(this.cardList.get(i).getShape()); 423 } 424 } 425 } 426 427 @Override 428 public String getMax() { 429 // TODO Auto-generated method stub 430 double sum1=0,sum2=0,sum3=0,sum4=0; 431 for(Shape a1:this.cir) 432 sum1=a1.getArea()+sum1; 433 for(Shape a2:this.rec) 434 sum2=a2.getArea()+sum2; 435 for(Shape a3:this.tra) 436 sum3=a3.getArea()+sum3; 437 for(Shape a4:this.tri) 438 sum4=a4.getArea()+sum4; 439 440 double max=0; 441 double[] sum= {sum1,sum2,sum3,sum4}; 442 for(int i=0;i<4;i++) { 443 if(max<sum[i]) 444 max=sum[i]; 445 } 446 447 return String.format("%.2f",max); 448 } 449 } 450 451 interface Comparable{ 452 abstract public int compareTo(Card card); 453 } 454 455 abstract class Group{ 456 public ArrayList<Shape> cir=new ArrayList<Shape>(); 457 public ArrayList<Shape> rec=new ArrayList<Shape>(); 458 public ArrayList<Shape> tri=new ArrayList<Shape>(); 459 public ArrayList<Shape> tra=new ArrayList<Shape>(); 460 abstract public void groupby(); 461 abstract public String getMax(); 462 }
SourceMonitor:

思路:
这道题目就主要是在第一个题目的基础上进行输出的格式修改,一开始我是在DealCardList类中增加一些存按照图形分类的动态数组,便于后面按照图形形状进行分类输出,后来觉得太麻烦了就创建了一个专门用来分组的抽象父类让DealCardList类继承,分类的动态数组中的排序可以让排序函数将所有图形面积排序好,然后再按照图形形状进行分类就好。
踩坑心得:
这道题目的难点我觉得就是分类和 "[" "]"这两个括号,特别要注意的是,在使用动态数组输出的时候要注意,在每个不同输出的种类之前清空一下数组,这样就避免,以前使用过这个数组,然后会输出很多多余的数据,然后就是要注意"[" "]"这两个括号的位置。
改进建议:
我觉得我创建的这个用来分组的abstract class Group抽象父类,他不应该是一个父类,应该是一个接口,因为这个类就是为了给DealCardList类增加功能而已,他不太符合父类和子类的定义,所以将这个父类改成接口更好。
题目集八、九


7-3 ATM机类结构设计(一) (100 分) 源码:
略(错误代码我就不放上来了)
SourceMonitor:

思路 :
一开始我就觉得要有Bank,Account,User,Card,Check这几个类,然后利用父类和子类继承来实现功能。
踩坑心得:
这次的错误真的是相当离谱,导致我一运行我的代码,整个eclipse就卡住不能动,运行超时,实在太离谱了,我的代码整个就是循环嵌套循环,整个复杂度就非常高,最后实在是无法解决,eclipse也卡住不能动,感觉就是写了一个病毒代码一样,其他人运行我的代码也是eclipse卡住不能动。
改进建议:
因为我为每个用户都建立了一个类,他们都继承User父类,也为每一个银行建立了一个类,他们都继承Bank父类,就导致类非常多,然后他们又与Check进行组合,这样就导致耦合度很高,后期改代码的时候改到崩溃,这些子类可以整合到一个动态数组里面,然后再主函数中进行添加。
7-1 ATM机类结构设计(二) (100 分) 源码:
1 import java.util.ArrayList; 2 3 import java.util.Scanner; 4 5 /******************************主类****************************************************/ 6 7 public class Main { 8 9 public static void main(String[] args) { 10 Scanner input=new Scanner(System.in); 11 12 /******************************银行初始化**********************************************/ 13 //建设银行 14 ArrayList<String> l1=new ArrayList<String>(); 15 l1.add("01"); 16 l1.add("02"); 17 l1.add("03"); 18 l1.add("04"); 19 Bank CCB=new Bank("中国建设银行",l1,0.02,0.05); 20 21 //工商 22 ArrayList<String> l2=new ArrayList<String>(); 23 l2.add("05"); 24 l2.add("06"); 25 Bank ICBC=new Bank("中国工商银行",l2,0.03,0.05); 26 27 //农业 28 ArrayList<String> l3=new ArrayList<String>(); 29 l3.add("07"); 30 l3.add("08"); 31 l3.add("09"); 32 l3.add("10"); 33 l3.add("11"); 34 Bank ABC=new Bank("中国农业银行",l3,0.04,0.05); 35 36 /******************************用户初始化**********************************************/ 37 38 //杨过 39 //账户1的卡号 40 ArrayList<Card> y1=new ArrayList<Card>(); 41 y1.add(new Card("6217000010041315709","88888888")); 42 y1.add(new Card("6217000010041315715","88888888")); 43 44 //账户2的卡号 45 ArrayList<Card> y2=new ArrayList<Card>(); 46 y2.add(new Card("6217000010041315718","88888888")); 47 48 //杨过账户 49 ArrayList<Account> a1=new ArrayList<Account>(); 50 a1.add(new Account("3217000010041315709",y1,1,10000)); 51 a1.add(new Account("3217000010041315715 ",y2,1,10000)); 52 53 User yg=new User("杨过",CCB,a1); 54 55 56 57 //郭靖 58 //账户1卡号 59 ArrayList<Card> g=new ArrayList<Card>(); 60 g.add(new Card("6217000010051320007","88888888")); 61 62 //郭靖账户 63 ArrayList<Account> a2=new ArrayList<Account>(); 64 a2.add(new Account(" 3217000010051320007",g,1,10000)); 65 66 User gj=new User("郭靖",CCB,a2); 67 68 //张无忌 69 //账户1卡号 70 ArrayList<Card> z1=new ArrayList<Card>(); 71 z1.add(new Card("6222081502001312389","88888888")); 72 73 //账户2卡号 74 ArrayList<Card> z2=new ArrayList<Card>(); 75 z2.add(new Card("6222081502001312390","88888888")); 76 77 //账户3卡号 78 ArrayList<Card> z3=new ArrayList<Card>(); 79 z3.add(new Card("6222081502001312399","88888888")); 80 z3.add(new Card("6222081502001312400","88888888")); 81 82 //账户 83 ArrayList<Account> a3=new ArrayList<Account>(); 84 a3.add(new Account("3222081502001312389",z1,1,10000)); 85 a3.add(new Account("3222081502001312390",z2,1,10000)); 86 a3.add(new Account("3222081502001312399",z3,1,10000)); 87 88 User zwj=new User("张无忌",ICBC,a3); 89 90 //韦小宝 91 //账户1 92 ArrayList<Card> w1=new ArrayList<Card>(); 93 w1.add(new Card("6222081502051320785","88888888")); 94 95 //账户2 96 ArrayList<Card> w2=new ArrayList<Card>(); 97 w2.add(new Card("6222081502051320786","88888888")); 98 99 //账户 100 ArrayList<Account> a4=new ArrayList<Account>(); 101 a4.add(new Account("3222081502051320785",w1,1,10000)); 102 a4.add(new Account("3222081502051320786",w2,1,10000)); 103 104 User wxb=new User("韦小宝",ICBC,a4); 105 106 //张三丰 107 //账户1 108 ArrayList<Card> z=new ArrayList<Card>(); 109 z.add(new Card("6640000010045442002","88888888")); 110 z.add(new Card("6640000010045442003","88888888")); 111 112 //账户 113 ArrayList<Account> a5=new ArrayList<Account>(); 114 a5.add(new Account("3640000010045442002",z,2,10000)); 115 116 User zsf=new User("张三丰",CCB,a5); 117 118 //令狐冲 119 //账户1 120 ArrayList<Card> l=new ArrayList<Card>(); 121 l.add(new Card("6640000010045441009","88888888")); 122 123 //账户 124 ArrayList<Account> a6=new ArrayList<Account>(); 125 a6.add(new Account("3640000010045441009",l,2,10000)); 126 127 User lhc=new User("令狐冲",ICBC,a6); 128 129 //乔峰 130 //账户1卡号 131 ArrayList<Card> q=new ArrayList<Card>(); 132 q.add(new Card("6630000010033431001","88888888")); 133 134 //账户 135 ArrayList<Account> a7=new ArrayList<Account>(); 136 a7.add(new Account("3630000010033431001",q,2,10000)); 137 138 User qf=new User("乔峰",ABC,a7); 139 140 //洪七公 141 //账户1卡号 142 ArrayList<Card> h=new ArrayList<Card>(); 143 h.add(new Card("6630000010033431008","88888888")); 144 145 //账户 146 ArrayList<Account> a8=new ArrayList<Account>(); 147 a8.add(new Account("3630000010033431008",h,2,10000)); 148 149 User hqg=new User("洪七公",ABC,a8); 150 151 152 153 /******************************检查初始化**********************************************/ 154 155 ArrayList<User> user=new ArrayList<User>(); 156 user.add(yg); 157 user.add(gj); 158 user.add(zwj); 159 user.add(wxb); 160 user.add(zsf); 161 user.add(lhc); 162 user.add(qf); 163 user.add(hqg); 164 Check check=new Check(user); 165 166 /******************************输入**************************************************/ 167 String s=input.nextLine(); 168 169 while(!s.equals("#")) { 170 String[] b=s.split("\\s+"); 171 172 if(b.length==1&&check.cardnumCheck(b[0])) { 173 User u=check.getHold(); 174 for(Account a:u.account) { 175 for(Card c:a.cardList) { 176 if(c.cardnum.equals(b[0])) 177 System.out.println("业务:查询余额 ¥"+String.format("%.2f",a.money)); 178 } 179 } 180 } 181 else { 182 if(!check.cardnumCheck(b[0])) 183 System.out.println("Sorry,this card does not exist."); 184 else if(!check.passwordCheck(b[1])) 185 System.out.println("Sorry,your password is wrong."); 186 else if(!check.atmCheck(b[2])) 187 System.out.println("Sorry,the ATM's id is wrong."); 188 else if(!check.withdrawalCheck(b[0],b[2],Double.parseDouble(b[3]))) 189 System.out.println("Sorry,your account balance is insufficient."); 190 else { 191 192 String bank=null; 193 for(User u:check.user) { 194 for(String a:u.bank.atmList) { 195 if(a.equals(b[2])) 196 bank=u.bank.name; 197 } 198 } 199 System.out.println("业务:取款 "+check.hold.name+"在"+bank+"的"+b[2]+"号ATM机上取款¥"+String.format("%.2f",Double.parseDouble(b[3]))); 200 User u=check.getHold(); 201 for(Account a:u.account) { 202 for(Card c:a.cardList) { 203 if(c.cardnum.equals(b[0])) { 204 double m=Double.parseDouble(b[3]); 205 a.money=a.money-m-check.fee; 206 System.out.println("当前余额为¥"+String.format("%.2f",a.money)); 207 } 208 209 210 } 211 } 212 } 213 214 } 215 s=input.nextLine(); 216 } 217 218 219 } 220 } 221 222 /******************************银行类**********************************************/ 223 224 class Bank{ 225 226 String name; 227 ArrayList<String> atmList; 228 double cross; 229 double overdraft; 230 231 public Bank(String string, ArrayList<String> l1, double d, double e) { 232 // TODO Auto-generated constructor stub 233 this.name=string; 234 this.atmList=l1; 235 this.cross=d; 236 this.overdraft=e;; 237 } 238 239 } 240 241 /******************************账户类**********************************************/ 242 class Account{ 243 244 String account; 245 ArrayList<Card> cardList; 246 double money; 247 int type; 248 249 public Account(String string, ArrayList<Card> c1, int i,double m) { 250 // TODO Auto-generated constructor stub 251 this.account=string; 252 this.cardList=c1; 253 this.type=i; 254 this.money=m; 255 } 256 257 } 258 259 /******************************卡类*************************************************/ 260 261 class Card{ 262 263 String password; 264 String cardnum; 265 266 267 public Card(String string, String string2) { 268 // TODO Auto-generated constructor stub 269 this.cardnum=string; 270 this.password=string2; 271 } 272 } 273 274 /******************************用户类**********************************************/ 275 276 class User{ 277 278 String name; 279 Bank bank; 280 ArrayList<Account> account; 281 282 public User(String s,Bank cCB, ArrayList<Account> a1) { 283 // TODO Auto-generated constructor stub 284 this.name=s; 285 this.account=a1; 286 this.bank=cCB; 287 } 288 } 289 290 /******************************检查类**********************************************/ 291 292 class Check{ 293 294 ArrayList<User> user; 295 User hold=new User(null, null, null);//持卡人 296 public static double fee=0; 297 298 public Check(ArrayList<User> user2) { 299 // TODO Auto-generated constructor stub 300 this.user=user2; 301 } 302 303 //检查卡号是否存在 304 public boolean cardnumCheck(String s) { 305 for(User u:this.user) { 306 for(Account a:u.account) { 307 for(Card c:a.cardList) { 308 if(c.cardnum.equals(s)) { 309 this.hold=u; 310 return true; 311 } 312 } 313 } 314 } 315 return false; 316 } 317 318 //返回输入的卡号的用户 319 public User getHold() { 320 return this.hold; 321 } 322 323 //检查ATM机是否存在 324 public boolean atmCheck(String s) { 325 for(User u:this.user) { 326 for(String atm:u.bank.atmList) { 327 if(atm.equals(s)) 328 return true; 329 } 330 } 331 return false; 332 } 333 334 //检查银行卡密码是否正确 335 public boolean passwordCheck(String s) { 336 if(s.equals("88888888")) 337 return true; 338 else 339 return false; 340 } 341 342 //检查是否跨行取款 343 public boolean atmCrossCheck(String s) { 344 //与持卡人所在的银行的atm机做比较 345 for(String atm:this.hold.bank.atmList) { 346 if(atm.equals(s)) 347 return true; 348 } 349 return false; 350 } 351 352 //检查取款金额大于账户余额或者透支金额超过规定最大透支金额 353 public boolean withdrawalCheck(String card,String atm,double m) { 354 355 fee=0; 356 357 //是否跨行取款 358 if(this.atmCrossCheck(atm)) { 359 fee=0; 360 } 361 else { 362 for(User u:this.user) { 363 for(String a:u.bank.atmList) { 364 if(a.equals(atm)) 365 fee=m*u.bank.cross; 366 } 367 } 368 } 369 370 //是否是贷记卡 371 if(this.hold.account.get(0).type==1) {//借记卡 372 for(Account a:this.hold.account) { 373 for(Card c:a.cardList) { 374 if(c.cardnum.equals(card)) { 375 if(a.money>=(m+fee)) 376 return true; 377 } 378 } 379 } 380 } 381 else{//贷记卡 382 for(Account a:this.hold.account) { 383 for(Card c:a.cardList) { 384 if(c.cardnum.equals(card)) { 385 if(a.money>=(m+fee)) { 386 return true; 387 } 388 else if((a.money+50000)>=(m+(m-a.money)*this.hold.bank.overdraft+fee)) { 389 if(a.money>=0) 390 fee=(m-a.money)*0.05+fee; 391 else 392 fee=m*0.05+fee; 393 return true; 394 } 395 else 396 return false; 397 398 } 399 } 400 } 401 } 402 403 return false; 404 } 405 406 //获得atm机所在银行 407 public Bank getBank(String atm) { 408 for(User u:this.user) { 409 for(String a:u.bank.atmList) { 410 if(a.equals(atm)) { 411 return u.bank; 412 } 413 414 } 415 } 416 return this.hold.bank; 417 } 418 419 420 }
SourceMonitor:

思路 :
这次我吸收了上次eclipse卡住的教训,首先就去CSDN学习了一下别人怎么设计的类,自己设计的实在太复杂了,后面我只保留了我上一个题目的父类,再在父类里面添加动态数组,在主函数中进行类的构造,这样也方面后续代码的维护,类也没那么多,简化了自己的代码。
踩坑心得:
这次踩坑的就是一开始题目没理解清楚,以为是一个账户中的每一个卡都有10000元,完成代码后才发现是一个账户总共这么多,还好自己设计的类便于维护,并不需要修改太多,编码前一定要先了解清楚需求,否则怎么做都是错的。还有一个就是输出的问题找了好久,一个保留两位小数的,又是细节问题,自己是实在是太粗心了,还有各种输出错误找了好久唉,细节!细节!细节!
改进建议:
我感觉主函数还是太复杂了,嵌套循环太多了,但是又没有什么好的解决方法,CSDN学习一些别人的方法看看
总结
通过这几次实验,面向对象的思想已经根植在我心里了但是我对于类的设计还是不够,还需要多学习其他大佬的设计方法。然后对于继承、接口和组合能够得心应手了,这几次作业收获特别大,动态数组真的是我的最爱,真的特别方便好用,希望以后能够多学习更多的java方法,然后在设计方面能更上一层楼。
浙公网安备 33010602011771号