OO第三次大作业——来自小白星球总结

一、前言

   第七次题目集:

  1.目量为两题:7-1 图形卡片排序游戏,7-2图形卡片分组游戏。

  2.知识点:(1)继承与多态;(2)使用private进行数据域的封装;(3)ArrayList数组的使用,用以存放不同类型的图形以及数据(4)TreeSet的使用。(5)compareTo()的重写与使用。

  3.难度:总体难度不是很大。

  第八次题目集:

  1.题目量为1题:7-3 ATM机类结构设计(一)。

  2.知识点:(1)类结构的设计;(2)通过引用变量访问对象,静态变量、方法、数据域封装;(3)方法的重写和重载,对象转化。(4)类的关系的关联,聚集和组合;(5)方法重写与重载,对象转化。

  3.难度:类结构设计如何才更加合理,难度系数**。

  第九次题目集:

   1.题目量为1题:7-1 ATM机类结构设计(二)

   2.知识点:(1)继承与多态;(2)instanceof操作符,protected数据据和方法;(3)object类的equals方法;(4)arraylist类;(5)继承和多态中的调用父类构造方法,构造方法链,调用父类的普通方法;(6)类的关系的关联,聚集和组合。

   3.难度:在第八次题目集的基础上新增了借贷卡难度系数***。

二、设计与分析

  ①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结

题目集7-1要求:

1. 作业目标

掌握类的继承、多态性使用方法以及接口的应用。

2. 作业要求

2.1 业务背景

(1)卡片大小排序游戏规则

   考虑一款适合于小学生的卡片(Card)排序游戏,其规则为随机发放一些卡片给学生,卡片分为 四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid),并给出各种 卡片的相应参数,要求学生能够迅速求出各卡片的面积大小然后将卡片按照其面积值从大到小进行排 序,同时求出所有卡片的面积之和。

(2)Comparable 接口的应用

   图形类设计可参考作业 6-1 的类层次结构,本次作业要求对卡片排序时使用 Comparable 接口, 即 Card 类需要实现 Comparable 接口中的 CompareTo()方法。

2.2 程序功能需求

(1)实现功能

   本程序仅用于为学生所玩的游戏提供正确答案的功能,即根据学生得到的卡片种类与数量,给出 排序前和排序后的卡片顺序,同时给出所有卡片的面积之和。

(2)输入输出规则

   ①输入规则 首先,在一行上输入一串数字(1~4,整数),其中,1 代表圆形卡片,2 代表矩形卡片,3 代表 三角形卡片,4 代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要 输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要 输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。

   ②输出规则  如果图形数量非法(小于 0)或图形属性值非法(数值小于 0 以及三角形三边不能组成三 角形),则输出“Wrong Format”。 2 / 4  如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:  排序前的各图形类型及面积,格式为“图形名称 1:面积值 1 图形名称 2:面积值 2 …图形名称 n:面积值 n ”,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;  排序后的各图形类型及面积,格式同排序前的输出;  所有图形的面积总和,格式为“Sum of area:总面积值”。 输出格式见输入输出示例。

题目集7-2要求:

1. 作业目标

掌握类的继承、多态性使用方法以及接口的应用。

2. 作业要求

2.1 业务背景

(1)卡片大小排序游戏规则

   沿袭作业 7-1,本次作业主要对卡片(Card)进行分组游戏,其规则为随机发放一些卡片给学生, 卡片仍然分为四种形状:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid), 并给出各种卡片的相应参数,要求学生首先根据卡片类型将所有卡片进行分组(一个类型分为一组, 所以最多四组),然后能够对每组内的卡片根据面积值从大到小进行排序,同时求出该组内所有卡片 的面积之和,最后求出每组卡片面积之和中的最大值。

(2)Comparable、Comparator 接口的应用

  图形类设计可参考作业 7-1 的类层次结构(需要进一步完善),本次作业要求对卡片排序时使用 Comparable 接口或 Comparator 接口。

2.2 程序功能需求

(1)实现功能  对各图形信息进行输入并输出; 输入数据的正确性校验; 对输入的图形按照图形类型进行分组并输出,分组原理如下图所示: 对每组内的图形按照面积值降序排序并输出;  求出每组内图形面积的总和; 求出所有组的面积总和的最大值并输出。

(2)输入输出规则 2 / 5 ①输入规则 首先,在一行上输入一串数字(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:面积值”。 输出格式见输入输出示例。

分析:这两次题目集主要基于图形卡片的多态排序。要求我们掌握类的继承,多态性使用以及接口的引用,要我们对图像进行分类,分为四种形状,分别为:圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid),并按图片的大小从小到大进行排序和求出面积之和,这题排序时要用comparable接口,Card 类需要实现 Comparable 接口中的 CompareTo()方法,comparable接口定义了compareto方法,用于比较对象,是找出两个相同类型对象中较大者的通用方法,这两个对象必须是可比较的,compareto方法判断这个对象相当于给定对象的顺序,并且但这个对象小于,等于或大于时,分别负整数,零或正整数。从类图上来看,题目集7(7-1)和题目集7(7-2)的类图相差不大,其中Main类、Shape类、Rectangle类、Triangle类、Circle类、Trapezoid类完全一样,类的属性和方法都没有改变,主要差距在于DealCardList类中,不难发现,7-2的DealCardList类中属性更多,方法也多了(这里插一句,接口的变化可以忽略,其实两个题的接口的实现可以不变,当时做完7-1的时候发现Comparable接口是自带的,可以直接使用,无需重新定义,后面7-2改了,同时发现可以利用Collections类中的sort()方法直接对面积的大小进行排序),从这两个题目的递进式设计可以充分体现代码的复用性,以及代码的可拓展性。从类的属性和方法上观察,可以发现每个类都有每个类的特定功能,比如getArea()方法,每个继承Shape类的子类都进行了重写,用以获得各自的面积,体现了设计的“单一职责”原则,一个类/接口只执行一个职责。忽略接口的变化,7-1到7-2的最大区别在于7-2的DealCardList类在7-1的基础上增加了属性和方法,这体现了设计的“开-闭”原则,对扩展开放,对修改关闭

题目集合7-1类图:

 

题目集7-1源码在SourceMonitor中的分析:

题目集合7-2类图:

 

题目集7-2源码在SourceMonitor中的分析图:

 

