Blog总结03(7~9次作业总结)
1.前言
(1)题目集07共有两道题目,第一题和第二题难度都适中,第二题难度较第一题来说,第一题主要考察类的继承、多态性使用方法以及接口的应用,难度适中,第二题也考察类的继承、多态性使用方法以及接口的应用,第二题要求对卡片排序时使用 Comparable 接口或 Comparator 接口。所以难度比第一题稍微大一些。可以说第二题就是第一题的升级版本,我们需要创建一个卡牌类,然后让圆形卡片类,矩形卡片类,三角形卡片类,梯形卡片类继承于卡牌类,每个类中都加入求面积的方法,这样就能实现继承多态的使用,直接调用card的求面积方法就可以实现很多类的求面积方法。然后在利用接口实现对面积的排序等问题的解决。
(2)题目集08只有一道题目,难度相对来说就比较难了,确实自己写了很久,思考了很多,查了很多资料,这道题的难度确实不低,这道题目要求我们编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能,重点考核类的封装性及类间关系设计(关联、组合及依赖),这道题的综合性很强,他考察了类与类的关系,这道题要求我们去思考怎样去设计,设计类与类之间的关系,好的设计可以让整个程序思路清晰,功能完善,所设计的到的知识点很全面,是综合性很强的题目。
(3)题目集09有一道题目,题目集09的题目其实是题目集08ATM机题目的扩展,该题目加了一些内容,比如该试题加了信用卡的功能,可以贷款,余额可以为负,然后允许跨行取款,只不过要收一些手续费,难度其实跟上一道差不多,比上一道题略难一些,本道题设计需要进一步思考,我们应该把账户设置成一个抽象类,然后有两个账户一个为借记账户,一个为贷记账户,都继承于账户类,方法都一样,这样就可以实现继承多态的一些方法,总体来说,这道题还是综合性的考察,作为题目集08作业的迭代作业,综合性较强,难度也是较大的。
2.设计与分析、
题目集07(7-1) 图形卡片排序游戏
输入格式:
- 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0 - 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出
Wrong Format。 - 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
- 排序前的各图形类型及面积,格式为
图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格; - 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为
Sum of area:总面积值。
本题源码如下:
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Scanner; 4 public class Main{ 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 ArrayList<Integer> list = new ArrayList<Integer>(); 8 int n = input.nextInt(); 9 /*if(n == 0) { 10 System.out.println("Wrong Format"); 11 12 }*/ 13 while(n != 0) { 14 if(n < 0 || n > 4) { 15 System.out.println("Wrong Format"); 16 System.exit(0); 17 } 18 list.add(n); 19 n = input.nextInt(); 20 21 } 22 23 24 ArrayList <Card> list2 = new ArrayList<Card>(); 25 for(int i = 0; i < list.size(); i ++) { 26 27 switch(list.get(i)) { 28 case 1: 29 Card cir = new Circle(input.nextDouble()); 30 list2.add(cir); 31 32 break; 33 case 2: 34 Rectangle rec = new Rectangle(input.nextDouble(),input.nextDouble()); 35 list2.add(rec); 36 break; 37 case 3: 38 Triangle tri = new Triangle(input.nextDouble(),input.nextDouble(),input.nextDouble()); 39 list2.add(tri); 40 break; 41 case 4: 42 Trapezium tra = new Trapezium(input.nextDouble(),input.nextDouble(),input.nextDouble()); 43 list2.add(tra); 44 break; 45 } 46 if(!list2.get(i).validate()) { 47 System.out.println("Wrong Format"); 48 System.exit(0); 49 } 50 } 51 System.out.println("The original list:"); 52 53 for(int i = 0; i < list.size(); i ++) { 54 list2.get(i).getName(); 55 System.out.print(String.format("%.2f", list2.get(i).getArea()).toString()+" "); 56 57 } 58 System.out.println(); 59 System.out.println("The sorted list:"); 60 for(int i = 0; i < list2.size() - 1; i++) 61 for(int j = 0; j < list2.size() - 1 - i;j ++) { 62 if(list2.get(j).getArea() < list2.get(j+1).getArea()) 63 Collections.swap(list2, j, j+1); 64 } 65 double sum = 0; 66 for(int i = 0; i < list.size(); i ++) { 67 list2.get(i).getName(); 68 System.out.print(String.format("%.2f", list2.get(i).getArea()).toString()+" "); 69 sum = sum + list2.get(i).getArea(); 70 } 71 System.out.println(); 72 73 System.out.println("Sum of area:"+String.format("%.2f", sum).toString()); 74 75 //System.out.println(String.format("%.2f", r.getArea()).toString()); 76 } 77 } 78 79 abstract class Card { 80 public abstract double getArea(); 81 public abstract boolean validate(); 82 public abstract void getName(); 83 } 84 85 86 class Circle extends Card{ 87 private double radius; 88 89 public Circle() { 90 super(); 91 } 92 93 public Circle(double radius) { 94 super(); 95 this.radius = radius; 96 } 97 public double getArea() { 98 return Math.PI*this.radius * this.radius ; 99 } 100 public boolean validate() { 101 if(this.radius <= 0) 102 return false; 103 else 104 return true; 105 } 106 public void getName() { 107 System.out.print("Circle:"); 108 } 109 } 110 111 112 class Rectangle extends Card{ 113 private double width; 114 private double length; 115 public Rectangle() { 116 super(); 117 // TODO 自动生成的构造函数存根 118 } 119 public Rectangle(double width, double length) { 120 super(); 121 this.width = width; 122 this.length = length; 123 } 124 public double getArea() { 125 return this.length *this.width; 126 } 127 public boolean validate() { 128 if(this.length > 0 && this.width > 0) 129 return true; 130 else 131 return false; 132 } 133 public void getName() { 134 System.out.print("Rectangle:"); 135 } 136 } 137 138 139 class Triangle extends Card{ 140 private double side1; 141 private double side2; 142 private double side3; 143 public Triangle() { 144 super(); 145 // TODO 自动生成的构造函数存根 146 } 147 public Triangle(double side1, double side2, double side3) { 148 super(); 149 this.side1 = side1; 150 this.side2 = side2; 151 this.side3 = side3; 152 } 153 154 public double getArea() { 155 double s = (this.side1 + this.side2 + this.side3)/2.0; 156 return Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3)); 157 } 158 public boolean validate() { 159 if(this.side1 > 0 && this.side2 > 0 && this.side3 > 0) { 160 if(this.side1 + this.side2 > this.side3 && this.side1 + this.side3 > this.side2 && this.side2 + this.side3 > this.side1) 161 return true; 162 else 163 return false; 164 165 } 166 else 167 return false; 168 } 169 public void getName() { 170 System.out.print("Triangle:"); 171 } 172 } 173 class Trapezium extends Card{ 174 double length; 175 double width; 176 double high; 177 public Trapezium() { 178 super(); 179 // TODO 自动生成的构造函数存根 180 } 181 public Trapezium(double length, double width, double high) { 182 super(); 183 this.length = length; 184 this.width = width; 185 this.high = high; 186 } 187 public double getLength() { 188 return length; 189 } 190 public void setLength(double length) { 191 this.length = length; 192 } 193 public double getWidth() { 194 return width; 195 } 196 public void setWidth(double width) { 197 this.width = width; 198 } 199 public double getHigh() { 200 return high; 201 } 202 public void setHigh(double high) { 203 this.high = high; 204 } 205 public double getArea() { 206 return (this.length + this.width)*this.high/2.0; 207 } 208 public boolean validate() { 209 if(this.high > 0 && this.length > 0 && this.width > 0) 210 return true; 211 else 212 return false; 213 } 214 public void getName() { 215 System.out.print("Trapezoid:"); 216 } 217 }
类图如下:

SourceMonitor的生成报表内容:


题目集07(7-2)图形卡片分组游戏
输入格式:
- 在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0 - 根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
- 如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出
Wrong Format。 - 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
- 排序前的各图形类型及面积,格式为
[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前; - 输出分组后的图形类型及面积,格式为
[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出; - 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
- 各组中面积之和的最大值输出,格式为
The max area:面积值。
此题源码如下:
1 import java.util.ArrayList; 2 import java.util.Collections; 3 import java.util.Scanner; 4 5 public class Main { 6 public static void main(String[] args) { 7 Scanner input = new Scanner(System.in); 8 ArrayList<Integer> list = new ArrayList<Integer>(); 9 int n = input.nextInt(); 10 if (n == 0) { 11 System.out.println("Wrong Format"); 12 System.exit(0); 13 14 } 15 while (n != 0) { 16 if (n < 0 || n > 4) { 17 System.out.println("Wrong Format"); 18 System.exit(0); 19 } 20 list.add(n); 21 n = input.nextInt(); 22 23 } 24 25 ArrayList<Card> list2 = new ArrayList<Card>(); 26 ArrayList<Card> c1 = new ArrayList<Card>(); 27 ArrayList<Card> c2 = new ArrayList<Card>(); 28 ArrayList<Card> c3 = new ArrayList<Card>(); 29 ArrayList<Card> c4 = new ArrayList<Card>(); 30 for (int i = 0; i < list.size(); i++) { 31 32 switch (list.get(i)) { 33 case 1: 34 Card cir = new Circle(input.nextDouble()); 35 list2.add(cir); 36 c1.add(cir); 37 38 break; 39 case 2: 40 Rectangle rec = new Rectangle(input.nextDouble(), input.nextDouble()); 41 list2.add(rec); 42 c2.add(rec); 43 break; 44 case 3: 45 Triangle tri = new Triangle(input.nextDouble(), input.nextDouble(), input.nextDouble()); 46 list2.add(tri); 47 c3.add(tri); 48 break; 49 case 4: 50 Trapezium tra = new Trapezium(input.nextDouble(), input.nextDouble(), input.nextDouble()); 51 list2.add(tra); 52 c4.add(tra); 53 break; 54 } 55 if (!list2.get(i).validate()) { 56 System.out.println("Wrong Format"); 57 System.exit(0); 58 } 59 } 60 System.out.println("The original list:"); 61 System.out.print("["); 62 63 for (int i = 0; i < list.size(); i++) { 64 list2.get(i).getName(); 65 System.out.print(String.format("%.2f", list2.get(i).getArea()).toString() + " "); 66 67 } 68 System.out.print("]"); 69 System.out.println(); 70 System.out.println("The Separated List:"); 71 System.out.print("["); 72 for (int i = 0; i < c1.size(); i++) { 73 c1.get(i).getName(); 74 System.out.print(String.format("%.2f", c1.get(i).getArea()).toString() + " "); 75 76 } 77 System.out.print("]"); 78 System.out.print("["); 79 for (int i = 0; i < c2.size(); i++) { 80 c2.get(i).getName(); 81 System.out.print(String.format("%.2f", c2.get(i).getArea()).toString() + " "); 82 83 } 84 System.out.print("]"); 85 System.out.print("["); 86 for (int i = 0; i < c3.size(); i++) { 87 c3.get(i).getName(); 88 System.out.print(String.format("%.2f", c3.get(i).getArea()).toString() + " "); 89 } 90 System.out.print("]"); 91 System.out.print("["); 92 for (int i = 0; i < c4.size(); i++) { 93 c4.get(i).getName(); 94 System.out.print(String.format("%.2f", c4.get(i).getArea()).toString() + " "); 95 } 96 System.out.print("]"); 97 System.out.println(); 98 System.out.println("The Separated sorted List:"); 99 100 for (int i = 0; i < c1.size() - 1; i++) 101 for (int j = 0; j < c1.size() - 1 - i; j++) { 102 if (c1.get(j).getArea() < c1.get(j + 1).getArea()) 103 Collections.swap(c1, j, j + 1); 104 } 105 106 for (int i = 0; i < c2.size() - 1; i++) 107 for (int j = 0; j < c2.size() - 1 - i; j++) { 108 if (c2.get(j).getArea() < c2.get(j + 1).getArea()) 109 Collections.swap(c2, j, j + 1); 110 } 111 for (int i = 0; i < c3.size() - 1; i++) 112 for (int j = 0; j < c3.size() - 1 - i; j++) { 113 if (c3.get(j).getArea() < c3.get(j + 1).getArea()) 114 Collections.swap(c3, j, j + 1); 115 } 116 for (int i = 0; i < c4.size() - 1; i++) 117 for (int j = 0; j < c4.size() - 1 - i; j++) { 118 if (c4.get(j).getArea() < c4.get(j + 1).getArea()) 119 Collections.swap(c4, j, j + 1); 120 } 121 122 System.out.print("["); 123 double m1 = 0, m2 = 0, m3 = 0, m4 = 0; 124 for (int i = 0; i < c1.size(); i++) { 125 c1.get(i).getName(); 126 System.out.print(String.format("%.2f", c1.get(i).getArea()).toString() + " "); 127 m1 += c1.get(i).getArea(); 128 } 129 System.out.print("]"); 130 System.out.print("["); 131 for (int i = 0; i < c2.size(); i++) { 132 c2.get(i).getName(); 133 System.out.print(String.format("%.2f", c2.get(i).getArea()).toString() + " "); 134 m2 += c2.get(i).getArea(); 135 } 136 System.out.print("]"); 137 System.out.print("["); 138 for (int i = 0; i < c3.size(); i++) { 139 c3.get(i).getName(); 140 System.out.print(String.format("%.2f", c3.get(i).getArea()).toString() + " "); 141 m3 += c3.get(i).getArea(); 142 } 143 System.out.print("]"); 144 System.out.print("["); 145 for (int i = 0; i < c4.size(); i++) { 146 c4.get(i).getName(); 147 System.out.print(String.format("%.2f", c4.get(i).getArea()).toString() + " "); 148 m4 += c4.get(i).getArea(); 149 } 150 System.out.print("]"); 151 152 double max = 0; 153 if (max < m1) 154 max = m1; 155 if (max < m2) 156 max = m2; 157 if (max < m3) 158 max = m3; 159 if (max < m4) 160 max = m4; 161 162 System.out.println(); 163 164 System.out.print("The max area:" + String.format("%.2f", max).toString()); 165 166 } 167 } 168 //System.out.print(String.format("%.2f", r.getArea()).toString()); 169 abstract class Card { 170 public abstract double getArea(); 171 172 public abstract boolean validate(); 173 174 public abstract void getName(); 175 176 } 177 178 class Circle extends Card { 179 private double radius; 180 181 public Circle() { 182 super(); 183 } 184 185 public Circle(double radius) { 186 super(); 187 this.radius = radius; 188 } 189 190 public double getArea() { 191 return Math.PI * this.radius * this.radius; 192 } 193 194 public boolean validate() { 195 if (this.radius <= 0) 196 return false; 197 else 198 return true; 199 } 200 201 public void getName() { 202 System.out.print("Circle:"); 203 } 204 } 205 206 class Rectangle extends Card { 207 private double width; 208 private double length; 209 210 public Rectangle() { 211 super(); 212 // TODO 自动生成的构造函数存根 213 } 214 215 public Rectangle(double width, double length) { 216 super(); 217 this.width = width; 218 this.length = length; 219 } 220 221 public double getArea() { 222 return this.length * this.width; 223 } 224 225 public boolean validate() { 226 if (this.length > 0 && this.width > 0) 227 return true; 228 else 229 return false; 230 } 231 232 public void getName() { 233 System.out.print("Rectangle:"); 234 } 235 } 236 237 class Triangle extends Card { 238 private double side1; 239 private double side2; 240 private double side3; 241 242 public Triangle() { 243 super(); 244 // TODO 自动生成的构造函数存根 245 } 246 247 public Triangle(double side1, double side2, double side3) { 248 super(); 249 this.side1 = side1; 250 this.side2 = side2; 251 this.side3 = side3; 252 } 253 254 public double getArea() { 255 double s = (this.side1 + this.side2 + this.side3) / 2.0; 256 return Math.sqrt(s * (s - this.side1) * (s - this.side2) * (s - this.side3)); 257 } 258 259 public boolean validate() { 260 if (this.side1 > 0 && this.side2 > 0 && this.side3 > 0) { 261 if (this.side1 + this.side2 > this.side3 && this.side1 + this.side3 > this.side2 262 && this.side2 + this.side3 > this.side1) 263 return true; 264 else 265 return false; 266 267 } else 268 return false; 269 } 270 271 public void getName() { 272 System.out.print("Triangle:"); 273 } 274 } 275 276 class Trapezium extends Card { 277 double length; 278 double width; 279 double high; 280 281 public Trapezium() { 282 super(); 283 // TODO 自动生成的构造函数存根 284 } 285 286 public Trapezium(double length, double width, double high) { 287 super(); 288 this.length = length; 289 this.width = width; 290 this.high = high; 291 } 292 293 public double getLength() { 294 return length; 295 } 296 297 public void setLength(double length) { 298 this.length = length; 299 } 300 301 public double getWidth() { 302 return width; 303 } 304 305 public void setWidth(double width) { 306 this.width = width; 307 } 308 309 public double getHigh() { 310 return high; 311 } 312 313 public void setHigh(double high) { 314 this.high = high; 315 } 316 317 public double getArea() { 318 return (this.length + this.width) * this.high / 2.0; 319 } 320 321 public boolean validate() { 322 if (this.high > 0 && this.length > 0 && this.width > 0) 323 return true; 324 else 325 return false; 326 } 327 328 public void getName() { 329 System.out.print("Trapezoid:"); 330 } 331 }
类图:

SourceMonitor的生成报表内容:


分析:
题目集07这两道题是一种递进的关系,第一道题让我们制作一个图形卡片排序游戏,用到了大量的继承多态的方法,主要考察目的也是继承多态方法的使用,需要我们设计一个图形类,然后让其他圆类,矩形类,三角类等类继承于图形类,然后都有同样的求面积方法,这样就能实现继承和多态,第二道题在此基础上加了一些其他的东西,比如要求我们用到了接口,通过接口去对面积进行排序求和等操作,总之呢第二题是第一题的一个进阶版,需要操作的东西更多一些,用到接口,对面积进行操作。
题目集08 ATM机类结构设计(一)
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。 - 查询余额功能输入数据格式:
卡号
输出格式:
①输入错误处理
- 如果输入卡号不存在,则输出
Sorry,this card does not exist.。 - 如果输入ATM机编号不存在,则输出
Sorry,the ATM's id is wrong.。 - 如果输入银行卡密码错误,则输出
Sorry,your password is wrong.。 - 如果输入取款金额大于账户余额,则输出
Sorry,your account balance is insufficient.。 - 如果检测为跨行存取款,则输出
Sorry,cross-bank withdrawal is not supported.。
②取款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③存款业务输出
输出共两行,格式分别为:
[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。
此题源码如下:
1 import java.util.Scanner; 2 import java.util.regex.Pattern; 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 6 public class Main { 7 public static void main(String[] args) { 8 Agent agent = new Agent(); 9 agent.main(args); 10 /*ChinaUnionPay chi = new ChinaUnionPay(); 11 Scanner input = new Scanner(System.in); 12 String shuru = input.nextLine(); 13 String[] str = shuru.split("\\s+"); 14 if(!agent.isExistCard(chi,str[0] )) { 15 16 } 17 else 18 System.out.println("Sorry,this card does not exist.");*/ 19 } 20 } 21 22 class ChinaUnionPay { 23 private ArrayList<Bank> list = new ArrayList<Bank>(); 24 25 public void addBank(Bank bank) { 26 list.add(bank); 27 } 28 29 public void removeBank(Bank bank) { 30 list.remove(bank); 31 } 32 33 public ChinaUnionPay() { 34 super(); 35 // TODO 自动生成的构造函数存根 36 } 37 38 public ArrayList<Bank> getList() { 39 return list; 40 } 41 42 public void setList(ArrayList<Bank> list) { 43 this.list = list; 44 } 45 46 public ChinaUnionPay(ArrayList<Bank> list) { 47 super(); 48 this.list = list; 49 } 50 51 } 52 53 class Bank { 54 private String Name; 55 private ArrayList<User> list = new ArrayList<User>(); 56 private ArrayList<ATM> list2 = new ArrayList<ATM>(); 57 58 public Bank() { 59 super(); 60 // TODO 自动生成的构造函数存根 61 } 62 63 public String getName() { 64 return Name; 65 } 66 67 public void setName(String name) { 68 Name = name; 69 } 70 71 public ArrayList<User> getList() { 72 return list; 73 } 74 75 public void setList(ArrayList<User> list) { 76 this.list = list; 77 } 78 79 public ArrayList<ATM> getList2() { 80 return list2; 81 } 82 83 public void setList2(ArrayList<ATM> list2) { 84 this.list2 = list2; 85 } 86 87 public Bank(String name, ArrayList<User> list, ArrayList<ATM> list2) { 88 super(); 89 Name = name; 90 this.list = list; 91 this.list2 = list2; 92 } 93 94 public void addUser(User user) { 95 list.add(user); 96 } 97 98 public void removeUser(User user) { 99 list.remove(user); 100 } 101 102 public void addATM(ATM atm) { 103 list2.add(atm); 104 } 105 106 public void removeATM(ATM atm) { 107 list2.remove(atm); 108 } 109 } 110 111 class User { 112 public String getName() { 113 return name; 114 } 115 116 public void setName(String name) { 117 this.name = name; 118 } 119 120 public ArrayList<Account> getList() { 121 return list; 122 } 123 124 public void setList(ArrayList<Account> list) { 125 this.list = list; 126 } 127 128 private String name; 129 private ArrayList<Account> list = new ArrayList<Account>(); 130 131 public void addAccount(Account account) { 132 list.add(account); 133 } 134 135 public void removeAccount(Account account) { 136 list.remove(account); 137 } 138 139 public User() { 140 super(); 141 // TODO 自动生成的构造函数存根 142 } 143 144 public User(String name, ArrayList<Account> list) { 145 super(); 146 this.name = name; 147 this.list = list; 148 } 149 } 150 151 class Account { 152 private String ID; 153 154 public ArrayList<Card> getList() { 155 return list; 156 } 157 158 public void setList(ArrayList<Card> list) { 159 this.list = list; 160 } 161 162 private ArrayList<Card> list = new ArrayList<Card>(); 163 164 public void addCard(Card card) { 165 list.add(card); 166 } 167 168 public void removeCard(Card card) { 169 list.add(card); 170 } 171 172 public Account() { 173 super(); 174 // TODO 自动生成的构造函数存根 175 } 176 177 public Account(String iD, ArrayList<Card> list) { 178 super(); 179 ID = iD; 180 this.list = list; 181 } 182 183 public String getID() { 184 return ID; 185 } 186 187 public void setID(String iD) { 188 ID = iD; 189 } 190 191 } 192 193 class Card { 194 private String number; 195 private String cipher; 196 private String BankID; 197 public String getBankID() { 198 return BankID; 199 } 200 201 public void setBankID(String bankID) { 202 BankID = bankID; 203 } 204 205 private double value; 206 207 public Card() { 208 super(); 209 // TODO 自动生成的构造函数存根 210 } 211 212 213 214 public String getNumber() { 215 return number; 216 } 217 218 public void setNumber(String number) { 219 this.number = number; 220 } 221 222 public String getCipher() { 223 return cipher; 224 } 225 226 public void setCipher(String cipher) { 227 this.cipher = cipher; 228 } 229 230 public double getValue() { 231 return value; 232 } 233 234 public void setValue(double value) { 235 this.value = value; 236 } 237 238 public Card(String number, String bankID, String cipher, double value) { 239 super(); 240 this.number = number; 241 this.cipher = cipher; 242 BankID = bankID; 243 this.value = value; 244 } 245 246 } 247 248 class ATM { 249 private String ID; 250 private String BankID; 251 private Card card = new Card(); 252 253 254 public String getID() { 255 return ID; 256 } 257 258 public void setID(String iD) { 259 ID = iD; 260 } 261 262 public Card getCard() { 263 return card; 264 } 265 266 public void setCard(Card card) { 267 this.card = card; 268 } 269 270 public void dealmoney(Card card, double money) { 271 card.setValue(card.getValue() - money); 272 } 273 274 public ATM() { 275 super(); 276 // TODO 自动生成的构造函数存根 277 } 278 279 public String getBankID() { 280 return BankID; 281 } 282 283 public void setBankID(String bankID) { 284 BankID = bankID; 285 } 286 public boolean isATMRight() { 287 if(this.BankID == this.card.getBankID()) 288 return true; 289 else 290 return false; 291 } 292 } 293 class Agent { 294 295 public void main(String[] args) { 296 // 银联 297 ChinaUnionPay chi = new ChinaUnionPay(); 298 // 银行 299 Bank bank1 = new Bank(); 300 Bank bank2 = new Bank(); 301 bank1.setName("中国建设银行"); 302 bank2.setName("中国工商银行"); 303 chi.addBank(bank1); 304 chi.addBank(bank2); 305 // ATM 306 ATM atm1 = new ATM(); 307 atm1.setID("01"); 308 atm1.setBankID(bank1.getName()); 309 ATM atm2 = new ATM(); 310 atm2.setID("02"); 311 atm2.setBankID(bank1.getName()); 312 ATM atm3 = new ATM(); 313 atm3.setID("03"); 314 atm3.setBankID(bank1.getName()); 315 ATM atm4 = new ATM(); 316 atm4.setID("04"); 317 atm4.setBankID(bank1.getName()); 318 bank1.addATM(atm1); 319 bank1.addATM(atm2); 320 bank1.addATM(atm3); 321 bank1.addATM(atm4); 322 ATM atm5 = new ATM(); 323 atm5.setID("05"); 324 atm5.setBankID(bank2.getName()); 325 ATM atm6 = new ATM(); 326 atm6.setID("06"); 327 atm6.setBankID(bank2.getName()); 328 bank2.addATM(atm5); 329 bank2.addATM(atm6); 330 // 用户 331 User user1 = new User(); 332 user1.setName("杨过"); 333 User user2 = new User(); 334 user2.setName("郭靖"); 335 User user3 = new User(); 336 user3.setName("张无忌"); 337 User user4 = new User(); 338 user4.setName("韦小宝"); 339 bank1.addUser(user1); 340 bank1.addUser(user2); 341 bank2.addUser(user3); 342 bank2.addUser(user4); 343 // 账户 344 Account account1 = new Account(); 345 account1.setID("3217000010041315709"); 346 Account account2 = new Account(); 347 account2.setID("3217000010041315715"); 348 user1.addAccount(account1); 349 user1.addAccount(account2); 350 Account account3 = new Account(); 351 account3.setID("3217000010051320007"); 352 user2.addAccount(account3); 353 Account account4 = new Account(); 354 account4.setID("3222081502001312389"); 355 Account account5 = new Account(); 356 account5.setID("3222081502001312390"); 357 Account account6 = new Account(); 358 account6.setID("3222081502001312399"); 359 user3.addAccount(account4); 360 user3.addAccount(account5); 361 user3.addAccount(account6); 362 Account account7 = new Account(); 363 account7.setID("3222081502051320785"); 364 Account account8 = new Account(); 365 account8.setID("3222081502051320786"); 366 user4.addAccount(account7); 367 user4.addAccount(account8); 368 // 银行卡 369 Card card1 = new Card("6217000010041315709",bank1.getName(), "88888888", 10000); 370 Card card2 = new Card("6217000010041315715",bank1.getName(), "88888888", 10000); 371 account1.addCard(card1); 372 account1.addCard(card2); 373 Card card3 = new Card("6217000010041315718",bank1.getName(), "88888888", 10000); 374 account2.addCard(card3); 375 Card card4 = new Card("6217000010051320007",bank1.getName(), "88888888", 10000); 376 account3.addCard(card4); 377 Card card5 = new Card("6222081502001312389",bank2.getName(), "88888888", 10000); 378 account4.addCard(card5); 379 Card card6 = new Card("6222081502001312390",bank2.getName(), "88888888", 10000); 380 account5.addCard(card6); 381 Card card7 = new Card("6222081502001312399", bank2.getName(),"88888888", 10000); 382 Card card8 = new Card("6222081502001312400", bank2.getName(),"88888888", 10000); 383 account6.addCard(card7); 384 account6.addCard(card8); 385 Card card9 = new Card("6222081502051320785",bank2.getName(), "88888888", 10000); 386 account7.addCard(card9); 387 Card card10 = new Card("6222081502051320786",bank2.getName(), "88888888", 10000); 388 account8.addCard(card10); 389 390 //处理数据 391 Scanner input = new Scanner(System.in); 392 String shuru = input.nextLine(); 393 String[] str = shuru.split("\\s+"); 394 if(!isExistCard(chi,str[0])) { 395 System.out.println("Sorry,this card does not exist."); 396 System.exit(0); 397 } 398 if(!isExistPassword(chi,str[1])) { 399 System.out.println("Sorry,your password is wrong."); 400 System.exit(0); 401 } 402 if(!Pattern.matches("[0][1-6]", str[2])){ 403 System.out.println("Sorry,the ATM's id is wrong."); 404 System.exit(0); 405 } 406 //阻断 407 for (int i = 0; i < chi.getList().size(); i++) { 408 for (int j = 0; j < chi.getList().get(i).getList().size(); j++) { 409 for (int k = 0; k < chi.getList().get(i).getList().get(j).getList().size(); k++) { 410 for (int l = 0; l < chi.getList().get(i).getList().get(j).getList().get(k).getList().size(); l++) { 411 if (chi.getList().get(i).getList().get(j).getList().get(k).getList().get(l).getNumber() 412 .equals(str[0])) { 413 //判断取钱大小 414 if(chi.getList().get(i).getList().get(j).getList().get(k) 415 .getList().get(l).getValue() < Double.parseDouble(str[3])) { 416 System.out.println("Sorry,your account balance is insufficient."); 417 System.exit(0); 418 } 419 420 421 422 423 for (int a = 0; a < chi.getList().size(); a++) { 424 for (int b = 0; b < chi.getList().get(a).getList2().size(); b++) { 425 426 if (str[2].equals(chi.getList().get(a).getList2().get(b).getID())) { 427 if(chi.getList().get(i).getList().get(j).getList().get(k).getList().get(l).getBankID() != chi.getList().get(a).getList2().get(b).getBankID()) { 428 System.out.println("Sorry,cross-bank withdrawal is not supported."); 429 System.exit(0); 430 } 431 } 432 433 434 chi.getList().get(a).getList2().get(b).dealmoney( 435 chi.getList().get(i).getList().get(j).getList().get(k).getList().get(l), 436 Double.parseDouble(str[3])); 437 if(Double.parseDouble(str[3]) < 0) { 438 System.out.println(chi.getList().get(i).getList().get(j).getName()+"在"+chi.getList().get(i).getName()+"的"+str[2]+"号ATM机上存款"+"¥"+String.format("%.2f", Math.abs(Double.parseDouble(str[3]))).toString()); 439 System.out.println("当前余额为¥"+String.format("%.2f", Math.abs(chi.getList().get(i).getList().get(j).getList().get(k) 440 .getList().get(l).getValue())).toString()); 441 System.exit(0); 442 } 443 if(Double.parseDouble(str[3]) > 0) { 444 System.out.println(chi.getList().get(i).getList().get(j).getName()+"在"+chi.getList().get(i).getName()+"的"+str[2]+"号ATM机上取款"+"¥"+String.format("%.2f", Math.abs(Double.parseDouble(str[3]))).toString()); 445 System.out.println("当前余额为¥"+String.format("%.2f", Math.abs(chi.getList().get(i).getList().get(j).getList().get(k) 446 .getList().get(l).getValue())).toString()); 447 System.exit(0); 448 } 449 450 451 } 452 } 453 454 455 } 456 } 457 } 458 } 459 } 460 461 462 463 464 465 } 466 public boolean isExistCard(ChinaUnionPay chi,String ID) { 467 Iterator<Bank> banki = chi.getList().iterator(); 468 while(banki.hasNext()){ 469 Iterator<User> useri = banki.next().getList().iterator(); 470 while(useri.hasNext()){ 471 Iterator<Account> acci = useri.next().getList().iterator(); 472 while(acci.hasNext()){ 473 Iterator<Card> cardi = acci.next().getList().iterator(); 474 while(cardi.hasNext()){ 475 if(ID.equals(cardi.next().getNumber())){ 476 return true; 477 } 478 } 479 } 480 } 481 } 482 return false; 483 } 484 public boolean isExistPassword(ChinaUnionPay chi,String mima) { 485 Iterator<Bank> banki = chi.getList().iterator(); 486 while(banki.hasNext()){ 487 Iterator<User> useri = banki.next().getList().iterator(); 488 while(useri.hasNext()){ 489 Iterator<Account> acci = useri.next().getList().iterator(); 490 while(acci.hasNext()){ 491 Iterator<Card> cardi = acci.next().getList().iterator(); 492 while(cardi.hasNext()){ 493 if(mima.equals(cardi.next().getCipher())){ 494 return true; 495 } 496 } 497 } 498 } 499 } 500 return false; 501 } 502 503 504 public Agent() { 505 super(); 506 // TODO 自动生成的构造函数存根 507 } 508 509 510 511 } 512 513 514 /*public boolean isExistCard(ChinaUnionPay chi,String ID){ 515 Iterator<Bank> banki = chi.getList().iterator(); 516 while(banki.hasNext()){ 517 Iterator<User> useri = banki.next().getList().iterator(); 518 while(useri.hasNext()){ 519 Iterator<Account> acci = useri.next().getList().iterator(); 520 while(acci.hasNext()){ 521 Iterator<Card> cardi = acci.next().getList().iterator(); 522 while(cardi.hasNext()){ 523 if(ID.equals(cardi.next().getNumber())){ 524 return true; 525 } 526 } 527 } 528 } 529 } 530 return false; 531 }*/ 532 533 534 535 /*while(input.next().equals("#")) { 536 for (int i = 0; i < chi.getList().size(); i++) { 537 for (int j = 0; j < chi.getList().get(i).getList().size(); j++) { 538 for (int k = 0; k < chi.getList().get(i).getList().get(j).getList().size(); k++) { 539 for (int l = 0; l < chi.getList().get(i).getList().get(j).getList().get(k).getList().size(); l++) { 540 541 if (chi.getList().get(i).getList().get(j).getList().get(k).getList().get(l).getNumber() 542 .equals(str[0])) { 543 544 int[] position = { i, j, k, l }; 545 if (!str[1].equals(chi.getList().get(position[0]).getList().get(position[1]).getList() 546 .get(position[2]).getList().get(position[3]).getCipher())) { 547 System.out.println("Sorry,your password is wrong."); 548 System.exit(0); 549 } 550 551 } 552 if(!Pattern.matches("[0][1-6]", str[2])){ 553 System.out.println("Sorry,the ATM's id is wrong."); 554 System.exit(0); 555 } 556 /* int f = 0; 557 Iterator<Bank>banki = chi.getList().iterator(); 558 while(banki.hasNext()) { 559 f++; 560 if(banki.next().getList2().get(f).equals(str[2])) { 561 System.out.println("Sorry,cross-bank withdrawal is not supported."); 562 System.exit(0); 563 } 564 }*/ 565 /*for (int a = 0; a < chi.getList().size(); a++) { 566 for (int b = 0; b < chi.getList().get(a).getList2().size(); b++) { 567 568 if (str[2].equals(chi.getList().get(a).getList2().get(b).getID())) { 569 570 if(chi.getList().get(i).getList().get(j).getList().get(k) 571 .getList().get(l).getValue() < Double.parseDouble(str[3])) { 572 System.out.println("Sorry,your account balance is insufficient."); 573 System.exit(0); 574 } 575 576 577 chi.getList().get(a).getList2().get(b).dealmoney( 578 chi.getList().get(i).getList().get(j).getList().get(k).getList().get(l), 579 Double.parseDouble(str[3])); 580 System.out.println(chi.getList().get(i).getList().get(j).getName()+"在"+chi.getList().get(i).getName()+"的"+str[2]+"号ATM机上存款"+"¥"+Math.abs(Double.parseDouble(str[3]))); 581 System.out.println("当前余额为¥"+String.format("%.2f", Math.abs(chi.getList().get(i).getList().get(j).getList().get(k) 582 .getList().get(l).getValue())).toString()); 583 System.exit(0); 584 /*chi.getList().get(i).getList().get(j).getList().get(k) 585 .getList().get(l).getValue()*/ 586 /* } 587 588 589 590 } 591 } 592 593 } 594 } 595 } 596 } 597 598 System.out.println("Sorry,this card does not exist."); 599 } 600 } 601 // } 602 /* 603 * for(int i = 0 ; i < chi.getList().size();i++) { for(int j = 0; j < 604 * chi.getList().get(i).getList2().size();j++) { 605 * if(str[2].equals(chi.getList().get(i).getList2().get(j).getID())) { int [] 606 * position2 = {i,j}; 607 * chi.getList().get(i).getList2().get(j).dealmoney(chi.getList().get(i).getList 608 * ().get(j).getList().get(k).getList().get(l), j); } } } 609 */ 610 611 /* 612 * Iterator<Bank>banki = chi.getList().iterator(); while(banki.hasNext()) { 613 * Iterator<User>useri = banki.next().getList().iterator(); 614 * while(useri.hasNext()) { Iterator<Account>acci = 615 * useri.next().getList().iterator(); while(useri.hasNext()) { 616 * Iterator<Card>cardi = acci.next().getList().iterator(); 617 * while(cardi.hasNext()) { if(!cardi.next().getNumber().equals(str[0]));{ 618 * System.out.println("Sorry,this card does not exist."); } } } } 619 * 620 * } 621 */ 622 623 /*if (!str[2].equals(chi.getList().get(a).getList2().get(b).getID())) { 624 System.out.println("Sorry,the ATM's id is wrong."); 625 System.exit(0); 626 }*/ 627 /*}*/
类图:

SourceMonitor的生成报表内容:


题目集09 ATM机类结构设计(二)
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 取款功能输入数据格式:
卡号 密码 ATM机编号 金额(由一个或多个空格分隔) - 查询余额功能输入数据格式:
卡号
输出格式:
①输入错误处理
- 如果输入卡号不存在,则输出
Sorry,this card does not exist.。 - 如果输入ATM机编号不存在,则输出
Sorry,the ATM's id is wrong.。 - 如果输入银行卡密码错误,则输出
Sorry,your password is wrong.。 - 如果输入取款金额大于账户余额,则输出
Sorry,your account balance is insufficient.。
②取款业务输出
输出共两行,格式分别为:
业务:取款 [用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③查询余额业务输出
业务:查询余额 ¥[金额]
金额保留两位小数。
此题代码如下:
1 import java.util.ArrayList; 2 import java.util.Scanner; 3 import java.util.Iterator; 4 import java.util.regex.Matcher; 5 import java.util.regex.Pattern; 6 public class Main { 7 public static void main(String[] args) { 8 Scanner input = new Scanner(System.in); 9 10 UnionPay unionPay = new UnionPay(); 11 12 Bank ccb = new Bank("1001","中国建设银行",0.02); 13 Bank icbc = new Bank("1002","中国工商银行",0.03); 14 Bank abc = new Bank("1003","中国农业银行",0.04); 15 unionPay.addBank(ccb); 16 unionPay.addBank(icbc); 17 unionPay.addBank(abc); 18 19 ATM aTM1 = new ATM("01",ccb); 20 ATM aTM2 = new ATM("02",ccb); 21 ATM aTM3 = new ATM("03",ccb); 22 ATM aTM4 = new ATM("04",ccb); 23 ATM aTM5 = new ATM("05",icbc); 24 ATM aTM6 = new ATM("06",icbc); 25 ATM aTM7 = new ATM("07",abc); 26 ATM aTM8 = new ATM("08",abc); 27 ATM aTM9 = new ATM("09",abc); 28 ATM aTM10 = new ATM("10",abc); 29 ATM aTM11 = new ATM("11",abc); 30 31 ccb.addATM(aTM1); 32 ccb.addATM(aTM2); 33 ccb.addATM(aTM3); 34 ccb.addATM(aTM4); 35 icbc.addATM(aTM5); 36 icbc.addATM(aTM6); 37 abc.addATM(aTM7); 38 abc.addATM(aTM8); 39 abc.addATM(aTM9); 40 abc.addATM(aTM10); 41 abc.addATM(aTM11); 42 43 44 User Yangguo = new User("杨过"); 45 User Guojing = new User("郭靖"); 46 User Zhangwuji = new User("张无忌"); 47 User Weixiaobao = new User("韦小宝"); 48 User zhangsanfeng = new User("张三丰"); 49 User linghuchong = new User("令狐冲"); 50 User qiaofeng = new User("乔峰"); 51 User hongqigong = new User("洪七公"); 52 53 Account ccbAcc1 = new Account("3217000010041315709",10000.00,Yangguo,ccb,"jieji"); 54 Account ccbAcc2 = new Account("3217000010041315715",10000.00,Yangguo,ccb,"jieji"); 55 Account ccbAcc3 = new Account("3217000010051320007",10000.00,Guojing,ccb,"jieji"); 56 Account ccbAcc4 = new Account("3640000010045442002",10000.00,zhangsanfeng,ccb,"daiji"); 57 Account icbcAcc1 = new Account("3222081502001312389",10000.00,Zhangwuji,icbc,"jieji"); 58 Account icbcAcc2 = new Account("3222081502001312390",10000.00,Zhangwuji,icbc,"jieji"); 59 Account icbcAcc3 = new Account("3222081502001312399",10000.00,Zhangwuji,icbc,"jieji"); 60 Account icbcAcc4 = new Account("3222081502051320785",10000.00,Weixiaobao,icbc,"jieji"); 61 Account icbcAcc5 = new Account("3222081502051320786",10000.00,Weixiaobao,icbc,"jieji"); 62 Account icbcAcc6 = new Account("3640000010045441009",10000.00,linghuchong,icbc,"daiji"); 63 Account abcAcc1 = new Account("3630000010033431001",10000.00,qiaofeng,abc,"daiji"); 64 Account abcAcc2 = new Account("3630000010033431008",10000.00,hongqigong,abc,"daiji"); 65 66 ccb.addAccount(ccbAcc1); 67 ccb.addAccount(ccbAcc2); 68 ccb.addAccount(ccbAcc3); 69 ccb.addAccount(ccbAcc4); 70 icbc.addAccount(icbcAcc1); 71 icbc.addAccount(icbcAcc2); 72 icbc.addAccount(icbcAcc3); 73 icbc.addAccount(icbcAcc4); 74 icbc.addAccount(icbcAcc5); 75 icbc.addAccount(icbcAcc6); 76 abc.addAccount(abcAcc1); 77 abc.addAccount(abcAcc2); 78 79 Yangguo.addAccount(ccbAcc1); 80 Yangguo.addAccount(ccbAcc2); 81 Guojing.addAccount(ccbAcc3); 82 Zhangwuji.addAccount(icbcAcc1); 83 Zhangwuji.addAccount(icbcAcc2); 84 Zhangwuji.addAccount(icbcAcc3); 85 Weixiaobao.addAccount(icbcAcc4); 86 Weixiaobao.addAccount(icbcAcc5); 87 zhangsanfeng.addAccount(ccbAcc4); 88 linghuchong.addAccount(icbcAcc6); 89 qiaofeng.addAccount(abcAcc1); 90 hongqigong.addAccount(abcAcc2); 91 92 Card ccbCard1 = new Card("6217000010041315709","88888888",ccbAcc1); 93 Card ccbCard2 = new Card("6217000010041315715","88888888",ccbAcc1); 94 Card ccbCard3 = new Card("6217000010041315718","88888888",ccbAcc2); 95 Card ccbCard4 = new Card("6217000010051320007","88888888",ccbAcc3); 96 Card icbcCard5 = new Card("6222081502001312389","88888888",icbcAcc1); 97 Card icbcCard6 = new Card("6222081502001312390","88888888",icbcAcc2); 98 Card icbcCard7 = new Card("6222081502001312399","88888888",icbcAcc3); 99 Card icbcCard8 = new Card("6222081502001312400","88888888",icbcAcc3); 100 Card icbcCard9 = new Card("6222081502051320785","88888888",icbcAcc4); 101 Card icbcCard10 = new Card("6222081502051320786","88888888",icbcAcc5); 102 Card ccbCard11 = new Card("6640000010045442002","88888888",ccbAcc4); 103 Card ccbCard12 = new Card("6640000010045442003","88888888",ccbAcc4); 104 Card icbcCard13 = new Card("6640000010045441009","88888888",icbcAcc6); 105 Card abcCard14 = new Card("6630000010033431001","88888888",abcAcc1); 106 Card abcCard15 = new Card("6630000010033431008","88888888",abcAcc2); 107 108 ccbAcc1.addCard(ccbCard1); 109 ccbAcc1.addCard(ccbCard2); 110 ccbAcc2.addCard(ccbCard3); 111 ccbAcc3.addCard(ccbCard4); 112 ccbAcc4.addCard(ccbCard11); 113 ccbAcc4.addCard(ccbCard12); 114 icbcAcc1.addCard(icbcCard5); 115 icbcAcc2.addCard(icbcCard6); 116 icbcAcc3.addCard(icbcCard7); 117 icbcAcc3.addCard(icbcCard8); 118 icbcAcc4.addCard(icbcCard9); 119 icbcAcc5.addCard(icbcCard10); 120 icbcAcc6.addCard(icbcCard13); 121 abcAcc1.addCard(abcCard14); 122 abcAcc1.addCard(abcCard15); 123 124 StringBuilder sb = new StringBuilder(); 125 126 String data; 127 128 while(!((data = input.nextLine()).equals("#"))) { 129 sb.append(data + "\n"); 130 } 131 132 String[] dt = sb.toString().split("\n"); 133 for(int i = 0; i < dt.length; i++) { 134 String[] dataLine = dt[i].toString().split("\\s+"); 135 136 if(dataLine.length == 1) { 137 GetBalance gb = new GetBalance(unionPay); 138 System.out.println(String.format("业务:查询余额 ¥%.2f", gb.getBalance(dataLine[0]))); 139 }else { 140 Withdraw wd = new 141 Withdraw(unionPay,dataLine[0],dataLine[1],dataLine[2],Double.parseDouble(dataLine[3])); 142 wd.withdraw(); 143 } 144 } 145 } 146 147 148 149 } 150 151 152 class Withdraw { 153 154 private UnionPay unionPay; 155 private String cardNO; 156 private String cardPassword; 157 private String ATMID; 158 private double amount; 159 160 public Withdraw() { 161 super(); 162 163 } 164 165 166 public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) { 167 super(); 168 this.unionPay = unionPay; 169 this.cardNO = cardNO; 170 this.cardPassword = cardPassword; 171 ATMID = aTMID; 172 this.amount = amount; 173 } 174 175 public void withdraw() { 176 /** 177 * 校验该卡是否存在 178 */ 179 if(!ValidateData.cardCheck(unionPay, cardNO)) { 180 System.out.println("Sorry,this card does not exist."); 181 System.exit(0); 182 } 183 /** 184 * 校验ATM是否存在 185 */ 186 187 if(!ValidateData.checkATMbyATMID(unionPay, ATMID)) { 188 System.out.println("Sorry,the ATM's id is wrong."); 189 System.exit(0); 190 } 191 /** 192 * 校验卡密码是否正确 193 */ 194 if(!ValidateData.checkpassword(unionPay, cardNO, cardPassword)) { 195 System.out.println("Sorry,your password is wrong."); 196 System.exit(0); 197 } 198 199 //获取卡对应的账户 200 Account account = Get.gatAccountbyCardNO(unionPay, cardNO); 201 boolean overbalance = ValidateData.checkbalance(unionPay, cardNO, amount); 202 boolean overbank = ValidateData.checkBank(unionPay, cardNO, ATMID); 203 boolean jieji = Get.gatAccountbyCardNO(unionPay, cardNO).getAccountType().equals("jieji"); 204 205 //跨银行且超额取款 206 if(overbank && overbalance) { 207 double charge ; 208 if(account.getBalance() < 0) 209 charge = Get.getBankbyATMID(unionPay, ATMID).getRate()*amount + amount*0.05; 210 else 211 charge = Get.getBankbyATMID(unionPay, ATMID).getRate()*amount + (amount - account.getBalance())*0.05; 212 account.setBalance(account.getBalance() - amount - charge); 213 } 214 //超额取款 215 else if(!overbank && overbalance) { 216 double charge ; 217 if(account.getBalance() < 0) 218 charge = amount*0.05; 219 else 220 charge = (amount - account.getBalance())*0.05; 221 account.setBalance(account.getBalance() - amount - charge); 222 } 223 //跨银行取款 224 else if(overbank && !overbalance) { 225 double charge = Get.getBankbyATMID(unionPay, ATMID).getRate()*amount; 226 account.setBalance(account.getBalance() - amount - charge); 227 } 228 //普通取款 229 else 230 account.setBalance(account.getBalance() - amount); 231 232 //透支借贷 或 借记卡超额取款 233 if(account.getBalance() < -50000 || (jieji&&(account.getBalance()<0))) { 234 System.out.println("Sorry,your account balance is insufficient."); 235 System.exit(0); 236 } 237 238 if(amount >= 0) { 239 showResult(account,ATMID,1); 240 }else { 241 showResult(account,ATMID,0); 242 } 243 244 } 245 246 public void showResult(Account account, String ATMID,int flag) { 247 String type = ""; 248 if(flag == 1) { 249 type = "取款"; 250 }else { 251 type = "存款"; 252 amount *= -1; 253 } 254 String userName = account.getUser().getName(); 255 String bankName = Get.getBankbyATMID(unionPay, ATMID).getBankName(); 256 System.out.println("业务:"+ type +" " +userName + "在" + 257 bankName + "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount)); 258 System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance())); 259 } 260 261 } 262 263 class GetBalance { 264 private UnionPay unionPay; 265 266 public GetBalance() { 267 super(); 268 269 } 270 271 public GetBalance(UnionPay unionPay) { 272 super(); 273 this.unionPay = unionPay; 274 } 275 276 public double getBalance(String cardNO) { 277 278 return Get.gatAccountbyCardNO(unionPay, cardNO).getBalance(); 279 280 } 281 } 282 283 class ValidateData { 284 285 // 检验卡号是否存在 286 public static boolean cardCheck(UnionPay unionPay, String cardNO) { 287 Card card = null; 288 ArrayList<Card> cardList = unionPay.getCardList(); 289 Iterator<Card> cardItr = cardList.iterator(); 290 while (cardItr.hasNext()) { 291 card = cardItr.next(); 292 if (card.getCardNO().equals(cardNO)) { 293 return true; 294 } 295 } 296 return false; 297 } 298 299 // 检验ATM机是否存在 300 public static boolean checkATMbyATMID(UnionPay unionPay, String ATMID) { 301 ATM aTM = null; 302 Iterator<ATM> aTMItr = unionPay.getATMList().iterator(); 303 304 while (aTMItr.hasNext()) { 305 aTM = aTMItr.next(); 306 if (aTM.getATMID().equals(ATMID)) { 307 return true; 308 } 309 } 310 return false; 311 } 312 313 // 检验是否跨银行存取 314 public static boolean checkBank(UnionPay unionPay, String cardNO, String ATMID) { 315 316 String bankname1 = Get.gatAccountbyCardNO(unionPay, cardNO).getBank().getBankName(); 317 String bankname2 = Get.getBankbyATMID(unionPay, ATMID).getBankName(); 318 if (bankname1.equals(bankname2)) 319 return false; 320 else 321 return true; 322 323 } 324 325 // 检验是否超额取款 326 public static boolean checkbalance(UnionPay unionPay, String cardNO, double amount) { 327 328 //获取卡对应的账户 329 Account account = Get.gatAccountbyCardNO(unionPay, cardNO); 330 if(account.getBalance() < amount) 331 return true; 332 else 333 return false; 334 } 335 336 // 检验密码是否输入正确 337 public static boolean checkpassword(UnionPay unionPay, String cardNO, String password) { 338 Iterator<Card> cardItr = unionPay.getCardList().iterator(); 339 340 while (cardItr.hasNext()) { 341 if(cardItr.next().getCardNO().equals(cardNO) && cardItr.next().getCardPassword().equals(password)) 342 return true; 343 } 344 return false; 345 } 346 347 } 348 349 class Get{ 350 351 //通过卡号获取卡 352 public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) { 353 Card card = null; 354 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 355 356 while(bankItr.hasNext()) { 357 ArrayList<Account> accountList = bankItr.next().getAccountList(); 358 Iterator<Account> accountItr = accountList.iterator(); 359 while(accountItr.hasNext()) { 360 ArrayList<Card> cardList = accountItr.next().getList(); 361 Iterator<Card> cardItr = cardList.iterator(); 362 while(cardItr.hasNext()) { 363 card = cardItr.next(); 364 if(card.getCardNO().equals(cardNO)) { 365 return card; 366 } 367 } 368 } 369 } 370 return null; 371 } 372 //通过卡号获取账户 373 public static Account gatAccountbyCardNO(UnionPay unionPay,String cardNO) { 374 Card card = null; 375 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 376 377 while(bankItr.hasNext()) { 378 ArrayList<Account> accountList = bankItr.next().getAccountList(); 379 Iterator<Account> accountItr = accountList.iterator(); 380 while(accountItr.hasNext()) { 381 ArrayList<Card> cardList = accountItr.next().getList(); 382 Iterator<Card> cardItr = cardList.iterator(); 383 while(cardItr.hasNext()) { 384 card = cardItr.next(); 385 if(card.getCardNO().equals(cardNO)) { 386 return card.getAccount(); 387 } 388 } 389 } 390 } 391 return null; 392 } 393 394 //通过ATM机编号获取银行 395 public static Bank getBankbyATMID(UnionPay unionPay,String ATMID) { 396 Iterator<Bank> bankItr = unionPay.getBankList().iterator(); 397 Bank bank = null; 398 ATM aTM = null; 399 400 while(bankItr.hasNext()) { 401 bank = bankItr.next(); 402 Iterator<ATM> aTMItr = bank.getATMList().iterator(); 403 404 while(aTMItr.hasNext()) { 405 aTM = aTMItr.next(); 406 if(aTM.getATMID().equals(ATMID)) { 407 return aTM.getBank(); 408 } 409 } 410 } 411 return null; 412 } 413 } 414 415 416 class UnionPay { 417 private ArrayList<Bank> bankList = new ArrayList<Bank>(); 418 419 420 public UnionPay() { 421 super(); 422 } 423 424 public UnionPay(ArrayList<Bank> bankList) { 425 super(); 426 this.bankList = bankList; 427 } 428 429 430 public ArrayList<Bank> getBankList() { 431 return bankList; 432 } 433 434 public void setBankList(ArrayList<Bank> bankList) { 435 this.bankList = bankList; 436 } 437 438 public void addBank(Bank bank) { 439 this.bankList.add(bank); 440 } 441 442 public void removeBank(Bank bank) { 443 this.bankList.remove(bank); 444 } 445 446 447 public ArrayList<Account> getAccountList() { 448 ArrayList<Account> AccountList = new ArrayList<Account>(); 449 Account account = null; 450 451 Iterator<Bank> bankItr = getBankList().iterator(); 452 while (bankItr.hasNext()) { 453 ArrayList<Account> accountList = bankItr.next().getAccountList(); 454 Iterator<Account> accountItr = accountList.iterator(); 455 while (accountItr.hasNext()) { 456 account = accountItr.next(); 457 AccountList.add(account); 458 } 459 } 460 461 return AccountList; 462 } 463 464 465 public ArrayList<Card> getCardList() { 466 ArrayList<Card> CardList = new ArrayList<Card>(); 467 Card card = null; 468 469 Iterator<Bank> bankItr = getBankList().iterator(); 470 while (bankItr.hasNext()) { 471 ArrayList<Account> accountList = bankItr.next().getAccountList(); 472 Iterator<Account> accountItr = accountList.iterator(); 473 while (accountItr.hasNext()) { 474 ArrayList<Card> cardList = accountItr.next().getList(); 475 Iterator<Card> cardItr = cardList.iterator(); 476 while (cardItr.hasNext()) { 477 card = cardItr.next(); 478 CardList.add(card); 479 } 480 } 481 } 482 483 return CardList; 484 } 485 486 487 public ArrayList<ATM> getATMList() { 488 ArrayList<ATM> ATMList = new ArrayList<ATM>(); 489 490 Iterator<Bank> bankItr = getBankList().iterator(); 491 Bank bank = null; 492 ATM aTM = null; 493 while (bankItr.hasNext()) { 494 bank = bankItr.next(); 495 Iterator<ATM> aTMItr = bank.getATMList().iterator(); 496 497 while (aTMItr.hasNext()) { 498 aTM = aTMItr.next(); 499 ATMList.add(aTM); 500 } 501 } 502 503 return ATMList; 504 505 } 506 } 507 508 class Bank { 509 private String bankNO; 510 private String bankName; 511 private double rate;//手续费 512 private ArrayList<Account> accountList = new ArrayList<Account>(); 513 private static ArrayList<ATM> ATMList = new ArrayList<ATM>(); 514 515 516 public Bank() { 517 super(); 518 } 519 520 public Bank(String bankNO, String bankName, double rate) { 521 super(); 522 this.bankNO = bankNO; 523 this.bankName = bankName; 524 this.rate = rate; 525 } 526 527 528 public String getBankNO() { 529 return bankNO; 530 } 531 532 public void setBankNO(String bankNO) { 533 this.bankNO = bankNO; 534 } 535 536 public String getBankName() { 537 return bankName; 538 } 539 540 public void setBankName(String bankName) { 541 this.bankName = bankName; 542 } 543 544 public void addAccount(Account account) { 545 this.accountList.add(account); 546 } 547 548 public void removeAccount(Account account) { 549 this.accountList.remove(account); 550 } 551 552 public void addATM(ATM aTM) { 553 this.ATMList.add(aTM); 554 } 555 556 public void removeATM(ATM aTM) { 557 this.ATMList.remove(aTM); 558 } 559 560 public ArrayList<Account> getAccountList() { 561 return accountList; 562 } 563 564 public void setAccountList(ArrayList<Account> accountList) { 565 this.accountList = accountList; 566 } 567 568 public static ArrayList<ATM> getATMList() { 569 return ATMList; 570 } 571 572 public void setATMList(ArrayList<ATM> aTMList) { 573 ATMList = aTMList; 574 } 575 576 public double getRate() { 577 return rate; 578 } 579 580 public void setRate(double rate) { 581 this.rate = rate; 582 } 583 584 585 } 586 587 class User { 588 private String name; 589 ArrayList<Account> list = new ArrayList<Account>(); 590 591 public User() { 592 super(); 593 } 594 595 public User(String name) { 596 super(); 597 this.name = name; 598 } 599 600 public String getName() { 601 return name; 602 } 603 604 public void setName(String name) { 605 this.name = name; 606 } 607 608 public void addAccount(Account account) { 609 this.list.add(account); 610 } 611 612 public void removeAccount(Account account) { 613 this.list.remove(account); 614 } 615 } 616 617 class Account { 618 private String accountNO; 619 private User user = null; 620 private Bank bank = null; 621 private double balance = 0; 622 private String accountType; 623 private static ArrayList<Card> list = new ArrayList<Card>(); 624 625 public Account() { 626 super(); 627 } 628 629 public Account(String accountNO, double balance, User user, Bank bank, String accountType) { 630 super(); 631 this.accountNO = accountNO; 632 this.balance = balance; 633 this.user = user; 634 this.bank = bank; 635 this.accountType = accountType; 636 } 637 638 public void addCard(Card card) { 639 list.add(card); 640 } 641 642 public void removeCard(Card card) { 643 list.remove(card); 644 } 645 646 public double getBalance() { 647 return balance; 648 } 649 650 public void setBalance(double balance) { 651 this.balance = balance; 652 } 653 654 public String getAccountNO() { 655 return accountNO; 656 } 657 658 public void setAccountNO(String accountNO) { 659 this.accountNO = accountNO; 660 } 661 662 public User getUser() { 663 return user; 664 } 665 666 public void setUser(User user) { 667 this.user = user; 668 } 669 670 public Bank getBank() { 671 return bank; 672 } 673 674 public void setBank(Bank bank) { 675 this.bank = bank; 676 } 677 678 public ArrayList<Card> getList() { 679 return list; 680 } 681 682 public void setList(ArrayList<Card> list) { 683 Account.list = list; 684 } 685 686 public String getAccountType() { 687 return accountType; 688 } 689 690 public void setAccountType(String accountType) { 691 this.accountType = accountType; 692 } 693 694 695 } 696 /*class CreditAccount extends Account{ 697 private String accountNO; 698 private double balance = 0; 699 private User user = null; 700 private Bank bank = null; 701 private static ArrayList<Card> list = new ArrayList<Card>(); 702 703 public CreditAccount() { 704 super(); 705 // TODO Auto-generated constructor stub 706 } 707 708 public CreditAccount(String accountNO, double balance, User user, Bank bank) { 709 super(); 710 this.accountNO = accountNO; 711 this.balance = balance; 712 this.user = user; 713 this.bank = bank; 714 } 715 716 public void addCard(Card card) { 717 list.add(card); 718 } 719 720 public void removeCard(Card card) { 721 list.remove(card); 722 } 723 724 public double getBalance() { 725 return balance; 726 } 727 728 public void setBalance(double balance) { 729 this.balance = balance; 730 } 731 732 public String getAccountNO() { 733 return accountNO; 734 } 735 736 public void setAccountNO(String accountNO) { 737 this.accountNO = accountNO; 738 } 739 740 public User getUser() { 741 return user; 742 } 743 744 public void setUser(User user) { 745 this.user = user; 746 } 747 748 public Bank getBank() { 749 return bank; 750 } 751 752 public void setBank(Bank bank) { 753 this.bank = bank; 754 } 755 756 public ArrayList<Card> getList() { 757 return list; 758 } 759 760 public void setList(ArrayList<Card> list) { 761 CreditAccount.list = list; 762 } 763 764 public static Account getAmountbyCardNO(String cardNO) { 765 Iterator<Card> cardItr = CreditAccount.list.iterator(); 766 Card card = null; 767 768 while(cardItr.hasNext()) { 769 card = cardItr.next(); 770 if(card.getCardNO().equals(cardNO)) { 771 return card.getAccount(); 772 } 773 } 774 775 return null; 776 } 777 778 779 } 780 class DebitAccount extends Account{ 781 private String accountNO; 782 private double balance = 0; 783 private User user = null; 784 private Bank bank = null; 785 private static ArrayList<Card> list = new ArrayList<Card>(); 786 787 public DebitAccount() { 788 super(); 789 // TODO Auto-generated constructor stub 790 } 791 792 public DebitAccount(String accountNO, double balance, User user, Bank bank) { 793 super(); 794 this.accountNO = accountNO; 795 this.balance = balance; 796 this.user = user; 797 this.bank = bank; 798 } 799 800 public void addCard(Card card) { 801 list.add(card); 802 } 803 804 public void removeCard(Card card) { 805 list.remove(card); 806 } 807 808 public double getBalance() { 809 return balance; 810 } 811 812 public void setBalance(double balance) { 813 this.balance = balance; 814 } 815 816 public String getAccountNO() { 817 return accountNO; 818 } 819 820 public void setAccountNO(String accountNO) { 821 this.accountNO = accountNO; 822 } 823 824 public User getUser() { 825 return user; 826 } 827 828 public void setUser(User user) { 829 this.user = user; 830 } 831 832 public Bank getBank() { 833 return bank; 834 } 835 836 public void setBank(Bank bank) { 837 this.bank = bank; 838 } 839 840 public ArrayList<Card> getList() { 841 return list; 842 } 843 844 public void setList(ArrayList<Card> list) { 845 DebitAccount.list = list; 846 } 847 848 public static Account getAmountbyCardNO(String cardNO) { 849 Iterator<Card> cardItr = DebitAccount.list.iterator(); 850 Card card = null; 851 852 while(cardItr.hasNext()) { 853 card = cardItr.next(); 854 if(card.getCardNO().equals(cardNO)) { 855 return card.getAccount(); 856 } 857 } 858 859 return null; 860 } 861 }*/ 862 863 class Card { 864 private String cardNO; 865 private String cardPassword; 866 private Account account = null; 867 868 public Card() { 869 super(); 870 } 871 872 public Card(String cardNO, String cardPassword, Account account) { 873 super(); 874 this.cardNO = cardNO; 875 this.cardPassword = cardPassword; 876 this.account = account; 877 } 878 879 public String getCardNO() { 880 return cardNO; 881 } 882 883 public void setCardNO(String cardNO) { 884 this.cardNO = cardNO; 885 } 886 887 public String getCardPassword() { 888 return cardPassword; 889 } 890 891 public Account getAccount() { 892 return account; 893 } 894 895 public void setAccount(Account account) { 896 this.account = account; 897 } 898 899 public void setCardPassword(String cardPassword) { 900 this.cardPassword = cardPassword; 901 } 902 903 904 905 // 检验卡号格式 906 public boolean checkCard() { 907 Pattern p = Pattern.compile("\\d{16}+"); 908 Matcher m = p.matcher(this.cardNO); 909 return m.matches(); 910 } 911 912 // 检验密码是否正确 913 public boolean checkPassword(String password) { 914 return this.cardPassword.equals(password); 915 } 916 917 } 918 919 class ATM { 920 private String ATMID; 921 private Bank bank = null; 922 923 public ATM() { 924 super(); 925 } 926 927 public ATM(String aTMID, Bank bank) { 928 super(); 929 ATMID = aTMID; 930 this.bank = bank; 931 } 932 933 public String getATMID() { 934 return ATMID; 935 } 936 937 public void setATMID(String aTMID) { 938 ATMID = aTMID; 939 } 940 941 public Bank getBank() { 942 return bank; 943 } 944 945 public void setBank(Bank bank) { 946 this.bank = bank; 947 } 948 }
类图:

SourceMonitor的生成报表内容:


分析:
这两道题都是仿真ATM机的程序设计,第二题比第一加了一些内容,我认为这道题最重要的就是类的设计,要根据老师给的题目说明,仔细阅读,好好进行类的设计,类设计的好,代码就是事半功倍,首先根据不同的东西,比如银联,银行,这些都要设计成单独的类,来保证类的单一性原则,然后这些都是一些实体类,这些实体类要注意它们之间的关系,很重要,之后还要设计一些其他的代理类去完成他们之间应该有的操作,第二题相比第一题最重要的就是多了一个账户类的思考,账户设计成了一个抽象类,然后分别有两个账户类,借记账户和贷记账户去继承账户类。总之,类的设计很重要,我们在设计类的过程中要注意类与类之间的关系和类要遵守的规则。
3.踩坑心得
(1)题目集07第二题,刚开始写的时候一直不知道接口怎么用,一直在探索接口的使用方法,刚开始想用其中的一个方法,自己研究了一会发现整个有点难,一开始是想按照面积直接排序的方法,没有研究明白,后来就利用了一个较为简单的调用,一个交换数据的方法,然后自己写了一个冒泡排序对面积进行排序。
(2)题目集08刚开始的时候就没有弄清楚钱应该存在哪里,刚开始的时候就把余额存到了卡里,然后存取款都是卡里,到最后发现,一张卡重复取两次前,余额不会叠加减少,才发现了余额应该是存在账户里,然后就重新改了很多东西,在这里浪费了很多时间,还有就是大循环,刚开始把一张卡的功能全部实现了,就是写不出来最后的大循环,把很多张卡一起存款取款,这个也研究了很久。最后发现整个不是仅仅用个while或者for循环就能解决的,它需要很多操作才能完成大循环。
4.改进建议
(1)对于题目集07第二题,感觉以后更加深入的了解了接口的一些相关知识以后代码可能会更简单效率更好,学会好的方法,就可以节约很多时间,提高很多效率。
(2)对于ATM机的题目,感觉可以是结构更加完整,类与类的关系还可以更加完善一些。就包括每个类的方法是应该放在这个类中,还是应该放在其它类中,包括类与类之间的关系的设计应该如何选择,都有操作空间。
5.总结
(1)通过这三次题目集的学习,学到了继承多态方法的使用,可以说是巩固加强了多态继承这方面的使用。
(2)对于类与类之间的关系的掌握,更加的清楚明白了。、
(3)拿到一道题目,一个需要你完成的程序,怎样去设计,有一定的理解,学到了一些怎样去设计的知识。
(4)因为写了ATM这样综合性非常强的程序,对于编程水平来说我觉得是有一定提升的。
浙公网安备 33010602011771号