Blog 3 第三阶段Java大作业总结
【题目一览 7-1】
7-1 图形卡片排序游戏 (40 分)
掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 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:
在这里给出一组输入。例如:
1 5 3 2 0
结尾无空行
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
输入样例2:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
结尾无空行
输出样例2:
在这里给出相应的输出。例如:
The original list:
Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02
The sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91
结尾无空行
输入样例3:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
结尾无空行
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
【源码】
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;
public class Main {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}
class DealCardList{
ArrayList<Card> cardList=new ArrayList<>();
public DealCardList() {
}
public DealCardList(ArrayList<Integer> card) {
for (Integer integer : card) {
if (integer==0)break;
switch (integer){
case 1:
Card card1=new Card(new Circle(Main.input.nextDouble()));
card1.getShape().setShapeName("Circle");
cardList.add(card1);
break;
case 2:
Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
card2.getShape().setShapeName("Rectangle");
cardList.add(card2);
break;
case 3:
Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
card3.getShape().setShapeName("Triangle");
cardList.add(card3);
break;
case 4:
Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
card4.getShape().setShapeName("Trapezoid");
cardList.add(card4);
break;
}
}
}
public boolean validate(){
boolean ret=true;
for (Card card : cardList) {
if (!card.getShape().validate()){
ret=false;
break;
}
}
return ret;
}
public void cardSort(){
TreeSet<Card> cards = new TreeSet<>(cardList);
for (Card card : cards) {
System.out.print(card.getShape());
}
}
public double getAllArea(){
double sum=0;
for (Card card : cardList) {
sum+=card.getShape().getArea();
}
return sum;
}
public void showResult(){
System.out.println("The original list:");
for (Card card : cardList) {
System.out.print(card.getShape());
}
System.out.println();
System.out.println("The sorted list:");
cardSort();
System.out.println();
System.out.printf("Sum of area:%.2f\n",getAllArea());
}
}
class Card implements Comparable<Card>{
private Shape shape;
public Card() {
}
public Card(Shape shape) {
this.shape = shape;
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
@Override
public int compareTo(Card card) {
return -(int)(shape.getArea()-card.getShape().getArea());
}
}
abstract class Shape{
private String shapeName;
public Shape() {
}
public Shape(String shapeName) {
this.shapeName = shapeName;
}
public String getShapeName() {
return shapeName;
}
public void setShapeName(String shapeName) {
this.shapeName = shapeName;
}
public abstract double getArea();
public abstract boolean validate();
@Override
public String toString() {
return getShapeName()+":"+String.format("%.2f ",getArea());
}
}
class Circle extends Shape{
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI*radius*radius;
}
@Override
public boolean validate() {
return this.radius>0;
}
}
class Rectangle extends Shape{
private double width,height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public double getArea() {
return height*width;
}
@Override
public boolean validate() {
return width>0&&height>0;
}
}
class Triangle extends Shape{
private double side1,side2,side3;
public Triangle() {
}
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double getArea() {
double p=(side1+side2+side3)/2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
@Override
public boolean validate() {
boolean ret=true;
if (!(side1>0&&side3>0&&side2>0))ret=false;
else{
if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1))ret=false;
}
return ret;
}
}
class Trapezoid extends Shape{
private double topSide,bottomSide,height;
public Trapezoid() {
}
public Trapezoid(double topSide, double bottomSide, double height) {
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
}
@Override
public double getArea() {
return (topSide+bottomSide)*height/2;
}
@Override
public boolean validate() {
return topSide>0&&height>0&&bottomSide>0;
}
}
【源码分析】