题目集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:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
  2. 排序后的各图形类型及面积,格式同排序前的输出;
  3. 所有图形的面积总和,格式为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
  1 //package PTA7;
  2 import java.util.*;
  3 import java.util.TreeSet;
  4 import java.util.ArrayList;
  5 
  6 public class Main{
  7     /*在Main1类中定义一个静态Scanner对象,这样在其他类中如果想要使用该对象进行
  8      * 输入,则直接使用Main.input.next...即可 */
  9     public static Scanner input = new Scanner(System.in);
 10 
 11     public static void main(String[] args) {
 12         ArrayList<Integer> list;
 13         list = new ArrayList<>();
 14         int num = input.nextInt();
 15         while (num != 0) {  //循环一直输入
 16             //输入非法
 17             if (num < 0 || num > 4) {
 18                 System.out.println("Wrong Format");
 19                 System.exit(0);
 20             }
 21             list.add(num);    /*把num存进list数组*/
 22             num = input.nextInt();
 23         }
 24         DealCardList dealCardList = new DealCardList(list);   //把list传进dealCardlist
 25         if (!dealCardList.validate()) {
 26             System.out.println("Wrong Format");
 27             System.exit(0);
 28         }
 29         dealCardList.showResult();
 30         input.close();
 31     }
 32 }
 33 //卡片处理
 34 class DealCardList {
 35     ArrayList<Card> cardList = new ArrayList<>();
 36     //无参构造
 37     public DealCardList() {
 38     }
 39     //带参构造
 40     public DealCardList(ArrayList<Integer> cardnum) {    //list==cardnum ,取数组进行判断
 41         for (Integer i = 0; i< cardnum.size(); i++) {
 42             if (cardnum.get(i) == 0) {
 43                 break;
 44             } else if (cardnum.get(i) == 1) {  //1为圆
 45                 Card c1 = new Card(new Circle(Main.input.nextDouble()));  //如果是圆,则new一个圆类,并输入半径
 46                 c1.getShape().setShapeName("Circle");
 47                 cardList.add(c1);
 48                 //break;
 49             } else if (cardnum.get(i) == 2) { //2为矩形
 50                 Card c2 = new Card(new Rectangle(Main.input.nextDouble(), Main.input.nextDouble()));
 51                 c2.getShape().setShapeName("Rectangle");
 52                 cardList.add(c2);
 53                 //break;
 54             } else if (cardnum.get(i) == 3) {  //3为三角形
 55                 Card c3 = new Card(new Triangle(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 56                 c3.getShape().setShapeName("Triangle");
 57                 cardList.add(c3);
 58                 // break;
 59             } else if (cardnum.get(i) == 4) {  //4为梯形
 60                 Card c4 = new Card(new Trapezoid(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 61                 c4.getShape().setShapeName("Trapezoid");
 62                 cardList.add(c4);
 63                 // break;
 64             }
 65         }
 66     }
 67     //输入合法性判断
 68     public boolean validate() {
 69         boolean b = true;
 70         for(int i = 0; i < cardList.size() ; i++){
 71             Card c = cardList.get(i);
 72             if (!c.getShape().validate()) {
 73                 b = false;
 74                 break;
 75             }
 76         }
 77         return b;
 78     }
 79     //卡片排序
 80     public void cardSort() {
 81         TreeSet<Card> kp = new TreeSet<>();
 82         for(int i = 0; i < cardList.size(); i++){
 83             kp.add(cardList.get(i));
 84         }
 85         for (Card a : kp) {
 86             System.out.print(a.getShape());
 87         }
 88     }
 89     //获取全部面积
 90     public double getAllArea() {
 91         double sum = 0;
 92         for (Card c : cardList) {
 93             sum = sum + c.getShape().getArea();
 94         }
 95         return sum;
 96     }
 97     //输出结果
 98     public void showResult() {
 99         System.out.println("The original list:");
100         for(int i = 0; i<cardList.size() ; i++){
101             Card c = cardList.get(i);
102             System.out.print(c.getShape());
103         }
104         System.out.println();
105         System.out.println("The sorted list:");
106         cardSort();
107         System.out.printf("\nSum of area:%.2f\n", getAllArea());
108     }
109 }
110 //卡片类
111 class Card implements Comparable<Card>{
112     Shape shape;
113 
114     //无参构造
115     Card() {
116     }
117 
118     //带参构造
119     Card(Shape shape) {
120         this.shape = shape;
121     }
122 
123     //shape getter
124     public Shape getShape() {
125         return shape;
126     }
127 
128     //shape setter
129     public void setShape(Shape shape) {
130         this.shape = shape;
131     }
132     //compareTo
133     public int compareTo(Card c) {
134         double value = shape.getArea()-c.getShape().getArea();
135         if(value > 0){
136             return -1;
137         }
138         else if(value < 0){
139             return 1;
140         }
141         return 0;
142     }
143 }
144 //抽象图形类
145 abstract class Shape{
146     private String shapeName;
147     //无参构造
148     Shape() {
149     }
150 
151     //带参构造
152     Shape(String shapeName) {
153         this.shapeName = shapeName;
154     }
155 
156     //shapeName getter
157     public String getShapeName() {
158         return shapeName;
159     }
160 
161     //shapeName setter
162     public void setShapeName(String shapeName) {
163         this.shapeName = shapeName;
164     }
165 
166     //计算面积
167     public double getArea(){
168         return 0.0;
169     }
170 
171     //合法性判断
172     public  boolean validate(){
173         return true;
174     }
175 
176     //toString
177     public String toString() {
178         return  getShapeName()+":"+String.format("%.2f ",getArea());
179     }
180 }
181 //
182 class Circle extends Shape {
183     private double radius;
184 
185     //无参构造
186     Circle() {
187     }
188 
189     //带参构造
190     Circle(double radius) {
191         this.radius = radius;
192     }
193 
194     //radius getter
195     public double getRadius() {
196         return radius;
197     }
198 
199     //radius setter
200     public void setRadius(double radius) {
201         this.radius = radius;
202     }
203 
204     //计算面积
205     @Override
206     public double getArea() {
207         return Math.PI * radius * radius;
208     }
209 
210     //合法性判断
211     @Override
212     public boolean validate() {
213         return radius > 0;
214     }
215 
216 }
217 //矩形
218 class Rectangle extends Shape {
219     private double width;
220     private double length;
221 
222     //无参构造
223     Rectangle() {
224     }
225 
226     //带参构造
227     Rectangle(double width, double length) {
228         this.width = width;
229         this.length = length;
230     }
231 
232     //width getter
233     public double getWidth() {
234         return width;
235     }
236 
237     //width setter
238     public void setWidth(double width) {
239         this.width = width;
240     }
241 
242     //length getter
243     public double getLength() {
244         return length;
245     }
246 
247     //length setter
248     public void setLength(double length) {
249         this.length = length;
250     }
251 
252     //计算面积
253     @Override
254     public double getArea() {
255         return width * length;
256     }
257 
258     //合法性判断
259     @Override
260     public boolean validate() {
261         return width > 0 && length > 0;
262     }
263 }
264 
265 //三角形
266 class Triangle extends Shape {
267     private double side1;
268     private double side2;
269     private double side3;
270 
271     //无参构造
272     Triangle() {
273     }
274 
275     //带参构造
276     Triangle(double side1, double side2, double side3) {
277         this.side1 = side1;
278         this.side2 = side2;
279         this.side3 = side3;
280     }
281 
282     //side1 getter
283     public double getSide1() {
284         return side1;
285     }
286 
287     //side1 setter
288     public void setSide1(double side1) {
289         this.side1 = side1;
290     }
291 
292     //side2 getter
293     public double getSide2() {
294         return side2;
295     }
296 
297     //side2 setter
298     public void setSide2(double side2) {
299         this.side2 = side2;
300     }
301 
302     //side3 getter
303     public double getSide3() {
304         return side3;
305     }
306 
307     //side3 setter
308     public void setSide3(double side3) {
309         this.side3 = side3;
310     }
311 
312     //计算面积
313     @Override
314     public double getArea() {
315         double p = (this.side1 + this.side2 + this.side3) / 2.0;
316         return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
317     }
318 
319     //合法性判断
320     @Override
321     public boolean validate() {
322         double temp;
323         if(side1 > side2){
324             temp = side1;
325             side1 = side2;
326             side2 = temp;
327         }
328         if(side1 > side3){
329             temp = side1;
330             side1 = side3;
331             side3 = temp;
332         }
333         if(side2 > side3){
334             temp = side2;
335             side2 = side3;
336             side3 = temp;
337         }
338         /*
339         double[] a = new double[3];
340         a[0] = side1;
341         a[1] = side2;
342         a[2] = side3;
343         //使用冒泡排序
344         boolean b = true;
345         while(b){
346             int flag = 0;
347             for(int i = 1; i < a.length; i++){
348                 if(a[i-1]>a[i]){
349                     double temp = a[i];
350                     a[i] = a[i-1];
351                     a[i-1] = temp;
352                     flag = 1;
353                     if(i==2)
354                         b = false;
355                 }
356                 if(flag == 1)
357                     break;
358             }
359         }*/
360        // return ((a[0] + a[1]) > a[2]) && a[0]> 0 && a[1] > 0 && a[2] > 0;
361         return ((side1 + side2) > side3) && side1 >0 && side2 > 0 && side3 > 0;
362     }
363 }
364 
365 //四边形
366 class Trapezoid extends Shape {
367     private double topSide;
368     private double bottomSide;
369     private double height;
370 
371     //无参构造
372     public Trapezoid() {
373     }
374 
375     //带参构造
376     public Trapezoid(double topSide, double bottomSide, double height) {
377         this.topSide = topSide;
378         this.bottomSide = bottomSide;
379         this.height = height;
380     }
381 
382     //topSide getter
383     public double getTopSide() {
384         return topSide;
385     }
386 
387     //topSide setter
388     public void setTopSide(double topSide) {
389         this.topSide = topSide;
390     }
391 
392     //bottomSide getter
393     public double getBottomSide() {
394         return bottomSide;
395     }
396 
397     //bottomSide setter
398     public void setBottomSide(double bottomSide) {
399         this.bottomSide = bottomSide;
400     }
401 
402     //height getter
403     public double getHeight() {
404         return height;
405     }
406 
407     // height setter
408     public void setHeight(double height) {
409         this.height = height;
410     }
411 
412     //计算面积
413     @Override
414     public double getArea() {
415         return (this.bottomSide + this.topSide) * this.height / 2.0;
416     }
417 
418     //合法性判断
419     @Override
420     public boolean validate() {
421         return this.topSide > 0 && this.bottomSide > 0 && this.height > 0;
422     }
423 }

题目集7-2题目及源码:

7-2 图形卡片分组游戏 (60 分)

掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。

2021-OO第07次作业-2指导书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:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
  2. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为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.5

输入样例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
  1 import java.util.*;
  2 import java.util.TreeSet;
  3 import java.util.ArrayList;
  4 
  5 public class Main{
  6     /*在Main1类中定义一个静态Scanner对象,这样在其他类中如果想要使用该对象进行
  7      * 输入,则直接使用Main.input.next...即可 */
  8     public static Scanner input = new Scanner(System.in);
  9 
 10     public static void main(String[] args) {
 11         ArrayList<Integer> list;
 12         list = new ArrayList<>();
 13         int num = input.nextInt();
 14         if(num == 0){
 15             System.out.println("Wrong Format");
 16             System.exit(0);
 17         }
 18         while (num != 0) {  //循环一直输入
 19             //输入非法
 20             if (num < 0 || num > 4) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24             list.add(num);    /*把num存进list数组*/
 25             num = input.nextInt();
 26         }
 27         DealCardList dealCardList = new DealCardList(list);   //把list传进dealCardlist
 28         if (!dealCardList.validate()) {
 29             System.out.println("Wrong Format");
 30             System.exit(0);
 31         }
 32         dealCardList.showResult();
 33         input.close();
 34     }
 35 }
 36 //卡片处理
 37 class DealCardList {
 38     ArrayList<Card> cardList = new ArrayList<>();
 39 
 40     //无参构造
 41     public DealCardList() {
 42     }
 43 
 44     //带参构造
 45     public DealCardList(ArrayList<Integer> cardnum) {    //list==cardnum ,取数组进行判断
 46         for (Integer integer : cardnum) {
 47             if (integer == 0) {
 48                 break;
 49             } else if (integer == 1) {  //1为圆
 50                 Card c1 = new Card(new Circle(Main.input.nextDouble()));  //如果是圆,则new一个圆类,并输入半径
 51                 c1.getShape().setShapeName("Circle");
 52                 cardList.add(c1);
 53             } else if (integer == 2) { //2为矩形
 54                 Card c2 = new Card(new Rectangle(Main.input.nextDouble(), Main.input.nextDouble()));
 55                 c2.getShape().setShapeName("Rectangle");
 56                 cardList.add(c2);
 57             } else if (integer == 3) {  //3为三角形
 58                 Card c3 = new Card(new Triangle(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 59                 c3.getShape().setShapeName("Triangle");
 60                 cardList.add(c3);
 61             } else if (integer == 4) {  //4为梯形
 62                 Card c4 = new Card(new Trapezoid(Main.input.nextDouble(), Main.input.nextDouble(), Main.input.nextDouble()));
 63                 c4.getShape().setShapeName("Trapezoid");
 64                 cardList.add(c4);
 65             }
 66         }
 67     }
 68 
 69     //输入合法性判断
 70     public boolean validate() {
 71         boolean b = true;
 72         for (int i = 0; i < cardList.size(); i++) {
 73             Card c = cardList.get(i);
 74             if (!c.getShape().validate()) {
 75                 b = false;
 76                 break;
 77             }
 78         }
 79         return b;
 80     }
 81 
 82     ArrayList<Card> c = new ArrayList<>();//
 83     ArrayList<Card> r = new ArrayList<>(); //矩形
 84     ArrayList<Card> t1 = new ArrayList<>();//三角形
 85     ArrayList<Card> t2 = new ArrayList<>();//梯形
 86     double[] sum = new double[4];
 87 
 88     public void Separated() {
 89         for (Card a : cardList) {
 90             switch (a.getShape().getShapeName()) {
 91                 case "Circle":
 92                     c.add(a);
 93                     break;
 94                 case "Rectangle":
 95                     r.add(a);
 96                     break;
 97                 case "Triangle":
 98                     t1.add(a);
 99                     break;
100                 case "Trapezoid":
101                     t2.add(a);
102                     break;
103             }
104         }
105     }
106 
107     public void spara(ArrayList<Card> list, double[] sum, int i) {
108         System.out.print("[");
109         sum[i] = 0;
110         for (Card a : list) {
111             sum[i] = sum[i] + a.getShape().getArea();
112             System.out.print(a.getShape());
113         }
114         System.out.print("]");
115     }
116 
117     public void spara1(ArrayList<Card> list) {
118         TreeSet<Card> kp = new TreeSet<>(list);
119         System.out.print("[");
120         for (Card a : kp) {
121             System.out.print(a.getShape());
122         }
123         System.out.print("]");
124     }
125 
126     public double getMax() {
127         double max = 0;
128         for (int i = 0; i < sum.length; i++) {
129             for (int j = 0; j < 4; j++) {
130                 if (max < sum[j]) {
131                     max = sum[j];
132                 }
133             }
134         }
135         return max;
136     }
137 
138     public void showResult() {
139         Separated();
140         System.out.println("The original list:");
141         spara(cardList, sum, 0);
142         System.out.printf("\nThe Separated List:\n");
143         spara(c, sum, 0); //
144         spara(r, sum, 1); //矩形
145         spara(t1, sum, 2); //三角形
146         spara(t2, sum, 3); //梯形
147         System.out.println("\nThe Separated sorted List:");
148         spara1(c);
149         spara1(r);
150         spara1(t1);
151         spara1(t2);
152         System.out.printf("\nThe max area:%.2f\n",getMax());
153     }
154 }
155 
156 //卡片类
157 class Card implements Comparable<Card>{
158     Shape shape;
159 
160     //无参构造
161     Card() {
162     }
163 
164     //带参构造
165     Card(Shape shape) {
166         this.shape = shape;
167     }
168 
169     //shape getter
170     public Shape getShape() {
171         return shape;
172     }
173 
174     //compareTo
175     public int compareTo(Card c) {
176         double value = shape.getArea()-c.getShape().getArea();
177         if(value > 0){
178             return -1;
179         }
180         else if(value < 0){
181             return 1;
182         }
183         return 0;
184     }
185 }
186 //抽象图形类
187 abstract class Shape{
188     private String shapeName;
189     //无参构造
190     Shape() {
191     }
192 
193     //带参构造
194     Shape(String shapeName) {
195         this.shapeName = shapeName;
196     }
197 
198     //shapeName getter
199     public String getShapeName() {
200         return shapeName;
201     }
202 
203     //shapeName setter
204     public void setShapeName(String shapeName) {
205         this.shapeName = shapeName;
206     }
207 
208     //计算面积
209     public double getArea(){
210         return 0.0;
211     }
212 
213     //合法性判断
214     public  boolean validate(){
215         return true;
216     }
217 
218     //toString
219     public String toString() {
220         return  getShapeName()+":"+String.format("%.2f ",getArea());
221     }
222 }
223 //
224 class Circle extends Shape {
225     private double radius;
226 
227     //无参构造
228     Circle() {
229     }
230 
231     //带参构造
232     Circle(double radius) {
233         this.radius = radius;
234     }
235 
236 
237     //计算面积
238     @Override
239     public double getArea() {
240         return Math.PI * radius * radius;
241     }
242 
243     //合法性判断
244     @Override
245     public boolean validate() {
246         return radius > 0;
247     }
248 
249 }
250 //矩形
251 class Rectangle extends Shape {
252     private double width;
253     private double length;
254 
255     //无参构造
256     Rectangle() {
257     }
258 
259     //带参构造
260     Rectangle(double width, double length) {
261         this.width = width;
262         this.length = length;
263     }
264 
265     //length getter
266     public double getLength() {
267         return length;
268     }
269 
270     //length setter
271     public void setLength(double length) {
272         this.length = length;
273     }
274 
275     //计算面积
276     @Override
277     public double getArea() {
278         return width * length;
279     }
280 
281     //合法性判断
282     @Override
283     public boolean validate() {
284         return width > 0 && length > 0;
285     }
286 }
287 
288 //三角形
289 class Triangle extends Shape {
290     private double side1;
291     private double side2;
292     private double side3;
293 
294     //无参构造
295     Triangle() {
296     }
297 
298     //带参构造
299     Triangle(double side1, double side2, double side3) {
300         this.side1 = side1;
301         this.side2 = side2;
302         this.side3 = side3;
303     }
304 
305     //计算面积
306     @Override
307     public double getArea() {
308         double p = (this.side1 + this.side2 + this.side3) / 2.0;
309         return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
310     }
311 
312     //合法性判断
313     @Override
314     public boolean validate() {
315         double temp;
316         if(side1 > side2){
317             temp = side1;
318             side1 = side2;
319             side2 = temp;
320         }
321         if(side1 > side3){
322             temp = side1;
323             side1 = side3;
324             side3 = temp;
325         }
326         if(side2 > side3){
327             temp = side2;
328             side2 = side3;
329             side3 = temp;
330         }
331         return ((side1 + side2) > side3) && side1 >0 && side2 > 0 && side3 > 0;
332     }
333 }
334 
335 //四边形
336 class Trapezoid extends Shape {
337     private double topSide;
338     private double bottomSide;
339     private double height;
340 
341     //无参构造
342     public Trapezoid() {
343     }
344 
345     //带参构造
346     public Trapezoid(double topSide, double bottomSide, double height) {
347         this.topSide = topSide;
348         this.bottomSide = bottomSide;
349         this.height = height;
350     }
351 
352 
353     //计算面积
354     @Override
355     public double getArea() {
356         return (this.bottomSide + this.topSide) * this.height / 2.0;
357     }
358 
359     //合法性判断
360     @Override
361     public boolean validate() {
362         return this.topSide > 0 && this.bottomSide > 0 && this.height > 0;
363     }
364 }

  ②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结

题目集8 ATM机类结构设计一

1.作业目标

   编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能。

2.作业要求

2.1 业务背景

(1)相关概念说明

   中国银联(China UnionPay):成立于 2002 年 3 月,是经国务院同意,中 国人民银行批准设立的中国银行卡联合组织,总部设于上海。主要负责建设和 运营全国统一的银行卡跨行信息交换网络、提供银行卡跨行信息交换相关的专 业化服务、管理和经营“银联”品牌、制定银行卡跨行交易业务规范和技术标 准。其包含多家银行机构。 银行(Bank):是依法成立的经营货币信贷业务的金融机构,是商品货币经 济发展到一定阶段的产物。 银行用户(User):能够在银行办理相应业务的用户。一个银行机构可以拥 有多个银行用户及多个银行账户,一个用户也可以在多个银行机构开立账户。 银行账户(Account):客户在银行开立的存款账户、贷款账户、往来账户 的总称。一个银行用户可以拥有多个银行账户。 银行卡(Card):经批准由商业银行(含邮政金融机构) 向社会发行的具有 消费信用、转账结算、存取现金等全部或部分功能的信用支付工具。一个银行 账户可以发行多张银行卡。  ATM(Automated Teller Machine),自动柜员机,因大部分用于取款,又 称自动取款机。它是一种高度精密的机电一体化装置,利用磁性代码卡或智能 卡实现金融交易的自助服务,代替银行柜面人员的工作。可提取现金、查询存 款余额、进行账户之间资金划拨、余额查询等工作。一般 ATM 机隶属于某一银 行机构。

(2)设计要求

   尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设 计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进 行扩充和完善。 注意:为降低难度,本次作业限定银行卡均为借记卡(不能透支),且不允 许跨行办理相关业务(例如在中国建设银行的 ATM 机上对其他银行的账户进行操 作)。

2.2 程序功能需求

  (1)实现功能 基础数据的初始化; 用户存款、取款及查询余额功能。

  (2)初始化数据表 1 银行相关数据 机构名称 银行名称 ATM 机编号 中国银联 中国建设银行 01 02 03 04 中国工商银行 05 06 表 2 用户相关数据 用户姓名 隶属银行 银行账号 初始余额 隶属卡号 杨过 中国建设银行 3217000010041315709 10000.00 6217000010041315709 6217000010041315715 3217000010041315715 10000.00 6217000010041315718 郭靖 中国建设银行 3217000010051320007 10000.00 6217000010051320007 张无忌 中国工商银行 3222081502001312389 10000.00 6222081502001312389 3222081502001312390 10000.00 6222081502001312390 3222081502001312399 10000.00 6222081502001312399 6222081502001312400 韦小宝 中国工商银行 3222081502051320785 10000.00 6222081502051320785 3222081502051320786 10000.00 6222081502051320786 注意:系统的测试用例均采用如上数据,且所有卡号密码默认为“88888888”。

(3)输入输出规则

  1)输入规则 每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业 务操作输入格式如下: ➢ 存款、取款功能输入数据格式 卡号 密码 ATM 机编号 金额(由一个或多个空格分隔) 其中,当金额大于 0 时,代表取款,否则代表存款。 ➢ 查询余额功能输入数据格式 卡号

  2)输出规则 ①输入错误处理  如果输入卡号不存在,则输出“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 编号]上存款¥[金额] 当前余额为¥[金额] 其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。 ④查询余额业务输出 ¥[金额] 金额保留两位小数。

题目集9 类结构设计二

1.作业目标

  编写一个银行 ATM 机的模拟程序,能够完成用户的取款以及查询余额功能。

2.作业要求

2.1 业务背景

  (1)相关概念说明 中国银联(China UnionPay):成立于 2002 年 3 月,是经国务院同意,中 国人民银行批准设立的中国银行卡联合组织,总部设于上海。主要负责建设和 运营全国统一的银行卡跨行信息交换网络、提供银行卡跨行信息交换相关的专 业化服务、管理和经营“银联”品牌、制定银行卡跨行交易业务规范和技术标 准。其包含多家银行机构。 银行(Bank):是依法成立的经营货币信贷业务的金融机构,是商品货币经 济发展到一定阶段的产物。 银行用户(User):能够在银行办理相应业务的用户。一个银行机构可以拥 有多个银行用户及多个银行账户,一个用户也可以在多个银行机构开立账户。 银行账户(Account):客户在银行开立的存款账户、贷款账户、往来账户 的总称。银行账户分为借记账户和贷记账户两种,其中,借记账户不能够透支 取款,而贷记账户可以透支取款(可能需要支付手续费)。一个银行用户可以 拥有多个银行账户。 银行卡(Card):经批准由商业银行(含邮政金融机构) 向社会发行的具有 消费信用、转账结算、存取现金等全部或部分功能的信用支付工具。根据账户 的不同,银行卡一般分为借记卡(针对借记账户)和信用卡(针对贷记账户) 两类。一个银行账户可以发行多张银行卡。  ATM(Automated Teller Machine),自动柜员机,因大部分用于取款,又 称自动取款机。它是一种高度精密的机电一体化装置,利用磁性代码卡或智能 卡实现金融交易的自助服务,代替银行柜面人员的工作。可提取现金、查询存 款余额、进行账户之间资金划拨、余额查询等工作。一般 ATM 机隶属于某一银行机构。

  (2)设计要求 尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设 计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进 行扩充和完善。 注意:本次作业中银行卡包含借记卡和信用卡两类,且允许跨行办理相关业 务(例如在中国建设银行的 ATM 机上使用中国工商银行的银行卡进行业务操作)。

2.2 程序功能需求

  (1)实现功能  基础数据的初始化;  用户取款及查询余额功能。

  (2)初始化数据 表 1 银行相关数据 机构名称 透支取款手续费 及透支最大限额 银行名称 跨行取款手续费 ATM 机编号 中国银联 取款金额的 5% /50000 元 中国建设银行 取款金额的 2% 01 02 03 04 中国工商银行 取款金额的 3% 05 06 中国农业银行 取款金额的 4% 07 08 09 10 11 表 2 用户相关数据 用户姓名 隶属银行 银行账号 账号种类 隶属卡号 杨过 中国建设银行 3217000010041315709 借记账号 6217000010041315709 6217000010041315715 3217000010041315715 借记账号 6217000010041315718 郭靖 中国建设银行 3217000010051320007 借记账号 6217000010051320007 张无忌 中国工商银行 3222081502001312389 借记账号 6222081502001312389 3222081502001312390 借记账号 6222081502001312390 3222081502001312399 借记账号 6222081502001312399 6222081502001312400 韦小宝 中国工商银行 3222081502051320785 借记账号 6222081502051320785 3222081502051320786 借记账号 6222081502051320786 张三丰 中国建设银行 3640000010045442002 贷记账号 6640000010045442002 6640000010045442003 令狐冲 中国工商银行 3640000010045441009 贷记账号 6640000010045441009 乔峰 中国农业银行 3630000010033431001 贷记账号 6630000010033431001 洪七公 中国农业银行 3630000010033431008 贷记账号 6630000010033431008

  (3)业务假定

   1)系统的测试用例均采用如上数据,且所有卡号密码默认为“88888888”, 初始余额均为 10000 元。

    2)手续费默认均从银行卡所对应的账户中自动扣除。

    3)跨行业务手续费收取比例由 ATM 机隶属银行决定,例如,用户手持中国 工商银行的银行卡在中国建设银行的 ATM 上进行取款操作,则手续费收取比例为 中国建设银行的相应比例,按初始化数据中的规定为取款金额的 2%。

   4)跨行查询余额不收取手续费。 5)透支取款手续费由中国银联统一规定为 5%,最大透支金额均为 50000 元。

 (4)输入输出规则 1)输入规则 每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业 务操作输入格式如下: ➢ 取款功能输入数据格式 卡号 密码 ATM 机编号 金额(由一个或多个空格分隔) ➢ 查询余额功能输入数据格式 卡号 2)输出规则 ①输入错误处理  如果输入卡号不存在,则输出“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 编号]上取款¥[金额] 当前余额为¥[金额] 其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。 ③查询余额业务输出 业务:查询余额 ¥[金额] 金额保留两位小数。

