题目集7~9的总结性Blog
一、前言
- 这七次作业涉及Comparable 接口的应用、compareTo()方法的重写、抽象类的应用、“单一职责”原则和“开-闭”原则,ArrayList的应用等。
- 第八次和第九次作业则比较综合,主要考验的就是对类图的理解和如何设计类与类之间的关系。
- 第八九次作业难度难度较大,第七次作业难度适中,题量都不多。
二、设计与分析
题目集7(7-1)

1 import java.util.*; 2 3 public class Main { 4 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 5 //使用Main.input.next…即可(避免采坑) 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 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 28 abstract class Shape{ 29 String shapeName; 30 Shape(){ 31 32 } 33 Shape(String shapeName){ 34 this.shapeName = shapeName; 35 } 36 void setShapeName(String shapeName) { 37 this.shapeName = shapeName; 38 } 39 String getShapeName() { 40 return shapeName; 41 } 42 43 abstract double getArea(); 44 abstract boolean validate(); 45 public String toString() { 46 return getShapeName()+":"+String.format("%.2f",getArea()); 47 } 48 } 49 /* 50 abstract class Card extends Shape implements Comparable<Shape>{ 51 private Shape shape; 52 public int compareTo(Card o) { 53 if(this.getArea() < o.getArea()) 54 return -1; 55 else if(this.getArea() > o.getArea()) 56 return 1; 57 else 58 return 0; 59 } 60 Card(){ 61 62 } 63 Card(Shape shape){ 64 this.shape = shape; 65 } 66 Shape getShape() { 67 return shape; 68 } 69 void setShape(Shape shape) { 70 this.shape = shape; 71 } 72 }*/ 73 //这个地方不太一样 74 class Card implements Comparable<Card>{ 75 private Shape shape; 76 @Override 77 public int compareTo(Card o) { 78 if(shape.getArea() < o.getShape().getArea()) 79 return -1; 80 else if(shape.getArea() > o.getShape().getArea()) 81 return 1; 82 else 83 return 0; 84 } 85 Card(){ 86 87 } 88 Card(Shape shape){ 89 this.shape = shape; 90 } 91 Shape getShape() { 92 return shape; 93 } 94 void setShape(Shape shape) { 95 this.shape = shape; 96 } 97 } 98 99 class Trapezoid extends Shape{ 100 private double topside; 101 private double bottomside; 102 private double height; 103 Trapezoid(){ 104 105 } 106 Trapezoid(double topside, double bottomside, double height){ 107 this.topside = topside; 108 this.bottomside = bottomside; 109 this.height = height; 110 } 111 @Override 112 double getArea() { 113 return (1/2)*height*(topside+bottomside); 114 } 115 116 @Override 117 boolean validate() { 118 if(topside>0 && bottomside>0 && height>0) 119 return true; 120 else 121 return false; 122 } 123 124 /*@Override 125 public String toString() { 126 // TODO Auto-generated method stub 127 return null; 128 }*/ 129 130 } 131 132 class Circle extends Shape{ 133 private double radius; 134 Circle(double radius){ 135 this.radius = radius; 136 } 137 void setRadius(double radius) { 138 this.radius = radius; 139 } 140 double getRadius() { 141 return radius; 142 } 143 @Override 144 double getArea() { 145 return Math.PI*radius*radius; 146 } 147 @Override 148 boolean validate() { 149 if(radius>0) 150 return true; 151 else 152 return false; 153 } 154 /*@Override 155 public String toString() { 156 return null; 157 }*/ 158 } 159 160 class Rectangle extends Shape{ 161 private double length; 162 private double width; 163 Rectangle(double length, double width){ 164 this.length = length; 165 this.width = width; 166 } 167 @Override 168 double getArea() { 169 return length * width; 170 } 171 172 @Override 173 boolean validate() { 174 if(width > 0 && length >0) 175 return true; 176 else 177 return false; 178 } 179 180 /*@Override 181 public String toString() { 182 return null; 183 }*/ 184 } 185 186 class Triangle extends Shape{ 187 double side1; 188 double side2; 189 double side3; 190 Triangle(double side1, double side2,double side3){ 191 this.side1 = side1; 192 this.side2 = side2; 193 this.side3 = side3; 194 } 195 196 197 @Override 198 double getArea() { 199 // TODO Auto-generated method stub 200 double p = (side1+side2+side3)/2; 201 return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 202 } 203 @Override 204 boolean validate() { 205 // TODO Auto-generated method stub 206 if(side1>0 && side2>0 && side3>0 207 && side1+side2>side3 208 && side2+side3>side1 209 && side1+side3>side2) 210 return true; 211 else 212 return false; 213 } 214 /*@Override 215 public String toString() { 216 // TODO Auto-generated method stub 217 return null; 218 }*/ 219 } 220 class DealCardList { 221 ArrayList<Card> cardList = new ArrayList<>(); 222 public DealCardList(){ 223 } 224 public void cardSort(){ 225 TreeSet<Card> cards = new TreeSet<>(cardList); 226 for (Card card : cards) { 227 System.out.print(card.getShape()); 228 } 229 } 230 231 public boolean validate() { 232 for(Card card : cardList) { 233 if(!card.getShape().validate()) { 234 return false; 235 } 236 } 237 return true; 238 } 239 /* 240 DealCardList(ArrayList<Integer> list){ 241 this.cardList= cardList; 242 }*/ 243 public DealCardList(ArrayList<Integer> card){ 244 for(Integer choice : card) { 245 if(choice==0) break; 246 switch(choice) { 247 248 case 2: 249 Card card2 = new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 250 cardList.add(card2); 251 card2.getShape().setShapeName("Rectangle"); 252 break; 253 case 1: 254 Card card1 = new Card(new Circle(Main.input.nextDouble())); 255 cardList.add(card1); 256 card1.getShape().setShapeName("Circle"); 257 break; 258 case 3 : 259 Card card3 = new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 260 card3.getShape().setShapeName("Triangle"); 261 cardList.add(card3); 262 break; 263 case 4: 264 Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 265 card4.getShape().setShapeName("Trapezoid"); 266 cardList.add(card4); 267 break; 268 default:System.out.println("Wrong Format"); 269 } 270 } 271 } 272 public double getAllArea() { 273 double sum = 0; 274 for(Card card : cardList) { 275 sum+=card.getShape().getArea(); 276 } 277 return sum; 278 } 279 public void showResult() { 280 System.out.println("The original list:"); 281 for (Card card : cardList) { 282 System.out.print(card.getShape()); 283 } 284 System.out.println(); 285 System.out.println("The sorted list:"); 286 Collections.sort(cardList); 287 //cardSort(); 288 for(Card card : cardList) { 289 System.out.printf("%.2f ",card.getShape()); 290 } 291 System.out.println(); 292 System.out.printf("Sum of area:%.2f\n",getAllArea()); 293 } 294 }


- 从绿圈圈来看各项数据良好,每个函数平均包含的语句个数略少,平均复杂度还是挺低的,代码挺多重复的,方法也因为使用了抽象类而大量重写,只有每个图形的面积计算公式不太一样.
题目集7(7-2)
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Scanner; 4 5 public class Main { 6 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 7 //使用Main.input.next…即可(避免采坑) 8 public static Scanner input = new Scanner(System.in); 9 public static void main(String[] args){ 10 ArrayList<Integer> list = new ArrayList<Integer>(); 11 int num = input.nextInt(); 12 if(num ==0) { 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 while(num != 0){ 17 if(num < 0 || num > 4){ 18 System.out.println("Wrong Format"); 19 System.exit(0); 20 } 21 list.add(num); 22 num = input.nextInt(); 23 } 24 DealCardList dealCardList = new DealCardList(list); 25 if(!dealCardList.validate()){ 26 System.out.println("Wrong Format"); 27 System.exit(0); 28 } 29 dealCardList.showResult(); 30 input.close(); 31 } 32 } 33 34 class DealCardList{ 35 ArrayList<Card> cardList=new ArrayList<>(); 36 37 public DealCardList() { 38 } 39 40 public DealCardList(ArrayList<Integer> card){ 41 for(Integer choice : card) { 42 if(choice==0) break; 43 switch(choice) { 44 45 case 2: 46 Card card2 = new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble())); 47 cardList.add(card2); 48 card2.getShape().setShapeName("Rectangle"); 49 break; 50 case 1: 51 Card card1 = new Card(new Circle(Main.input.nextDouble())); 52 cardList.add(card1); 53 card1.getShape().setShapeName("Circle"); 54 break; 55 case 3 : 56 Card card3 = new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 57 card3.getShape().setShapeName("Triangle"); 58 cardList.add(card3); 59 break; 60 case 4: 61 Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble())); 62 card4.getShape().setShapeName("Trapezoid"); 63 cardList.add(card4); 64 break; 65 default:System.out.println("Wrong Format"); 66 } 67 } 68 } 69 70 /*public void cardSort(){ 71 TreeSet<Card> cards = new TreeSet<>(cardList); 72 for (Card card : cards) { 73 System.out.print(card.getShape()); 74 } 75 }*/ 76 public boolean validate(){ 77 for (Card card : cardList) { 78 if (!card.getShape().validate()){ 79 return false; 80 } 81 } 82 return true; 83 } 84 public double getAllArea(){ 85 double sum=0; 86 for (Card card : cardList) { 87 sum+=card.getShape().getArea(); 88 } 89 return sum; 90 } 91 public void showResult(){ 92 93 //Double[] sumOfArea = new Double[4]; 94 //为什么这样就可以? 95 Double[] sumOfArea = new Double[] {0.0, 0.0, 0.0, 0.0}; //存放每种图形的面积和 96 /*for(double i : sumOfArea) { 97 i=0.0; 98 }*/ 99 //为每种图形单独开创Card类的数组链表 100 ArrayList<Card> cardList1=new ArrayList<>(); 101 ArrayList<Card> cardList2=new ArrayList<>(); 102 ArrayList<Card> cardList3=new ArrayList<>(); 103 ArrayList<Card> cardList4=new ArrayList<>(); 104 //原始数据 105 System.out.println("The original list:"); 106 System.out.print("["); 107 for (Card card : cardList) { 108 System.out.print(card.getShape()); 109 } 110 System.out.print("]"); 111 System.out.println(); 112 113 //分类数据 114 System.out.println("The Separated List:"); 115 //System.out.println("The sorted list:"); 116 //cardSort(); 117 System.out.print("["); 118 for (Card card : cardList) { 119 if(card.getShape().getShapeName() == "Circle") { 120 cardList1.add(card); 121 System.out.print(card.getShape()); 122 } 123 } 124 System.out.print("]"); 125 126 //System.out.println(); 127 System.out.print("["); 128 for (Card card : cardList) { 129 if(card.getShape().getShapeName() == "Rectangle") { 130 cardList2.add(card); 131 System.out.print(card.getShape()); 132 } 133 } 134 System.out.print("]"); 135 136 //System.out.println(); 137 System.out.print("["); 138 for (Card card : cardList) { 139 if(card.getShape().getShapeName() == "Triangle") { 140 cardList3.add(card); 141 System.out.print(card.getShape()); 142 } 143 }System.out.print("]"); 144 145 //System.out.println(); 146 System.out.print("["); 147 for (Card card : cardList) { 148 if(card.getShape().getShapeName() == "Trapezoid") { 149 System.out.print(card.getShape()); 150 cardList4.add(card); 151 } 152 } 153 System.out.print("]"); 154 //System.out.println(); 155 /*Collections.sort(cardList); 156 for (Card card : cardList) { 157 System.out.print(card.getShape()); 158 }*/ 159 System.out.println(); 160 161 //分类排序数据 162 Collections.sort(cardList1); 163 Collections.sort(cardList2); 164 Collections.sort(cardList3); 165 Collections.sort(cardList4); 166 167 System.out.println("The Separated sorted List:"); 168 System.out.print("["); 169 for(Card card : cardList1) { 170 System.out.print(card.getShape()); 171 sumOfArea[0]+=card.getShape().getArea(); 172 } 173 System.out.print("]"); 174 175 System.out.print("["); 176 for(Card card : cardList2) { 177 System.out.print(card.getShape()); 178 sumOfArea[1]+=card.getShape().getArea(); 179 } 180 System.out.print("]"); 181 182 System.out.print("["); 183 for(Card card : cardList3) { 184 System.out.print(card.getShape()); 185 sumOfArea[2]+=card.getShape().getArea(); 186 } 187 System.out.print("]"); 188 189 System.out.print("["); 190 for(Card card : cardList4) { 191 System.out.print(card.getShape()); 192 sumOfArea[3]+=card.getShape().getArea(); 193 } 194 System.out.print("]\n"); 195 196 double maxArea=0; 197 for (int i =0; i<cardList.size(); i++) { 198 if(cardList.get(i).getShape().getArea()>maxArea) 199 maxArea=cardList.get(i).getShape().getArea(); 200 } 201 //用冒泡排下sumOfArea 202 for(int i=0; i<4-1; i++) { 203 for(int j=0; j<4-i-1; j++) { 204 if(sumOfArea[j]>sumOfArea[j+1]) { 205 double temp = sumOfArea[j]; 206 sumOfArea[j] = sumOfArea[j+1]; 207 sumOfArea[j+1] = temp; 208 } 209 } 210 }//傻比了,直接for循环找最大的不就好了嘛 211 212 System.out.print("The max area:"+String.format("%.2f", sumOfArea[3]) ); 213 //System.out.printf("Sum of area:%.2f\n",getAllArea()); 214 } 215 } 216 class Card implements Comparable<Card>{ 217 private Shape shape; 218 @Override 219 public int compareTo(Card o) { 220 if(shape.getArea() < o.getShape().getArea()) 221 return 1; 222 else if(shape.getArea() > o.getShape().getArea()) 223 return -1; 224 else 225 return 0; 226 } 227 Card(){ 228 229 } 230 Card(Shape shape){ 231 this.shape = shape; 232 } 233 Shape getShape() { 234 return shape; 235 } 236 void setShape(Shape shape) { 237 this.shape = shape; 238 } 239 } 240 241 abstract class Shape{ 242 private String shapeName; 243 244 public Shape() { 245 } 246 247 public Shape(String shapeName) { 248 this.shapeName = shapeName; 249 } 250 251 public String getShapeName() { 252 return shapeName; 253 } 254 255 public void setShapeName(String shapeName) { 256 this.shapeName = shapeName; 257 } 258 public abstract double getArea(); 259 public abstract boolean validate(); 260 261 @Override 262 public String toString() { 263 return getShapeName()+":"+String.format("%.2f ",getArea()); 264 } 265 } 266 267 class Circle extends Shape{ 268 private double radius; 269 270 public Circle() { 271 } 272 273 public Circle(double radius) { 274 this.radius = radius; 275 } 276 277 public double getRadius() { 278 return radius; 279 } 280 281 public void setRadius(double radius) { 282 this.radius = radius; 283 } 284 285 @Override 286 public double getArea() { 287 return Math.PI*radius*radius; 288 } 289 290 @Override 291 public boolean validate() { 292 return this.radius>0; 293 } 294 } 295 296 class Rectangle extends Shape{ 297 private double width,height; 298 299 public Rectangle() { 300 } 301 302 public Rectangle(double width, double height) { 303 this.width = width; 304 this.height = height; 305 } 306 307 public double getWidth() { 308 return width; 309 } 310 311 public void setWidth(double width) { 312 this.width = width; 313 } 314 315 public double getHeight() { 316 return height; 317 } 318 319 public void setHeight(double height) { 320 this.height = height; 321 } 322 323 @Override 324 public double getArea() { 325 return height*width; 326 } 327 328 @Override 329 public boolean validate() { 330 return width>0&&height>0; 331 } 332 } 333 334 class Triangle extends Shape{ 335 private double side1,side2,side3; 336 337 public Triangle() { 338 } 339 340 public Triangle(double side1, double side2, double side3) { 341 this.side1 = side1; 342 this.side2 = side2; 343 this.side3 = side3; 344 } 345 346 @Override 347 public double getArea() { 348 double p=(side1+side2+side3)/2; 349 return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3)); 350 } 351 352 @Override 353 public boolean validate() { 354 if (!(side1>0&&side3>0&&side2>0)) return false; 355 else{ 356 if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1)) return false; 357 } 358 return true; 359 } 360 } 361 362 class Trapezoid extends Shape{ 363 private double topSide,bottomSide,height; 364 365 public Trapezoid() { 366 } 367 368 public Trapezoid(double topSide, double bottomSide, double height) { 369 this.topSide = topSide; 370 this.bottomSide = bottomSide; 371 this.height = height; 372 } 373 374 @Override 375 public double getArea() { 376 return (topSide+bottomSide)*height/2; 377 } 378 379 @Override 380 public boolean validate() { 381 return topSide>0&&height>0&&bottomSide>0; 382 } 383 }

- 平均复杂度2.18,在2~4之间还好
- 方法调用语句数相比7-1,由53上升到了101,应该主要多在cardList.add()方法上。
- 7-2相比7-1还是挺多变化的,尤其在存储数据上,7-1只管计算面积,而7-2还需要对数据进一步整合排序,找出最大值,所以我在7-1的基础上,进行如下操作:
//为每种图形单独开创Card类的数组链表
ArrayList<Card> cardList1=new ArrayList<>(); ArrayList<Card> cardList2=new ArrayList<>(); ArrayList<Card> cardList3=new ArrayList<>(); ArrayList<Card> cardList4=new ArrayList<>();
然后每次都检索输入数字所匹配对象的名字:
//分类数据 System.out.println("The Separated List:"); //System.out.println("The sorted list:"); //cardSort(); System.out.print("["); for (Card card : cardList) { if(card.getShape().getShapeName() == "Circle") { cardList1.add(card); System.out.print(card.getShape()); } }
然后分类排序数据,使用默认的排序就好:
//分类排序数据 Collections.sort(cardList1); Collections.sort(cardList2); Collections.sort(cardList3); Collections.sort(cardList4);
题目集8(7-1)

1 //package ATM; 2 3 import java.util.*; 4 /* 5 import ATM_others.Account; 6 import ATM_others.UP; 7 import ATM_others.User; 8 import ATM_others.WithDraw;*/ 9 10 //import ATM_others.Account; 11 //import ATM_others.User; 12 13 public class Main { 14 15 public static void main(String[] args) { 16 // TODO Auto-generated method stub 17 //System.out.println("Wrong Format"); 18 Scanner scan = new Scanner (System.in); 19 String s = scan.nextLine(); 20 WithDraw w = new WithDraw(); 21 22 while(!s.equals("#")) { 23 String[] arr=s.split("\\s+"); 24 if(arr.length==1) { 25 //for(int i=0;i<arr.length;i++) { 26 27 double x=w.account(arr[0],w).getBalance(); 28 System.out.printf("¥%.2f\n",x); 29 30 //System.out.println(); 31 32 //} 33 34 } 35 else if(arr.length==4) { 36 if(w.cardNa(arr[0])) { 37 if(w.ATM(arr[2])) { 38 if(arr[1].equals("88888888")) { 39 40 41 if(w.amount(arr[3],w.account(arr[0],w))) { 42 43 if(w.ATMID(arr[2],w.user(arr[0],w))) { 44 w.ShowResult(arr[2], arr[3], w.user(arr[0],w),w.account(arr[0],w)); 45 } 46 else{ 47 System.out.println("Sorry,cross-bank withdrawal is not supported."); 48 49 System.exit(0); 50 } 51 } 52 else{ 53 System.out.println("Sorry,your account balance is insufficient."); 54 System.exit(0); 55 } 56 } 57 else{ 58 System.out.println("Sorry,your password is wrong."); 59 System.exit(0); 60 } 61 } 62 else{ 63 System.out.println("Sorry,the ATM's id is wrong."); 64 System.exit(0); 65 } 66 } 67 else { 68 System.out.println("Sorry,this card does not exist."); 69 System.exit(0); 70 } 71 } 72 s=scan.nextLine(); 73 } 74 } 75 76 } 77 class VlidateData{ 78 79 } 80 class UnionPay{ 81 Bank CCB = new Bank(); //CCB中国建设银行 82 Bank ICBC = new Bank(); //ICBC中国工商银行 83 UnionPay(){ 84 CCB.user1.setBank("中国建设银行"); 85 CCB.user1.setName("杨过"); 86 CCB.user2.setBank("中国工商银行"); 87 CCB.user1.setName("郭靖"); 88 CCB.user1.a1.first.setCard("6217000010041315709"); 89 CCB.user1.a1.second.setCard("6217000010041315715"); 90 CCB.user1.a2.first.setCard("6217000010041315718"); 91 CCB.user2.a1.first.setCard("6217000010051320007"); 92 93 ICBC.user1.a1.first.setCard("6222081502001312389"); 94 ICBC.user1.a2.first.setCard("6222081502001312390"); 95 ICBC.user1.a3.first.setCard("6222081502001312399"); 96 ICBC.user1.a3.second.setCard("6222081502001312400"); 97 ICBC.user2.a1.first.setCard("6222081502051320785"); 98 ICBC.user2.a2.first.setCard("6222081502051320786"); 99 100 ICBC.user1.setName("张无忌"); 101 ICBC.user1.setBank("中国工商银行"); 102 ICBC.user2.setName("韦小宝"); 103 ICBC.user2.setBank("中国工商银行"); 104 } 105 /*class WithDraw{ 106 String cardNum; 107 String cardPassword; 108 int ATMid; 109 double balance = 10000.00; 110 double amount; 111 Card card = new Card(); 112 Account account = new Account(); 113 ATM atm = new ATM(); 114 public void withdraw() { 115 116 } 117 }*/ 118 } 119 class WithDraw{ 120 void ShowResult(String a ,String b,User c,Account d) { 121 double x=Double.parseDouble(b); 122 double y=d.balance-x; 123 d.setBalance(y); 124 if(x<0) { 125 System.out.printf(c.getName()+"在"+c.getBank()+"的"+a+"号ATM机上存款¥"+"%.2f",-x); 126 System.out.println(); 127 } 128 else { 129 System.out.printf(c.getName()+"在"+c.getBank()+"的"+a+"号ATM机上取款¥"+"%.2f",x); 130 System.out.println(); 131 } 132 System.out.printf("当前余额为¥"+"%.2f",y); 133 System.out.println(); 134 } 135 UnionPay up=new UnionPay(); 136 int flag=0; 137 boolean cardNa(String s) { 138 String str="6217000010041315709|6217000010041315715|6217000010041315718|6217000010051320007|6222081502001312389|6222081502001312390|6222081502001312399|6222081502001312400|6222081502051320785|6222081502051320786"; 139 String str2="6217000010041315709|6217000010041315715|6217000010041315718|6217000010051320007"; 140 if(!s.matches(str2)) { 141 flag=1; 142 } 143 boolean card = s.matches(str); 144 return card; 145 } 146 boolean ATM(String s) { 147 if(s.equals("01")||s.equals("02")||s.equals("03")||s.equals("04")||s.equals("05")||s.equals("06")) 148 return true; 149 else 150 return false; 151 } 152 boolean ATMID(String s,User a) { 153 if(a.getBank().equals("中国建设银行")) { 154 if(s.equals("01")||s.equals("02")||s.equals("03")||s.equals("04")) 155 return true; 156 else 157 return false; 158 } 159 else { 160 if(s.equals("05")||s.equals("06")) 161 return true; 162 else 163 return false; 164 } 165 } 166 Bank CCB = new Bank(); //CCB中国建设银行 167 Bank ICBC = new Bank(); //ICBC中国工商银行 168 Account account(String s,WithDraw a) { 169 if(s.equals("6217000010041315709")) 170 return a.up.CCB.user1.a1; 171 else if(s.equals("6217000010041315715")) 172 return a.up.CCB.user1.a1; 173 else if(s.equals("6217000010041315718")) 174 return a.up.CCB.user1.a2; 175 else if(s.equals("6217000010051320007")) 176 return a.up.CCB.user2.a1; 177 else if(s.equals("6222081502001312389")) 178 return a.up.ICBC.user1.a1; 179 else if(s.equals("6222081502001312390")) 180 return a.up.ICBC.user1.a2; 181 else if(s.equals("6222081502001312399")) 182 return a.up.ICBC.user1.a3; 183 else if(s.equals("6222081502001312400")) 184 return a.up.ICBC.user1.a3; 185 else if(s.equals("6222081502051320785")) 186 return a.up.ICBC.user2.a1; 187 else 188 return a.up.ICBC.user2.a2; 189 } 190 User user(String s,WithDraw a) { 191 if(s.equals(CCB.user1.a1.first.getCard())) 192 return a.up.CCB.user1; 193 else if(s.equals(CCB.user1.a1.second.getCard())) 194 return a.up.CCB.user1; 195 else if(s.equals(CCB.user1.a2.first.getCard())) 196 return a.up.CCB.user1; 197 else if(s.equals("6217000010051320007")) 198 return a.up.CCB.user2; 199 else if(s.equals("6222081502001312389")) 200 return a.up.ICBC.user1; 201 else if(s.equals("6222081502001312390")) 202 return a.up.ICBC.user1; 203 else if(s.equals("6222081502001312399")) 204 return a.up.ICBC.user1; 205 else if(s.equals("6222081502001312400")) 206 return a.up.ICBC.user1; 207 else if(s.equals("6222081502051320785")) 208 return a.up.ICBC.user2; 209 else 210 return a.up.ICBC.user2; 211 } 212 boolean amount(String s,Account account) { 213 double x=Double.parseDouble(s); 214 if(x<account.balance) 215 return true; 216 else 217 return false; 218 } 219 void showResult(String a ,String b,User c,Account d) { 220 double x=Double.parseDouble(b); 221 double y=d.balance-x; 222 d.setBalance(y); 223 if(x<0) { 224 System.out.printf(c.getName()+"在"+c.getBank()+"的"+a+"号ATM机上存款¥"+"%.2f",-x); 225 System.out.println(); 226 } 227 else { 228 System.out.printf(c.getName()+"在"+c.getBank()+"的"+a+"号ATM机上取款¥"+"%.2f",x); 229 System.out.println(); 230 } 231 System.out.printf("当前余额为¥"+"%.2f",y); 232 System.out.println(); 233 } 234 } 235 class Card{ 236 String card; 237 public void setCard(String card){ 238 this.card = card; 239 } 240 public String getCard() { 241 return card; 242 } 243 } 244 class Account{ 245 /*Card card; 246 public void setCard(){ 247 this.card = card; 248 } 249 public Card getCard() { 250 return card; 251 }*/ 252 double balance = 10000.00; 253 String acconut; 254 Card first = new Card(); 255 Card second = new Card(); 256 public void setAccoun(String account) { 257 this.acconut = account; 258 } 259 public String getAccount() { 260 return acconut; 261 } 262 public void setBalance(double balance) { 263 this.balance = balance; 264 } 265 public double getBalance(){ 266 return balance; 267 } 268 } 269 class User{ 270 Account a1 = new Account(); 271 Account a2 = new Account(); 272 Account a3 = new Account(); 273 String bank = ""; 274 275 public void setBank(String bank) { 276 this.bank = bank; 277 } 278 public String getBank() { 279 return bank; 280 } 281 String name = ""; 282 public void setName(String name) { 283 this.name = name; 284 } 285 public String getName() { 286 return name; 287 } 288 } 289 class Bank{ 290 User user; 291 //User user3 = new User(); 292 User user1 = new User(); 293 User user2 = new User(); 294 public void setUser(){ 295 this.user = user; 296 } 297 public User getUser() { 298 return user; 299 } 300 } 301 class ATM{ 302 String atmid; 303 public void setATMid(String atmid) { 304 this.atmid = atmid; 305 } 306 public String getATMid(){ 307 return atmid; 308 } 309 }
非满分,只得了80分

- 复杂度分布挺不均的,平均复杂度低了,但最大复杂度超了挺多的。
- 有两个测试点最后也没能过,挺可惜的,说实话开始是没看懂类图的。老师给的类图感觉很模糊尤其是Validate类的作用
- 总体结构还是比较像之前的日期类的聚合比较相似的
题目集9(7-1)

1 //package ATM_luangaiTiJiao; 2 3 import java.util.ArrayList; 4 import java.util.Scanner; 5 import java.util.Iterator; 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 9 public class Main { 10 public static void main(String[] args) { 11 Scanner input = new Scanner(System.in); 12 13 UnionPay unionPay = new UnionPay(); 14 15 Bank ccb = new Bank("1001","中国建设银行"); 16 Bank icbc = new Bank("1002","中国工商银行"); 17 Bank abc = new Bank("1003","中国农业银行"); 18 19 unionPay.addBank(ccb); 20 unionPay.addBank(icbc); 21 unionPay.addBank(abc); 22 23 ATM aTM1 = new ATM("01",ccb); 24 ATM aTM2 = new ATM("02",ccb); 25 ATM aTM3 = new ATM("03",ccb); 26 ATM aTM4 = new ATM("04",ccb); 27 ATM aTM5 = new ATM("05",icbc); 28 ATM aTM6 = new ATM("06",icbc); 29 ATM aTM7 = new ATM("07",abc); 30 ATM aTM8 = new ATM("08",abc); 31 ATM aTM9 = new ATM("09",abc); 32 ATM aTM10 = new ATM("10",abc); 33 ATM aTM11 = new ATM("11",abc); 34 35 36 37 ccb.addATM(aTM1); 38 ccb.addATM(aTM2); 39 ccb.addATM(aTM3); 40 ccb.addATM(aTM4); 41 icbc.addATM(aTM5); 42 icbc.addATM(aTM6); 43 abc.addATM(aTM11); 44 abc.addATM(aTM10); 45 abc.addATM(aTM9); 46 abc.addATM(aTM8); 47 abc.addATM(aTM7); 48 49 User Zhangsanfeng = new User("3640000010045442002","张三丰","8208820-THC"); 50 User Linghuchong = new User("3640000010045441009","令狐冲","0"); 51 User Qiaofeng = new User("3630000010033431001","乔峰","1"); 52 53 User Yangguo = new User("360101200102122324","杨过","13856223254"); 54 User Guojing = new User("360101200012302552","郭靖","13800021124"); 55 User Zhangwuji = new User("360502199805163221","张无忌","13952110011"); 56 User Weixiaobao = new User("360201200513243326","韦小宝","13025996587"); 57 User Hongqigong = new User("3630000010033431008","洪七公","7"); 58 59 Account abcAcc1 = new Account("3630000010033431001",10000.00,Qiaofeng,abc); 60 Account ccbAcc4 = new Account("3640000010045442002",10000.00,Zhangsanfeng,ccb); 61 //Account ccbAcc5 = new Account("6640000010045442003",10000.00,Zhangsanfeng,ccb); 62 Account icbcAcc6 = new Account("3640000010045441009",10000.00,Linghuchong,icbc); 63 Account abcAcc2 = new Account("3630000010033431008",10000.00,Hongqigong,abc); 64 65 Account ccbAcc1 = new Account("3217000010041315709",10000.00,Yangguo,ccb); 66 Account ccbAcc2 = new Account("3217000010041315715",10000.00,Yangguo,ccb); 67 Account ccbAcc3 = new Account("3217000010051320007",10000.00,Guojing,ccb); 68 Account icbcAcc1 = new Account("3222081502001312389",10000.00,Zhangwuji,icbc); 69 Account icbcAcc2 = new Account("3222081502001312390",10000.00,Zhangwuji,icbc); 70 Account icbcAcc3 = new Account("3222081502001312399",10000.00,Zhangwuji,icbc); 71 Account icbcAcc4 = new Account("3222081502051320785",10000.00,Weixiaobao,icbc); 72 Account icbcAcc5 = new Account("3222081502051320786",10000.00,Weixiaobao,icbc); 73 74 Card ccbCard1 = new Card("6217000010041315709","88888888",ccbAcc1,"借记"); 75 Card ccbCard2 = new Card("6217000010041315715","88888888",ccbAcc1,"借记"); 76 Card ccbCard3 = new Card("6217000010041315718","88888888",ccbAcc2,"借记"); 77 Card ccbCard4 = new Card("6217000010051320007","88888888",ccbAcc3,"借记"); 78 Card icbcCard5 = new Card("6222081502001312389","88888888",icbcAcc1,"借记"); 79 Card icbcCard6 = new Card("6222081502001312390","88888888",icbcAcc2,"借记"); 80 Card icbcCard7 = new Card("6222081502001312399","88888888",icbcAcc3,"借记"); 81 Card icbcCard8 = new Card("6222081502001312400","88888888",icbcAcc3,"借记"); 82 Card icbcCard9 = new Card("6222081502051320785","88888888",icbcAcc4,"借记"); 83 Card icbcCard10 = new Card("6222081502051320786","88888888",icbcAcc5,"借记"); 84 85 Card ccbCard15 = new Card("6640000010045442002","88888888",ccbAcc4,"贷记"); 86 Card ccbCard11 = new Card("6640000010045442003","88888888",ccbAcc4,"贷记"); 87 Card icbcCard12 = new Card("6640000010045441009","88888888",icbcAcc6,"贷记"); 88 Card abcCard13 = new Card("6630000010033431001","88888888",abcAcc1,"贷记"); 89 Card abcCard14 = new Card("6630000010033431008","88888888",abcAcc2,"贷记"); 90 91 abc.addAccount(abcAcc2); 92 abc.addAccount(abcAcc1); 93 ccb.addAccount(ccbAcc4); 94 icbc.addAccount(icbcAcc6); 95 96 97 Yangguo.addAccount(ccbAcc1); 98 Yangguo.addAccount(ccbAcc2); 99 Guojing.addAccount(ccbAcc3); 100 Zhangwuji.addAccount(icbcAcc1); 101 Zhangwuji.addAccount(icbcAcc2); 102 Zhangwuji.addAccount(icbcAcc3); 103 Weixiaobao.addAccount(icbcAcc4); 104 Weixiaobao.addAccount(icbcAcc5); 105 106 ccb.addAccount(ccbAcc1); 107 ccb.addAccount(ccbAcc2); 108 ccb.addAccount(ccbAcc3); 109 icbc.addAccount(icbcAcc1); 110 icbc.addAccount(icbcAcc2); 111 icbc.addAccount(icbcAcc3); 112 icbc.addAccount(icbcAcc4); 113 icbc.addAccount(icbcAcc5); 114 115 Zhangsanfeng.addAccount(ccbAcc4); 116 Linghuchong.addAccount(icbcAcc6); 117 Qiaofeng.addAccount(abcAcc1); 118 Hongqigong.addAccount(abcAcc2); 119 120 121 122 ccbAcc1.addCard(ccbCard1); 123 ccbAcc1.addCard(ccbCard2); 124 ccbAcc2.addCard(ccbCard3); 125 ccbAcc3.addCard(ccbCard4); 126 icbcAcc1.addCard(icbcCard5); 127 icbcAcc2.addCard(icbcCard6); 128 icbcAcc3.addCard(icbcCard7); 129 icbcAcc3.addCard(icbcCard8); 130 icbcAcc4.addCard(icbcCard9); 131 icbcAcc5.addCard(icbcCard10); 132 133 ccbAcc4.addCard(ccbCard11); 134 icbcAcc6.addCard(icbcCard12); 135 abcAcc1.addCard(abcCard13); 136 abcAcc1.addCard(abcCard13); 137 abcAcc2.addCard(abcCard14); 138 139 StringBuilder sb = new StringBuilder(); 140 141 String data; 142 143 while(!((data = input.nextLine()).equals("#"))) { 144 sb.append(data + "\n"); 145 } 146 147 String[] dt = sb.toString().split("\n"); 148 for(int i = 0; i < dt.length; i++) { 149 String[] dataLine = dt[i].toString().split("\\s+"); 150 151 if(dataLine.length == 1) { 152 GetBalance gb = new GetBalance(unionPay); 153 System.out.println("业务:查询余额 "+String.format("¥%.2f", gb.getBalance(dataLine[0]))); 154 }else { 155 Withdraw wd = new Withdraw(unionPay,dataLine[0],dataLine[1],dataLine[2],Double.parseDouble(dataLine[3])); 156 wd.withdraw(); 157 } 158 } 159 } 160 } 161 162 class Account { 163 private String accountNO; 164 private double balance = 0; 165 private User user = null; 166 private Bank bank = null; 167 private static ArrayList<Card> list = new ArrayList<Card>(); 168 169 public Account() { 170 super(); 171 // TODO Auto-generated constructor stub 172 } 173 174 public Account(String accountNO, double balance, User user, Bank bank) { 175 super(); 176 this.accountNO = accountNO; 177 this.balance = balance; 178 this.user = user; 179 this.bank = bank; 180 } 181 182 public void addCard(Card card) { 183 list.add(card); 184 } 185 186 public void removeCard(Card card) { 187 list.remove(card); 188 } 189 190 public double getBalance() { 191 return balance; 192 } 193 194 public void setBalance(double balance) { 195 this.balance = balance; 196 } 197 198 public String getAccountNO() { 199 return accountNO; 200 } 201 202 public void setAccountNO(String accountNO) { 203 this.accountNO = accountNO; 204 } 205 206 public User getUser() { 207 return user; 208 } 209 210 public void setUser(User user) { 211 this.user = user; 212 } 213 214 public Bank getBank() { 215 return bank; 216 } 217 218 public void setBank(Bank bank) { 219 this.bank = bank; 220 } 221 222 public ArrayList<Card> getList() { 223 return list; 224 } 225 226 public void setList(ArrayList<Card> list) { 227 Account.list = list; 228 } 229 230 public static Account getAmountbyCardNO(String cardNO) { 231 Iterator<Card> cardItr = Account.list.iterator(); 232 Card card = null; 233 234 while(cardItr.hasNext()) { 235 card = cardItr.next(); 236 if(card.getCardNO().equals(cardNO)) { 237 return card.getAccount(); 238 } 239 } 240 241 return null; 242 } 243 } 244 245 class User { 246 private String ID; 247 private String name; 248 private String Phone; 249 ArrayList<Account> list = new ArrayList<Account>(); 250 251 public User() { 252 super(); 253 // TODO Auto-generated constructor stub 254 } 255 256 public User(String iD, String name, String phone) { 257 super(); 258 ID = iD; 259 this.name = name; 260 Phone = phone; 261 } 262 263 public String getID() { 264 return ID; 265 } 266 267 public void setID(String iD) { 268 ID = iD; 269 } 270 271 public String getPhone() { 272 return Phone; 273 } 274 275 public void setPhone(String phone) { 276 Phone = phone; 277 } 278 279 public String getName() { 280 return name; 281 } 282 283 public void setName(String name) { 284 this.name = name; 285 } 286 287 public void addAccount(Account account) { 288 this.list.add(account); 289 } 290 291 public void removeAccount(Account account) { 292 this.list.remove(account); 293 } 294 } 295 296 class Card { 297 private String cardNO; 298 private String cardPassword; 299 private Account account = null; 300 String mode=""; 301 public String getmode() { 302 return mode; 303 } 304 public void setmode(String mode) { 305 this.mode = mode; 306 } 307 public Card() { 308 super(); 309 // TODO Auto-generated constructor stub 310 } 311 312 public Card(String cardNO, String cardPassword, Account account,String mode) { 313 super(); 314 this.cardNO = cardNO; 315 this.cardPassword = cardPassword; 316 this.account = account; 317 this.mode = mode; 318 } 319 320 public String getCardNO() { 321 return cardNO; 322 } 323 324 public void setCardNO(String cardNO) { 325 this.cardNO = cardNO; 326 } 327 328 public String getCardPassword() { 329 return cardPassword; 330 } 331 332 public Account getAccount() { 333 return account; 334 } 335 336 public void setAccount(Account account) { 337 this.account = account; 338 } 339 340 public void setCardPassword(String cardPassword) { 341 this.cardPassword = cardPassword; 342 } 343 344 public boolean checkCard() { 345 Pattern p = Pattern.compile("\\d{16}+"); 346 Matcher m = p.matcher(this.cardNO); 347 return m.matches(); 348 } 349 350 public boolean checkPassword(String password) { 351 return this.cardPassword.equals(password); 352 } 353 } 354 355 class Withdraw { 356 private UnionPay unionPay; 357 private String cardNO; 358 private String cardPassword; 359 private String ATMID; 360 private double amount; 361 362 public Withdraw() { 363 super(); 364 // TODO Auto-generated constructor stub 365 } 366 367 368 public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) { 369 super(); 370 this.unionPay = unionPay; 371 this.cardNO = cardNO; 372 this.cardPassword = cardPassword; 373 ATMID = aTMID; 374 this.amount = amount; 375 } 376 377 public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) { 378 super(); 379 this.cardNO = cardNO; 380 this.cardPassword = cardPassword; 381 ATMID = aTMID; 382 this.amount = amount; 383 } 384 385 386 public void withdraw() { 387 /** 388 * 校验该卡是否存在 389 */ 390 Card card = ValidateData.getCardbyCardNO(unionPay, cardNO); 391 if(card == null) { 392 System.out.println("Sorry,this card does not exist."); 393 System.exit(0); 394 } 395 396 /** 397 * 校验ATM是否存在 398 */ 399 ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); 400 if(aTM == null) { 401 System.out.println("Sorry,the ATM's id is wrong."); 402 System.exit(0); 403 } 404 405 Account account = Account.getAmountbyCardNO(cardNO); 406 double balance = account.getBalance(); 407 408 /** 409 * 校验卡密码是否正确 410 */ 411 if(!card.getCardPassword().equals(cardPassword)) { 412 System.out.println("Sorry,your password is wrong."); 413 System.exit(0); 414 } 415 416 /** 417 * 校验取款金额是否大于余额 418 */ 419 if ((amount > balance && card.getmode()=="借记" ) || amount > balance+50000) { 420 System.out.println("Sorry,your account balance is insufficient."); 421 System.exit(0); 422 } 423 else if(amount > balance && card.getmode()=="贷记") { //省了个判断贷款大于50000的情况,上面一个if截断了 424 /*if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { 425 //System.out.println("Sorry,cross-bank withdrawal is not supported."); 426 //System.exit(0); 427 double rate =0.00; 428 if(aTM.getBank().getBankName()=="中国建设银行") 429 rate = 0.02; 430 else if(aTM.getBank().getBankName()=="中国工商银行") 431 rate = 0.03; 432 else if(aTM.getBank().getBankName()=="中国农业银行") 433 rate = 0.04; 434 account.setBalance((balance-amount)-(amount-balance)*0.05-amount*rate); 435 //balance = (balance-amount)-(amount-balance)*0.05-amount*rate; 436 //account.setBalance(balance); 437 //account.setBalance(balance - amount*(1+rate+0.05));//取款更新余额操作 438 } 439 else { 440 account.setBalance((balance-amount)-(amount-balance)*0.05); 441 //balance = (balance-amount)-(amount-balance)*0.05; 442 //account.setBalance(balance);//取款更新余额操作 443 //balance = (balance-amount)-(amount-balance)*0.05; 444 //account.setBalance(balance); 445 }*/ 446 balance = (balance-amount)-(amount-balance)*0.05; 447 account.setBalance(balance); 448 } 449 450 /** 451 * 校验是否为跨行取款 452 */ 453 if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) { 454 //System.out.println("Sorry,cross-bank withdrawal is not supported."); 455 //System.exit(0); 456 double rate =0.00; 457 if(aTM.getBank().getBankName()=="中国建设银行") 458 rate = 0.02; 459 else if(aTM.getBank().getBankName()=="中国工商银行") 460 rate = 0.03; 461 else if(aTM.getBank().getBankName()=="中国农业银行") 462 rate = 0.04; 463 account.setBalance(balance - amount*(1+rate));//取款更新余额操作 464 } 465 else 466 account.setBalance(balance - amount);//取款更新余额操作 467 468 if(amount > 0) { 469 showResult(account,1); 470 }else { 471 showResult(account,0); 472 } 473 474 } 475 476 public void showResult(Account account,int flag) { 477 String type = ""; 478 if(flag == 1) { 479 type = "取款"; 480 }else { 481 type = "存款"; 482 amount *= -1; 483 } 484 ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID); 485 String userName = account.getUser().getName(); 486 String bankName = account.getBank().getBankName(); 487 System.out.println("业务:"+type+" "+ userName + "在" + 488 aTM.getBank().getBankName() + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount)); 489 System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance())); 490 } 491 } 492 493 class ValidateData { 494 /** 495 * 校验卡号是否存在 496 * @param unionPay 497 * @param cardNO 498 * @return 499 */ 500 public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) { 501 Card card = null; 502 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 503 504 while(bankItr.hasNext()) { 505 ArrayList<Account> accountList = bankItr.next().getAccountList(); 506 Iterator<Account> accountItr = accountList.iterator(); 507 while(accountItr.hasNext()) { 508 ArrayList<Card> cardList = accountItr.next().getList(); 509 Iterator<Card> cardItr = cardList.iterator(); 510 while(cardItr.hasNext()) { 511 card = cardItr.next(); 512 if(card.getCardNO().equals(cardNO)) { 513 return card; 514 } 515 } 516 } 517 } 518 return null; 519 } 520 521 /** 522 * 校验ATM ID是否存在 523 * @param unionPay 524 * @param ATMID 525 * @return 526 */ 527 public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) { 528 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 529 Bank bank = null; 530 ATM aTM = null; 531 532 while(bankItr.hasNext()) { 533 bank = bankItr.next(); 534 Iterator<ATM> aTMItr = bank.getATMList().iterator(); 535 536 while(aTMItr.hasNext()) { 537 aTM = aTMItr.next(); 538 if(aTM.getATMID().equals(ATMID)) { 539 return aTM; 540 } 541 } 542 } 543 return null; 544 } 545 } 546 547 class UnionPay { 548 private ArrayList<Bank> bankList = new ArrayList<Bank>(); 549 550 public UnionPay() { 551 super(); 552 // TODO Auto-generated constructor stub 553 } 554 555 public UnionPay(ArrayList<Bank> bankList) { 556 super(); 557 this.bankList = bankList; 558 } 559 560 public ArrayList<Bank> getBankList() { 561 return bankList; 562 } 563 564 public void setBankList(ArrayList<Bank> bankList) { 565 this.bankList = bankList; 566 } 567 568 public void addBank(Bank bank) { 569 this.bankList.add(bank); 570 } 571 572 public void removeBank(Bank bank) { 573 this.bankList.remove(bank); 574 } 575 } 576 577 class GetBalance { 578 private UnionPay unionPay; 579 580 public GetBalance() { 581 super(); 582 // TODO Auto-generated constructor stub 583 } 584 585 public GetBalance(UnionPay unionPay) { 586 super(); 587 this.unionPay = unionPay; 588 } 589 590 public double getBalance(String cardNO) { 591 return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance(); 592 } 593 } 594 595 class Bank { 596 private String bankNO; 597 private String bankName; 598 private ArrayList<Account> accountList = new ArrayList<Account>(); 599 private ArrayList<ATM> ATMList = new ArrayList<ATM>(); 600 601 public Bank() { 602 super(); 603 // TODO Auto-generated constructor stub 604 } 605 606 public Bank(String bankNO, String bankName) { 607 super(); 608 this.bankNO = bankNO; 609 this.bankName = bankName; 610 } 611 612 public String getBankNO() { 613 return bankNO; 614 } 615 616 public void setBankNO(String bankNO) { 617 this.bankNO = bankNO; 618 } 619 620 public String getBankName() { 621 return bankName; 622 } 623 624 public void setBankName(String bankName) { 625 this.bankName = bankName; 626 } 627 628 public void addAccount(Account account) { 629 this.accountList.add(account); 630 } 631 632 public void removeAccount(Account account) { 633 this.accountList.remove(account); 634 } 635 636 public void addATM(ATM aTM) { 637 this.ATMList.add(aTM); 638 } 639 640 public void removeATM(ATM aTM) { 641 this.ATMList.remove(aTM); 642 } 643 644 public ArrayList<Account> getAccountList() { 645 return accountList; 646 } 647 648 public void setAccountList(ArrayList<Account> accountList) { 649 this.accountList = accountList; 650 } 651 652 public ArrayList<ATM> getATMList() { 653 return ATMList; 654 } 655 656 public void setATMList(ArrayList<ATM> aTMList) { 657 ATMList = aTMList; 658 } 659 } 660 661 class ATM { 662 private String ATMID; 663 private Bank bank = null; 664 665 public ATM() { 666 super(); 667 // TODO Auto-generated constructor stub 668 } 669 670 public ATM(String aTMID, Bank bank) { 671 super(); 672 ATMID = aTMID; 673 this.bank = bank; 674 } 675 676 public String getATMID() { 677 return ATMID; 678 } 679 680 public void setATMID(String aTMID) { 681 ATMID = aTMID; 682 } 683 684 public Bank getBank() { 685 return bank; 686 } 687 688 public void setBank(Bank bank) { 689 this.bank = bank; 690 } 691 }

- 这次也没能实现全部功能,需要多加些注释。
- 跨行操作的手续费与借贷卡的利息计算还是有些复杂,如下,但从测试样例看貌似张三丰莫名其妙被ATM多欠了10000多,应该是某个步骤重复了:
![]()
![]()
三、踩坑心得
题目集7
Card不能被实例化,因为接口错误。



如果不继承Shape的话就下面这样:



Comparable<>中比较对象错误

数组初始化:

四、改进建议
- 测试样例可以多一点,有些测试点人工寻找样例还是有点困难的。
- 类图包含的信息可以不可以再多一点
五、总结
- 了解了“单一职责”原则,单一职责就是一个的类只干一件事。每个类只负责一个职责
- 对Comparable 接口的应用、compareTo()方法的重写、抽象类的应用有了一定认识
- 对聚合关系的理解更加深刻了





浙公网安备 33010602011771号