【题目一览 7-2】
7-2 图形卡片分组游戏 (60 分)
掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。
输入格式:
- 在一行上输入一串数字(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:
在这里给出一组输入。例如:
1 5 3 2 0
结尾无空行
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
输入样例2:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
结尾无空行
输出样例2:
在这里给出相应的输出。例如:
The original list:
[Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 ]
The Separated List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The Separated sorted List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The max area:98.52
结尾无空行
输入样例3:
在这里给出一组输入。例如:
2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65
结尾无空行
输出样例3:
在这里给出相应的输出。例如:
The original list:
[Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]
The Separated List:
[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]
The Separated sorted List:
[Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
The max area:1601.31
结尾无空行
输入样例4:
在这里给出一组输入。例如:
1 1 3 0
6.5 12.54 3.6 5.3 6.4
结尾无空行
输出样例4:
在这里给出相应的输出。例如:
The original list:
[Circle:132.73 Circle:494.02 Triangle:9.54 ]
The Separated List:
[Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]
The Separated sorted List:
[Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]
The max area:626.75
结尾无空行
【代码】
1 import java.util.Scanner; 2 import java.util.Arrays; 3 import java.util.ArrayList; 4 import java.util.TreeSet; 5 class Handle{ 6 ArrayList<Card> cardList=new ArrayList<>(); 7 double[] ptr=new double[4]; 8 9 public Handle(ArrayList<Integer> c){ 10 for (Integer a:c){ 11 if (a==0){ 12 break; 13 } 14 else { 15 switch (a) { 16 case 1 : 17 Card a1 = new Card(new Circle(Main.x.nextDouble())); 18 cardList.add(a1); 19 a1.getShape().setName("Circle"); 20 break; 21 case 2 : 22 Card b1 = new Card(new Rectangle(Main.x.nextDouble(), Main.x.nextDouble())); 23 cardList.add(b1); 24 b1.getShape().setName("Rectangle"); 25 break; 26 case 3 : 27 Card c1 = new Card(new Triangle(Main.x.nextDouble(), Main.x.nextDouble(), Main.x.nextDouble())); 28 cardList.add(c1); 29 c1.getShape().setName("Triangle"); 30 break; 31 case 4 : 32 Card d1 = new Card(new Trapezoid(Main.x.nextDouble(), Main.x.nextDouble(), Main.x.nextDouble())); 33 cardList.add(d1); 34 d1.getShape().setName("Trapezoid"); 35 break; 36 } 37 } 38 } 39 } 40 //判断数据是否合法 41 public boolean pdsfhf(){ 42 boolean kp=true; 43 for (Card a:cardList){ 44 if (!a.getShape().pdsfhf()){ 45 kp=false; 46 break; 47 } 48 } 49 return kp; 50 } 51 //输出圆的数据并计算总面积 52 public void yuan(){ 53 ptr[0]=0; 54 System.out.print("["); 55 for (Card a:cardList){ 56 if(a.getShape().getName().equals("Circle")){ 57 System.out.print(a.getShape()); 58 ptr[0]=ptr[0]+a.getShape().Mj(); 59 } 60 } 61 System.out.print("]"); 62 } 63 //输出矩形的数据并计算总面积 64 public void juxing(){ 65 ptr[1]=0; 66 System.out.print("["); 67 for (Card a:cardList){ 68 if(a.getShape().getName().equals("Rectangle")){ 69 System.out.print(a.getShape()); 70 ptr[1]=ptr[1]+a.getShape().Mj(); 71 } 72 } 73 System.out.print("]"); 74 } 75 //输出三角形的数据并计算总面积 76 public void sanjiaoxing(){ 77 ptr[2]=0; 78 System.out.print("["); 79 for (Card a:cardList){ 80 if(a.getShape().getName().equals("Triangle")){ 81 System.out.print(a.getShape()); 82 ptr[2]=ptr[2]+a.getShape().Mj(); 83 } 84 } 85 System.out.print("]"); 86 } 87 //输出梯形的数据并计算总面积 88 public void tixing(){ 89 ptr[3]=0; 90 System.out.print("["); 91 for (Card a:cardList){ 92 if(a.getShape().getName().equals("Trapezoid")){ 93 System.out.print(a.getShape()); 94 ptr[3]=ptr[3]+a.getShape().Mj(); 95 } 96 } 97 System.out.print("]"); 98 } 99 //从大到小输出圆的数据 100 public void ypx(){ 101 TreeSet<Card> kp = new TreeSet<>(cardList); 102 System.out.print("["); 103 for (Card a:kp){ 104 if(a.getShape().getName().equals("Circle")){ 105 System.out.print(a.getShape()); 106 } 107 } 108 System.out.print("]"); 109 } 110 //从大到小输出矩形的数据 111 public void jxpx(){ 112 TreeSet<Card> kp = new TreeSet<>(cardList); 113 System.out.print("["); 114 for (Card a:kp){ 115 if(a.getShape().getName().equals("Rectangle")){ 116 System.out.print(a.getShape()); 117 } 118 } 119 System.out.print("]"); 120 } 121 //从大到小输出三角形的数据 122 public void sjxpx(){ 123 TreeSet<Card> kp = new TreeSet<>(cardList); 124 System.out.print("["); 125 for (Card a:kp){ 126 if(a.getShape().getName().equals("Triangle")){ 127 System.out.print(a.getShape()); 128 } 129 } 130 System.out.print("]"); 131 } 132 //从大到小输出梯形的数据 133 public void txpx(){ 134 TreeSet<Card> kp = new TreeSet<>(cardList); 135 System.out.print("["); 136 for (Card a:kp){ 137 if(a.getShape().getName().equals("Trapezoid")){ 138 System.out.print(a.getShape()); 139 } 140 } 141 System.out.print("]"); 142 } 143 //找出最大总面积 144 public double getMax(){ 145 double max=0; 146 int i; 147 for (i=0;i<4;i++){ 148 if(max<ptr[i]){ 149 max=ptr[i]; 150 } 151 } 152 return max; 153 } 154 public void show(){//输出 155 System.out.println("The original list:"); 156 System.out.print("["); 157 for (Card a : cardList) { 158 System.out.print(a.getShape()); 159 } 160 System.out.print("]"); 161 System.out.println("\nThe Separated List:"); 162 yuan();juxing();sanjiaoxing();tixing(); 163 System.out.println("\nThe Separated sorted List:"); 164 ypx();jxpx();sjxpx();txpx(); 165 System.out.printf("\nThe max area:%.2f\n",getMax()); 166 } 167 } 168 //Card类 169 class Card implements Comparable<Card>{ 170 private Shape shape; 171 //创建无参构造方法 172 public Card() { 173 174 } 175 //创建带参构造方法 176 public Card(Shape shape) { 177 178 179 this.shape = shape; 180 } 181 //getter 182 public Shape getShape() { 183 return shape; 184 } 185 //setter 186 public void setShape(Shape shape) { 187 this.shape = shape; 188 } 189 public int compareTo(Card card) { 190 return -(int)(shape.Mj()-card.getShape().Mj()); 191 } 192 } 193 //Shape类 194 abstract class Shape { 195 private String name; 196 public Shape() { 197 } 198 public Shape(String name) { 199 this.name = name; 200 } 201 public String getName() { 202 203 return name; 204 } 205 public void setName(String name) { 206 this.name = name; 207 } 208 public abstract double Mj(); 209 public abstract boolean pdsfhf(); 210 public String toString() { 211 return getName()+":"+String.format("%.2f ",Mj()); 212 } 213 } 214 class Circle extends Shape{ 215 private double r; 216 public Circle(){ 217 } 218 public Circle(double r){ 219 this.r=r; 220 } 221 public double getR(){ 222 return r; 223 } 224 public void setR(double r){ 225 this.r = r; 226 } 227 public boolean pdsfhf() { 228 return r>0; 229 } 230 public double Mj(){ 231 double mj=Math.PI*r*r; 232 return mj; 233 } 234 } 235 class Rectangle extends Shape{ 236 private double a,b; 237 public Rectangle(){ 238 } 239 public Rectangle(double a, double b){ 240 this.a=a; 241 this.b=b; 242 } 243 public double A(){ 244 return a; 245 } 246 public double B(){ 247 return b; 248 } 249 public void setA(double a){ 250 this.a=a; 251 } 252 public void setB(double b){ 253 this.b=b; 254 } 255 public boolean pdsfhf() { 256 return a>0&&b>0; 257 } 258 public double Mj(){ 259 return a*b; 260 } 261 } 262 class Triangle extends Shape{ 263 private double a; 264 private double b; 265 private double c; 266 public Triangle() { 267 } 268 public Triangle(double a, double b, double c){ 269 this.a = a; 270 this.b = b; 271 this.c = c; 272 } 273 public double A(){ 274 return a; 275 } 276 public double B(){ 277 return b; 278 } 279 public double C(){ 280 return c; 281 } 282 public void setA(double a){ 283 this.a=a; 284 } 285 public void setB(double b){ 286 this.b=b; 287 } 288 public void setC(double c){ 289 this.c=c; 290 } 291 public boolean pdsfhf(){ 292 Double[] bc=new Double[3]; 293 bc[0]=a;bc[1]=b;bc[2]=c; 294 boolean z=true; 295 Arrays.sort(bc); 296 if(a<=0&&b<=0&&c<=0){ 297 z=false; 298 } 299 else{ 300 if(!(a+b>c)){ 301 z=false; 302 } 303 } 304 return z; 305 } 306 public double Mj(){ 307 double s1,s2; 308 s1=(a+b+c)/2; 309 s2=Math.sqrt(s1*(s1-a)*(s1-b)*(s1-c)); 310 return s2; 311 } 312 } 313 class Trapezoid extends Shape{ 314 private double c,b,h; 315 public Trapezoid() { 316 } 317 public Trapezoid(double c, double b, double h) { 318 this.c = c; 319 this.b = b; 320 this.h = h; 321 } 322 public double C(){ 323 return c; 324 } 325 public double B(){ 326 return b; 327 } 328 public double H(){ 329 return h; 330 } 331 public void setC(double a){ 332 this.c=c; 333 } 334 public void setB(double b){ 335 this.b=b; 336 } 337 public void setH(double c){ 338 this.h=h; 339 } 340 public boolean pdsfhf() { 341 return b>0&&c>0&&h>0; 342 } 343 public double Mj() { 344 return (c+b)*h/2; 345 } 346 } 347 348 public class Main{ 349 public static Scanner x = new Scanner(System.in); 350 public static void main(String[] args){ 351 ArrayList<Integer> xzlist = new ArrayList<>(); 352 int xz; 353 xz=x.nextInt(); 354 if (xz==0){ 355 System.out.println("Wrong Format"); 356 System.exit(0); 357 } 358 while (xz!=0){ 359 if(xz>4||xz<0){ 360 System.out.println("Wrong Format"); 361 System.exit(0); 362 } 363 xzlist.add(xz); 364 xz=x.nextInt(); 365 } 366 Handle handle=new Handle(xzlist); 367 if(!handle.pdsfhf()){ 368 System.out.println("Wrong Format"); 369 System.exit(0); 370 } 371 handle.show(); 372 x.close(); 373 } 374 }
【类图】

【源码分析】

【题目一览 8】
7-3 ATM机类结构设计(一) (100 分)
设计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:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
#
结尾无空行
输出样例1:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
结尾无空行
输入样例2:
在这里给出一组输入。例如:
6217000010041315709 88888888 02 3500.00
#
结尾无空行
输出样例2:
在这里给出相应的输出。例如:
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥6500.00
结尾无空行
输入样例3:
在这里给出一组输入。例如:
6217000010041315715
#
结尾无空行
输出样例3:
在这里给出相应的输出。例如:
¥10000.00
结尾无空行
输入样例4:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709 88888888 02 3500.00
6217000010041315715
#
结尾无空行
输出样例4:
在这里给出相应的输出。例如:
张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
¥5000.00
结尾无空行
【源代码】
1 import java.util.Scanner; 2 import java.util.HashMap; 3 import java.util.Map; 4 import java.text.DecimalFormat; 5 public class Main{ 6 Scanner input=new Scanner(System.in); 7 public static void main(String[] args) { 8 UnionPay unionpay=new UnionPay(); 9 Bank bank=new Bank(); 10 User user=new User(); 11 Account account=new Account(); 12 Bankcard bankcard=new Bankcard(); 13 ATM atm=new ATM(); 14 Scanner input=new Scanner(System.in); 15 StringBuilder stringbuilder = new StringBuilder(); 16 String b=input.nextLine(); 17 while(!b.equals("#")) { 18 stringbuilder.append(b).append("\n"); 19 b=input.nextLine(); 20 21 } 22 String stringstring= String.valueOf(stringbuilder); 23 String rows[]=stringstring.split("\n"); 24 for(int i=0;i<rows.length;i++) { 25 String dan[]=rows[i].split(" "); 26 bankcard.put(i,rows); 27 bankcard.check(user,dan.length); 28 if(bankcard.flag!=0) 29 System.exit(0); 30 else { 31 if(dan.length!=1) 32 atm.showcz(bankcard,bank); 33 else 34 atm.showmoney(bankcard); 35 } 36 } 37 } 38 } 39 class UnionPay{ 40 Map<String, String> union = new HashMap<String, String>();{ 41 this.union.put("中国工商银行","银联"); 42 this.union.put("中国建设银行","银联"); 43 44 } 45 } 46 class Bank extends UnionPay{ 47 Map<String, String> bank1 = new HashMap<String, String>();{ 48 this.bank1.put("杨过","中国建设银行"); 49 this.bank1.put("郭靖","中国建设银行"); 50 this.bank1.put("张无忌","中国工商银行"); 51 this.bank1.put("韦小宝","中国工商银行"); 52 53 } 54 Map<String, String> kind = new HashMap<String, String>();{ 55 this.kind.put("6217000010041315709","借记账号"); 56 this.kind.put("6217000010041315715","借记账号"); 57 this.kind.put("6217000010041315718","借记账号"); 58 this.kind.put("6217000010051320007","借记账号"); 59 this.kind.put("6222081502001312389","借记账号"); 60 this.kind.put("6222081502001312390","借记账号"); 61 this.kind.put("6222081502001312399","借记账号"); 62 this.kind.put("6222081502001312400","借记账号"); 63 this.kind.put("6222081502051320785","借记账号"); 64 this.kind.put("6222081502051320786","借记账号"); 65 66 } 67 Map<String, String> sxf = new HashMap<String, String>();{ 68 this.sxf.put("中国建设银行","0.02"); 69 this.sxf.put("中国工商银行","0.03"); 70 71 } 72 } 73 class User extends Bank{ 74 Map<String, String> yangguo = new HashMap<String, String>();{ 75 this.yangguo.put("6217000010041315709","杨过"); 76 this.yangguo.put("6217000010041315715","杨过"); 77 this.yangguo.put("6217000010041315718","杨过"); 78 this.yangguo.put("6217000010051320007","郭靖"); 79 this.yangguo.put("6222081502001312389","张无忌"); 80 this.yangguo.put("6222081502001312390","张无忌"); 81 this.yangguo.put("6222081502001312399","张无忌"); 82 this.yangguo.put("6222081502001312400","张无忌"); 83 this.yangguo.put("6222081502051320785","韦小宝"); 84 this.yangguo.put("6222081502051320786","韦小宝"); 85 86 } 87 } 88 class ATM extends User{ 89 Map<String, String> giao = new HashMap<String, String>();{ 90 this.giao.put("01","中国建设银行"); 91 this.giao.put("02","中国建设银行"); 92 this.giao.put("03","中国建设银行"); 93 this.giao.put("04","中国建设银行"); 94 this.giao.put("05","中国工商银行"); 95 this.giao.put("06","中国工商银行"); 96 97 } 98 DecimalFormat df2 = new DecimalFormat("####0.00"); 99 public void showcz(Bankcard bankcard,Bank bank) { 100 if(bankcard.money>0) { 101 System.out.println(super.yangguo.get(bankcard.card)+"在"+giao.get(bankcard.bh)+"的"+bankcard.bh+"号ATM机上取款¥"+df2.format(bankcard.money)); 102 System.out.println("当前余额为¥"+df2.format(bankcard.yue())); 103 104 } 105 else { 106 System.out.println(super.yangguo.get(bankcard.card)+"在"+giao.get(bankcard.bh)+"的"+bankcard.bh+"号ATM机上存款¥"+df2.format(-bankcard.money)); 107 System.out.println("当前余额为¥"+df2.format(bankcard.yue())); 108 } 109 } 110 public void showmoney(Bankcard bankcard) { 111 System.out.println("¥"+df2.format(bankcard.yue())); 112 113 } 114 } 115 class Account{ 116 117 } 118 class Bankcard extends ATM{ 119 Map<String, String> qian = new HashMap<String, String>();{ 120 this.qian.put("杨过","1"); 121 this.qian.put("郭靖","2"); 122 this.qian.put("张无忌","3"); 123 this.qian.put("韦小宝","4"); 124 } 125 String card; 126 String passport; 127 String bh; 128 double money; 129 double[] startmoney={10000.00,10000.00,10000.00,10000.00,10000.00,10000.00,10000.00,10000.00}; 130 int flag=0; 131 public void put(int i,String rows[]) { 132 rows[i]=rows[i].replaceAll("\\s+", " "); 133 String dan[]=rows[i].split(" "); 134 int length=dan.length; 135 if(length!=1) { 136 card=dan[0]; 137 passport=dan[1]; 138 bh=dan[2]; 139 money=Double.parseDouble(dan[3]); 140 } 141 else { 142 card=dan[0]; 143 passport="88888888"; 144 bh="01"; 145 money=0; 146 } 147 } 148 public void check(User user,int length) { 149 if(user.yangguo.get(card)==null) { 150 System.out.println("Sorry,this card does not exist."); 151 flag++; 152 } 153 else if(!passport.equals("88888888")) { 154 System.out.println("Sorry,your password is wrong."); 155 flag++; 156 } 157 else if(!bh.equals("11")&&!bh.equals("10")&&!bh.equals("09")&&!bh.equals("08")&&!bh.equals("07")&&!bh.equals("06")&&!bh.equals("05")&&!bh.equals("04")&&!bh.equals("03")&&!bh.equals("02")&&!bh.equals("01")) { 158 System.out.println("Sorry,the ATM's id is wrong."); 159 flag++; 160 } 161 else if((kind.get(card).equals("借记账号")&&money>startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1])||(kind.get(card).equals("借记账号")&&sfkh()&&money==startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1])||(yue1(startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1],money)<-50000)) { 162 System.out.println("Sorry,your account balance is insufficient."); 163 flag++; 164 } 165 else 166 flag=0; 167 } 168 public double yue() { 169 if(startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]<money&&startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]>0) { 170 startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]=startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]-(money-startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1])*0.05; 171 } 172 if(startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]<money&&startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]<=0) { 173 startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]=startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]-money*0.05; 174 } 175 if(sfkh()) 176 startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]=startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]-money-money*Double.parseDouble(sxf.get(giao.get(bh))); 177 else 178 startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]=startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]-money; 179 return startmoney[Integer.valueOf(qian.get(yangguo.get(card)))-1]; 180 } 181 public double yue1(double a,double money1) { 182 if(a<money1&&a>0) { 183 a=a-(money1-a)*0.05; 184 } 185 if(a<money1&&a<=0) { 186 a=a-money1*0.05; 187 } 188 if(sfkh()) 189 a=a-money1-money1*Double.parseDouble(sxf.get(giao.get(bh))); 190 else 191 a=a-money1; 192 return a; 193 } 194 public boolean sfkh() { 195 if(bank1.get(yangguo.get(card)).equals(giao.get(bh))) 196 return false; 197 else 198 return true; 199 } 200 }
【类图设计】