分析:题目集8,银联--银行--账户--卡,依次从上往下聚合,另外ATM与银行绑定,用户与账户绑定。从而实现各类之间的关系。对输入校验时,从银联依次往下搜索卡号、ATM机、余额等信息。在题目集9中,要求在题目集8的基础上增加功能,为增加信用卡类,将账户类写成抽象类,借记账户和贷记账户继承自抽象账户类。

题目集合8类图:

 

 

 

题目集8源码在SourceMonitor中的分析图:

题集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 //package PTA8;
  2 import java.util.*;
  3 
  4 public class Main{
  5     public static void main(String[] args){
  6         Scanner input = new Scanner(System.in);
  7         String str = input.nextLine();  //str用于记录输入的字符
  8         StringBuilder s = new StringBuilder();  //s用于暂存字符串
  9         String[] accountAll = new String[]{"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};
 10         //输入判断以#结束
 11         while(!str.equals("#")){
 12             s.append(str);  //追加字符串在同一个地址空间
 13             s.append("\n");
 14             str = input.nextLine();
 15             // System.out.println(s);
 16         }
 17         String data = String.valueOf(s);  //将s中缓存的字符串存到字符串data中
 18         String[] deal = data.split("\n");
 19 
 20         for(int i = 0; i < deal.length; i++){
 21             String deal1 = String.valueOf(deal[i]);
 22             String[] deal2 = deal1.split("\\s+");
 23             Account account = new Account(deal2[0]);
 24             if(deal2.length != 1){ //存取款操作
 25                 for(int j = 0; j < deal2.length; j++){
 26                     ValidateDate validate = new ValidateDate(deal2[2],deal2[0]);
 27                     if(validate.accountValidateDate()==1 && validate.atmValidateDate()==1){
 28                         withDraw withdraw = new withDraw(deal2[0],deal2[1],deal2[2],Double.parseDouble(deal2[3]));
 29                         withdraw.withDrawMate();
 30                     }
 31                     break;
 32                 }
 33             }
 34             else{ //查询余额
 35                 int index= 20;
 36                 for(int k = 0; k < accountAll.length; k++){
 37                     if(account.getAccount().equals(accountAll[k]))
 38                         index = k;
 39                 }
 40                 if(index==0 || index==1 || index==2){  //杨过
 41                     System.out.printf("¥%.2f\n",account.getBalanceY());
 42                 }
 43                 else if(index==3){  //郭靖
 44                     System.out.printf("¥%.2f\n",account.getBalanceG());
 45                 }
 46                 else if(index==4 || index==5 || index==6 || index==7){ //张无忌
 47                     System.out.printf("¥%.2f\n",account.getBalanceZ());
 48                 }
 49                 else if(index==8 || index==9){  //韦小宝
 50                     System.out.printf("¥%.2f\n",account.getBalanceW());
 51                 }
 52             }
 53         }
 54     }
 55 }
 56 //用户类
 57 class User{
 58     private String who;
 59     //private String which;
 60     String[] countnum = new String[]{"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};
 61     //无参构造
 62     public User(){
 63     }
 64     //带参构造
 65     public User(String who){
 66         this.who = who;
 67         // this.which = which;
 68     }
 69     //用户名字
 70     public String getUserName(){
 71         int index = 0;
 72         for(int i = 0; i < countnum.length; i++){
 73             if(countnum[i].equals(who))
 74                 index= i;
 75         }
 76         if(index==0 || index==1 || index==2)
 77             return "杨过";
 78         else if(index==3)
 79             return "郭靖";
 80         else if(index==4 || index==5 || index==6 || index ==7)
 81             return "张无忌";
 82         else if(index==8 || index==9)
 83             return "韦小宝";
 84         else
 85             return "0";
 86     }
 87     //银行名字
 88     public String getBankName(){
 89         int index = 0;
 90         for(int i = 0; i < countnum.length; i++){
 91             if(countnum[i].equals(who))
 92                 index= i;
 93         }
 94         if(index==0 || index==1 || index==2 || index==3)
 95             return "中国建设银行";
 96         else if(index==4 || index==5 || index==6 || index==7 || index==8 || index==9)
 97             return "中国工商银行";
 98         else
 99             return "0";
100     }
101 }
102 //账户类
103 class Account{
104     private String account;
105     static private double balanceY = 10000;  //杨过
106     static private double balanceG = 10000;  //郭靖
107     static private double balanceZ = 10000;  //张无忌
108     static private double balanceW = 10000;  //韦小宝
109     //无参构造
110     public Account(){
111     }
112     //带参构造
113     public Account(String account){
114         this.account = account;
115     }
116     //account setter
117     public void setAccount(String account) {
118         this.account = account;
119     }
120     //account getter
121     public String getAccount(){
122         return account;
123     }
124     //BalanceY setter
125     public void setBalanceY(double balanceY){
126         this.balanceY = balanceY;
127     }
128     //BalanceY getter
129     public double getBalanceY() {
130         return balanceY;
131     }
132     //BalanceG getter
133     public double getBalanceG() {
134         return balanceG;
135     }
136     //BalanceG setter
137     public void setBalanceG(double balanceG) {
138         this.balanceG = balanceG;
139     }
140     //BalanceZ getter
141     public double getBalanceZ() {
142         return balanceZ;
143     }
144     //BalanceZ setter
145     public void setBalanceZ(double balanceZ) {
146         this.balanceZ = balanceZ;
147     }
148     //BalanceW setter
149     public void setBalanceW(double balanceW) {
150         this.balanceW = balanceW;
151     }
152     //BalanceW getter
153     public double getBalanceW() {
154         return balanceW;
155     }
156 }
157 //数据输入合法性检验
158 class ValidateDate{
159     private String atmNum;
160     private String accountNum;
161     String[] accountAll = new String[]{"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786"};
162     String[] atmAll = new String[]{"01","02","03","04","05","06"};
163     //无参构造
164     public ValidateDate(){
165     }
166     //带参构造
167     public ValidateDate(String atmNum, String accountNum){
168         this.atmNum = atmNum;
169         this.accountNum = accountNum;
170     }
171     //atmNum getter
172     public String getAtmNum() {
173         return atmNum;
174     }
175     //atmNum setter
176     public void setAtmNum(String atmNum) {
177         this.atmNum = atmNum;
178     }
179     //accountNum setter
180     public void setAccountNum(String accountNum) {
181         this.accountNum = accountNum;
182     }
183     //accountNum getter
184     public String getAccountNum() {
185         return accountNum;
186     }
187     //账户输入合法性判断
188     public int accountValidateDate(){
189         int flag = 0; //标志,用于记录是否匹配
190         for (String s : accountAll){
191             if(s.equals(accountNum)){
192                 flag = 1;
193                 break;
194             }
195         }
196         if(flag==1)
197             return 1;
198         else{
199             System.out.println("Sorry,this card does not exist.");
200             return 0;
201         }
202     }
203     //ATM机输入合法性检验
204     public int atmValidateDate(){
205         int flag = 0; //标志,用于记录是否匹配
206         for(String s : atmAll){
207             if(s.equals(atmNum)){
208                 flag = 1;
209                 break;
210             }
211         }
212         if(flag==1)
213             return 1;
214         else{
215             System.out.println("Sorry,the ATM's id is wrong.");
216             return 0;
217         }
218     }
219     //判断跨行转账
220     public int mateValidateDate(){
221         int flag1 = 0;
222         int flag2 = 0;
223         /*建设银行*/
224         for(int i = 0; i < 4; i++){
225             if(accountNum.equals(accountAll[i])){  //确定账户银行
226                 for(int j = 0; j < 4; j++) {
227                     if (atmNum.equals(atmAll[j]))  //确定ATM
228                     {
229                         flag1 = 1;
230                         break;
231                     }
232                 }
233             }
234         }
235         /*工商银行*/
236         for(int i = 4; i < accountAll.length; i++){
237             if(accountNum.equals(accountAll[i])){//确定银行账户
238                 for(int j = 4; j < atmAll.length; j++){
239                     if (atmNum.equals(atmAll[j]))  //确定ATM
240                     {
241                         flag2 = 1;
242                         break;
243                     }
244                 }
245             }
246         }
247         //非跨行转账
248         if(flag1 ==1 || flag2 == 1)
249             return 1;
250         else{
251             System.out.println("Sorry,cross-bank withdrawal is not supported.");
252             return 0;
253         }
254     }
255 }
256 //存取款
257 class withDraw{
258     String cardNum; //卡号
259     String cardPassword; //卡密码
260     String atmNum;  //ATM机号
261     double money;  //存取金额
262     double balanceY;
263     double balanceG;
264     double balanceZ;
265     double balanceW;
266     String[] accountAll = new String[]{"6217000010041315709", "6217000010041315715", "6217000010041315718", "6217000010051320007", "6222081502001312389", "6222081502001312390", "6222081502001312399", "6222081502001312400", "6222081502051320785", "6222081502051320786"};
267     //无参构造
268     public withDraw(){
269     }
270     //带参构造
271     public withDraw(String cardNum, String cardPassword, String atmNum, double money){
272         this.cardNum = cardNum;
273         this.cardPassword = cardPassword;
274         this.atmNum = atmNum;
275         this.money = money;
276     }
277     //存取款操作
278     public void withDrawMate(){
279 
280         ValidateDate validate  = new ValidateDate(atmNum, cardNum);
281         Account account = new Account(cardNum);
282         User user = new User(cardNum);
283 
284         int index = 20; //用于记录匹配账户下标
285         for(int i = 0; i < accountAll.length; i++){
286             if(account.getAccount().equals(accountAll[i])) {
287                 index = i;
288                 break;
289             }
290         }
291 
292 
293         //若输入账号合法、ATM机号合法、非跨行取款
294         if(validate.mateValidateDate()==1 && validate.atmValidateDate()==1 && validate.accountValidateDate()==1){
295             if(cardPassword.equals("88888888")){ //密码合法
296                 if(10000 - money < 0)  //取款金额不合法
297                     System.out.println("Sorry,your account balance is insufficient.");
298                 else{  //取款金额合法
299                     if(money < 0) //如果是存款
300                         System.out.println(user.getUserName()+"在"+user.getBankName()+"的"+atmNum+"号ATM机上存款¥"+String.format("%.2f",-money));
301                     else  //如果是取款
302                         System.out.println(user.getUserName()+"在"+user.getBankName()+"的"+atmNum+"号ATM机上取款¥"+String.format("%.2f",money));
303 
304                     if(index==0 || index==1 || index==2){  //杨过
305                         account.setBalanceY(account.getBalanceY() - money);
306                         System.out.printf("当前余额为¥%.2f\n",account.getBalanceY());
307                     }
308                     else if(index==3){  //郭靖
309                         account.setBalanceG(account.getBalanceG() - money);
310                         System.out.printf("当前余额为¥%.2f\n",account.getBalanceG());
311                     }
312                     else if(index==4 || index==5 || index==6 || index==7){ //张无忌
313                         account.setBalanceZ(account.getBalanceZ() - money);
314                         System.out.printf("当前余额为¥%.2f\n",account.getBalanceZ());
315                     }
316                     else if(index==8 || index==9){  //韦小宝
317                         account.setBalanceW(account.getBalanceW() - money);
318                         System.out.printf("当前余额为¥%.2f\n",account.getBalanceW());
319                     }
320                 }
321             }
322             else //密码不合法
323                 System.out.println("Sorry,your password is wrong.");
324         }
325     }
326 
327 }

题目集合9类图:

题目集9源码在SourceMonitor中的分析图:

题集9题目及源码:

  1 //package PTA9;
  2 import java.util.*;
  3 public class Main {
  4     public static void main(String[] args){
  5         Scanner input = new Scanner(System.in);
  6         String str = input.nextLine(); //str用于记录输入的字符串
  7         StringBuilder s = new StringBuilder();  //s用于暂存字符串
  8         String[] aAll = new String[]{"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786","6640000010045442002","6640000010045442003","6640000010045441009","6630000010033431001","6630000010033431008"};
  9         //输入判断以#结束
 10         while(!str.equals("#")){
 11             s.append(str);  //追加字符串在同一个地址空间
 12             s.append("\n");
 13             str = input.nextLine();
 14         }
 15         String data = String.valueOf(s); //将s中缓存的字符串存到字符串data中
 16         String[] deal = data.split("\n");
 17         for (String value : deal) {
 18             String deal1 = String.valueOf(value);
 19             String[] deal2 = deal1.split("\\s+");
 20             Account a = new Account(deal2[0]);
 21             if (deal2.length != 1) { //存取款操作
 22                 for (int j = 0; j < deal2.length; j++) {
 23                     ValidateDate validate = new ValidateDate(deal2[2], deal2[0]);
 24                     if (validate.accountValidateDate() == 1 ) {
 25                         withDraw withdraw = new withDraw(deal2[0], deal2[1], deal2[2], Double.parseDouble(deal2[3]));
 26                         withdraw.withDrawMate();
 27                     }
 28                     break;
 29                 }
 30             } else { //查询余额
 31                 int index = 20;
 32                 for (int k = 0; k < aAll.length; k++) {
 33                     if (a.getAccount().equals(aAll[k]))
 34                         index = k;
 35                 }
 36                 if (index == 0 || index == 1 || index == 2) {  //杨过
 37                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getY());
 38                 } else if (index == 3) {  //郭靖
 39                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getG());
 40                 } else if (index == 4 || index == 5 || index == 6 || index == 7) { //张无忌
 41                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getZ());
 42                 } else if (index == 8 || index == 9) {  //韦小宝
 43                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getW());
 44                 }
 45                 else if (index == 10 || index==11){ //张三丰
 46                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getZS());
 47                 }
 48                 else if ( index == 12){   //令狐冲
 49                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getL());
 50                 }
 51                 else if (index == 13){  //乔峰
 52                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getQ());
 53                 }
 54                 else if (index == 14){  //洪七公
 55                     System.out.printf("业务:查询余额 ¥%.2f\n", a.getH());
 56                 }
 57             }
 58         }
 59     }
 60 }
 61 //用户类
 62 class User{
 63     private String who;
 64     private String which;
 65     String[] countnum = new String[]{"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786","6640000010045442002","6640000010045442003","6640000010045441009","6630000010033431001","6630000010033431008"};
 66     String[] num = new String[]{"01","02","03","04","05","06","07","08","09","10","11"};
 67     //无参构造
 68     public User(){
 69     }
 70     //带参构造
 71     public User(String who,String which){
 72         this.who = who;
 73          this.which = which;
 74     }
 75     //用户名字
 76     public String getUName(){
 77         int index = 0;
 78         for(int i = 0; i < countnum.length; i++){
 79             if(countnum[i].equals(who))
 80                 index= i;
 81         }
 82         if(index==0 || index==1 || index==2) return "杨过";
 83         else if(index==3) return "郭靖";
 84         else if(index==4 || index==5 || index==6 || index ==7) return "张无忌";
 85         else if(index==8 || index==9) return "韦小宝";
 86         else if(index==10 || index==11) return "张三丰";
 87         else if(index==12) return "令狐冲";
 88         else if(index==13) return "乔峰";
 89         else if(index==14) return "洪七公";
 90         else return "0";
 91     }
 92     //银行名字
 93     public String getBankName(){
 94         int index = 0;
 95         for(int i = 0; i < num.length; i++){
 96             if(num[i].equals(which))
 97                 index= i;
 98         }
 99         if(index==0 || index==1 || index==2 || index==3) return "中国建设银行";
100         else if(index==4 || index==5) return "中国工商银行";
101         else if(index==6 || index==7 || index==8 || index==9 || index==10) return "中国农业银行";
102         else return "0";
103     }
104 }
105 //账户类
106 class Account{
107     private String account;
108     static private double Y = 10000;  //杨过
109     static private double G = 10000;  //郭靖
110     static private double Z = 10000;  //张无忌
111     static private double W = 10000;  //韦小宝
112     static private double ZS = 10000; //张三丰
113     static private double L = 10000;  //令狐冲
114     static private double Q = 10000;  //乔峰
115     static private double H = 10000; //洪七公
116     String[] atmAll = new String[]{"01","02","03","04","05","06","07","08","09","10","11"};
117     //无参构造
118     public Account(){
119     }
120     //带参构造
121     public Account(String account){this.account = account;}
122     //account getter
123     public String getAccount(){return account;}
124     //BalanceY setter
125     public void setY(double Y){Account.Y = Y;}
126     //BalanceY getter
127     public double getY() {return Y;}
128     //BalanceG getter
129     public double getG() {return G;}
130     //BalanceG setter
131     public void setG(double G) {Account.G = G;}
132     //BalanceZ getter
133     public  double getZ() {return Z;}
134     //BalanceZ setter
135     public  void setZ(double Z) {Account.Z = Z;}
136     //BalanceW setter
137     public  void setW(double W) {Account.W = W;}
138     //BalanceW getter
139     public  double getW() {return W;}
140     //BalanceZS getter
141     public  double getZS() {return ZS;}
142     //BalanceZS setter
143     public void setZS(double ZS) {Account.ZS = ZS;}
144     //BalanceL setter
145     public void setL(double L) {Account.L = L;}
146     //BalanceL getter
147     public  double getL() {return L;}
148     //BalanceQ setter
149     public  void setQ(double Q) {Account.Q = Q;}
150     //BalanceQ getter
151     public  double getQ() {return Q;}
152     //BalanceH getter
153     public  double getH() {return H;}
154     //BalanceH setter
155     public void setH(double H) {Account.H = H;}
156     //计算利率
157     public double countRate(String atmNum, String accountNum, double balance){
158         ValidateDate valadate = new ValidateDate(atmNum,accountNum);
159         int index = 20;
160         for(int i = 0; i < atmAll.length; i++){
161             if(atmNum.equals(atmAll[i])){
162                 index = i;
163                 break;
164             }
165         }
166         if(index==0 || index==1 || index==2 || index==3){
167             if((valadate.atmkh() == 1)) return 0;  //非跨行
168             else return balance*0.02;
169         }
170         if(index==4 || index==5){
171             if((valadate.atmkh() == 1)) return 0;  //非跨行
172             else return balance*0.03;
173         }
174         if(index==6 || index==7 || index==8 || index==9 || index==10){
175             if((valadate.atmkh() == 1)) return 0;  //非跨行
176             else return balance*0.04;
177         }
178         else return 0;
179     }
180 }
181 //数据输入合法性检验
182 class ValidateDate{
183     private String atmNum;
184     private String accountNum;
185     String[] accountAll = new String[]{"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007","6222081502001312389","6222081502001312390","6222081502001312399","6222081502001312400","6222081502051320785","6222081502051320786","6640000010045442002","6640000010045442003","6640000010045441009","6630000010033431001","6630000010033431008"};
186     String[] atmAll = new String[]{"01","02","03","04","05","06","07","08","09","10","11"};
187     //无参构造
188     public ValidateDate(){
189     }
190     //带参构造
191     public ValidateDate(String atmNum, String accountNum){
192         this.atmNum = atmNum;
193         this.accountNum = accountNum;
194     }
195     //账户输入合法性判断
196     public int accountValidateDate(){
197         int flag = 0; //标志,用于记录是否匹配
198         for (int i=0;i<accountAll.length;i++){
199             if(accountNum.equals(accountAll[i])){
200                 flag = 1;
201                 break;
202             }
203         }
204         if(flag==1) return 1;
205         else{
206             System.out.println("Sorry,this card does not exist.");
207             return 0;
208         }
209     }
210     //ATM机输入合法性检验
211     public int atm(){
212         int flag=0;
213         for (int i = 0; i < atmAll.length; i++) {
214             if (atmNum.equals(atmAll[i])) {
215                 flag=1;
216                 break;
217             }
218         }
219         if(flag==1)
220             return 1;
221         else
222             System.out.println("Sorry,the ATM's id is wrong.");
223             return 0;
224     }
225     //ATM机跨行检验
226     public int atmkh() {
227         int flag = 0; //标志,用于记录是否匹配
228         int index = 20;  //用于记录账户下标
229         int index0 = 20; //用于记录atm下标
230         for (int i = 0; i < atmAll.length; i++) {
231             if (atmNum.equals(atmAll[i])) {
232                 index0 = i;
233                 break;
234             }
235         }
236         for(int i = 0; i < accountAll.length; i++){
237             if(accountNum.equals(accountAll[i])) {
238                 index = i;
239                 break;
240             }
241         }
242         /*判断是否为跨行取款*/
243         if(index==0 || index==1 || index==2 || index==3 || index==10 || index==11){
244             if(index0==0 || index0==1 || index0==2 || index0==3){
245                 flag = 1;
246             }
247         }
248         if(index==4 || index==5 || index==6 || index==7 || index==8 || index==9 || index==12){
249             if(index0==4 || index0==5) {
250                 flag = 1;
251             }
252         }
253         if(index==13 || index==14){
254             if(index0==6 || index0==7 || index0==8 || index0==9 || index0==10) {
255                 flag = 1;
256             }
257         }
258         if(flag == 1) return 1;
259         else return 0;
260     }
261 }
262 //存取款
263 class withDraw{
264     String cardNum; //卡号
265     String cardPassword; //卡密码
266     String atmNum;  //ATM机号
267     double money;  //存取金额
268     String[] accountAll = new String[]{"6217000010041315709", "6217000010041315715", "6217000010041315718", "6217000010051320007", "6222081502001312389", "6222081502001312390", "6222081502001312399", "6222081502001312400", "6222081502051320785", "6222081502051320786", "6640000010045442002", "6640000010045442003", "6640000010045441009", "6630000010033431001", "6630000010033431008"};
269     String[] atmAll = new String[]{"01","02","03","04","05","06","07","08","09","10","11"};
270     //无参构造
271     public withDraw(){
272     }
273     //带参构造
274     public withDraw(String cardNum, String cardPassword, String atmNum, double money){
275         this.cardNum = cardNum;
276         this.cardPassword = cardPassword;
277         this.atmNum = atmNum;
278         this.money = money;
279     }
280     //存取款操作
281     public void withDrawMate() {
282 
283         ValidateDate validate = new ValidateDate(atmNum, cardNum);
284         Account account = new Account(cardNum);
285         User user = new User(cardNum,atmNum);
286 
287         int index = 20; //用于记录匹配账户下标
288         int index0 = 20;  //用于记录atm下标
289         for (int i = 0; i < accountAll.length; i++) {
290             if (account.getAccount().equals(accountAll[i])) {
291                 index = i;
292                 break;
293             }
294         }
295         for(int i = 0; i < atmAll.length ;i++){
296             if(atmNum.equals(atmAll[i])){
297                 index0 = i;
298                 break;
299             }
300         }
301 
302         //若输入账号和银行卡号合法
303         if (validate.accountValidateDate() == 1 && validate.atm()==1) {
304                 if(cardPassword.equals("88888888")){
305                     //若存款
306                     if(money < 0){
307                         System.out.println("业务:存款 " + user.getUName() + "在" + user.getBankName() + "的" + atmNum + "号ATM机上存款¥" + String.format("%.2f", -money));
308                         if(index==0 || index==1 || index==2) {  System.out.printf("当前余额为¥%.2f\n",(account.getY() - money));account.setY(account.getY() - money);
309                         }
310                         else if(index==3){System.out.printf("当前余额为¥%.2f\n",(account.getG() - money));account.setG(account.getG() - money);
311                         }
312                         else if(index==4 || index==5 || index==6 || index==7){System.out.printf("当前余额为¥%.2f\n",(account.getZ() - money));account.setZ(account.getZ() - money);
313                         }
314                         else if(index==8 || index==9){System.out.printf("当前余额为¥%.2f\n",account.getW() - money);account.setW(account.getW() - money);
315                         }
316                         else if(index==10 || index==11){System.out.printf("当前余额为¥%.2f\n", account.getZS() - money);account.setZS(account.getZS() - money);
317                         }
318                         else if(index==12){System.out.printf("当前余额为¥%.2f\n", account.getL() - money);account.setL(account.getL() - money);
319                                 }
320                         else if(index==13){System.out.printf("当前余额为¥%.2f\n", account.getQ() - money);account.setQ(account.getQ() - money);
321                         }
322                         else if(index==14){System.out.printf("当前余额为¥%.2f\n", account.getH() - money);account.setH(account.getH() - money);
323                         }
324                     }
325                     //取款
326                     else{
327 
328                         if((index==0|| index==1|| index==2||index==4 || index==5 || index==6||index==7 ||index==8 || index==9)&&(account.getY()-money<=0||account.getG()-money<=0||account.getZ()-money<=0||account.getW()-money<=0)||((index==10||index==11||index==12||index==13||index==14)&&account.getZS()-money<=-50000||account.getL()-money<=-50000||account.getQ()-money<=-50000||account.getH()-money<=-50000)||account.getZS()<=-50000||account.getL()<=-50000||account.getQ()<-50000||account.getH()<=-50000){
329                             System.out.println("Sorry,your account balance is insufficient."); return; }
330 
331                             System.out.println("业务:取款 " + user.getUName() + "在" + user.getBankName() + "的" + atmNum + "号ATM机上取款¥" + String.format("%.2f", money));
332 
333                         //利率
334                         double rate = account.countRate(atmNum,cardNum,money);
335 
336                         if(index==0 || index==1 || index==2){ //杨过
337                             if(account.getY()-money-rate>=0){
338                                 System.out.printf("当前余额为¥%.2f\n", account.getY() - money - rate);
339                                 account.setY(account.getY() - money - rate);
340                             }
341                             else{
342                                 System.out.println("Sorry,your account balance is insufficient.");
343                             }
344                         }
345 
346                         if(index==3){ //郭靖
347                             if(account.getG()-money-rate>=0){
348                                 System.out.printf("当前余额为¥%.2f\n", account.getG() - money - rate);
349                                 account.setG(account.getG() - money - rate);
350                             }
351                             else{
352                                 System.out.println("Sorry,your account balance is insufficient.");
353                             }
354                         }
355 
356                         if(index==4||index==5||index==6||index==7){ //张无忌
357                             if(account.getZ()-money-rate>=0){
358                                 System.out.printf("当前余额为¥%.2f\n", account.getZ() - money - rate);
359                                 account.setZ(account.getZ() - money - rate);
360                             }
361                             else{
362                                 System.out.println("Sorry,your account balance is insufficient.");
363                             }
364 
365                         }
366 
367                         if(index==8||index==9){ //韦小宝
368                             if(account.getW()-money-rate>=0){
369                                 System.out.printf("当前余额为¥%.2f\n", account.getW() - money - rate);
370                                 account.setW(account.getW() - money - rate);
371                             }
372                             else
373                                 System.out.println("Sorry,your account balance is insufficient.");
374                         }
375 
376                         if(index==10||index==11){ //张三丰
377                             if(account.getZS()-money>=0 && account.getZS()>=0){
378                                 System.out.printf("当前余额为¥%.2f\n", account.getZS() - money - rate);
379                                 account.setZS(account.getZS() - money - rate);
380                             }
381                             else if(account.getZS()-money<=0 && account.getZS()>0){
382                                 System.out.printf("当前余额为¥%.2f\n", account.getZS()-money-rate-(money-account.getZS())*0.05);
383                                 account.setZS(account.getZS()-money-rate-(money-account.getZS())*0.05);
384                             }
385                             else if(account.getZS()<=0&&(account.getZS()-money-rate-money*0.05)>=-50000){
386                                 System.out.printf("当前余额为¥%.2f\n", account.getZS()-money-rate-money*0.05);
387                                 account.setZS(account.getZS()-money-rate-money*0.05);
388                             }
389                             else
390                                 System.out.println("Sorry,your account balance is insufficient.");
391 
392                         }
393 
394                         if(index==12){  //令狐冲
395                             if(account.getL()>=0&&account.getL()-money>=0){
396                                 System.out.printf("当前余额为¥%.2f\n", account.getL() - money - rate);
397                                 account.setL(account.getL() - money - rate);
398                             }
399                             else if(account.getL()>0 && account.getL()-money<=0){
400                                 System.out.printf("当前余额为¥%.2f\n", account.getL()-money-rate-(money-account.getL())*0.05);
401                                 account.setL(account.getL()-money-rate-(money-account.getL())*0.05);
402                             }
403                             else if(account.getL()<=0&&(account.getL()-money-rate-money*0.05)>=-50000){
404                                 System.out.printf("当前余额为¥%.2f\n", account.getL()-money-rate-money*0.05);
405                                 account.setL(account.getL()+money-rate-money*0.05);
406                             }
407                             else
408                                 System.out.println("Sorry,your account balance is insufficient.");
409                         }
410 
411                         if(index==13){  //乔峰
412                             if(account.getQ()>=0&&account.getQ()-money>=0){
413                                 System.out.printf("当前余额为¥%.2f\n", account.getQ() - money - rate);
414                                 account.setQ(account.getQ() - money - rate);
415                             }
416                             else if(account.getQ()>0 && account.getQ()-money<=0){
417                                 System.out.printf("当前余额为¥%.2f\n", account.getQ()-money-rate-(money-account.getQ())*0.05);
418                                 account.setQ(account.getQ()-money-rate-(money-account.getQ())*0.05);
419                             }
420                             else if(account.getQ()<=0 && (account.getQ()-money-rate-money*0.05)>=-50000){
421                                 System.out.printf("当前余额为¥%.2f\n", account.getQ()-money-rate-money*0.05);
422                                 account.setQ(account.getQ()-money-rate-money*0.05);
423                             }
424                             else
425                                 System.out.println("Sorry,your account balance is insufficient.");
426                         }
427 
428                         if(index==14){  //洪七公
429                             if(account.getH()>=0&&account.getH()-money>=0){
430                                 System.out.printf("当前余额为¥%.2f\n", account.getH() - money - rate);
431                                 account.setH(account.getH() - money - rate);
432                             }
433                             else if(account.getH()>0 && account.getH()-money<=0){
434                                 System.out.printf("当前余额为¥%.2f\n", account.getH()-money-rate-(money-account.getH())*0.05);
435                                 account.setH(account.getH()-money-rate-(money-account.getH())*0.05);
436                             }
437                             else if(account.getH()<=0&&(account.getH()-money-rate-money*0.05)>=-50000){
438                                 System.out.printf("当前余额为¥%.2f\n", account.getH()-money-rate-money*0.05);
439                                 account.setH(account.getH()-money-rate-money*0.05);
440                             }
441                             else
442                                 System.out.println("Sorry,your account balance is insufficient.");
443                         }
444                     }
445 
446                 }
447                 else System.out.println("Sorry,your password is wrong.");
448         }
449     }
450 }

