JAVA阶段性作业总结三
一、前言:
本篇博客主要是对Java第三阶段作业进行总结。相较于前两阶段作业,第三阶段作业更多是综合性应用。第七次作业有两道题,7-1 图形卡片排序游戏 、7-2 图形卡片分组游戏,涉及的知识点有继承、多态的应用,ArrayList泛型的应用方法,Comparable接口及泛型的应用等,体现了JAVA单一职责原则和“开-闭”原则;第八次作业有一道题,7-3 ATM机类结构设计(一),是Java知识的综合运用,包括类的设计、继承和多态的运用、ArrayList数组的应用等;第九次作业有一道题,7-1 ATM机类结构设计(二),在第八次作业的基础上增加了信用卡(贷记账户),需要考虑透支情况,涉及的知识点有类的设计、抽象类的应用、继承和多态的运用、ArrayList数组的应用等。通过这几次作业,我能够将JAVA的知识进行综合性运用。
二、设计与分析:
7-1 图形卡片排序游戏
掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf
输入格式:
- 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:
1 3 4 2 1 3 4 2 1 3 0
- 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出
Wrong Format
。 - 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
- 排序前的各图形类型及面积,格式为
图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n
,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格; - 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为
Sum of area:总面积值
。
我的代码:
1 import java.awt.*; 2 import java.util.*; 3 4 public class Main { 5 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 6 //使用Main.input.next…即可(避免采坑) 7 public static Scanner input = new Scanner(System.in); 8 public static void main(String[] args){ 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 int num = input.nextInt(); 11 while(num != 0){ 12 if(num < 0 || num > 4){ 13 System.out.println("Wrong Format"); 14 System.exit(0); 15 } 16 list.add(num); 17 num = input.nextInt(); 18 } 19 DealCardList dealCardList = new DealCardList(list); 20 if(!dealCardList.validate()){ 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 dealCardList.showResult(); 25 input.close(); 26 } 27 } 28 29 class DealCardList{ 30 ArrayList<Card> cardList= new ArrayList<Card>(); 31 public DealCardList(){ 32 } 33 public DealCardList(ArrayList<Integer>list){ 34 for (int i = 0 ; i < list.size() ; i ++){ 35 int n = list.get(i); 36 switch (n){ 37 case 1:{ 38 double radius = Main.input.nextDouble(); 39 Card card1 = new Card(new Circle(radius)); 40 cardList.add(card1); 41 card1.getShape().setShapeName("Circle"); 42 break; 43 } 44 case 2:{ 45 double width = Main.input.nextDouble(); 46 double length = Main.input.nextDouble(); 47 Card card2 = new Card(new Rectangle(width,length)); 48 card2.getShape().setShapeName("Rectangle"); 49 cardList.add(card2); 50 break; 51 } 52 case 3:{ 53 double side1 = Main.input.nextDouble(); 54 double side2 = Main.input.nextDouble(); 55 double side3 = Main.input.nextDouble(); 56 Card card3 = new Card(new Triangle(side1,side2,side3)); 57 card3.getShape().setShapeName("Triangle"); 58 cardList.add(card3); 59 break; 60 } 61 case 4:{ 62 double topSide = Main.input.nextDouble(); 63 double bottomSide = Main.input.nextDouble(); 64 double height = Main.input.nextDouble(); 65 Card card4 = new Card(new Trapezoid(topSide,bottomSide,height)); 66 card4.getShape().setShapeName("Trapezoid"); 67 cardList.add(card4); 68 break; 69 } 70 default:break; 71 } 72 } 73 } 74 public boolean validate(){ 75 int count = 0; 76 for (int i = 0 ; i < cardList.size() ; i ++){ 77 if(!cardList.get(i).getShape().validate()){ 78 count ++; 79 } 80 } 81 if(count != 0){ 82 return false; 83 } 84 return true; 85 } 86 //卡片排序 87 public void cardSort(){ 88 89 Collections.sort(cardList, new Comparator<Card>() { 90 @Override 91 public int compare(Card o1, Card o2) { 92 return -((int)(o1.getShape().getArea() )-(int)( o2.getShape().getArea())); 93 // return -(int)(o1.getShape().getArea() - o2.getShape().getArea()); 94 } 95 }); 96 97 for (int i = 0 ; i < cardList.size() ; i ++){ 98 System.out.printf("%s:%.2f ",cardList.get(i).getShape().shapeName,cardList.get(i).getShape().getArea()); 99 } 100 } 101 //获取所有面积 102 public double getAllArea(){ 103 double sum = 0; 104 for (int i = 0 ; i < cardList.size() ; i ++){ 105 sum = sum + cardList.get(i).getShape().getArea(); 106 } 107 return sum; 108 } 109 //输出结果 110 public void showResult(){ 111 System.out.println("The original list:"); 112 for (int i = 0 ; i < cardList.size() ; i ++){ 113 System.out.printf("%s:%.2f ",cardList.get(i).getShape().shapeName,cardList.get(i).getShape().getArea()); 114 } 115 System.out.println(); 116 System.out.println("The sorted list:"); 117 cardSort(); 118 System.out.println(); 119 System.out.printf("Sum of area:%.2f",getAllArea()); 120 } 121 } 122 //对卡片排序 123 interface Comparable{ 124 } 125 126 class Card implements Comparable { 127 Shape shape; 128 Card(){ 129 } 130 Card(Shape shape){ 131 this.shape = shape; 132 } 133 public Shape getShape(){ 134 return shape; 135 } 136 public void setShape(Shape shape) { 137 this.shape = shape; 138 } 139 //对卡片排序 140 public int compareTo(Card card){ 141 return 0; 142 } 143 } 144 145 abstract class Shape { 146 String shapeName; 147 Shape(){ 148 } 149 Shape(String shapeName){ 150 this.shapeName = shapeName; 151 } 152 153 public String getShapeName() { 154 return shapeName; 155 } 156 public void setShapeName(String shapeName) { 157 this.shapeName = shapeName; 158 } 159 public abstract double getArea(); 160 public abstract boolean validate(); 161 //输出图形面积 162 public String toString(){ 163 return String.valueOf(true); 164 } 165 } 166 167 class Circle extends Shape { 168 double radius; 169 170 Circle(){ 171 } 172 Circle(double radius){ 173 this.radius = radius; 174 } 175 176 public double getRadius() { 177 return radius; 178 } 179 180 public void setRadius(double radius) { 181 this.radius = radius; 182 } 183 @Override 184 public double getArea() { 185 return this.radius * this.radius *Math.PI; 186 } 187 @Override 188 public boolean validate(){ 189 return this.radius > 0; 190 } 191 } 192 class Rectangle extends Shape{ 193 double width; 194 double length; 195 Rectangle(double width , double length){ 196 this.width = width; 197 this.length = length; 198 } 199 public double getWidth() { 200 return width; 201 } 202 public void setWidth(double width) { 203 this.width = width; 204 } 205 public double getLength() { 206 return length; 207 } 208 public void setLength(double length) { 209 this.length = length; 210 } 211 212 @Override 213 public double getArea() { 214 return this.width * this.length; 215 } 216 217 @Override 218 public boolean validate() { 219 return this.width > 0 && this.length > 0; 220 } 221 } 222 class Triangle extends Shape{ 223 double side1; 224 double side2; 225 double side3; 226 Triangle(double side1,double side2,double side3){ 227 this.side1 = side1; 228 this.side2 = side2; 229 this.side3 = side3; 230 } 231 public double getSide1() { 232 return side1; 233 } 234 public double getSide2() { 235 return side2; 236 } 237 public double getSide3() { 238 return side3; 239 } 240 public void setSide1(double side1) { 241 this.side1 = side1; 242 } 243 public void setSide2(double side2) { 244 this.side2 = side2; 245 } 246 public void setSide3(double side3) { 247 this.side3 = side3; 248 } 249 250 @Override 251 public double getArea() { 252 double p = 0; 253 p = (side1 + side2 + side3) / 2; 254 return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); 255 } 256 257 @Override 258 public boolean validate() { 259 return (this.side1 > 0) && (this.side2 > 0) &&(this.side3 > 0) && (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1); 260 } 261 } 262 class Trapezoid extends Shape{ 263 double topSide; 264 double bottomSide; 265 double height; 266 Trapezoid(){ 267 } 268 Trapezoid(double topSide,double bottomSide,double height){ 269 this.topSide = topSide; 270 this.bottomSide = bottomSide; 271 this.height = height; 272 } 273 274 @Override 275 public double getArea() { 276 return (this.topSide + this.bottomSide) * this.height * 0.5; 277 } 278 279 @Override 280 public boolean validate() { 281 return (this.topSide > 0) && (this.bottomSide > 0) && (this.height > 0); 282 } 283 }
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.awt.*; 2 import java.util.*; 3 4 public class Main { 5 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接 6 //使用Main.input.next…即可(避免采坑) 7 public static Scanner input = new Scanner(System.in); 8 public static void main(String[] args){ 9 ArrayList<Integer> list = new ArrayList<Integer>(); 10 int num = input.nextInt(); 11 if(num == 0){ 12 System.out.println("Wrong Format"); 13 System.exit(0); 14 } 15 while(num != 0){ 16 if(num < 0 || num > 4){ 17 System.out.println("Wrong Format"); 18 System.exit(0); 19 } 20 list.add(num); 21 num = input.nextInt(); 22 } 23 DealCardList dealCardList = new DealCardList(list); 24 if(!dealCardList.validate()){ 25 System.out.println("Wrong Format"); 26 System.exit(0); 27 } 28 dealCardList.showResult(); 29 input.close(); 30 } 31 } 32 33 class DealCardList{ 34 ArrayList<Card> cardList= new ArrayList<Card>(); 35 ArrayList<Card> cardList1= new ArrayList<Card>(); 36 ArrayList<Card> cardList2= new ArrayList<Card>(); 37 ArrayList<Card> cardList3= new ArrayList<Card>(); 38 ArrayList<Card> cardList4= new ArrayList<Card>(); 39 public DealCardList(){ 40 } 41 public DealCardList(ArrayList<Integer>list){ 42 for (int i = 0 ; i < list.size() ; i ++){ 43 int n = list.get(i); 44 switch (n){ 45 case 1:{ 46 double radius = Main.input.nextDouble(); 47 Card card1 = new Card(new Circle(radius)); 48 cardList.add(card1); 49 cardList1.add(card1); 50 card1.getShape().setShapeName("Circle"); 51 break; 52 } 53 case 2:{ 54 double width = Main.input.nextDouble(); 55 double length = Main.input.nextDouble(); 56 Card card2 = new Card(new Rectangle(width,length)); 57 card2.getShape().setShapeName("Rectangle"); 58 cardList.add(card2); 59 cardList2.add(card2); 60 break; 61 } 62 case 3:{ 63 double side1 = Main.input.nextDouble(); 64 double side2 = Main.input.nextDouble(); 65 double side3 = Main.input.nextDouble(); 66 Card card3 = new Card(new Triangle(side1,side2,side3)); 67 card3.getShape().setShapeName("Triangle"); 68 cardList.add(card3); 69 cardList3.add(card3); 70 break; 71 } 72 case 4:{ 73 double topSide = Main.input.nextDouble(); 74 double bottomSide = Main.input.nextDouble(); 75 double height = Main.input.nextDouble(); 76 Card card4 = new Card(new Trapezoid(topSide,bottomSide,height)); 77 card4.getShape().setShapeName("Trapezoid"); 78 cardList.add(card4); 79 cardList4.add(card4); 80 break; 81 } 82 default:break; 83 } 84 } 85 } 86 public boolean validate(){ 87 int count = 0; 88 for (int i = 0 ; i < cardList.size() ; i ++){ 89 if(!cardList.get(i).getShape().validate()){ 90 count ++; 91 } 92 } 93 if(count != 0){ 94 return false; 95 } 96 return true; 97 } 98 //卡片排序 99 public void cardSort(){ 100 101 Collections.sort(cardList1, new Comparator<Card>() { 102 @Override 103 public int compare(Card o1, Card o2) { 104 return -((int)(o1.getShape().getArea() )-(int)( o2.getShape().getArea())); 105 // return -(int)(o1.getShape().getArea() - o2.getShape().getArea()); 106 } 107 }); 108 Collections.sort(cardList2, new Comparator<Card>() { 109 @Override 110 public int compare(Card o1, Card o2) { 111 return -((int)(o1.getShape().getArea() )-(int)( o2.getShape().getArea())); 112 // return -(int)(o1.getShape().getArea() - o2.getShape().getArea()); 113 } 114 }); 115 Collections.sort(cardList3, new Comparator<Card>() { 116 @Override 117 public int compare(Card o1, Card o2) { 118 return -((int)(o1.getShape().getArea() )-(int)( o2.getShape().getArea())); 119 // return -(int)(o1.getShape().getArea() - o2.getShape().getArea()); 120 } 121 }); 122 Collections.sort(cardList4, new Comparator<Card>() { 123 @Override 124 public int compare(Card o1, Card o2) { 125 return -((int)(o1.getShape().getArea() )-(int)( o2.getShape().getArea())); 126 // return -(int)(o1.getShape().getArea() - o2.getShape().getArea()); 127 } 128 }); 129 Output(cardList1); 130 Output(cardList2); 131 Output(cardList3); 132 Output(cardList4); 133 } 134 //获取所有面积 135 public double getAllArea(){ 136 double sum1 = 0; 137 double sum2 = 0; 138 double sum3 = 0; 139 double sum4 = 0; 140 double sum = 0; 141 for (int i = 0 ; i < cardList1.size() ; i ++){ 142 sum1 = sum1 + cardList1.get(i).getShape().getArea(); 143 } 144 for (int i = 0 ; i < cardList2.size() ; i ++){ 145 sum2 = sum2 + cardList2.get(i).getShape().getArea(); 146 } 147 for (int i = 0 ; i < cardList3.size() ; i ++){ 148 sum3 = sum3 + cardList3.get(i).getShape().getArea(); 149 } 150 for (int i = 0 ; i < cardList4.size() ; i ++){ 151 sum4 = sum4 + cardList4.get(i).getShape().getArea(); 152 } 153 sum1 = Math.max(sum1,sum2); 154 sum2 = Math.max(sum3,sum4); 155 sum = Math.max(sum1,sum2); 156 return sum; 157 } 158 //输出结果 159 public void showResult(){ 160 System.out.println("The original list:"); 161 System.out.print("["); 162 for (int i = 0 ; i < cardList.size() ; i ++){ 163 System.out.printf("%s:%.2f ",cardList.get(i).getShape().shapeName,cardList.get(i).getShape().getArea()); 164 } 165 System.out.println("]"); 166 System.out.println("The Separated List:"); 167 Output(cardList1); 168 Output(cardList2); 169 Output(cardList3); 170 Output(cardList4); 171 172 System.out.println(); 173 System.out.println("The Separated sorted List:"); 174 cardSort(); 175 System.out.println(); 176 System.out.printf("The max area:%.2f",getAllArea()); 177 } 178 public void Output(ArrayList<Card>cards){ 179 System.out.print("["); 180 for (int i = 0 ; i < cards.size() ; i ++){ 181 System.out.printf("%s:%.2f ",cards.get(i).getShape().shapeName,cards.get(i).getShape().getArea()); 182 } 183 System.out.print("]"); 184 } 185 } 186 //对卡片排序 187 interface Comparable{ 188 } 189 190 class Card implements Comparable { 191 Shape shape; 192 Card(){ 193 } 194 Card(Shape shape){ 195 this.shape = shape; 196 } 197 public Shape getShape(){ 198 return shape; 199 } 200 public void setShape(Shape shape) { 201 this.shape = shape; 202 } 203 //对卡片排序 204 public int compareTo(Card card){ 205 return 0; 206 } 207 } 208 209 abstract class Shape { 210 String shapeName; 211 Shape(){ 212 } 213 Shape(String shapeName){ 214 this.shapeName = shapeName; 215 } 216 217 public String getShapeName() { 218 return shapeName; 219 } 220 public void setShapeName(String shapeName) { 221 this.shapeName = shapeName; 222 } 223 public abstract double getArea(); 224 public abstract boolean validate(); 225 //输出图形面积 226 public String toString(){ 227 return String.valueOf(true); 228 } 229 } 230 231 class Circle extends Shape { 232 double radius; 233 234 Circle(){ 235 } 236 Circle(double radius){ 237 this.radius = radius; 238 } 239 240 public double getRadius() { 241 return radius; 242 } 243 244 public void setRadius(double radius) { 245 this.radius = radius; 246 } 247 @Override 248 public double getArea() { 249 return this.radius * this.radius *Math.PI; 250 } 251 @Override 252 public boolean validate(){ 253 return this.radius > 0; 254 } 255 } 256 class Rectangle extends Shape{ 257 double width; 258 double length; 259 Rectangle(double width , double length){ 260 this.width = width; 261 this.length = length; 262 } 263 public double getWidth() { 264 return width; 265 } 266 public void setWidth(double width) { 267 this.width = width; 268 } 269 public double getLength() { 270 return length; 271 } 272 public void setLength(double length) { 273 this.length = length; 274 } 275 276 @Override 277 public double getArea() { 278 return this.width * this.length; 279 } 280 281 @Override 282 public boolean validate() { 283 return this.width > 0 && this.length > 0; 284 } 285 } 286 class Triangle extends Shape{ 287 double side1; 288 double side2; 289 double side3; 290 Triangle(double side1,double side2,double side3){ 291 this.side1 = side1; 292 this.side2 = side2; 293 this.side3 = side3; 294 } 295 296 @Override 297 public double getArea() { 298 double p = 0; 299 p = (side1 + side2 + side3) / 2; 300 return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3)); 301 } 302 @Override 303 public boolean validate() { 304 return (this.side1 > 0) && (this.side2 > 0) &&(this.side3 > 0) && (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1); 305 } 306 } 307 class Trapezoid extends Shape{ 308 double topSide; 309 double bottomSide; 310 double height; 311 Trapezoid(){ 312 } 313 Trapezoid(double topSide,double bottomSide,double height){ 314 this.topSide = topSide; 315 this.bottomSide = bottomSide; 316 this.height = height; 317 } 318 @Override 319 public double getArea() { 320 return (this.topSide + this.bottomSide) * this.height * 0.5; 321 } 322 @Override 323 public boolean validate() { 324 return (this.topSide > 0) && (this.bottomSide > 0) && (this.height > 0); 325 } 326 }
(1)题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
两道题的类图:
两道题的圈复杂度:圈复杂度差不多,排序的圈复杂度低于分组的圈复杂度
从类图上来看,两道题的类图相差不大。7-2在7-1的基础上增加了将每一类图形分组处理的功能,7-2的DealCardList类在7-1的基础上增加了属性和方法,7-2的DealCardList类中属性更多,这体现了设计的“开-闭”原则,对扩展开放,对修改关闭。
每一个图形都继承于一个图形父类,这里继承的使用大大方便了代码的修改。每个类都有每个类的特定功能,比如getArea()方法,每个继承Shape类的子类都进行了方法的重写,体现了JAVA单一职责的原则。这两道题都使用了Comparable 接口以及其中的 CompareTo()方法。
ATM机类结构设计(一)
设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 存款、取款功能输入数据格式:
卡号 密码 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.ArrayList; 3 public class Main { 4 public static void main(String[] args) { 5 6 Scanner input = new Scanner(System.in); 7 String s; 8 String str[]; 9 Initall initall = new Initall(); 10 initall.initall(); 11 while (!(s = input.nextLine()).equals("#")) { 12 str = s.split("\\s+"); 13 Bank bank = new Bank(); 14 Card card = new Card(); 15 ATM atm = new ATM(); 16 User user = new User(); 17 Account account = new Account(); 18 if (str.length == 1) { 19 card.setId(str[0]); 20 if(CardValidity(card.getId())){ 21 account = initall.scanAccount(card.getId()); 22 System.out.printf("¥%.2f\n",account.getBalance()); 23 }else { 24 System.out.println("Sorry,this card does not exist."); 25 } 26 } else { 27 double b; 28 card.setId(str[0]); 29 card.setPassword(str[1]); 30 atm.setId(str[2]); 31 double m = Double.parseDouble(str[3]); 32 33 if (!CardValidity(card.getId())){ 34 System.out.println("Sorry,this card does not exist."); 35 System.exit(0); 36 } 37 if (!AtmValidity(atm.getId())){ 38 System.out.println("Sorry,the ATM's id is wrong."); 39 System.exit(0); 40 } 41 if (!card.getPassword().equals("88888888")){ 42 System.out.println("Sorry,your password is wrong."); 43 System.exit(0); 44 } 45 account = initall.scanAccount(card.getId()); 46 user = initall.scanUser(account); 47 if (!initall.IscrossBank(user,atm.getId())){ 48 System.out.println("Sorry,cross-bank withdrawal is not supported."); 49 System.exit(0); 50 } 51 52 if (account.getBalance()-m < 0){ 53 System.out.println("Sorry,your account balance is insufficient."); 54 System.exit(0); 55 } 56 if (m < 0){ 57 System.out.printf(user.getName()+"在"+user.getBankName()+"的"+atm.getId()+"号ATM机上存款¥%.2f\n",-m); 58 }else { 59 System.out.printf(user.getName()+"在"+user.getBankName()+"的"+atm.getId()+"号ATM机上取款¥%.2f\n",m); 60 } 61 62 account.setBalance(account.getBalance()-m); 63 System.out.printf("当前余额为¥%.2f\n",account.getBalance()); 64 65 } 66 } 67 68 } 69 70 public static boolean CardValidity(String id){ 71 int flag = 1; 72 String s[] = {"6217000010041315709", "6217000010041315715", "6217000010041315718", "6217000010051320007", 73 "6222081502001312389", "6222081502001312390", "6222081502001312399", 74 "6222081502001312400", "6222081502051320785", "6222081502051320786"}; 75 for (int i = 0 ;i < s.length; i ++){ 76 if(id.equals( s[i])){ 77 flag++; 78 break; 79 } 80 } 81 if(flag == 1){ 82 return false; 83 } else { 84 return true; 85 } 86 } 87 public static boolean AtmValidity(String atmId){ 88 int flag = 1; 89 String s[] = {"01","02","03","04","05","06"}; 90 for (int i = 0 ;i < s.length; i ++){ 91 if(atmId.equals( s[i])){ 92 flag++; 93 break; 94 } 95 } 96 if(flag == 1){ 97 return false; 98 } else { 99 return true; 100 } 101 } 102 103 } 104 105 /* 106 ArrayList<String> cardst1 = new ArrayList<String>(); 107 */ 108 109 110 class Initall{ 111 //卡号数组 112 ArrayList<String> cardst1 = new ArrayList<String>(); 113 ArrayList<String> cardst2 = new ArrayList<String>(); 114 ArrayList<String> cardst3 = new ArrayList<String>(); 115 ArrayList<String> cardst4 = new ArrayList<String>(); 116 ArrayList<String> cardst5 = new ArrayList<String>(); 117 ArrayList<String> cardst6 = new ArrayList<String>(); 118 ArrayList<String> cardst7 = new ArrayList<String>(); 119 ArrayList<String> cardst8 = new ArrayList<String>(); 120 121 //账户数组 122 ArrayList<Account> accounts1 = new ArrayList<Account>(); 123 ArrayList<Account> accounts2 = new ArrayList<Account>(); 124 ArrayList<Account> accounts3 = new ArrayList<Account>(); 125 ArrayList<Account> accounts4 = new ArrayList<Account>(); 126 127 //用户数组 128 ArrayList<User> users1 = new ArrayList<User>(); 129 ArrayList<User> users2 = new ArrayList<User>(); 130 131 //ATM数组 132 ArrayList<ATM> atmst1 = new ArrayList<ATM>(); 133 ArrayList<ATM> atmst2 = new ArrayList<ATM>(); 134 135 //银行数组 136 ArrayList<Bank> bankst = new ArrayList<Bank>(); 137 Account account1 = new Account("3217000010041315709", 10000, cardst1); 138 Account account2 = new Account("3217000010041315715", 10000, cardst2); 139 Account account3 = new Account("3217000010051320007", 10000, cardst3); 140 Account account4 = new Account("3222081502001312389", 10000, cardst4); 141 Account account5 = new Account("3222081502001312390", 10000, cardst5); 142 Account account6 = new Account("3222081502001312399", 10000, cardst6); 143 Account account7 = new Account("3222081502051320785", 10000, cardst7); 144 Account account8 = new Account("3222081502051320786", 10000, cardst8); 145 146 User user1 = new User("杨过", "中国建设银行", accounts1); 147 User user2 = new User("郭靖", "中国建设银行", accounts2); 148 User user3 = new User("张无忌", "中国工商银行", accounts3); 149 User user4 = new User("韦小宝", "中国工商银行", accounts4); 150 151 ATM atm1 = new ATM("01", "中国建设银行", users1); 152 ATM atm2 = new ATM("02", "中国建设银行", users1); 153 ATM atm3 = new ATM("03", "中国建设银行", users1); 154 ATM atm4 = new ATM("04", "中国建设银行", users1); 155 ATM atm5 = new ATM("05", "中国工商银行", users2); 156 ATM atm6 = new ATM("06", "中国工商银行", users2); 157 Bank bank1 = new Bank("中国建设银行", atmst1); 158 Bank bank2 = new Bank("中国工商银行", atmst2); 159 public void initall(){ 160 161 cardst1.add("6217000010041315709"); 162 cardst1.add("6217000010041315715"); 163 cardst2.add("6217000010041315718"); 164 cardst3.add("6217000010051320007"); 165 cardst4.add("6222081502001312389"); 166 cardst5.add("6222081502001312390"); 167 cardst6.add("6222081502001312399"); 168 cardst6.add("6222081502001312400"); 169 cardst7.add("6222081502051320785"); 170 cardst8.add("6222081502051320786"); 171 172 173 accounts1.add(account1); 174 accounts1.add(account2); 175 accounts2.add(account3); 176 accounts3.add(account4); 177 accounts3.add(account5); 178 accounts3.add(account6); 179 accounts4.add(account7); 180 accounts4.add(account8); 181 182 183 users1.add(user1); 184 users1.add(user2); 185 users2.add(user3); 186 users2.add(user4); 187 188 189 atmst1.add(atm1); 190 atmst1.add(atm2); 191 atmst1.add(atm3); 192 atmst1.add(atm4); 193 atmst2.add(atm5); 194 atmst2.add(atm6); 195 196 197 bankst.add(bank1); 198 bankst.add(bank2); 199 } 200 public Account scanAccount(String card){ 201 int flag = 1; 202 if (cardst1.contains(card)) 203 return account1; 204 else if (cardst2.contains(card)) 205 return account2; 206 else if (cardst3.contains(card)) 207 return account3; 208 else if (cardst4.contains(card)) 209 return account4; 210 else if (cardst5.contains(card)) 211 return account5; 212 else if (cardst6.contains(card)) 213 return account6; 214 else if (cardst7.contains(card)) 215 return account7; 216 else if (cardst8.contains(card)) 217 return account8; 218 else return null; 219 } 220 public User scanUser(Account account){ 221 if (accounts1.contains(account)) 222 return user1; 223 else if (accounts2.contains(account)) 224 return user2; 225 else if (accounts3.contains(account)) 226 return user3; 227 else if (accounts4.contains(account)) 228 return user4; 229 else return null; 230 } 231 232 public boolean IscrossBank(User user,String atmid){ 233 if (atm1.getId().equals(atmid) && atm1.getUser().contains(user)) 234 return true; 235 if (atm2.getId().equals(atmid) && atm2.getUser().contains(user)) 236 return true; 237 if (atm3.getId().equals(atmid) && atm3.getUser().contains(user)) 238 return true; 239 if (atm4.getId().equals(atmid) && atm4.getUser().contains(user)) 240 return true; 241 if (atm5.getId().equals(atmid) && atm5.getUser().contains(user)) 242 return true; 243 if (atm6.getId().equals(atmid) && atm6.getUser().contains(user)) 244 return true; 245 return false; 246 } 247 } 248 249 250 class Bank{ 251 String name; 252 ArrayList<ATM>atms = new ArrayList<ATM>(); 253 Bank(){ 254 } 255 Bank(String name,ArrayList<ATM> atms){ 256 this.name = name; 257 this.atms = atms; 258 } 259 260 public void setName(String name) { 261 this.name = name; 262 } 263 264 public String getName() { 265 return name; 266 } 267 268 public ArrayList<ATM> getAtm() { 269 return atms; 270 } 271 272 public void setAtm(ArrayList<ATM> atms) { 273 this.atms = atms; 274 } 275 276 277 } 278 class User{ 279 String name; 280 String bankName; 281 ArrayList<Account> accounts = new ArrayList<Account>(); 282 User(){ 283 } 284 User(String name,String bankName,ArrayList<Account>accounts){ 285 this.name = name; 286 this.bankName = bankName; 287 this.accounts = accounts; 288 } 289 290 public String getName() { 291 return name; 292 } 293 294 public void setName(String name) { 295 this.name = name; 296 } 297 298 public String getBankName() { 299 return bankName; 300 } 301 302 public void setBankName(String bankName) { 303 this.bankName = bankName; 304 } 305 306 public ArrayList<Account> getAccount() { 307 return accounts; 308 } 309 310 public void setAccount(ArrayList<Account> account) { 311 this.accounts = account; 312 } 313 public void addAccount(Account account){ 314 accounts.add(account); 315 } 316 } 317 class Account{ 318 String id; 319 double balance = 10000; 320 ArrayList<String> cards = new ArrayList<String>(); 321 Account(){ 322 } 323 Account(String id,double balance,ArrayList<String> cards ){ 324 this.id = id; 325 this.balance = balance; 326 this.cards = cards; 327 } 328 329 public String getId() { 330 return id; 331 } 332 333 public void setId(String id) { 334 this.id = id; 335 } 336 337 public ArrayList<String> getCard() { 338 return cards; 339 } 340 341 public void setCard(ArrayList<String> cards) { 342 this.cards = cards; 343 } 344 345 public double getBalance() { 346 return balance; 347 } 348 349 public void setBalance(double balance) { 350 this.balance = balance; 351 } 352 } 353 class Card{ 354 String id; 355 String password; 356 Card(){ 357 } 358 Card(String id){ 359 this.id = id; 360 } 361 362 public String getId() { 363 return id; 364 } 365 366 public void setId(String id) { 367 this.id = id; 368 } 369 370 public String getPassword() { 371 return password; 372 } 373 374 public void setPassword(String password) { 375 this.password = password; 376 } 377 } 378 class ATM{ 379 String id; 380 String bankName; 381 ArrayList<User> users = new ArrayList<User>(); 382 ATM(){ 383 } 384 ATM(String id , String bankName,ArrayList<User> users){ 385 this.id = id; 386 this.bankName = bankName; 387 this.users = users; 388 } 389 390 public void setId(String id) { 391 this.id = id; 392 } 393 394 public String getId() { 395 return id; 396 } 397 398 public void setBankName(String bankName) { 399 this.bankName = bankName; 400 } 401 402 public String getBankName() { 403 return bankName; 404 } 405 406 public ArrayList<User> getUser() { 407 return users; 408 } 409 410 public void setUser(ArrayList<User> users) { 411 this.users = users; 412 } 413 }
ATM机类结构设计(二)
设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf
输入格式:
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:
- 取款功能输入数据格式:
卡号 密码 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.Scanner; 2 import java.util.ArrayList; 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 String s; 7 String str[]; 8 int flag = 0; 9 Initall initall = new Initall(); 10 initall.initall(); 11 while (!(s = input.nextLine()).equals("#")) { 12 str = s.split("\\s+"); 13 Bank bank = new Bank(); 14 Card card = new Card(); 15 ATM atm = new ATM(); 16 User user = new User(); 17 Account account = new Account(); 18 if (str.length == 1) { 19 card.setId(str[0]); 20 if(CardValidity(card.getId())){ 21 account = initall.scanAccount(card.getId()); 22 System.out.printf("业务:查询余额 ¥%.2f",account.getBalance()); 23 }else { 24 System.out.println("Sorry,this card does not exist."); 25 } 26 } else { 27 double b; 28 card.setId(str[0]); 29 card.setPassword(str[1]); 30 atm.setId(str[2]); 31 double m = Double.parseDouble(str[3]); 32 double d = 0; 33 if (!CardValidity(card.getId())) { 34 System.out.println("Sorry,this card does not exist."); 35 System.exit(0); 36 } 37 if (!AtmValidity(atm.getId())) { 38 System.out.println("Sorry,the ATM's id is wrong."); 39 System.exit(0); 40 } 41 if (!card.getPassword().equals("88888888")) { 42 System.out.println("Sorry,your password is wrong."); 43 System.exit(0); 44 } 45 account = initall.scanAccount(card.getId()); 46 user = initall.scanUser(account); 47 bank = initall.scanBank(atm.getId()); 48 49 if (account.getBalance() - m <= 0) { 50 if ((user.getName().equals("张三丰") || user.getName().equals("令狐冲") || user.getName().equals("乔峰") || user.getName().equals("洪七公")) && m - account.getBalance() <= 50000) { 51 if (flag == 0) { 52 d = m - account.getBalance(); 53 flag++; 54 } else { 55 d = m; 56 } 57 account.setBalance(account.getBalance() - d * 0.05); //透支手续费 58 if (!initall.IscrossBank(user, atm.getId())) { 59 switch (bank.getName()) { 60 case "中国建设银行": 61 account.setBalance(account.getBalance() - m - m * 0.02); 62 break; 63 case "中国工商银行": 64 account.setBalance(account.getBalance() - m - m * 0.03); 65 break; 66 case "中国农业银行": 67 account.setBalance(account.getBalance() - m - m * 0.04); 68 break; 69 default: 70 break; 71 } 72 } else { 73 account.setBalance(account.getBalance() - m); 74 } 75 } else if (account.getBalance() - m <= 0) { 76 System.out.println("Sorry,your account balance is insufficient."); 77 System.exit(0); 78 } 79 } else { 80 if (!initall.IscrossBank(user, atm.getId())) { 81 switch (bank.getName()) { 82 case "中国建设银行": 83 account.setBalance(account.getBalance() - m - m * 0.02); 84 break; 85 case "中国工商银行": 86 account.setBalance(account.getBalance() - m - m * 0.03); 87 break; 88 case "中国农业银行": 89 account.setBalance(account.getBalance() - m - m * 0.04); 90 break; 91 default: 92 break; 93 } 94 } else { 95 account.setBalance(account.getBalance() - m); 96 } 97 } 98 if (-account.getBalance() > 50000) { 99 System.out.println("Sorry,your account balance is insufficient."); 100 System.exit(0); 101 } 102 System.out.printf("业务:取款 " + user.getName() + "在" + bank.getName() + "的" + atm.getId() + "号ATM机上取款¥%.2f\n", m); 103 System.out.printf("当前余额为¥%.2f\n", account.getBalance()); 104 } 105 } 106 } 107 108 109 public static boolean CardValidity(String id){ 110 int flag = 1; 111 String s[] = {"6217000010041315709", "6217000010041315715", "6217000010041315718", "6217000010051320007", 112 "6222081502001312389", "6222081502001312390", "6222081502001312399", 113 "6222081502001312400", "6222081502051320785", "6222081502051320786", 114 "6640000010045442002","6640000010045442003","6640000010045441009", 115 "6630000010033431001","6630000010033431008"}; 116 for (int i = 0 ;i < s.length; i ++){ 117 if(id.equals( s[i])){ 118 flag++; 119 break; 120 } 121 } 122 if(flag == 1){ 123 return false; 124 } else { 125 return true; 126 } 127 } 128 public static boolean AtmValidity(String atmId){ 129 int flag = 1; 130 String s[] = {"01","02","03","04","05","06","07","08","09","10","11"}; 131 for (int i = 0 ;i < s.length; i ++){ 132 if(atmId.equals( s[i])){ 133 flag++; 134 break; 135 } 136 } 137 if(flag == 1){ 138 return false; 139 } else { 140 return true; 141 } 142 } 143 144 } 145 146 /* 147 ArrayList<String> cardst1 = new ArrayList<String>(); 148 */ 149 150 151 class Initall{ 152 //卡号数组 153 ArrayList<String> cardst1 = new ArrayList<String>(); 154 ArrayList<String> cardst2 = new ArrayList<String>(); 155 ArrayList<String> cardst3 = new ArrayList<String>(); 156 ArrayList<String> cardst4 = new ArrayList<String>(); 157 ArrayList<String> cardst5 = new ArrayList<String>(); 158 ArrayList<String> cardst6 = new ArrayList<String>(); 159 ArrayList<String> cardst7 = new ArrayList<String>(); 160 ArrayList<String> cardst8 = new ArrayList<String>(); 161 ArrayList<String> cardjt1 = new ArrayList<String>(); 162 ArrayList<String> cardjt2 = new ArrayList<String>(); 163 ArrayList<String> cardjt3 = new ArrayList<String>(); 164 ArrayList<String> cardjt4 = new ArrayList<String>(); 165 166 //账户数组 167 ArrayList<Account> accounts1 = new ArrayList<Account>(); 168 ArrayList<Account> accounts2 = new ArrayList<Account>(); 169 ArrayList<Account> accounts3 = new ArrayList<Account>(); 170 ArrayList<Account> accounts4 = new ArrayList<Account>(); 171 ArrayList<Account> accountsj1 = new ArrayList<Account>(); 172 ArrayList<Account> accountsj2 = new ArrayList<Account>(); 173 ArrayList<Account> accountsj3 = new ArrayList<Account>(); 174 ArrayList<Account> accountsj4 = new ArrayList<Account>(); 175 176 177 178 179 //用户数组 180 ArrayList<User> users1 = new ArrayList<User>(); 181 ArrayList<User> users2 = new ArrayList<User>(); 182 ArrayList<User> users3 = new ArrayList<User>(); 183 184 //ATM数组 185 ArrayList<ATM> atmst1 = new ArrayList<ATM>(); 186 ArrayList<ATM> atmst2 = new ArrayList<ATM>(); 187 ArrayList<ATM> atmst3 = new ArrayList<ATM>(); 188 189 //银行数组 190 ArrayList<Bank> bankst = new ArrayList<Bank>(); 191 Account account1 = new Account("3217000010041315709", 10000, cardst1); 192 Account account2 = new Account("3217000010041315715", 10000, cardst2); 193 Account account3 = new Account("3217000010051320007", 10000, cardst3); 194 Account account4 = new Account("3222081502001312389", 10000, cardst4); 195 Account account5 = new Account("3222081502001312390", 10000, cardst5); 196 Account account6 = new Account("3222081502001312399", 10000, cardst6); 197 Account account7 = new Account("3222081502051320785", 10000, cardst7); 198 Account account8 = new Account("3222081502051320786", 10000, cardst8); 199 Account account9 = new Account("3640000010045442002", 10000, cardjt1); 200 Account account10 = new Account("3640000010045441009", 10000, cardjt2); 201 Account account11 = new Account("3630000010033431001", 10000, cardjt3); 202 Account account12 = new Account("3630000010033431008", 10000, cardjt4); 203 204 205 User user1 = new User("杨过", "中国建设银行", accounts1); 206 User user2 = new User("郭靖", "中国建设银行", accounts2); 207 User user3 = new User("张无忌", "中国工商银行", accounts3); 208 User user4 = new User("韦小宝", "中国工商银行", accounts4); 209 User user5 = new User("张三丰","中国建设银行",accountsj1); 210 User user6 = new User("令狐冲","中国工商银行",accountsj2); 211 User user7 = new User("乔峰","中国农业银行",accountsj3); 212 User user8 = new User("洪七公","中国农业银行",accountsj4); 213 214 215 ATM atm1 = new ATM("01", "中国建设银行", users1); 216 ATM atm2 = new ATM("02", "中国建设银行", users1); 217 ATM atm3 = new ATM("03", "中国建设银行", users1); 218 ATM atm4 = new ATM("04", "中国建设银行", users1); 219 ATM atm5 = new ATM("05", "中国工商银行", users2); 220 ATM atm6 = new ATM("06", "中国工商银行", users2); 221 ATM atm7 = new ATM("07", "中国农业银行", users3); 222 ATM atm8 = new ATM("08", "中国农业银行", users3); 223 ATM atm9 = new ATM("09", "中国农业银行", users3); 224 ATM atm10 = new ATM("10", "中国农业银行", users3); 225 ATM atm11 = new ATM("11", "中国农业银行", users3); 226 227 Bank bank1 = new Bank("中国建设银行", atmst1); 228 Bank bank2 = new Bank("中国工商银行", atmst2); 229 Bank bank3 = new Bank("中国农业银行", atmst3); 230 231 public void initall(){ 232 233 cardst1.add("6217000010041315709"); 234 cardst1.add("6217000010041315715"); 235 cardst2.add("6217000010041315718"); 236 cardst3.add("6217000010051320007"); 237 cardst4.add("6222081502001312389"); 238 cardst5.add("6222081502001312390"); 239 cardst6.add("6222081502001312399"); 240 cardst6.add("6222081502001312400"); 241 cardst7.add("6222081502051320785"); 242 cardst8.add("6222081502051320786"); 243 cardjt1.add("6640000010045442002"); 244 cardjt1.add("6640000010045442003"); 245 cardjt2.add("6640000010045441009"); 246 cardjt3.add("6630000010033431001"); 247 cardjt4.add("6630000010033431008"); 248 249 accounts1.add(account1); 250 accounts1.add(account2); 251 accounts2.add(account3); 252 accounts3.add(account4); 253 accounts3.add(account5); 254 accounts3.add(account6); 255 accounts4.add(account7); 256 accounts4.add(account8); 257 accountsj1.add(account9); 258 accountsj2.add(account10); 259 accountsj3.add(account11); 260 accountsj4.add(account12); 261 262 263 users1.add(user1); 264 users1.add(user2); 265 users1.add(user5); 266 users2.add(user3); 267 users2.add(user4); 268 users2.add(user6); 269 users3.add(user7); 270 users3.add(user8); 271 272 atmst1.add(atm1); 273 atmst1.add(atm2); 274 atmst1.add(atm3); 275 atmst1.add(atm4); 276 atmst2.add(atm5); 277 atmst2.add(atm6); 278 atmst3.add(atm7); 279 atmst3.add(atm8); 280 atmst3.add(atm9); 281 atmst3.add(atm10); 282 atmst3.add(atm11); 283 284 bankst.add(bank1); 285 bankst.add(bank2); 286 bankst.add(bank3); 287 } 288 289 public Bank scanBank(String atmID){ 290 if (atm1.getId().equals(atmID) || atm2.getId().equals(atmID) || atm3.getId().equals(atmID) || atm4.getId().equals(atmID)) 291 return bank1; 292 if (atm5.getId().equals(atmID) || atm6.getId().equals(atmID)) 293 return bank2; 294 if (atm7.getId().equals(atmID) || atm8.getId().equals(atmID) || atm9.getId().equals(atmID) || atm10.getId().equals(atmID) || atm11.getId().equals(atmID)) 295 return bank3; 296 return null; 297 } 298 299 300 public Account scanAccount(String card){ 301 int flag = 1; 302 if (cardst1.contains(card)) 303 return account1; 304 else if (cardst2.contains(card)) 305 return account2; 306 else if (cardst3.contains(card)) 307 return account3; 308 else if (cardst4.contains(card)) 309 return account4; 310 else if (cardst5.contains(card)) 311 return account5; 312 else if (cardst6.contains(card)) 313 return account6; 314 else if (cardst7.contains(card)) 315 return account7; 316 else if (cardst8.contains(card)) 317 return account8; 318 else if (cardjt1.contains(card)) 319 return account9; 320 else if (cardjt2.contains(card)) 321 return account10; 322 else if (cardjt3.contains(card)) 323 return account11; 324 else if (cardjt4.contains(card)) 325 return account12; 326 else return null; 327 } 328 public User scanUser(Account account){ 329 if (accounts1.contains(account)) 330 return user1; 331 else if (accounts2.contains(account)) 332 return user2; 333 else if (accounts3.contains(account)) 334 return user3; 335 else if (accounts4.contains(account)) 336 return user4; 337 else if (accountsj1.contains(account)) 338 return user5; 339 else if (accountsj2.contains(account)) 340 return user6; 341 else if (accountsj3.contains(account)) 342 return user7; 343 else if (accountsj4.contains(account)) 344 return user8; 345 else return null; 346 } 347 348 public boolean IscrossBank(User user,String atmid){ 349 if (atm1.getId().equals(atmid) && atm1.getUser().contains(user)) 350 return true; 351 if (atm2.getId().equals(atmid) && atm2.getUser().contains(user)) 352 return true; 353 if (atm3.getId().equals(atmid) && atm3.getUser().contains(user)) 354 return true; 355 if (atm4.getId().equals(atmid) && atm4.getUser().contains(user)) 356 return true; 357 if (atm5.getId().equals(atmid) && atm5.getUser().contains(user)) 358 return true; 359 if (atm6.getId().equals(atmid) && atm6.getUser().contains(user)) 360 return true; 361 if (atm7.getId().equals(atmid) && atm7.getUser().contains(user)) 362 return true; 363 if (atm8.getId().equals(atmid) && atm8.getUser().contains(user)) 364 return true; 365 if (atm9.getId().equals(atmid) && atm9.getUser().contains(user)) 366 return true; 367 if (atm10.getId().equals(atmid) && atm10.getUser().contains(user)) 368 return true; 369 if (atm11.getId().equals(atmid) && atm11.getUser().contains(user)) 370 return true; 371 return false; 372 } 373 } 374 375 376 class Bank{ 377 String name; 378 ArrayList<ATM>atms = new ArrayList<ATM>(); 379 Bank(){ 380 } 381 Bank(String name,ArrayList<ATM> atms){ 382 this.name = name; 383 this.atms = atms; 384 } 385 386 public void setName(String name) { 387 this.name = name; 388 } 389 390 public String getName() { 391 return name; 392 } 393 394 public ArrayList<ATM> getAtm() { 395 return atms; 396 } 397 398 public void setAtm(ArrayList<ATM> atms) { 399 this.atms = atms; 400 } 401 402 403 } 404 class User{ 405 String name; 406 String bankName; 407 ArrayList<Account> accounts = new ArrayList<Account>(); 408 User(){ 409 } 410 User(String name,String bankName,ArrayList<Account>accounts){ 411 this.name = name; 412 this.bankName = bankName; 413 this.accounts = accounts; 414 } 415 416 public String getName() { 417 return name; 418 } 419 420 public void setName(String name) { 421 this.name = name; 422 } 423 424 public String getBankName() { 425 return bankName; 426 } 427 428 public void setBankName(String bankName) { 429 this.bankName = bankName; 430 } 431 432 public ArrayList<Account> getAccount() { 433 return accounts; 434 } 435 436 public void setAccount(ArrayList<Account> account) { 437 this.accounts = account; 438 } 439 public void addAccount(Account account){ 440 accounts.add(account); 441 } 442 } 443 class Account{ 444 String id; 445 double balance = 10000; 446 ArrayList<String> cards = new ArrayList<String>(); 447 Account(){ 448 } 449 Account(String id,double balance,ArrayList<String> cards ){ 450 this.id = id; 451 this.balance = balance; 452 this.cards = cards; 453 } 454 455 public String getId() { 456 return id; 457 } 458 459 public void setId(String id) { 460 this.id = id; 461 } 462 463 public ArrayList<String> getCard() { 464 return cards; 465 } 466 467 public void setCard(ArrayList<String> cards) { 468 this.cards = cards; 469 } 470 471 public double getBalance() { 472 return balance; 473 } 474 475 public void setBalance(double balance) { 476 /* if (-balance > 50000){ 477 System.out.print("Sorry,your account balance is insufficient."); 478 System.exit(0); 479 }*/ 480 this.balance = balance; 481 } 482 } 483 484 class Card{ 485 String id; 486 String password; 487 Card(){ 488 } 489 Card(String id){ 490 this.id = id; 491 } 492 493 public String getId() { 494 return id; 495 } 496 497 public void setId(String id) { 498 this.id = id; 499 } 500 501 public String getPassword() { 502 return password; 503 } 504 505 public void setPassword(String password) { 506 this.password = password; 507 } 508 } 509 class ATM{ 510 String id; 511 String bankName; 512 ArrayList<User> users = new ArrayList<User>(); 513 ATM(){ 514 } 515 ATM(String id , String bankName,ArrayList<User> users){ 516 this.id = id; 517 this.bankName = bankName; 518 this.users = users; 519 } 520 521 public void setId(String id) { 522 this.id = id; 523 } 524 525 public String getId() { 526 return id; 527 } 528 529 public void setBankName(String bankName) { 530 this.bankName = bankName; 531 } 532 533 public String getBankName() { 534 return bankName; 535 } 536 537 public ArrayList<User> getUser() { 538 return users; 539 } 540 541 public void setUser(ArrayList<User> users) { 542 this.users = users; 543 } 544 }
(2)题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
两道题生成的报表:
两道题的类图:
题目集八和题目集九都是先构建六个实体类,Initall类(用来初始化数据)、Bank类、User类、Account类、Card类、ATM类,这六个类都有特定的属性,比如ATM类有id和bankname,Card类有id和password,Account类有id和balance,Bank类有name和ATM数组,然后使用ArrayList数组将各个类进行连接,使两个类之间实现一对多的关系,完成类间关系的设计。题目集九在题目集八的基础上增加了信用卡和跨行取款,需要考虑透支取款和手续费的情况。手续费默认均从银行卡所对应的账户中自动扣除。跨行业务手续费收取比例由 ATM 机隶属银行决定,例如,用户手持中国 工商银行的银行卡在中国建设银行的 ATM 上进行取款操作,则手续费收取比例为 中国建设银行的相应比例,按初始化数据中的规定为取款金额的 2%。跨行查询余额不收取手续费。透支取款手续费由中国银联统一规定为 5%,最大透支金额均为 50000 元。
三、采坑心得:
(1)在题目集九中,由于我scanAccount方法返回的account账户错误,导致输入案例输出错误,修改后:
1 public Account scanAccount(String card){ 2 int flag = 1; 3 if (cardst1.contains(card)) 4 return account1; 5 else if (cardst2.contains(card)) 6 return account2; 7 else if (cardst3.contains(card)) 8 return account3; 9 else if (cardst4.contains(card)) 10 return account4; 11 else if (cardst5.contains(card)) 12 return account5; 13 else if (cardst6.contains(card)) 14 return account6; 15 else if (cardst7.contains(card)) 16 return account7; 17 else if (cardst8.contains(card)) 18 return account8; 19 else if (cardjt1.contains(card)) 20 return account9; 21 else if (cardjt2.contains(card)) 22 return account10; 23 else if (cardjt3.contains(card)) 24 return account11; 25 else if (cardjt4.contains(card)) 26 return account12; 27 else return null; 28 }
(2)最大透支金额均为 50000 元,最开始没有考虑到取全部金额后透支超过50000元的情况,出现了错误:
1 if (account.getBalance() - m <= 0) { 2 if ((user.getName().equals("张三丰") || user.getName().equals("令狐冲") || user.getName().equals("乔峰") || user.getName().equals("洪七公")) && m - account.getBalance() <= 50000) { 3 if (flag == 0) { 4 d = m - account.getBalance(); 5 flag++; 6 } else { 7 d = m; 8 } 9 account.setBalance(account.getBalance() - d * 0.05); //透支手续费 10 if (!initall.IscrossBank(user, atm.getId())) { 11 switch (bank.getName()) { 12 case "中国建设银行": 13 account.setBalance(account.getBalance() - m - m * 0.02); 14 break; 15 case "中国工商银行": 16 account.setBalance(account.getBalance() - m - m * 0.03); 17 break; 18 case "中国农业银行": 19 account.setBalance(account.getBalance() - m - m * 0.04); 20 break; 21 default: 22 break; 23 } 24 } else { 25 account.setBalance(account.getBalance() - m); 26 } 27 } else if (account.getBalance() - m <= 0) { 28 System.out.println("Sorry,your account balance is insufficient."); 29 System.exit(0); 30 } 31 } else { 32 if (!initall.IscrossBank(user, atm.getId())) { 33 switch (bank.getName()) { 34 case "中国建设银行": 35 account.setBalance(account.getBalance() - m - m * 0.02); 36 break; 37 case "中国工商银行": 38 account.setBalance(account.getBalance() - m - m * 0.03); 39 break; 40 case "中国农业银行": 41 account.setBalance(account.getBalance() - m - m * 0.04); 42 break; 43 default: 44 break; 45 } 46 } else { 47 account.setBalance(account.getBalance() - m); 48 } 49 }
(3)题目集7中的7-2:重写图片排序的方法
1 Collections.sort(cardList1, new Comparator<Card>() { 2 @Override 3 public int compare(Card o1, Card o2) { 4 return -((int)(o1.getShape().getArea() )-(int)( o2.getShape().getArea())); 5 // return -(int)(o1.getShape().getArea() - o2.getShape().getArea()); 6 } 7 });
四、改进建议:
(1)在题目集九中根据输入的信息查找用户和账户时不够简便,可以通过ArrayList数组存储的数据进行查询,修改后:
1 public User scanUser(Account account){ 2 if (accounts1.contains(account)) 3 return user1; 4 else if (accounts2.contains(account)) 5 return user2; 6 else if (accounts3.contains(account)) 7 return user3; 8 else if (accounts4.contains(account)) 9 return user4; 10 else if (accountsj1.contains(account)) 11 return user5; 12 else if (accountsj2.contains(account)) 13 return user6; 14 else if (accountsj3.contains(account)) 15 return user7; 16 else if (accountsj4.contains(account)) 17 return user8; 18 else return null; 19 } 20 21 public boolean IscrossBank(User user,String atmid){ 22 if (atm1.getId().equals(atmid) && atm1.getUser().contains(user)) 23 return true; 24 if (atm2.getId().equals(atmid) && atm2.getUser().contains(user)) 25 return true; 26 if (atm3.getId().equals(atmid) && atm3.getUser().contains(user)) 27 return true; 28 if (atm4.getId().equals(atmid) && atm4.getUser().contains(user)) 29 return true; 30 if (atm5.getId().equals(atmid) && atm5.getUser().contains(user)) 31 return true; 32 if (atm6.getId().equals(atmid) && atm6.getUser().contains(user)) 33 return true; 34 if (atm7.getId().equals(atmid) && atm7.getUser().contains(user)) 35 return true; 36 if (atm8.getId().equals(atmid) && atm8.getUser().contains(user)) 37 return true; 38 if (atm9.getId().equals(atmid) && atm9.getUser().contains(user)) 39 return true; 40 if (atm10.getId().equals(atmid) && atm10.getUser().contains(user)) 41 return true; 42 if (atm11.getId().equals(atmid) && atm11.getUser().contains(user)) 43 return true; 44 return false; 45 }
(2)题目集7中的7-2输出结果不够简洁,增加了一个Output()方法,使代码更加直观,修改后:
1 public void showResult(){ 2 System.out.println("The original list:"); 3 System.out.print("["); 4 for (int i = 0 ; i < cardList.size() ; i ++){ 5 System.out.printf("%s:%.2f ",cardList.get(i).getShape().shapeName,cardList.get(i).getShape().getArea()); 6 } 7 System.out.println("]"); 8 System.out.println("The Separated List:"); 9 Output(cardList1); 10 Output(cardList2); 11 Output(cardList3); 12 Output(cardList4); 13 14 System.out.println(); 15 System.out.println("The Separated sorted List:"); 16 cardSort(); 17 System.out.println(); 18 System.out.printf("The max area:%.2f",getAllArea()); 19 } 20 public void Output(ArrayList<Card>cards){ 21 System.out.print("["); 22 for (int i = 0 ; i < cards.size() ; i ++){ 23 System.out.printf("%s:%.2f ",cards.get(i).getShape().shapeName,cards.get(i).getShape().getArea()); 24 } 25 System.out.print("]"); 26 }
五、总结:
通过这几次作业,让我对JAVA是一门面向对象的编程语言有了更深刻的了解,对JAVA三大特性:封装、继承和多态的运用也更加熟练。这次的题目集较前两次都更难,对JAVA知识点进行了综合性的运用,比如类的设计、抽象类的应用、继承和多态的运用、ArrayList数组的应用和接口的使用,感受到JAVA单一职责原则和开放封闭原则。刚看到最后两个习题集,没有类图要自己进行类的设计是有点懵的,理清了思路画出类图后题目清晰了很多,在写代码的过程中要仔细不能马虎,不然就是和室友一起找bug,一起debug一整晚。经过这段时间的学习还是收获了很多东西,也发现了自己存在的不足,对Java面对对象的思想还不够成熟,学无止境。
这次还学习到接口的用法:一个接口可以有多个直接父接口,但接口只能继承接口,不能继承类。具有 public 访问控制符的接口,允许任何类使用;没有指定 public 的接口,其访问将局限于所属的包。方法的声明不需要其他修饰符,在接口中声明的方法,将隐式地声明为公有的(public)和抽象的(abstract)。在 Java 接口中声明的变量其实都是常量,接口中的变量声明,将隐式地声明为 public、static 和 final,即常量,所以接口中定义的变量必须初始化。一个接口不能够实现另一个接口,但它可以继承多个其他接口。子接口可以对父接口的方法和常量进行重写。接口的主要用途就是被实现类实现,一个类可以实现一个或多个接口,继承使用 extends 关键字,实现则使用 implements 关键字。因为一个类可以实现多个接口,这也是 Java 为单继承灵活性不足所作的补充。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人