【源码分析】

【题目一览 9】
7-1 ATM机类结构设计(二) (100 分)
设计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:
在这里给出一组输入。例如:
6222081502001312390 88888888 06 500.00
#
结尾无空行
输出样例1:
在这里给出相应的输出。例如:
业务:取款 张无忌在中国工商银行的06号ATM机上取款¥500.00
当前余额为¥9500.00
结尾无空行
输入样例2:
在这里给出一组输入。例如:
6217000010041315709 88888888 06 3500.00
#
结尾无空行
输出样例2:
在这里给出相应的输出。例如:
业务:取款 杨过在中国工商银行的06号ATM机上取款¥3500.00
当前余额为¥6395.00
结尾无空行
输入样例3:
在这里给出一组输入。例如:
6217000010041315715
#
结尾无空行
输出样例3:
在这里给出相应的输出。例如:
业务:查询余额 ¥10000.00
结尾无空行
输入样例4:
在这里给出一组输入。例如:
6222081502001312390 88888888 01 500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709 88888888 02 3500.00
6217000010041315715
#
结尾无空行
输出样例4:
在这里给出相应的输出。例如:
业务:取款 张无忌在中国建设银行的01号ATM机上取款¥500.00
当前余额为¥9490.00
业务:取款 韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
业务:取款 杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
业务:取款 杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
业务:查询余额 ¥5000.00
结尾无空行
输入样例5:
在这里给出一组输入。例如:
6640000010045442002 88888888 09 3000
6640000010045442002 88888888 06 8000
6640000010045442003 88888888 01 10000
6640000010045442002
#
结尾无空行
输出样例5:
在这里给出相应的输出。例如:
业务:取款 张三丰在中国农业银行的09号ATM机上取款¥3000.00
当前余额为¥6880.00
业务:取款 张三丰在中国工商银行的06号ATM机上取款¥8000.00
当前余额为¥-1416.00
业务:取款 张三丰在中国建设银行的01号ATM机上取款¥10000.00
当前余额为¥-11916.00
业务:查询余额 ¥-11916.00
结尾无空行
【源代码】
1 import java.util.Scanner; 2 import java.util.HashMap; 3 import java.util.Map; 4 import java.text.DecimalFormat; 5 public class Main{ 6 Scanner input=new Scanner(System.in); 7 public static void main(String[] args) { 8 UnionPay unionpay=new UnionPay(); 9 Bank bank=new Bank(); 10 User user=new User(); 11 Account account=new Account(); 12 Bankcard bankcard=new Bankcard(); 13 ATM atm=new ATM(); 14 Scanner input=new Scanner(System.in); 15 StringBuilder toBuildString = new StringBuilder(); 16 String yewu=input.nextLine(); 17 while(yewu.equals("#")==false) { 18 toBuildString.append(yewu).append("\n"); 19 20 yewu=input.nextLine(); 21 } 22 String SourceString= String.valueOf(toBuildString); 23 String rows[]=SourceString.split("\n"); 24 for(int i=0;i<rows.length;i++) { 25 String ack[]=rows[i].split(" "); 26 bankcard.put(i,rows); 27 bankcard.check(user,ack.length); 28 if(bankcard.flag!=0) 29 System.exit(0); 30 else { 31 if(ack.length!=1) 32 atm.showDepositsOrWithdrawals(bankcard,bank); 33 else 34 atm.showmoneybalance(bankcard); 35 } 36 } 37 } 38 } 39 class UnionPay{ 40 Map<String, String> Unionpay = new HashMap<String, String>();{ 41 this.Unionpay.put("中国工商银行","银联"); 42 this.Unionpay.put("中国建设银行","银联"); 43 this.Unionpay.put("中国农业银行","银联"); 44 45 } 46 } 47 class Bank extends UnionPay{ 48 Map<String, String> Bankmap = new HashMap<String, String>();{ 49 this.Bankmap.put("杨过","中国建设银行"); 50 this.Bankmap.put("郭靖","中国建设银行"); 51 this.Bankmap.put("张三丰","中国建设银行"); 52 this.Bankmap.put("张无忌","中国工商银行"); 53 this.Bankmap.put("韦小宝","中国工商银行"); 54 this.Bankmap.put("令狐冲","中国工商银行"); 55 this.Bankmap.put("乔峰","中国农业银行"); 56 this.Bankmap.put("洪七公","中国农业银行"); 57 58 } 59 Map<String, String> accountTable = new HashMap<String, String>();{ 60 this.accountTable.put("6217000010041315709","借记账号"); 61 this.accountTable.put("6217000010041315715","借记账号"); 62 this.accountTable.put("6217000010041315718","借记账号"); 63 this.accountTable.put("6217000010051320007","借记账号"); 64 this.accountTable.put("6222081502001312389","借记账号"); 65 this.accountTable.put("6222081502001312390","借记账号"); 66 this.accountTable.put("6222081502001312399","借记账号"); 67 this.accountTable.put("6222081502001312400","借记账号"); 68 this.accountTable.put("6222081502051320785","借记账号"); 69 this.accountTable.put("6222081502051320786","借记账号"); 70 this.accountTable.put("6640000010045442002","贷记账号"); 71 this.accountTable.put("6640000010045442003","贷记账号"); 72 this.accountTable.put("6640000010045441009","贷记账号"); 73 this.accountTable.put("6630000010033431001","贷记账号"); 74 this.accountTable.put("6630000010033431008","贷记账号"); 75 76 77 } 78 Map<String, String> ServicesCharge = new HashMap<String, String>();{ 79 this.ServicesCharge.put("中国建设银行","0.02"); 80 this.ServicesCharge.put("中国工商银行","0.03"); 81 this.ServicesCharge.put("中国农业银行","0.04"); 82 83 } 84 } 85 class User extends Bank{ 86 Map<String, String> Username_MappingTable = new HashMap<String, String>();{ 87 this.Username_MappingTable.put("6217000010041315709","杨过"); 88 this.Username_MappingTable.put("6217000010041315715","杨过"); 89 this.Username_MappingTable.put("6217000010041315718","杨过"); 90 this.Username_MappingTable.put("6217000010051320007","郭靖"); 91 this.Username_MappingTable.put("6222081502001312389","张无忌"); 92 this.Username_MappingTable.put("6222081502001312390","张无忌"); 93 this.Username_MappingTable.put("6222081502001312399","张无忌"); 94 this.Username_MappingTable.put("6222081502001312400","张无忌"); 95 this.Username_MappingTable.put("6222081502051320785","韦小宝"); 96 this.Username_MappingTable.put("6222081502051320786","韦小宝"); 97 this.Username_MappingTable.put("6640000010045442002","张三丰"); 98 this.Username_MappingTable.put("6640000010045442003","张三丰"); 99 this.Username_MappingTable.put("6640000010045441009","令狐冲"); 100 this.Username_MappingTable.put("6630000010033431001","乔峰"); 101 this.Username_MappingTable.put("6630000010033431008","洪七公"); 102 103 } 104 } 105 class ATM extends User{ 106 Map<String, String> ATMnum = new HashMap<String, String>();{ 107 this.ATMnum.put("01","中国建设银行"); 108 this.ATMnum.put("02","中国建设银行"); 109 this.ATMnum.put("03","中国建设银行"); 110 this.ATMnum.put("04","中国建设银行"); 111 this.ATMnum.put("05","中国工商银行"); 112 this.ATMnum.put("06","中国工商银行"); 113 this.ATMnum.put("07","中国农业银行"); 114 this.ATMnum.put("08","中国农业银行"); 115 this.ATMnum.put("09","中国农业银行"); 116 this.ATMnum.put("10","中国农业银行"); 117 this.ATMnum.put("11","中国农业银行"); 118 119 } 120 DecimalFormat df2 = new DecimalFormat("####0.00"); 121 public void showDepositsOrWithdrawals(Bankcard bankcard,Bank bank) { 122 if(bankcard.money>0) { 123 System.out.println("业务:取款 "+super.Username_MappingTable.get(bankcard.card)+"在"+ATMnum.get(bankcard.bh)+"的"+bankcard.bh+"号ATM机上取款¥"+df2.format(bankcard.money)); 124 System.out.println("当前余额为¥"+df2.format(bankcard.yue())); 125 } 126 else { 127 System.out.println("业务:存款 "+super.Username_MappingTable.get(bankcard.card)+"在"+ATMnum.get(bankcard.bh)+"的"+bankcard.bh+"号ATM机上存款¥"+df2.format(-bankcard.money)); 128 System.out.println("当前余额为¥"+df2.format(bankcard.yue())); 129 } 130 } 131 public void showmoneybalance(Bankcard bankcard) { 132 System.out.println("业务:查询余额 "+"¥"+df2.format(bankcard.yue())); 133 } 134 } 135 class Account{ 136 137 } 138 class Bankcard extends ATM{ 139 Map<String, String> Serial = new HashMap<String, String>();{ 140 this.Serial.put("杨过","1"); 141 this.Serial.put("郭靖","2"); 142 this.Serial.put("张无忌","3"); 143 this.Serial.put("韦小宝","4"); 144 this.Serial.put("张三丰","5"); 145 this.Serial.put("令狐冲","6"); 146 this.Serial.put("乔峰","7"); 147 this.Serial.put("洪七公","8"); 148 149 } 150 String card; 151 String password4; 152 String bh; 153 double money; 154 double[] startmoney={10000.00,10000.00,10000.00,10000.00,10000.00,10000.00,10000.00,10000.00}; 155 int flag=0; 156 public void put(int i,String rows[]) { 157 rows[i]=rows[i].replaceAll("\\s+", " "); 158 String ack[]=rows[i].split(" "); 159 int length=ack.length; 160 if(length!=1) { 161 card=ack[0]; 162 password4=ack[1]; 163 bh=ack[2]; 164 165 money=Double.parseDouble(ack[3]); 166 } 167 else { 168 card=ack[0]; 169 password4="88888888"; 170 bh="01"; 171 money=0; 172 } 173 } 174 public void check(User user,int length) { 175 if(user.Username_MappingTable.get(card)==null) { 176 System.out.println("Sorry,this card does not exist."); 177 178 flag++; 179 } 180 else if(!password4.equals("88888888")) { 181 System.out.println("Sorry,your password is wrong."); 182 183 flag++; 184 } 185 else if(!bh.equals("11")&&!bh.equals("10")&&!bh.equals("09")&&!bh.equals("08")&&!bh.equals("07")&&!bh.equals("06")&&!bh.equals("05")&&!bh.equals("04")&&!bh.equals("03")&&!bh.equals("02")&&!bh.equals("01")) { 186 System.out.println("Sorry,the ATM's id is wrong."); 187 188 flag++; 189 } 190 else if((accountTable.get(card).equals("借记账号")&&money>startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1])||(accountTable.get(card).equals("借记账号")&&sfkh()&&money==startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1])||(yue1(startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1],money)<-50000)) { 191 System.out.println("Sorry,your account balance is insufficient."); 192 flag++; 193 } 194 else 195 flag=0; 196 } 197 public double yue() { 198 if(startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1] 199 <money&&startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]>0) { 200 startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]=startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]-(money-startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1])*0.05; 201 } 202 if(startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]<money&&startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]<=0) { 203 startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]=startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]-money*0.05; 204 } 205 if(sfkh()) 206 startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]=startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]-money-money*Double.parseDouble(ServicesCharge.get(ATMnum.get(bh))); 207 else 208 startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]=startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]-money; 209 return startmoney[Integer.valueOf(Serial.get(Username_MappingTable.get(card)))-1]; 210 } 211 public double yue1(double a,double money1) { 212 if(a<money1&&a>0) { 213 a=a-(money1-a)*0.05; 214 } 215 if(a<money1&&a<=0) { 216 a=a-money1*0.05; 217 } 218 if(sfkh()) 219 a=a-money1-money1*Double.parseDouble(ServicesCharge.get(ATMnum.get(bh))); 220 else 221 a=a-money1; 222 return a; 223 } 224 public boolean sfkh() { 225 if(Bankmap.get(Username_MappingTable.get(card)).equals(ATMnum.get(bh))) 226 return false; 227 else 228 return true; 229 } 230 }
【类图设计】

