一、前言
这次blog主要是为了总结第7-9次pta作业的知识点和心得体会,这次总结的题目是最后三次作业的内容,学习java已经一学期,这次题目的难度对我来说很大,题目也更偏向实际应用,就要求我们更加全面、细节的考虑,代码的行数也突破到了几百行,对我们的知识储备和编码能力来说都是极大的挑战,虽然每次作业都只有一道题或两道题目,但我几乎都需要花费几天的时间设计和写出代码,再利用剩下的时间不断调试,更改细节问题。大量的时间和精力没有白费,我得到了自己较满意的成果,接下来是我对于这几次题目的具体分析和完成情况以及遇到的问题和我是如何解决的。
二、设计与分析
①题目集7(7-1)、(7-2)两道题目的递进式设计分析总结
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; interface Comparable { } abstract class Shape { private String shapeName; public Shape() { super(); } // 无参构造方法 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(); // 图形属性合法性校验 public abstract String toString(); // 输出图形的面积信息 } class Circle extends Shape { private double radius = 0; public Circle() { super(); super.setShapeName("Circle"); } public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public boolean validate() { return (radius > 0 ? true : false); } @Override public String toString() { return null; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } class Rectangle extends Shape { private double width; private double length; public Rectangle() { super(); super.setShapeName("Rectangle"); } public Rectangle(double width, double length) { this.width = width; this.length = length; } @Override public double getArea() { return width * length; } @Override public boolean validate() { return (width >= 0 && length >= 0 ? true : false); } @Override public String toString() { return null; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } } class Triangle extends Shape { private double side1 = 0; private double side2 = 0; private double side3 = 0; public Triangle() { super(); super.setShapeName("Triangle"); } public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } @Override public double getArea() { return 0.25 * Math.sqrt( (side1 + side2 + side3) * (side1 + side2 - side3) * (side1 + side3 - side2) * (side2 + side3 - side1)); } @Override public boolean validate() { return ((side1 >= 0 && side2 >= 0 && side3 >= 0 && (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1)) ? true : false); } @Override public String toString() { return null; } public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double height; public Trapezoid() { super(); super.setShapeName("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 && bottomSide >= 0 && height >= 0 ? true : false); } @Override public String toString() { return null; } public double getTopSide() { return topSide; } public void setTopSide(double topSide) { this.topSide = topSide; } public double getBottomSide() { return bottomSide; } public void setBottomSide(double bottomSide) { this.bottomSide = bottomSide; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } } class Card implements Comparable { private Shape shape; public Card() { super(); } public Card(Shape shape) { this.shape = shape; } public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape = shape; } public int compareTo(Card card) { return 0; } } class DealCardList { ArrayList<Card> cardList = new ArrayList<Card>(); public DealCardList() { super(); } public DealCardList(ArrayList<Integer> list) { Card card; for(int i = 0; i < list.size();i++) { switch(list.get(i)) { case 1: Circle circle = new Circle(); circle.setRadius(Main.input.nextDouble()); if(!circle.validate()) { //判断圆形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(circle); cardList.add(card); break; case 2: Rectangle rectangle = new Rectangle(); rectangle.setWidth(Main.input.nextDouble()); rectangle.setLength(Main.input.nextDouble()); if(!rectangle.validate()) { //判断矩形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(rectangle); cardList.add(card); break; case 3: Triangle triangle = new Triangle(); triangle.setSide1(Main.input.nextDouble()); triangle.setSide2(Main.input.nextDouble()); triangle.setSide3(Main.input.nextDouble()); if(!triangle.validate()) { //判断三角形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(triangle); cardList.add(card); break; case 4: Trapezoid trapezoid = new Trapezoid(); trapezoid.setTopSide(Main.input.nextDouble()); trapezoid.setBottomSide(Main.input.nextDouble()); trapezoid.setHeight(Main.input.nextDouble()); if(!trapezoid.validate()) { //判断梯形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(trapezoid); cardList.add(card); break; default: System.out.println("Wrong Format"); System.exit(0); } } } public boolean validate() { return true; } public void cardSort() { for(int i=0;i<cardList.size();i++) { for(int j=i+1;j<cardList.size();j++) { if(cardList.get(i).getShape().getArea()<cardList.get(j).getShape().getArea()) java.util.Collections.swap(cardList, i, j); } } } public double getAllArea() { double sumArea = 0; for(int i = 0; i<cardList.size(); i++) { sumArea += cardList.get(i).getShape().getArea(); } return sumArea; } public void showResult() { int k = 0; System.out.println("The original list:"); for( k=0; k<cardList.size(); k++) { System.out.print(cardList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cardList.get(k).getShape().getArea()) + " "); } System.out.println(); cardSort(); System.out.println("The sorted list:"); for(k=0; k<cardList.size(); k++) { System.out.print(cardList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cardList.get(k).getShape().getArea()) + " "); } System.out.println(); System.out.println("Sum of area:" + String.format("%.2f", getAllArea())); } } public class Main { // 其它类中如果想要使用该对象进行输入Main.input.next… 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(); } }结果测试:
结果正确
根据SourceMonitor的生成报表:
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
源程序:
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; interface Comparable { } abstract class Shape { private String shapeName; public Shape() { super(); } // 无参构造方法 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(); // 图形属性合法性校验 public abstract String toString(); // 输出图形的面积信息 } class Circle extends Shape { private double radius = 0; public Circle() { super(); super.setShapeName("Circle"); } public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public boolean validate() { return (radius > 0 ? true : false); } @Override public String toString() { return null; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } class Rectangle extends Shape { private double width; private double length; public Rectangle() { super(); super.setShapeName("Rectangle"); } public Rectangle(double width, double length) { this.width = width; this.length = length; } @Override public double getArea() { return width * length; } @Override public boolean validate() { return (width >= 0 && length >= 0 ? true : false); } @Override public String toString() { return null; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } } class Triangle extends Shape { private double side1 = 0; private double side2 = 0; private double side3 = 0; public Triangle() { super(); super.setShapeName("Triangle"); } public Triangle(double side1, double side2, double side3) { this.side1 = side1; this.side2 = side2; this.side3 = side3; } @Override public double getArea() { return 0.25 * Math.sqrt( (side1 + side2 + side3) * (side1 + side2 - side3) * (side1 + side3 - side2) * (side2 + side3 - side1)); } @Override public boolean validate() { return ((side1 >= 0 && side2 >= 0 && side3 >= 0 && (side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1)) ? true : false); } @Override public String toString() { return null; } public double getSide1() { return side1; } public void setSide1(double side1) { this.side1 = side1; } public double getSide2() { return side2; } public void setSide2(double side2) { this.side2 = side2; } public double getSide3() { return side3; } public void setSide3(double side3) { this.side3 = side3; } } class Trapezoid extends Shape { private double topSide; private double bottomSide; private double height; public Trapezoid() { super(); super.setShapeName("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 && bottomSide >= 0 && height >= 0 ? true : false); } @Override public String toString() { return null; } public double getTopSide() { return topSide; } public void setTopSide(double topSide) { this.topSide = topSide; } public double getBottomSide() { return bottomSide; } public void setBottomSide(double bottomSide) { this.bottomSide = bottomSide; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } } class Card implements Comparable { private Shape shape; private double cardAllArea = 0; public Card() { super(); } public Card(Shape shape) { this.shape = shape; } public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape = shape; } public int compareTo() { return 0; } public double getCardAllArea() { return cardAllArea; } public void setCardAllArea(double area) { this.cardAllArea = this.cardAllArea + area; } } class DealCardList { ArrayList<Card> cardList = new ArrayList<Card>(); ArrayList<Card> cirList = new ArrayList<Card>(); ArrayList<Card> recList = new ArrayList<Card>(); ArrayList<Card> triList = new ArrayList<Card>(); ArrayList<Card> traList = new ArrayList<Card>(); public DealCardList() { super(); } public DealCardList(ArrayList<Integer> list) { Card card; for(int i = 0; i < list.size();i++) { switch(list.get(i)) { case 1: Circle circle = new Circle(); circle.setRadius(Main.input.nextDouble()); if(!circle.validate()) { //判断圆形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(circle); cardList.add(card); cirList.add(card); break; case 2: Rectangle rectangle = new Rectangle(); rectangle.setWidth(Main.input.nextDouble()); rectangle.setLength(Main.input.nextDouble()); if(!rectangle.validate()) { //判断矩形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(rectangle); cardList.add(card); recList.add(card); break; case 3: Triangle triangle = new Triangle(); triangle.setSide1(Main.input.nextDouble()); triangle.setSide2(Main.input.nextDouble()); triangle.setSide3(Main.input.nextDouble()); if(!triangle.validate()) { //判断三角形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(triangle); cardList.add(card); triList.add(card); break; case 4: Trapezoid trapezoid = new Trapezoid(); trapezoid.setTopSide(Main.input.nextDouble()); trapezoid.setBottomSide(Main.input.nextDouble()); trapezoid.setHeight(Main.input.nextDouble()); if(!trapezoid.validate()) { //判断梯形是否合法,不合法Wrong Format System.out.println("Wrong Format"); System.exit(0); } card = new Card(); card.setShape(trapezoid); cardList.add(card); traList.add(card); break; default: System.out.println("Wrong Format"); System.exit(0); } } } public boolean validate() { return true; } public void cardSort(ArrayList<Card> list) { for(int i=0;i<list.size();i++) { for(int j=i+1;j<list.size();j++) { if(list.get(i).getShape().getArea()<list.get(j).getShape().getArea()) java.util.Collections.swap(list, i, j); } } } public double getMaxArea() { double cirArea = 0, recArea = 0, triArea = 0, traArea = 0; for(int i = 0; i<cirList.size(); i++) { cirArea += cirList.get(i).getShape().getArea(); } for(int i = 0; i<recList.size(); i++) { recArea += recList.get(i).getShape().getArea(); } for(int i = 0; i<triList.size(); i++) { triArea += triList.get(i).getShape().getArea(); } for(int i = 0; i<traList.size(); i++) { traArea += traList.get(i).getShape().getArea(); } double[] arr = {cirArea, recArea, triArea, traArea}; java.util.Arrays.sort(arr); return arr[3]; } public void show() { int k = 0; System.out.print("["); for( k=0; k<cirList.size(); k++) { System.out.print(cirList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cirList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.print("["); for( k=0; k<recList.size(); k++) { System.out.print(recList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", recList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.print("["); for( k=0; k<triList.size(); k++) { System.out.print(triList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", triList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.print("["); for( k=0; k<traList.size(); k++) { System.out.print(traList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", traList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.println(); } public void showResult() { int k = 0; System.out.println("The original list:"); System.out.print("["); for( k=0; k<cardList.size(); k++) { System.out.print(cardList.get(k).getShape().getShapeName() + ":" + String.format("%.2f", cardList.get(k).getShape().getArea()) + " "); } System.out.print("]"); System.out.println(); System.out.println("The Separated List:"); show(); System.out.println("The Separated sorted List:"); cardSort(cirList); cardSort(recList); cardSort(triList); cardSort(traList); show(); System.out.println("The max area:" + String.format("%.2f", getMaxArea())); } } public class Main { // 其它类中如果想要使用该对象进行输入Main.input.next… public static Scanner input = new Scanner(System.in); public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); int num = input.nextInt(); if(num == 0){ System.out.println("Wrong Format"); System.exit(0); } 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(); } }
结果测试:
结果正确
根据SourceMonitor的生成报表:
从类图以及SourceMonitor的生成报表上可以看出两程序的类结构非常相似,甚至复杂度都基本一样。 分析两题的不同递进设计。在题目集7(7-1)中是并不需要将卡片进行分类的,据题目给出的类图,分别定义了:Shape类作为父类,而圆形(Circle)、矩形(Rectangle)、三角形(Triangle)及梯形(Trapezoid)四个类作为子类,子类只需要利用多态重构Shape中的方法。 而在题目集7(7-2)中是要求卡片分组的,在进行分组处理时ArrayList数组,因此在DealCardList类中与题目集7(7-1)中不同。 ②题目集8和题目集9两道ATM机仿真题目的设计思路分析总结 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
源程序:
import java.util.ArrayList; import java.util.Scanner; class find { } class ChinaUnionPay { private ArrayList<Bank> banks = new ArrayList<Bank>(); private ArrayList<Card> cards = new ArrayList<Card>(); public ChinaUnionPay() { } public ArrayList<Bank> getBank() { return banks; } public void addBank(Bank bank) { // 向银行列表中添加银行 banks.add(bank); } public void addcards(Card card) { cards.add(card); } public Card getCard(String cardNum) { for (Card o : this.cards) { if (cardNum.equals(o.getNumber())) { return o; } } return null; } public void setCards(ArrayList<Card> cards) { this.cards = cards; } public boolean validate(String cardNum) { for (Card o : cards) { if (cardNum.equals(o.getNumber())) { return true; } } return false; } public ATM getATM(String atmid) { for (Bank bank : this.getBank()) { for (ATM atm : bank.getATMid()) { if (atmid.equals(atm.ATMid)) { return atm; } } } return null; } public User getUser(String cardNum) { for (Bank bank : this.getBank()) { for (User user : bank.getUserName()) { for (Account account : user.getAccount()) { for (Card card : account.getCard()) { if (card.getNumber().equals(cardNum)) { return user; } } } } } return null; } public Bank getBank(String cardNum) { for (Bank bank : this.getBank()) { for (User user : bank.getUserName()) { for (Account account : user.getAccount()) { for (Card card : account.getCard()) { if (card.getNumber().equals(cardNum)) { return bank; } } } } } return null; } public Account getAccount(String cardNum) { for (Bank bank : this.getBank()) { for (User user : bank.getUserName()) { for (Account account : user.getAccount()) { for (Card card : account.getCard()) { if (card.getNumber().equals(cardNum)) { return account; } } } } } return null; } } class Bank { private String bankName; private ArrayList<User> userNames = new ArrayList<User>(); private ArrayList<ATM> ATMids = new ArrayList<ATM>(); public Bank() { } public Bank(String bankName) { this.bankName = bankName; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public ArrayList<User> getUserName() { return userNames; } public void addUserName(User userName) { userNames.add(userName); } public ArrayList<ATM> getATMid() { return ATMids; } public void addATMid(ATM ATMid) { ATMids.add(ATMid); } public static void user(User userName) { } public static void ATM(ATM ATMid) { } } class User { private String userName; private ArrayList<Account> accounts = new ArrayList<Account>(); public User() { } public User(String userName) { this.userName = userName; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public ArrayList<Account> getAccount() { return accounts; } public void addAccount(Account account) { // 向账户列表添加一个账户 accounts.add(account); } public void Account(Account account) { } } class ATM { String ATMid; public ATM() { } public ATM(String ATMid) { this.ATMid = ATMid; } public void takeOut(Account account, double money) { account.setBalance(account.getBalance() - money); } public void storage(Account account, double money) { account.setBalance(account.getBalance() + money); } public void check() { } } class Account { private String accountName; private double balance; private ArrayList<Card> cards = new ArrayList<Card>(); public Account() { } public Account(String accountName) { this.accountName = accountName; } public String getAccountName() { return accountName; } public void setAccountName(String accountName) { this.accountName = accountName; } public ArrayList<Card> getCard() { return cards; } public void addCard(Card card) { cards.add(card); } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } class Card { private String number; private String password; public Card() { } public Card(String number) { this.number = number; } public boolean checkPassword(String password) { if (this.password.equals(password)) return true; return false; } public void setPassword(String password) { this.password = password; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } public class Main { static ChinaUnionPay 中国银联 = new ChinaUnionPay(); public static Scanner input = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub init(); String cardNum = ""; String password = ""; String ATMid = ""; double money = 0; Card card = new Card(); Account account = new Account(); ATM atm = new ATM(); String in = input.nextLine(); while (!(in.equals("#"))) { String[] msg = in.split(" "); cardNum = msg[0]; if (!中国银联.validate(cardNum)) { System.out.println("Sorry,this card does not exist."); System.exit(0); } card = 中国银联.getCard(cardNum); account = 中国银联.getAccount(cardNum); switch (msg.length) { case 1: // 查询账户余额 card = 中国银联.getCard(cardNum); System.out.println("¥" + String.format("%.2f", account.getBalance())); break; case 4: // 存款或取款 password = msg[1]; if (!card.checkPassword(password)) { System.out.println("Sorry,your password is wrong."); System.exit(0); } ATMid = msg[2]; atm = 中国银联.getATM(ATMid); if (atm==null) { System.out.println("Sorry,the ATM's id is wrong."); System.exit(0); } money = Double.parseDouble(msg[3]); if (money >= 0) { if (money > account.getBalance()) { System.out.println("Sorry,your account balance is insufficient."); System.exit(0); } atm.takeOut(account, money); System.out.println(中国银联.getUser(cardNum).getUserName() + "在" + 中国银联.getBank(cardNum).getBankName() + "的" + ATMid + "号ATM机上取款¥" + String.format("%.2f", money)); System.out.println("当前余额为¥" + String.format("%.2f", account.getBalance())); } else { atm.storage(account, (-1)*money); System.out.println(中国银联.getUser(cardNum).getUserName() + "在" + 中国银联.getBank(cardNum).getBankName() + "的" + ATMid + "号ATM机上存款¥" + String.format("%.2f", (-1)*money)); System.out.println("当前余额为¥" + String.format("%.2f", account.getBalance())); } break; } in = input.nextLine(); String[] str = in.split(""); if(str[0].equals("#")) { System.exit(0); } } } private static void init() { // 初始化资料库 // 增加银行 Bank 中国建设银行 = new Bank("中国建设银行"); Bank 中国工商银行 = new Bank("中国工商银行"); 中国银联.addBank(中国建设银行); 中国银联.addBank(中国工商银行); // 增加ATM ATM ATM01 = new ATM("01"); ATM ATM02 = new ATM("02"); ATM ATM03 = new ATM("03"); ATM ATM04 = new ATM("04"); 中国建设银行.addATMid(ATM01); 中国建设银行.addATMid(ATM02); 中国建设银行.addATMid(ATM03); 中国建设银行.addATMid(ATM04); ATM ATM05 = new ATM("05"); ATM ATM06 = new ATM("06"); 中国工商银行.addATMid(ATM05); 中国工商银行.addATMid(ATM06); // 增加用户、账户、卡号、初始化密码和余额 User 杨过 = new User("杨过"); 中国建设银行.addUserName(杨过); Account 杨过A1 = new Account("3217000010041315709"); 杨过A1.setBalance(10000.00); 杨过.addAccount(杨过A1); Card 杨过A1C1 = new Card("6217000010041315709"); 杨过A1C1.setPassword("88888888"); 杨过A1.addCard(杨过A1C1); 中国银联.addcards(杨过A1C1); Card 杨过A1C2 = new Card("6217000010041315715"); 杨过A1C2.setPassword("88888888"); 杨过A1.addCard(杨过A1C2); 中国银联.addcards(杨过A1C2); Account 杨过A2 = new Account("3217000010041315715"); 杨过A2.setBalance(10000.00); 杨过.addAccount(杨过A2); Card 杨过A2C1 = new Card("6217000010041315718"); 杨过A2C1.setPassword("88888888"); 杨过A2.addCard(杨过A2C1); 中国银联.addcards(杨过A2C1); User 郭靖 = new User("郭靖"); 中国建设银行.addUserName(郭靖); Account 郭靖A1 = new Account("3217000010051320007"); 郭靖A1.setBalance(10000.00); 郭靖.addAccount(郭靖A1); Card 郭靖A1C1 = new Card("6217000010051320007"); 郭靖A1C1.setPassword("88888888"); 郭靖A1.addCard(郭靖A1C1); 中国银联.addcards(郭靖A1C1); User 张无忌 = new User("张无忌"); 中国工商银行.addUserName(张无忌); Account 张无忌A1 = new Account("3222081502001312389"); 张无忌A1.setBalance(10000.00); 张无忌.addAccount(张无忌A1); Card 张无忌A1C1 = new Card("6222081502001312389"); 张无忌A1C1.setPassword("88888888"); 张无忌A1.addCard(张无忌A1C1); 中国银联.addcards(张无忌A1C1); Account 张无忌A2 = new Account("3222081502001312390"); 张无忌.addAccount(张无忌A2); 张无忌A2.setBalance(10000.00); Card 张无忌A2C1 = new Card("6222081502001312390"); 张无忌A2C1.setPassword("88888888"); 张无忌A2.addCard(张无忌A2C1); 中国银联.addcards(张无忌A2C1); Account 张无忌A3 = new Account("3222081502001312399"); 张无忌A3.setBalance(10000.00); 张无忌.addAccount(张无忌A3); Card 张无忌A3C1 = new Card("6222081502001312399"); 张无忌A3C1.setPassword("88888888"); 张无忌A3.addCard(张无忌A3C1); 中国银联.addcards(张无忌A3C1); Card 张无忌A3C2 = new Card("6222081502001312400"); 张无忌A3C2.setPassword("88888888"); 张无忌A3.addCard(张无忌A3C2); 中国银联.addcards(张无忌A3C2); User 韦小宝 = new User("韦小宝"); 中国工商银行.addUserName(韦小宝); Account 韦小宝A1 = new Account("3222081502051320785"); 韦小宝A1.setBalance(10000.00); 韦小宝.addAccount(韦小宝A1); Card 韦小宝A1C1 = new Card("6222081502051320785"); 韦小宝A1C1.setPassword("88888888"); 韦小宝A1.addCard(韦小宝A1C1); 中国银联.addcards(韦小宝A1C1); Account 韦小宝A2 = new Account("3222081502051320786"); 韦小宝.addAccount(韦小宝A2); 韦小宝A2.setBalance(10000.00); Card 韦小宝A2C1 = new Card("6222081502051320786"); 韦小宝A2C1.setPassword("88888888"); 韦小宝A2.addCard(韦小宝A2C1); 中国银联.addcards(韦小宝A2C1); } }
这道题目的得分只有70分,不断修改之后还是有三个测试点不能通过
因为代码做了多次删改,冗长且复杂,不利于删改或者增加要求,写成了一次性代码
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
源程序:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Org chinaUnionPay = new ChinaUnionPay(); //中国建设银行 Bank CCB = new Bank(chinaUnionPay,"中国建设银行",0.02); //中国工商银行 Bank ICBC = new Bank(chinaUnionPay,"中国工商银行",0.03); //中国农业银行 Bank ABC = new Bank(chinaUnionPay,"中国农业银行",0.04); //添加ATM机 for (int i = 1; i <= 11; i++) { ATM atm = new ATM(); if (i < 5){ atm.setNO("0"+i); CCB.getATMs().add(atm); continue; } if (i < 7){ atm.setNO("0"+i); ICBC.getATMs().add(atm); continue; } if (i < 10){ atm.setNO("0"+i); ABC.getATMs().add(atm); continue; } atm.setNO(""+i); ABC.getATMs().add(atm); } //创建账户 //杨过 List<Card> cards1 = new ArrayList<>(); cards1.add(new Card("6217000010041315709","88888888")); cards1.add(new Card("6217000010041315715","88888888")); Account account1 = new Account("3217000010041315709", "借记账号", cards1, CCB, 10000); List<Card> cards2 = new ArrayList<>(); cards2.add(new Card("6217000010041315718","88888888")); Account account2 = new Account("3217000010041315715", "借记账号", cards2, CCB, 10000); ArrayList<Account> accounts = new ArrayList<>(); accounts.add(account1); accounts.add(account2); User yg = new User("杨过", accounts); //郭靖 List<Card> cards3 = new ArrayList<>(); cards3.add(new Card("6217000010051320007","88888888")); Account account3 = new Account("3217000010051320007", "借记账号", cards3, CCB, 10000); ArrayList<Account> accounts1 = new ArrayList<>(); accounts1.add(account3); User gj = new User("郭靖", accounts1); //张无忌 List<Card> cards4 = new ArrayList<>(); cards4.add(new Card("6222081502001312389","88888888")); Account account4 = new Account("3222081502001312389", "借记账号", cards4, ICBC, 10000); List<Card> cards5 = new ArrayList<>(); cards5.add(new Card("6222081502001312390","88888888")); Account account5 = new Account("3222081502001312390", "借记账号", cards5, ICBC, 10000); List<Card> cards6 = new ArrayList<>(); cards6.add(new Card("6222081502001312399","88888888")); cards6.add(new Card("6222081502001312400","88888888")); Account account6 = new Account("3222081502001312399", "借记账号", cards6, ICBC, 10000); ArrayList<Account> accounts2 = new ArrayList<>(); accounts2.add(account4); accounts2.add(account5); accounts2.add(account6); User zwj = new User("张无忌", accounts2); //韦小宝 List<Card> cards7 = new ArrayList<>(); cards7.add(new Card("6222081502051320785","88888888")); Account account7 = new Account("3222081502051320785", "借记账号", cards7, ICBC, 10000); List<Card> cards8 = new ArrayList<>(); cards8.add(new Card("6222081502051320786","88888888")); Account account8 = new Account("3222081502051320786", "借记账号", cards8, ICBC, 10000); ArrayList<Account> accounts3 = new ArrayList<>(); accounts3.add(account7); accounts3.add(account8); User wxb = new User("韦小宝", accounts3); //张三丰 List<Card> cards9 = new ArrayList<>(); cards9.add(new Card("6640000010045442002","88888888")); cards9.add(new Card("6640000010045442003","88888888")); Account account9 = new Account("3640000010045442002", "贷记账号", cards9, CCB, 10000); ArrayList<Account> accounts4 = new ArrayList<>(); accounts4.add(account9); User zsf = new User("张三丰", accounts4); //令狐冲 List<Card> cards10 = new ArrayList<>(); cards10.add(new Card("6640000010045441009","88888888")); Account account10 = new Account("3640000010045441009", "贷记账号", cards10, ICBC, 10000); ArrayList<Account> accounts5 = new ArrayList<>(); accounts5.add(account10); User lfc = new User("令狐冲", accounts5); //乔峰 List<Card> cards11 = new ArrayList<>(); cards11.add(new Card("6630000010033431001","88888888")); Account account11 = new Account("3630000010033431001", "贷记账号", cards11, ABC, 10000); ArrayList<Account> accounts6 = new ArrayList<>(); accounts6.add(account11); User qf = new User("乔峰", accounts6); //洪七公 List<Card> cards12 = new ArrayList<>(); cards12.add(new Card("6630000010033431008","88888888")); Account account12 = new Account("3630000010033431008", "贷记账号", cards12, ABC, 10000); ArrayList<Account> accounts7 = new ArrayList<>(); accounts7.add(account12); User hqg = new User("洪七公", accounts7); List<User> users = new ArrayList<>(); users.add(yg); users.add(gj); users.add(zwj); users.add(wxb); users.add(zsf); users.add(lfc); users.add(qf); users.add(hqg); List<Bank> banks = new ArrayList<>(); banks.add(ABC); banks.add(ICBC); banks.add(CCB); Scanner scanner = new Scanner(System.in); List<String> inputs = new ArrayList<>(); for (;;) { String s = scanner.nextLine(); if (s.equals("#")){ break; } inputs.add(s); } for (String input : inputs) { doAction(users, banks, input); } } private static void doAction(List<User> users, List<Bank> banks, String s) { String[] input = s.split("\\s+"); if (input.length == 4){ String carNo = input[0]; String pwd = input[1]; String ATMNo = input[2];// double amount = Double.parseDouble(input[3]); Account inputAccount = null; double privilegePoundage = 0; Bank atmBank = null; String bankName = null; Card inputCard = null; User inputUser = null; for (User user : users) { List<Account> accountList = user.getAccounts(); for (Account account : accountList) { List<Card> cards = account.getCards(); for (Card card : cards) { if (card.getNO().equals(carNo)) { inputAccount = account; inputCard = card; inputUser = user; break; } } } } if (inputAccount == null){ System.out.println("Sorry,this card does not exist."); return; } boolean hasATM = false; for (Bank bank : banks) { List<ATM> atMs1 = bank.getATMs(); for (ATM atm : atMs1) { if (atm.getNO().equals(ATMNo)) { hasATM = true; bankName = bank.getBankName(); if (bank != inputAccount.getBank()) { privilegePoundage = bank.getPrivilegePoundage(); } break; } } } if (!hasATM) { System.out.println("Sorry,the ATM's id is wrong."); // System.out.println(carNo); return; } if (!inputCard.getPassword().equals(pwd)) { System.out.println("Sorry,your password is wrong."); // System.out.println(carNo+" "+ ATMNo); return; } if (inputAccount.getType().equals("借记账号")) { if (inputAccount.getBalance() < amount*(1+privilegePoundage)) { System.out.println("Sorry,your account balance is insufficient."); // System.out.println(carNo+" "+ pwd+" "+ATMNo); return; } double v = inputAccount.getBalance() - amount * (1 + privilegePoundage); inputAccount.setBalance(v); System.out.println("业务:取款 "+inputUser.getName()+"在"+bankName+"的"+ATMNo+"号ATM机上取款¥"+String.format("%.2f",amount)+"\n" + "当前余额为¥"+String.format("%.2f", v)); }else { Org org = inputAccount.getBank().getOrg(); double balance = inputAccount.getBalance(); double overdraftFeeWithdrawal = org.getOverdraftFeeWithdrawal(); double overdraftLimit = org.getOverdraftLimit(); if (balance < amount*(1+privilegePoundage)) { double v = 0; if (balance > 0) { v = (balance-amount) * (1 + overdraftFeeWithdrawal) - amount * privilegePoundage; }else { v = balance - (amount * (1 + overdraftFeeWithdrawal) + amount * privilegePoundage) ; //System.out.println("Aaaaa"); } if (Math.abs(v) > overdraftLimit) { System.out.println("Sorry,your account balance is insufficient."); // System.out.println(carNo+" "+ pwd+" "+ATMNo); return; } inputAccount.setBalance(v); System.out.println("业务:取款 "+inputUser.getName()+"在"+bankName+"的"+ATMNo+"号ATM机上取款¥"+String.format("%.2f",amount)+"\n" + "当前余额为¥"+String.format("%.2f",v)); }else { double v = inputAccount.getBalance() - amount * (1 + privilegePoundage); inputAccount.setBalance(v); System.out.println("业务:取款 "+inputUser.getName()+"在"+bankName+"的"+ATMNo+"号ATM机上取款¥"+String.format("%.2f",amount)+"\n" + "当前余额为¥"+String.format("%.2f", v)); } } } else if (input.length == 1){ for (User user : users) { List<Account> accountList = user.getAccounts(); for (Account account : accountList) { for (Card card : account.getCards()) { if (card.getNO().equals(input[0])) { double balance = account.getBalance(); System.out.println("业务:查询余额 ¥"+String.format("%.2f",balance)); return; } } } } System.out.println("Sorry,this card does not exist."); } } /** * 银行账户 */ public static class Account { //银行账号 private String NO; //账号种类 private String type; //账户拥有的银行卡 private List<Card> cards; //隶属银行 private Bank bank; //余额 private double balance; public Account(String NO, String type, List<Card> cards, Bank bank, double balance) { this.NO = NO; this.type = type; this.cards = cards; this.bank = bank; this.balance = balance; } public String getNO() { return NO; } public void setNO(String NO) { this.NO = NO; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List<Card> getCards() { return cards; } public void setCards(List<Card> cards) { this.cards = cards; } public Bank getBank() { return bank; } public void setBank(Bank bank) { this.bank = bank; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } /** * 用户实体 */ public static class User { //姓名 private String name; //用户拥有的账户 private List<Account> accounts; public User(String name, List<Account> accounts) { this.name = name; this.accounts = accounts; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Account> getAccounts() { return accounts; } public void setAccounts(List<Account> accounts) { this.accounts = accounts; } } /** * ATM机实体类 */ public static class ATM { //编号 private String NO; public String getNO() { return NO; } public void setNO(String NO) { this.NO = NO; } } /** * 银行实体类 */ public static class Bank { //所属机构 private Org org; //名称 private String bankName; //跨行取款手续费 private double privilegePoundage; //银行拥有的ATM机 private List<ATM> ATMs; public Bank(Org org, String bankName, double privilegePoundage) { this.org = org; this.bankName = bankName; this.privilegePoundage = privilegePoundage; this.ATMs = new ArrayList<>(); } public Org getOrg() { return org; } public void setOrg(Org org) { this.org = org; } public String getBankName() { return bankName; } public void setBankName(String bankName) { this.bankName = bankName; } public double getPrivilegePoundage() { return privilegePoundage; } public void setPrivilegePoundage(double privilegePoundage) { this.privilegePoundage = privilegePoundage; } public List<ATM> getATMs() { return ATMs; } public void setATMs(List<ATM> ATMs) { this.ATMs = ATMs; } } /** * 银行实体类 */ public static class Card { //银行卡号 private String NO; //密码 private String password; public Card(String NO, String password) { this.NO = NO; this.password = password; } public String getNO() { return NO; } public void setNO(String NO) { this.NO = NO; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } //组织接口 public interface Org { public String getName(); double getOverdraftFeeWithdrawal(); double getOverdraftLimit(); } /** * 中国银联实体 */ public static class ChinaUnionPay implements Org { //名称 private String name = "中国银联"; //透支取款手续费 private double OverdraftFeeWithdrawal = 0.05; //透支最大限额 private double OverdraftLimit = 50000; @Override public String getName() { return this.name; } @Override public double getOverdraftFeeWithdrawal() { return OverdraftFeeWithdrawal; } @Override public double getOverdraftLimit() { return OverdraftLimit; } } }
这次题目我吸取了上次的教训,先把类图设计好,设计方面难点在于如何实现多态。
题目说明着重写了卡能否贷款取决于账户类型,所以账户是肯定要继承于抽象类的,同时也便于再添加新类型的账户。
而两种账户唯一的差别就在于能否贷款,所以可以针对能否贷款这一点来构建抽象类。跨行手续费,和贷款手续费就要在相应类里面增加属性。
输出测试:
根据SourceMonitor的生成报表:
三、踩坑心得
这次题目的难点主要在于,有多种复杂功能需要实现,类与类之间的各种关系分析,尤其是第八第九次实验,老师没有给出类图,就需要我们前期先把类的结构设计好,这样才能保证后期能顺畅的写代码。
在我写第八题的时候,就是因为类的联系设计不好,后期要不断修改以得到正确输出,导致代码结构冗杂混乱,只能一次性使用。
各种类聚合和多态输入的时候是字符串形式而用到检测或者处理的时候需要数字形式,所以要用到包装类的方法的设计。可以运用arraylist降低程序复杂度
四、总结
学习java已经一学期,这次题目的难度对我来说很大,题目也更偏向实际应用,就要求我们更加全面、细节的考虑,代码的行数也突破到了几百行,对我们的知识储备和编码能力来说都是极大的挑战,虽然每次作业都只有一道题或两道题目,但我几乎都需要花费几天的时间设计和写出代码。
虽然这次作业对我来说偏难,是最耗时耗力的一次,但我的收获也是很大的,对类之间的关系有了更深的了解,并且更深的明白了对象的意义。以后我在写代码的时候,可以不光为了得到正确的输出,还可以在代码结构的设计上多下功夫,写出可以重复使用的代码。