三、踩坑心得

(1)由于粗心,类设计不够完善,出现很多细节问题。例如index写成index0,然后智能通过调试慢慢的找错。

 1 //用户名字
 2     public String getUName(){
 3         int index = 0;
 4         for(int i = 0; i < countnum.length; i++){
 5             if(countnum[i].equals(who))
 6                 index= i;
 7         }
 8         if(index==0 || index==1 || index==2) return "杨过";
 9         else if(index==3) return "郭靖";
10         else if(index==4 || index==5 || index==6 || index ==7) return "张无忌";
11         else if(index==8 || index==9) return "韦小宝";
12         else if(index==10 || index==11) return "张三丰";
13         else if(index==12) return "令狐冲";
14         else if(index==13) return "乔峰";
15         else if(index==14) return "洪七公";
16         else return "0";
17     }

(2)因为习惯性喜欢写很多注释,所以在第一次提交的时候又因为代码过长而提交不了,然后删除空行和部分注释。

(3)第一次设计类结构时,没有注意到一个用户虽然拥有多张银行卡,但是余额都是归属于一个账户的。

 1 //账户类
 2 class Account{
 3     private String account;
 4     static private double balanceY = 10000;  //杨过
 5     static private double balanceG = 10000;  //郭靖
 6     static private double balanceZ = 10000;  //张无忌
 7     static private double balanceW = 10000;  //韦小宝
 8     //无参构造
 9     public Account(){
10     }
11     //带参构造
12     public Account(String account){
13         this.account = account;
14     }
15     //account setter
16     public void setAccount(String account) {
17         this.account = account;
18     }
19     //account getter
20     public String getAccount(){
21         return account;
22     }
23     //BalanceY setter
24     public void setBalanceY(double balanceY){
25         this.balanceY = balanceY;
26     }
27     //BalanceY getter
28     public double getBalanceY() {
29         return balanceY;
30     }
31     //BalanceG getter
32     public double getBalanceG() {
33         return balanceG;
34     }
35     //BalanceG setter
36     public void setBalanceG(double balanceG) {
37         this.balanceG = balanceG;
38     }
39     //BalanceZ getter
40     public double getBalanceZ() {
41         return balanceZ;
42     }
43     //BalanceZ setter
44     public void setBalanceZ(double balanceZ) {
45         this.balanceZ = balanceZ;
46     }
47     //BalanceW setter
48     public void setBalanceW(double balanceW) {
49         this.balanceW = balanceW;
50     }
51     //BalanceW getter
52     public double getBalanceW() {
53         return balanceW;
54     }
55 }