【代码分析】

【重点分析】
一、 题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
题目7-1的设计分析:
这一道题目主要考察对于大量的集合数据信息进行处理,需要使用集合来对域数据的分门别类进行存储,在整理完数据之后就要进行特定的数据处理,处理之后要进特定的形式输出。主要涉及到集合对数据的整理储存的应用、条件语句的判断。
用户输入何种图形卡片已经图片对应的参数信息,而我们就要对于数据的整理与处理,然后输出对应的结果返回给用户,在整理数据的时候要分门别类的整理对应的数据,处理数据的时候要按照不同卡牌的形态来处理数据。
题目7-2的设计分析:
对于数据输出的处理有着不一样的形式。对于数据的处理们这里运用到了在集合中排序,也就是说对于数据的处理有着并不一样的形式,这里使用到了比较器的办法,去处理数据,同时覆盖了部分数据结构的相关知识,需要复习。
二、 题目集8和题目集9两道ATM机仿真题目的设计思路分析总结
总体上讲,题目集9相对比题目集8增加了ATM机的功能(增加借记卡,支持跨行办理相关业务等功能),且增加了一个银行的种类。题目集9的设计相对比题目集8中的设计要更加完善,程序的功能也更加齐全。
题目集8的ATM设计:
首先确定设计的类,银行类、ATM类、账户类并且ATM类继承了银行是银行的一个子类;
其次:在类设计完之后就要确认类中有什么属性,调用什么样的方法来完成对应的功能,各种的方分模块的设计完成之后就要进一步思考,模拟出ATM取款的全部过程。用户在ATM机器之前输入自己的银行卡号与密码进行登入验证的操作,成功之后就可以做出相关的操作(取款、存款、查询余额)的操作。
题目9的ATM设计:
本次在修改上一次地方法时候加上了新的类,来提供更加完备地操作ATM系统。用户根据自己的卡信息在不同地银行都可以进行相关地操作,但这里对于我们要判断的条件就更加的多了,出游不同银行的卡在不同的银行进行取款操作有着不同的跨行手续费用的问题,以及对于信用卡用户在透支的条件下又有着不一样的取款手续费用的问题,所以这里就又不同的规则所以加上的条件语句就很多,根据不同的条件下的情况分门别类的整理用户类型与取款规则,然后在次调用不同的方法进行相应的取款服务,而并不是放在一个方法中进行一次性的取款操作,但是判断条件多,就免不了if扎堆的问题。而且对于编码的过程有着很严重的影响,在将来增加业务就需要修改很多的代码,更新的成本很高。
三、 踩坑心得
以题目集7中的7-1和7-2为例,在这两道题用户输入的数据是很多的,一次进行多次的操作,但实际上是分开来的,一次操作结束后再进行第二次的操作,但这里用户输入的信息是很复杂的,所以这里就要对于数据进进行前期的处理。
如果可以用集合来分开用户来进行操作,将数据分割成每一个用户,然后将这些数据封装成一个用户。然后对于集合是采用泛型的方式来完成储存用户,然后再遍历集合对每一个用户进行功能的操作与完成。
但如果是最常用处理数据存储的手段,集合就是最好的选择。当然这部分涉及到了数据结构的相关知识,需要提前复习,通过这一道题使得我对于数据结构的理解更进一步,因地制宜,具体情况具体分析,使用不同的数据存储形式来对对于数据进行不同的处理。
对于两道ATM题目,一定要学会使用开闭原则,增加方法,使用方法重载的机制,已经接口的调用,分好情况根据不同的类型来分配调用方法来完成功能,不然重写的时间成本实在是太高了,受不了。
四、改进建议
需要尽可能学会分任务进行设计,把功能进行划分,做到一个实体做一件事,做到合理分配,做到面向对象的设计思想。
优化的功夫还需要继续进步。圈复杂度分析中,大多数代码的圈复杂度还是不理想。
对于多个类的调用的类,如果可以,增加一个数据类,来进行数据封装,调用时对数据进行改动,不需要在调用类中对多个子类进行组合。
对于开闭原则,在概念设计阶段就需要针对可能存在的扩展进行开闭原则设计。
五、 总结
第三阶段Blog通过三次题目集也落下帷幕,所有的题目都以类设计为基础,完成题目的构建。在类设计的基础上,通过对继承,多态,接口等多方面学习,使得我的面向对象编程的思想也在本次学习中真正进一步加深了理解。
本次大作页的ATM题目让我认识到了不同类的类型:实体类,业务类,接口类;对于类设计上,需要学会考虑如何将一个题目设计到恰如其分,不同的设计思路必然会带来的不同效果。
在对字符串的校验处理上,通过对正则表达式的初步学习,掌握了对复杂数据的校验,截取,替换等技巧。正则表达式作为一项工具,可以使得程序复杂度大大降低。初步掌握list的功能,在多对象处理上大显神通。对于hashmap的学习,其中遍历的运用进行了思考,对于多字符校验上有了其他的思路,触类旁通。
本学期面向对象程序设计的课程即将落下尾声,在此诚挚感谢蔡老师对我的悉心指导和耐心讲解,祝愿软件学院捷报频传!

浙公网安备 33010602011771号