四、改进意见

(1)在银行卡号和ATM机号输入非法问题上,使用循环获得下标在进行账户判断优化了程序。

 

 1 String getUName(){
 2         int index = 0;
 3         for(int i = 0; i < countnum.length; i++){
 4             if(countnum[i].equals(who))
 5                 index= i;
 6         }
 7         if(index==0 || index==1 || index==2) return "杨过";
 8         else if(index==3) return "郭靖";
 9         else if(index==4 || index==5 || index==6 || index ==7) return "张无忌";
10         else if(index==8 || index==9) return "韦小宝";
11         else if(index==10 || index==11) return "张三丰";
12         else if(index==12) return "令狐冲";
13         else if(index==13) return "乔峰";
14         else if(index==14) return "洪七公";
15         else return "0";
16     }
17     //银行名字
18     public String getBankName(){
19         int index = 0;
20         for(int i = 0; i < num.length; i++){
21             if(num[i].equals(which))
22                 index= i;
23         }
24         if(index==0 || index==1 || index==2 || index==3) return "中国建设银行";
25         else if(index==4 || index==5) return "中国工商银行";
26         else if(index==6 || index==7 || index==8 || index==9 || index==10) return "中国农业银行";
27         else return "0";
28     }
29 }

 

(2)跨行检验上也使用下标

 1 //ATM机跨行检验
 2     public int atmkh() {
 3         int flag = 0; //标志,用于记录是否匹配
 4         int index = 20;  //用于记录账户下标
 5         int index0 = 20; //用于记录atm下标
 6         for (int i = 0; i < atmAll.length; i++) {
 7             if (atmNum.equals(atmAll[i])) {
 8                 index0 = i;
 9                 break;
10             }
11         }
12         for(int i = 0; i < accountAll.length; i++){
13             if(accountNum.equals(accountAll[i])) {
14                 index = i;
15                 break;
16             }
17         }
18         /*判断是否为跨行取款*/
19         if(index==0 || index==1 || index==2 || index==3 || index==10 || index==11){
20             if(index0==0 || index0==1 || index0==2 || index0==3){
21                 flag = 1;
22             }
23         }
24         if(index==4 || index==5 || index==6 || index==7 || index==8 || index==9 || index==12){
25             if(index0==4 || index0==5) {
26                 flag = 1;
27             }
28         }
29         if(index==13 || index==14){
30             if(index0==6 || index0==7 || index0==8 || index0==9 || index0==10) {
31                 flag = 1;
32             }
33         }
34         if(flag == 1) return 1;
35         else return 0;
36     }
37 }

 

五、总结

  这是JAVA面对对象数据结构学习的最后一次大作业,不知不觉,我们已经完成了9次题目集,从面对过程到面对对象过程的转变却是艰难的,从单一职责到继承再到多态,从数组到ArrayList到HashSet到HashMap到Tree...学会了一些JAVA自带的数组和类,但是掌握的还不够扎实,也发现还有很多等着我去学习的。从这三次题目集中学会了如何去设计一个类,先思考这个类的属性,作用等,再用封装,继承,多态技术去设计多个需要的类,在设计类之间的关系时,注意他们之间的关系(组合,依赖,关联等),也可以定义抽象类,利用抽象类中可能有抽象方法的特点,实现类型隐藏技术(因为继承的子类实现这个抽象方法时有很多种情况)。也可以利用接口实现多态性,提高程序的拓展性和可维护性。在设计代码的过程中,要学会运用面向对象的思想去设计一个程序,考虑"单一职责"原则、"开放封闭"原则、"Liskov替换原则"原则。希望日后的自己更上一层楼,加油!少年!

 

posted @ 2021-12-11 20:46  小白写博客  阅读(130)  评论(0)    收藏  举报