前言
题目集7两题都是有关图形的题目题型也类似,考到的知识点有继承、多态、接口和排序,写起来并不困难。
题目集8一题是一道银行ATM存钱取钱的题,考到的知识点有继承、多态和类图的设计,写起来也不困难。
题目集9和题目集8是一样的题目不过就是新增了一些功能和情况,有些考验你上一个题目集的类设计是否合理,如果类设计合理的话,这一题就不太需要去大修改,考到的知识点仍是继承、多态和类图设计,写起来也不困难。
设计与分析
题目集7
(7-1)和(7-2)首先我们来分别读题
由题可得这两题是十分类似的,首先很明显输入格式是一摸一样的,只有输出上的要求不同。题1的输出要求是通过输入的的图形及对应图形边参数算出图形的面积后逐个输出,再排序后在输出排序后情况,最后输出所有面积总和,如有输入非法则输出“Wrong Format”。题2的非法输出与题1相同,合法输入要求按照“Circle、Rectangle、Triangle、Trapezoid”的顺序按图形类型输出,再相同图形类型内部再排序再输出,最后输出各组中面积之和的最大值。等于题2即是在题1的基础上添加了一个分类的要求。下面是我两题的代码以及Monitor分析图。
1 import java.util.*;
2 import java.math.*;
3 import java.util.ArrayList;
4 public class Main {
5 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
6 //使用Main.input.next…即可(避免采坑)
7 public static Scanner input = new Scanner(System.in);
8 public static void main(String[] args){
9 ArrayList<Integer> list = new ArrayList<Integer>();
10 int num = input.nextInt();
11 while(num != 0){
12 if(num < 0 || num > 4){
13 System.out.println("Wrong Format");
14 System.exit(0);
15 }
16 list.add(num);
17 num = input.nextInt();
18 }
19 DealCardList dealCardList = new DealCardList(list);
20 if(!dealCardList.validate()){
21 System.out.println("Wrong Format");
22 System.exit(0);
23 }
24 dealCardList.showResult();
25 input.close();
26 }
27 }
28 class DealCardList{
29 ArrayList<Card> cardList = new ArrayList<Card>();
30
31 DealCardList(){}
32 DealCardList(ArrayList<Integer> list){
33 int size = list.size();
34 for(int i=0; i<size;i++) {
35 switch(list.get(i)) {
36 case 1:
37 Circle circle = new Circle(Main.input.nextDouble());
38 circle.setShapeName("Circle");
39 Card card_C = new Card(circle);
40 cardList.add(card_C);
41 break;
42 case 2:
43 Rectangle rectangle = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
44 rectangle.setShapeName("Rectangle");
45 Card card_R = new Card(rectangle);
46 cardList.add(card_R);
47 break;
48 case 3:
49 Triangle triangle = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
50 triangle.setShapeName("Triangle");
51 Card card_S = new Card(triangle);
52 cardList.add(card_S);
53 break;
54 case 4:
55 Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
56 trapezoid.setShapeName("Trapezoid");
57 Card card_T = new Card(trapezoid);
58 cardList.add(card_T);
59 break;
60 }
61 }
62 }
63 public boolean validate(){
64 int size = this.cardList.size();
65 if(this.cardList.size()==0)
66 return false;
67 for(int i=0 ; i<size;i++) {
68 if(this.cardList.get(i).getShape().validate()==false) {
69 // System.out.println(this.cardList.get(i).getShape().getShapeName());
70 return false;
71 }
72 }
73 return true;
74 }
75 public void cardSort() {
76 this.cardList.sort(null);
77 }
78 public double getMaxArea() {
79 double a=0.0,b=0.0,c=0.0,d=0.0;
80 for(int i=0; i<this.cardList.size();i++ ) {
81 switch(this.cardList.get(i).getShape().getShapeName()) {
82 case "Circle":
83 a=a+this.cardList.get(i).getShape().getArea();
84 break;
85 case "Rectangle":
86 b=b+this.cardList.get(i).getShape().getArea();
87 break;
88 case "Triangle":
89 c=c+this.cardList.get(i).getShape().getArea();
90 break;
91 case "Trapezoid":
92 d=d+this.cardList.get(i).getShape().getArea();
93 break;
94 }
95 }
96 if(a>=b&&a>=c&&a>=d)
97 return a;
98 else if(b>=a&&b>=c&&b>=d)
99 return b;
100 else if(c>=a&&c>=b&&c>=d)
101 return c;
102 return d;
103 }
104 public void showSeparated(String name) {
105 System.out.print("[");
106 for(int i=0; i<this.cardList.size();i++ ) {
107 if(this.cardList.get(i).getShape().getShapeName().equals(name)) {
108 System.out.print(this.cardList.get(i).getShape().getShapeName() + ":" + String .format("%.2f",this.cardList.get(i).getShape().getArea()) + " ");
109 }
110 }
111 System.out.print("]");
112 }
113 public void showResult() {
114 System.out.println("The original list:");
115 for(int i=0 ; i<this.cardList.size();i++) {
116 if(i==0)
117 System.out.print("[");
118 System.out.print(this.cardList.get(i).getShape().getShapeName() + ":" + String .format("%.2f",this.cardList.get(i).getShape().getArea()) + " ");
119 if(i==this.cardList.size()-1)
120 System.out.print("]");
121 }
122 System.out.println("\nThe Separated List:");
123 showSeparated("Circle");
124 showSeparated("Rectangle");
125 showSeparated("Triangle");
126 showSeparated("Trapezoid");
127 System.out.println("\nThe Separated sorted List:");
128 cardSort();
129 showSeparated("Circle");
130 showSeparated("Rectangle");
131 showSeparated("Triangle");
132 showSeparated("Trapezoid");
133 System.out.printf("\nThe max area:%.2f",getMaxArea());
134 }
135 }
136
137 class Card implements Comparable<Card>{ //卡片类
138 private Shape shape;
139
140 Card(){}
141 Card(Shape shape){
142 this.shape = shape;
143 }
144 public Shape getShape() {
145 return shape;
146 }
147 public void setShape(Shape shape) {
148 this.shape = shape;
149 }
150 public int compareTo(Card card) { //排序函数
151 if(this.shape.getArea() > card.shape.getArea())
152 return -1;
153 else if(this.shape.getArea() < card.shape.getArea())
154 return 1;
155 return 0;
156 }
157 }
158 abstract class Shape{ //抽象父类图形类
159 private String shapeName;
160
161 Shape(){}
162 Shape(String shapeName){
163 this.shapeName = shapeName;
164 }
165 public String getShapeName() {
166 return shapeName;
167 }
168 public void setShapeName(String shapeName) {
169 this.shapeName = shapeName;
170 }
171 public double getArea() {
172 return 0.0;
173 }
174 public boolean validate() {
175 return false;
176 }
177 public String toString() {
178 return this.shapeName;
179 }
180 }
181 class Circle extends Shape{ //圆
182 private double radius;
183
184 Circle(){}
185 Circle(double radius){
186 this.radius = radius;
187 }
188 public double getRadius() {
189 return radius;
190 }
191 public void setRadius(double radius) {
192 this.radius = radius;
193 }
194 public double getArea() {
195 return this.radius*this.radius*Math.PI;
196 }
197 public boolean validate() {
198 if(this.radius>=0.0)
199 return true;
200 return false;
201 }
202 }
203 class Rectangle extends Shape{ //方型
204 private double width;
205 private double length;
206
207 Rectangle(){}
208 Rectangle(double width,double length){
209 this.width = width;
210 this.length = length;
211 }
212 public double getWidth() {
213 return width;
214 }
215 public void setWidth(double width) {
216 this.width = width;
217 }
218 public double getLength() {
219 return length;
220 }
221 public void setLength(double length) {
222 this.length = length;
223 }
224 public double getArea() {
225 return this.length*this.width;
226 }
227 public boolean validate() {
228 if(this.length>=0.0&&this.width>=0.0)
229 return true;
230 return false;
231 }
232 }
233 class Triangle extends Shape{ //三角形
234 private double sider1;
235 private double sider2;
236 private double sider3;
237
238 Triangle(){}
239 Triangle(double x,double y,double z){
240 this.sider1 = x;
241 this.sider2 = y;
242 this.sider3 = z;
243 }
244
245 public double getSider1() {
246 return sider1;
247 }
248 public void setSider1(double sider1) {
249 this.sider1 = sider1;
250 }
251 public double getSider2() {
252 return sider2;
253 }
254 public void setSider2(double sider2) {
255 this.sider2 = sider2;
256 }
257 public double getSider3() {
258 return sider3;
259 }
260 public void setSider3(double sider3) {
261 this.sider3 = sider3;
262 }
263
264 public double getArea() {
265 double p = (this.sider1 + this.sider2 + this.sider3)/2.0;
266 //海伦公式 Math.sqrt(s*(s-a)*(s-b)*(s-c))
267 double area = Math.sqrt(p*(p-this.sider1)*(p-this.sider2)*(p-this.sider3));
268 return area;
269 }
270 public boolean validate() {
271 if(this.sider1<0 || this.sider2<0 || this.sider3<0)
272 return false;
273 //两边之和大于第三边
274 if(this.sider1 + this.sider2 <= this.sider3 ||this.sider1+this.sider3<=this.sider2 ||this.sider2+this.sider3 <=this.sider1)
275 return false;
276 return true;
277 }
278 }
279 class Trapezoid extends Shape{ //梯形
280 private double toSide;
281 private double bottomSide;
282 private double height;
283
284 Trapezoid(){}
285 Trapezoid(double toSide,double bottomSide,double height){
286 this.toSide = toSide;
287 this.bottomSide = bottomSide;
288 this.height = height;
289 }
290 public double getToSide() {
291 return toSide;
292 }
293 public void setToSide(double toSide) {
294 this.toSide = toSide;
295 }
296 public double getBottomSide() {
297 return bottomSide;
298 }
299 public void setBottomSide(double bottomSide) {
300 this.bottomSide = bottomSide;
301 }
302 public double getHeight() {
303 return height;
304 }
305 public void setHeight(double height) {
306 this.height = height;
307 }
308 public double getArea() {
309 return (this.toSide+this.bottomSide)*this.height/2.0;
310 }
311 public boolean validate() {
312 if(this.toSide>=0.0 && this.bottomSide>=0.0 && this.height>=0.0)
313 return true;
314 return false;
315 }
316
317 }
(7-1)
1 import java.util.*;
2 import java.math.*;
3 import java.util.ArrayList;
4 public class Main {
5 //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
6 //使用Main.input.next…即可(避免采坑)
7 public static Scanner input = new Scanner(System.in);
8 public static void main(String[] args){
9 ArrayList<Integer> list = new ArrayList<Integer>();
10 int num = input.nextInt();
11 while(num != 0){
12 if(num < 0 || num > 4){
13 System.out.println("Wrong Format");
14 System.exit(0);
15 }
16 list.add(num);
17 num = input.nextInt();
18 }
19 DealCardList dealCardList = new DealCardList(list);
20 if(!dealCardList.validate()){
21 System.out.println("Wrong Format");
22 System.exit(0);
23 }
24 dealCardList.showResult();
25 input.close();
26 }
27 }
28 class DealCardList{
29 ArrayList<Card> cardList = new ArrayList<Card>();
30
31 DealCardList(){}
32 DealCardList(ArrayList<Integer> list){
33 int size = list.size();
34 for(int i=0; i<size;i++) {
35 switch(list.get(i)) {
36 case 1:
37 Circle circle = new Circle(Main.input.nextDouble());
38 circle.setShapeName("Circle");
39 Card card_C = new Card(circle);
40 cardList.add(card_C);
41 break;
42 case 2:
43 Rectangle rectangle = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
44 rectangle.setShapeName("Rectangle");
45 Card card_R = new Card(rectangle);
46 cardList.add(card_R);
47 break;
48 case 3:
49 Triangle triangle = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
50 triangle.setShapeName("Triangle");
51 Card card_S = new Card(triangle);
52 cardList.add(card_S);
53 break;
54 case 4:
55 Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
56 trapezoid.setShapeName("Trapezoid");
57 Card card_T = new Card(trapezoid);
58 cardList.add(card_T);
59 break;
60 }
61 }
62 }
63 public boolean validate(){
64 int size = this.cardList.size();
65 if(this.cardList.size()==0)
66 return false;
67 for(int i=0 ; i<size;i++) {
68 if(this.cardList.get(i).getShape().validate()==false) {
69 // System.out.println(this.cardList.get(i).getShape().getShapeName());
70 return false;
71 }
72 }
73 return true;
74 }
75 public void cardSort() {
76 this.cardList.sort(null);
77 }
78 public double getMaxArea() {
79 double a=0.0,b=0.0,c=0.0,d=0.0;
80 for(int i=0; i<this.cardList.size();i++ ) {
81 switch(this.cardList.get(i).getShape().getShapeName()) {
82 case "Circle":
83 a=a+this.cardList.get(i).getShape().getArea();
84 break;
85 case "Rectangle":
86 b=b+this.cardList.get(i).getShape().getArea();
87 break;
88 case "Triangle":
89 c=c+this.cardList.get(i).getShape().getArea();
90 break;
91 case "Trapezoid":
92 d=d+this.cardList.get(i).getShape().getArea();
93 break;
94 }
95 }
96 if(a>=b&&a>=c&&a>=d)
97 return a;
98 else if(b>=a&&b>=c&&b>=d)
99 return b;
100 else if(c>=a&&c>=b&&c>=d)
101 return c;
102 return d;
103 }
104 public void showSeparated(String name) {
105 System.out.print("[");
106 for(int i=0; i<this.cardList.size();i++ ) {
107 if(this.cardList.get(i).getShape().getShapeName().equals(name)) {
108 System.out.print(this.cardList.get(i).getShape().getShapeName() + ":" + String .format("%.2f",this.cardList.get(i).getShape().getArea()) + " ");
109 }
110 }
111 System.out.print("]");
112 }
113 public void showResult() {
114 System.out.println("The original list:");
115 for(int i=0 ; i<this.cardList.size();i++) {
116 if(i==0)
117 System.out.print("[");
118 System.out.print(this.cardList.get(i).getShape().getShapeName() + ":" + String .format("%.2f",this.cardList.get(i).getShape().getArea()) + " ");
119 if(i==this.cardList.size()-1)
120 System.out.print("]");
121 }
122 System.out.println("\nThe Separated List:");
123 showSeparated("Circle");
124 showSeparated("Rectangle");
125 showSeparated("Triangle");
126 showSeparated("Trapezoid");
127 System.out.println("\nThe Separated sorted List:");
128 cardSort();
129 showSeparated("Circle");
130 showSeparated("Rectangle");
131 showSeparated("Triangle");
132 showSeparated("Trapezoid");
133 System.out.printf("\nThe max area:%.2f",getMaxArea());
134 }
135 }
136
137 class Card implements Comparable<Card>{ //卡片类
138 private Shape shape;
139
140 Card(){}
141 Card(Shape shape){
142 this.shape = shape;
143 }
144 public Shape getShape() {
145 return shape;
146 }
147 public void setShape(Shape shape) {
148 this.shape = shape;
149 }
150 public int compareTo(Card card) { //排序函数
151 if(this.shape.getArea() > card.shape.getArea())
152 return -1;
153 else if(this.shape.getArea() < card.shape.getArea())
154 return 1;
155 return 0;
156 }
157 }
158 abstract class Shape{ //抽象父类图形类
159 private String shapeName;
160
161 Shape(){}
162 Shape(String shapeName){
163 this.shapeName = shapeName;
164 }
165 public String getShapeName() {
166 return shapeName;
167 }
168 public void setShapeName(String shapeName) {
169 this.shapeName = shapeName;
170 }
171 public double getArea() {
172 return 0.0;
173 }
174 public boolean validate() {
175 return false;
176 }
177 public String toString() {
178 return this.shapeName;
179 }
180 }
181 class Circle extends Shape{ //圆
182 private double radius;
183
184 Circle(){}
185 Circle(double radius){
186 this.radius = radius;
187 }
188 public double getRadius() {
189 return radius;
190 }
191 public void setRadius(double radius) {
192 this.radius = radius;
193 }
194 public double getArea() {
195 return this.radius*this.radius*Math.PI;
196 }
197 public boolean validate() {
198 if(this.radius>=0.0)
199 return true;
200 return false;
201 }
202 }
203 class Rectangle extends Shape{ //方型
204 private double width;
205 private double length;
206
207 Rectangle(){}
208 Rectangle(double width,double length){
209 this.width = width;
210 this.length = length;
211 }
212 public double getWidth() {
213 return width;
214 }
215 public void setWidth(double width) {
216 this.width = width;
217 }
218 public double getLength() {
219 return length;
220 }
221 public void setLength(double length) {
222 this.length = length;
223 }
224 public double getArea() {
225 return this.length*this.width;
226 }
227 public boolean validate() {
228 if(this.length>=0.0&&this.width>=0.0)
229 return true;
230 return false;
231 }
232 }
233 class Triangle extends Shape{ //三角形
234 private double sider1;
235 private double sider2;
236 private double sider3;
237
238 Triangle(){}
239 Triangle(double x,double y,double z){
240 this.sider1 = x;
241 this.sider2 = y;
242 this.sider3 = z;
243 }
244
245 public double getSider1() {
246 return sider1;
247 }
248 public void setSider1(double sider1) {
249 this.sider1 = sider1;
250 }
251 public double getSider2() {
252 return sider2;
253 }
254 public void setSider2(double sider2) {
255 this.sider2 = sider2;
256 }
257 public double getSider3() {
258 return sider3;
259 }
260 public void setSider3(double sider3) {
261 this.sider3 = sider3;
262 }
263
264 public double getArea() {
265 double p = (this.sider1 + this.sider2 + this.sider3)/2.0;
266 //海伦公式 Math.sqrt(s*(s-a)*(s-b)*(s-c))
267 double area = Math.sqrt(p*(p-this.sider1)*(p-this.sider2)*(p-this.sider3));
268 return area;
269 }
270 public boolean validate() {
271 if(this.sider1<0 || this.sider2<0 || this.sider3<0)
272 return false;
273 //两边之和大于第三边
274 if(this.sider1 + this.sider2 <= this.sider3 ||this.sider1+this.sider3<=this.sider2 ||this.sider2+this.sider3 <=this.sider1)
275 return false;
276 return true;
277 }
278 }
279 class Trapezoid extends Shape{ //梯形
280 private double toSide;
281 private double bottomSide;
282 private double height;
283
284 Trapezoid(){}
285 Trapezoid(double toSide,double bottomSide,double height){
286 this.toSide = toSide;
287 this.bottomSide = bottomSide;
288 this.height = height;
289 }
290 public double getToSide() {
291 return toSide;
292 }
293 public void setToSide(double toSide) {
294 this.toSide = toSide;
295 }
296 public double getBottomSide() {
297 return bottomSide;
298 }
299 public void setBottomSide(double bottomSide) {
300 this.bottomSide = bottomSide;
301 }
302 public double getHeight() {
303 return height;
304 }
305 public void setHeight(double height) {
306 this.height = height;
307 }
308 public double getArea() {
309 return (this.toSide+this.bottomSide)*this.height/2.0;
310 }
311 public boolean validate() {
312 if(this.toSide>=0.0 && this.bottomSide>=0.0 && this.height>=0.0)
313 return true;
314 return false;
315 }
316
317 }
(7-2)
先创建一个Shape类作为所有图形类的父类,再有Card类去引用Shape类且使用接口Comparable<Card>即使用接口函数compareTo(Card card)改变其中的返回值来改变ArrayList中排序函数sort()的判定。compareTo(Card card)返回-1表示为this.card将会被放在队列前面,返回1表示card将会被放在队列前面。通过改写该接口函数就可以使得使用了sort函数后数列按降序排列。我写的函数如下
public int compareTo(Card card) { //排序函数
if(this.shape.getArea() > card.shape.getArea())
return -1;
else if(this.shape.getArea() < card.shape.getArea())
return 1;
return 0;
}
然后从Monitor图中可以看出题1中是DealCardList是复杂度最高的,我为了判断输入是否合法先创建了一个ArrayList list如果输入全部合法就创建cardList通过判断list中存的都是什么数字来创建什么样的图形并获得图形参数。题2的Monitor图中可以看到是getMaxArea这个函数的复杂度最高,对比两个圆形折线图可以看到Max Complexity这一项题2明显高些其他没有什么变化,这个复杂度的变化就在getMaxArea这个函数上,因为两题之间有大改动的就是这个函数,原本这个函数在题1中是求和函数,由于我在写题1时并没有注意到这种分类问题所以并没有给每个 图形的类创建数列,所以要取得和最大的那个图型组的面积就需要我再对图形总数列进行区分在求和,几个和之间又要比较所有产生了很多循环和判断,增大了复杂度。下面是7-2的类图。
题集8和题集9
题集8和题集9都只有1道题,这里只放一道题题目因为两题题目一样(图一),是题目给的条件有些不一样(图二),如下
从给的文件中看过给的例子后明白了题集8的场景是有两个银行、六个ATM机,每个银行有自己对应的ATM机,有四个用户隶属不同的银行,每个用户有至少一个账户,每个账户有一万元,这些账户都不能够透支取款,每个账户又有至少一张卡,用户使用ATM机必须使用自己存取卡所在银行的ATM机才能够操作。而题集9的场景是在题集8的场景下新增一个银行,可以跨行存取但是要收手续费,还增加了贷款账号,也就是可以透支的账户,然后透支有需要收手续费且存在最大透支限额。需要输入的就是用户在ATM机上的存取款或者查询余额,输出就是用户对对应账户的操作以及余额的显示。下面是我两个题集的代码以及Monitor分析图。
1 import java.util.ArrayList;
2 import java.util.Scanner;
3
4 public class Main {
5
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 DataBase date = new DataBase();
9 DealData deal = new DealData(date);
10 deal.deal();
11 //out(date);
12 }
13 }
14 class DealData{
15 ArrayList<String> out = new ArrayList<String>();
16 DataBase Date;
17 ////银行号/用户号/账户号/atm机号
18 int b=-1,u=-1,n=-1,a=-1;
19
20 DealData(DataBase date){
21 this.Date = date;
22 }
23 public void deal() {
24 Scanner input = new Scanner(System.in );
25 double flag1;
26 int flag2;
27 String str ;
28 str = input.nextLine();
29 while(!str.equals("#")) {//遇到“#”终止
30 String[] part=str.split("\\s+"); //空格分隔
31 if(part.length == 1) { //查询余额
32 //System.out.println(part[0]);
33 flag1= this.checka(part[0]);
34 if(flag1!=-1.0) { //如果查询到了
35 out.add("¥"+String.format("%.2f",flag1));
36 str = input.nextLine();
37 }
38 else
39 str = "#";
40 }
41 else if(part.length==4) { //存取款
42 flag2= this.checkb(part);
43 if(flag2!=-1) { //一切正确
44 double y = this.Date.bank.get(b).getUserlist().get(u).getAccountlist().get(n).getAccount();
45 double sum = Double.parseDouble(part[3]);
46 double x=sum;
47 if(sum<0.0) { //存款
48 x=-x;
49 sum = y-sum;
50 this.Date.bank.get(b).getUserlist().get(u).getAccountlist().get(n).setAccount(sum);
51 out.add(this.Date.bank.get(b).getUserlist().get(u).getName() +
52 "在" + this.Date.bank.get(b).getName() +
53 "的" + this.Date.bank.get(b).getAtm().get(a) +
54 "号ATM机上存款¥" + String.format("%.2f", x) +
55 "\n当前余额为¥" + String.format("%.2f", sum));
56 }
57 else { //取款
58 sum = y-sum;
59 this.Date.bank.get(b).getUserlist().get(u).getAccountlist().get(n).setAccount(sum);
60 out.add(this.Date.bank.get(b).getUserlist().get(u).getName() +
61 "在" + this.Date.bank.get(b).getName() +
62 "的" + this.Date.bank.get(b).getAtm().get(a) +
63 "号ATM机上取款¥" + String.format("%.2f", x) +
64 "\n当前余额为¥" + String.format("%.2f", sum));
65 }
66 str = input.nextLine();
67 }
68 else
69 str = "#";
70 }
71
72 }
73 this.output();
74 }
75 public void output() {
76 for(int i=0;i<out.size();i++) {
77 if(i!=out.size()-1)
78 System.out.println(out.get(i));
79 else {
80 System.out.print(out.get(i));
81 }
82 }
83 }
84 public double checka(String str) {
85 for(int x=0;x<this.Date.bank.size();x++) { // 找银行
86 for(int i=0;i<this.Date.bank.get(x).getUserlist().size();i++) { //找用户
87 for(int j=0;j<this.Date.bank.get(x).getUserlist().get(i).getAccountlist().size();j++) { //找账户
88 for(int y=0;y<this.Date.bank.get(x).getUserlist().get(i).getAccountlist().get(j).getCard().size();y++) { //找卡号
89 if(str.equals(this.Date.bank.get(x).getUserlist().get(i).getAccountlist().get(j).getCard().get(y))) {
90 b=x;u=i;n=j;
91 // System.out.println(b+" "+u+" "+c);
92 return this.Date.bank.get(x).getUserlist().get(i).getAccountlist().get(j).getAccount();
93 }
94 }
95 }
96 }
97 }
98 System.out.print("Sorry,this card does not exist.");
99 return -1.0;
100 }
101 public int checkb(String[] part) {
102 double flag = checka(part[0]);
103 // System.out.println(flag);
104 if(flag == -1.0) { //匹配卡号
105 return -1;
106 }
107 else if(!part[1].equals("88888888")) { //匹配密码
108 System.out.print("Sorry,your password is wrong.");
109 return -1;
110 }
111 else {//匹配Atm机判断金额
112 // System.out.println(u);
113 for(int j=0;j<this.Date.atm.length;j++) { //Atm是否存在
114 if(part[2].equals(this.Date.atm[j])) {
115 for(int i=0;i<this.Date.bank.get(b).getAtm().size();i++) { //是否跨行存取
116 if(part[2].equals(this.Date.bank.get(b).getAtm().get(i))) {
117 double x=Double.parseDouble(part[3]);
118 double y = this.Date.bank.get(b).getUserlist().get(u).getAccountlist().get(n).getAccount();
119 // System.out.println(y);
120 if(x>0.0 && x>y) { //取款超额
121 System.out.print("Sorry,your account balance is insufficient.");
122 return -1;
123 }
124 else {//输入信息正确
125 a=i;
126 return a;
127 }
128 }
129 }
130 System.out.print("Sorry,cross-bank withdrawal is not supported.");
131 return -1;
132 }
133 }
134 System.out.print("Sorry,the ATM's id is wrong.");
135 return -1;
136 }
137 }
138 }
139 class DataBase{
140 //初始化银行相关数据
141 ArrayList<Bank> bank = new ArrayList<Bank>();
142 String[] atm = {"01","02","03","04","05","06"};
143 String[] atm1 = {"01","02","03","04"};
144 String[] atm2 = {"05","06"};
145 DataBase(){
146 Bank bank1 = new Bank(atm1,"中国建设银行");
147 Bank bank2 = new Bank(atm2,"中国工商银行");
148
149 User use1 = new User("杨过");
150 Account a1 = new Account("3217000010041315709");
151 a1.getCard().add("6217000010041315709");
152 a1.getCard().add("6217000010041315715");
153 Account a2 = new Account("3217000010041315715");
154 a2.getCard().add("6217000010041315718");
155 use1.addAccount(a1);
156 use1.addAccount(a2);
157 bank1.adduser(use1);
158
159 User use2 = new User("郭靖");
160 Account a3 = new Account("3217000010051320007");
161 a3.getCard().add("6217000010051320007");
162 use2.addAccount(a3);
163 bank1.adduser(use2);
164
165 User use3 = new User("张无忌");
166 Account a4 = new Account("3222081502001312389");
167 a4.getCard().add("6222081502001312389");
168 Account a5 = new Account("3222081502001312390");
169 a5.getCard().add("6222081502001312390");
170 Account a6 = new Account("3222081502001312399");
171 a6.getCard().add("6222081502001312399");
172 a6.getCard().add("6222081502001312400");
173 use3.addAccount(a4);
174 use3.addAccount(a5);
175 use3.addAccount(a6);
176 bank2.adduser(use3);
177
178 User use4 = new User("韦小宝");
179 Account a7 = new Account("3222081502051320785");
180 a7.getCard().add("6222081502051320785");
181 Account a8 = new Account("3222081502051320786");
182 a8.getCard().add("6222081502051320786");
183 use4.addAccount(a7);
184 use4.addAccount(a8);
185 bank2.adduser(use4);
186 bank.add(bank1);
187 bank.add(bank2);
188 }
189
190 }
191 class Bank{
192 private ArrayList<String> Atm = new ArrayList<String>();
193 private ArrayList<User> userlist = new ArrayList<User>();
194 private String name;
195 Bank(String[] str,String name){
196 this.name = name;
197 for(int i=0;i<str.length;i++) {
198 Atm.add(str[i]);
199 }
200 }
201 public void adduser(User user) {
202 this.userlist.add(user);
203 }
204
205 public ArrayList<User> getUserlist() {
206 return userlist;
207 }
208 public void setUserlist(ArrayList<User> userlist) {
209 this.userlist = userlist;
210 }
211 public ArrayList<String> getAtm() {
212 return Atm;
213 }
214 public void setAtm(ArrayList<String> atm) {
215 Atm = atm;
216 }
217 public String getName() {
218 return name;
219 }
220 public void setName(String name) {
221 this.name = name;
222 }
223
224 }
225
226 class Account{
227 private ArrayList<String> card = new ArrayList<String>();
228 private String number;
229 private double account;
230 private String key;
231
232 Account(String number){
233 this.number = number;
234 this.account = 10000.00;
235 this.key="88888888";
236 }
237
238
239 public ArrayList<String> getCard() {
240 return card;
241 }
242
243
244 public void setCard(ArrayList<String> card) {
245 this.card = card;
246 }
247
248
249 public String getNumber() {
250 return number;
251 }
252
253 public void setNumber(String number) {
254 this.number = number;
255 }
256
257 public double getAccount() {
258 return account;
259 }
260
261 public void setAccount(double account) {
262 this.account = account;
263 }
264
265 public String getKey() {
266 return key;
267 }
268
269 public void setKey(String key) {
270 this.key = key;
271 }
272
273 }
274 class User{
275 private String name;
276 private ArrayList<Account> accountlist;
277
278 User(String name){
279 this.name = name;
280 this.accountlist = new ArrayList<Account>();
281 }
282 public void addAccount(Account card) {
283 this.accountlist.add(card);
284 }
285 public String getName() {
286 return name;
287 }
288 public void setName(String name) {
289 this.name = name;
290 }
291 public ArrayList<Account> getAccountlist() {
292 return accountlist;
293 }
294 public void setAccountlist(ArrayList<Account> cardlist) {
295 this.accountlist = cardlist;
296 }
297
298 }
题集8
1 import java.util.ArrayList;
2 import java.util.Scanner;
3 public class Main {
4 public static void main(String[] args) {
5 // TODO Auto-generated method stub
6 BankFederation BF = Initiate.Init_bank();
7 Deal.dealData(BF);
8 }
9 }
10 class Deal {
11 static void dealData(BankFederation BF) {
12 ArrayList<String> cmd;
13 cmd = Input.in_cmd();
14 boolean flag = true;
15 String[] temp;
16 for(String i:cmd) {
17 temp = i.split("\\s+");
18 if(temp.length==1)
19 flag = deal_cmd_2(i,BF);
20 else if(temp.length==4)
21 flag = deal_cmd_1(i,BF);
22 if(flag == false)
23 return ;
24 }
25 }
26
27 protected static boolean deal_cmd_1(String temp,BankFederation BF) {
28 //卡号 密码 ATM机编号 金额
29 //当金额大于0时,代表取款,否则代表存款。
30 String[] cmd = temp.split("\\s+");
31 String s1 = cmd[0].substring(0, 3);
32 int i = findATM(temp,BF);
33 boolean flag = false;
34 /*
35 if(i>=1&&i<=4&&s1.equals("621"))
36 {
37 if(Double.valueOf(cmd[3])>0)
38 flag = BF.getBankList().get(0).getAtmList().get(i-1).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]));
39 else
40 flag = BF.getBankList().get(0).getAtmList().get(i-1).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]));
41 }
42 else if(i>=5&&s1.equals("622"))
43 {
44 if(Double.valueOf(cmd[3])>0)
45 flag = BF.getBankList().get(1).getAtmList().get(i-5).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]));
46 else
47 flag = BF.getBankList().get(1).getAtmList().get(i-5).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]));
48 }
49 else if(i!=-1) {
50 System.out.println("Sorry,cross-bank withdrawal is not supported.");
51 } */
52 if(i>=1&&i<=4) {
53 if(Double.valueOf(cmd[3])>0)
54 flag = BF.getBankList().get(0).getAtmList().get(i-1).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF);
55 // else
56 // flag = BF.getBankList().get(0).getAtmList().get(i-1).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF);
57 }
58 else if(i==5||i==6) {
59 if(Double.valueOf(cmd[3])>0)
60 flag = BF.getBankList().get(1).getAtmList().get(i-5).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF);
61 // else
62 // flag = BF.getBankList().get(1).getAtmList().get(i-5).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF);
63 }
64 else if(i>=7&&i<=11) {
65 if(Double.valueOf(cmd[3])>0)
66 flag = BF.getBankList().get(2).getAtmList().get(i-7).withdrawal(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF);
67 // else
68 // flag = BF.getBankList().get(2).getAtmList().get(i-7).deposit(cmd[0], cmd[1], Double.valueOf(cmd[3]),BF);
69 }
70
71 return flag;
72 }
73
74 public static int findATM(String temp,BankFederation BF)
75 {
76 String[] cmd = temp.split("\\s+");
77 String s1 = cmd[0].substring(0, 3);
78 int num = 0;
79
80 for(ATM i:BF.getBankList().get(0).getAtmList())
81 {
82 num++;
83 if(cmd[2].equals(i.getNum()))
84 {
85 return num;
86 // return 0;
87 }
88
89 }
90
91 for(ATM i:BF.getBankList().get(1).getAtmList())
92 {
93 num++;
94 if(cmd[2].equals(i.getNum()))
95 {
96 return num;
97 //return 1;
98 }
99
100 }
101
102 for(ATM i:BF.getBankList().get(2).getAtmList())
103 {
104 num++;
105 if(cmd[2].equals(i.getNum()))
106 {
107 return num;
108 //return 2;
109 }
110
111 }
112
113 System.out.println("Sorry,the ATM's id is wrong.");
114 return -1;
115 }
116
117 protected static boolean deal_cmd_2(String temp,BankFederation BF) {
118
119 String cardName = temp.substring(0, 3);
120 boolean flag = false;
121
122 if(cardName.equals("621"))
123 flag = BF.getBankList().get(0).getAtmList().get(0).checkBalance(temp);
124 else if (cardName.equals("622"))
125 flag = BF.getBankList().get(1).getAtmList().get(0).checkBalance(temp);
126 else
127 flag = BF.getBankList().get(2).getAtmList().get(0).checkBalance(temp,BF);
128
129 return flag;
130 }
131 }
132 class Bank {
133
134 private String bankName;
135 private double rate;
136 private ArrayList<Account> accountList = new ArrayList<Account>();
137 // private ArrayList<Account> accountDList = new ArrayList<Account>(); //贷记账户
138 private ArrayList<ATM> atmList = new ArrayList<ATM>();
139
140 Bank(String bankName,double rate){
141 this.bankName = bankName;
142 this.rate = rate;
143 }
144
145 public String getBankName() {
146 return bankName;
147 }
148 public void setBankName(String bankName) {
149 this.bankName = bankName;
150 }
151 public ArrayList<Account> getAccountList() {
152 return accountList;
153 }
154 public void addAccountList(Account accountListmen) {
155 this.accountList.add(accountListmen);
156
157 }
158
159 public ArrayList<ATM> getAtmList() {
160 return atmList;
161 }
162
163 public void addAtmList(ATM atmListmen) {
164 this.atmList.add(atmListmen);
165 }
166
167 public double getRate() {
168 return rate;
169 }
170
171 public void setRate(double rate) {
172 this.rate = rate;
173 }
174
175 /*
176 * public ArrayList<Account> getAccountDList() { return accountDList; }
177 *
178 * public void addAccountDList(Account accountDList) {
179 * this.accountDList.add(accountDList); }
180 */
181
182 }
183 class ATM {
184
185 private Bank bank;
186 private String num;
187
188 ATM(Bank bank,String num){
189 this.bank = bank;
190 this.num = num;
191 }
192
193 public Bank getBank() {
194 return bank;
195 }
196 public void setBank(Bank bank) {
197 this.bank = bank;
198 }
199 public String getNum() {
200 return num;
201 }
202 public void setNum(String num) {
203 this.num = num;
204 }
205
206 public boolean checkBalance(String cardNum,BankFederation BF) {
207 Bank k;
208 Account nowUse = findBankCard(cardNum,0);
209 boolean flag = false;
210 if(nowUse==null)
211 {
212 cir4: for(Bank i:BF.getBankList()) {
213 flag = i.getAtmList().get(0).checkBalance(cardNum);
214 if(flag==true)
215 break cir4;
216 }
217 if(flag==false)
218 System.out.println("Sorry,this card does not exist.");
219 return flag;
220 }
221 else
222 System.out.print("业务:查询余额 ¥"+String.format("%.2f",nowUse.getAccountBalance()));
223 return true;
224
225 }
226
227 public boolean checkBalance(String cardNum) {
228 // Bank k;
229 Account nowUse = findBankCard(cardNum,0);
230 if(nowUse==null)
231 return false;
232 else
233 System.out.print("业务:查询余额 ¥"+String.format("%.2f",nowUse.getAccountBalance()));
234 return true;
235
236 }
237
238 //外部进入,取钱
239 public boolean withdrawal(String cardNum,String password,double quantity,Bank bank,String num) {
240 Account nowUse = findBankCard(cardNum,password);
241 double renew;
242 int flag = 0;
243 if(nowUse==null)
244 return false;
245 else
246 {
247 if(nowUse.getAccountBalance()>=(quantity+(quantity*(bank.getRate()))))
248 {
249 flag = 1;
250 renew = nowUse.getAccountBalance()-quantity-(quantity*bank.getRate());
251 nowUse.setAccountBalance(renew);
252 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+bank.getBankName()+"的"+num+"号ATM机上取款¥"+Math.abs(quantity));
253 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance()));
254 }
255 else if(nowUse.getType().equals("debit"))
256 System.out.print("Sorry,your account balance is insufficient.");
257 else if(nowUse.getType().equals("credits")) {
258 flag = 1;
259 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+bank.getBankName()+"的"+num+"号ATM机上取款¥"+Math.abs(quantity));
260 if(nowUse.getAccountBalance()>=0) {
261 renew = nowUse.getAccountBalance()-quantity;
262 renew = renew+renew*0.05;
263 renew = renew-quantity*bank.getRate();
264 }else {
265 renew = nowUse.getAccountBalance()-quantity;
266 renew = renew-quantity*0.05;
267 renew = renew-quantity*bank.getRate();
268 }
269 if(renew<-50000)
270 {System.out.print("Sorry,your account balance is insufficient.");return false;}
271 nowUse.setAccountBalance(renew);
272 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance()));
273 }
274
275 }
276 if(flag==0)
277 return false;
278 else
279 return true;
280 }
281
282 public boolean withdrawal(String cardNum,String password,double quantity,BankFederation BF) {
283 int ii = 0;
284 Account nowUse = findBankCard(cardNum,password);
285 Account external;
286 Bank k = null;
287 double renew;
288 int flag = 0;
289 if(nowUse==null)
290 {
291 cir2: for(Bank i:BF.getBankList()) {
292 if(!this.bank.getBankName().equals(i.getBankName())) {
293 external = i.getAtmList().get(0).findBankCard(cardNum, password);
294 if(external!=null)
295 {
296 ii = 1;
297 k = i;
298 break cir2;
299 }
300 else
301 {
302 if(i.getAtmList().get(0).findBankCard(cardNum,0)!=null)
303 ii = 2;
304 }
305 }
306 }
307 if(ii==0) {
308 System.out.print("Sorry,this card does not exist.");
309 return false;
310 }else if(ii==2)
311 return false;
312 return k.getAtmList().get(0).withdrawal(cardNum, password, quantity,this.bank,this.num);
313 }
314 else
315 {
316 if(nowUse.getAccountBalance()>=quantity)
317 {
318 flag = 1;
319 renew = nowUse.getAccountBalance()-quantity;
320 nowUse.setAccountBalance(renew);
321 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+Math.abs(quantity));
322 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance()));
323 }
324 else if(nowUse.getType().equals("debit"))
325 System.out.println("Sorry,your account balance is insufficient.");
326 else if(nowUse.getType().equals("credits")) {
327 flag = 1;
328 System.out.println("业务:取款 "+nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上取款¥"+Math.abs(quantity));
329 if(nowUse.getAccountBalance()>=0) {
330 renew = nowUse.getAccountBalance()-quantity;
331 renew = renew+renew*0.05;
332 // renew = renew-quantity*this.bank.getRate();
333 }else {
334 renew = nowUse.getAccountBalance()-quantity;
335 renew = renew-quantity*0.05;
336 // renew = renew-quantity*this.bank.getRate();
337 }
338 if(renew<-50000)
339 {System.out.print("Sorry,your account balance is insufficient.");return false;}
340 nowUse.setAccountBalance(renew);
341 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance()));
342 }
343 }
344 if(flag==0)
345 return false;
346 else
347 return true;
348 }
349 /*
350 public boolean deposit(String cardNum,String password,double quantity,Bank bank,String num) {
351 Account nowUse = findBankCard(cardNum,password);
352 double renew;
353 int flag = 0;
354 if(nowUse==null)
355 return false;
356 else
357 {
358 flag = 1;
359 renew = nowUse.getAccountBalance()+Math.abs(quantity);
360 nowUse.setAccountBalance(renew);
361 System.out.println(nowUse.getUser().getUserName()+"在"+bank.getBankName()+"的"+num+"号ATM机上存款¥"+Math.abs(quantity));
362 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance()));
363 }
364 if(flag==0)
365 return false;
366 else
367 return true;
368 }
369
370 public boolean deposit(String cardNum,String password,double quantity,BankFederation BF) {
371 Account nowUse = findBankCard(cardNum,password);
372 Account external;
373 Bank k = null;
374 int ii = 0;
375 double renew;
376 int flag = 0;
377 if(nowUse==null) {
378 cir3: for(Bank i:BF.getBankList()) {
379 if(!this.bank.getBankName().equals(i.getBankName())) {
380 external = i.getAtmList().get(0).findBankCard(cardNum, password);
381 if(external!=null)
382 {
383 ii = 1;
384 k = i;
385 break cir3;
386 }
387 }
388 }
389 if(ii==0) {
390 System.out.println("Sorry,this card does not exist.");
391 return false;
392 }
393 k.getAtmList().get(0).deposit(cardNum, password, quantity,this.bank,this.num);
394 }
395 else
396 {
397 flag = 1;
398 renew = nowUse.getAccountBalance()+Math.abs(quantity);
399 nowUse.setAccountBalance(renew);
400 System.out.println(nowUse.getUser().getUserName()+"在"+nowUse.getBankName()+"的"+this.num+"号ATM机上存款¥"+Math.abs(quantity));
401 System.out.println("当前余额为¥"+String.format("%.2f",nowUse.getAccountBalance()));
402 }
403 if(flag==0)
404 return false;
405 else
406 return true;
407 }
408 */
409 public Account findBankCard(String cardNum,int n) {
410 String s1 = cardNum.substring(3, 16);
411 int flag = 0;
412
413 for(Account i:bank.getAccountList()) {
414 String s2 = i.getAccountNum().substring(3, 16);
415 if(s1.equals(s2)) {
416 for(BankCard j:i.getBankCardList()) {
417 if(cardNum.equals(j.getCardNum()))
418 {
419 // System.out.println("¥"+i.getAccountBalance());
420 return i;
421
422 }
423 }
424 }
425 }
426
427 return null;
428 }
429
430 public Account findBankCard(String cardNum,String pass) {
431 String s1 = cardNum.substring(3, 16);
432 int flag = 0;
433
434 cir1: for(Account i:bank.getAccountList()) {
435 String s2 = i.getAccountNum().substring(3, 16);
436 if(s1.equals(s2)) {
437 for(BankCard j:i.getBankCardList()) {
438 if(cardNum.equals(j.getCardNum()))
439 {
440 // System.out.println("¥"+i.getAccountBalance());
441 if(pass.equals(j.getPassword()))
442 return i;
443 else
444 {
445 flag = 1;
446 System.out.println("Sorry,your password is wrong.");
447 break cir1;
448 }
449
450 }
451 }
452 }
453 }
454 /*
455 * if(flag == 0) System.out.println("Sorry,this card does not exist.");
456 */
457 return null;
458 }
459 }
460 class BankFederation {
461
462 private ArrayList<Bank> bankList = new ArrayList<Bank>();
463
464 public ArrayList<Bank> getBankList() {
465 return bankList;
466 }
467
468 public void addBankList(Bank bankListmen) {
469 this.bankList.add(bankListmen);
470 }
471
472 }
473 class Initiate {
474
475 static BankFederation Init_bank() {
476 BankFederation BF = new BankFederation();
477 Bank bank_1 = new Bank("中国建设银行",0.02);
478 Bank bank_2 = new Bank("中国工商银行",0.03);
479 Bank bank_3 = new Bank("中国农业银行",0.04);
480 BF.addBankList(bank_1);
481 BF.addBankList(bank_2);
482 BF.addBankList(bank_3);
483
484 //bank_1
485 User user_1_1 = new User("杨过");
486 Account account_1_1 = new Account("3217000010041315709",10000,user_1_1,"中国建设银行","debit");
487 BankCard bankcard_1_1_1 = new BankCard("6217000010041315709","88888888");
488 BankCard bankcard_1_1_2 = new BankCard("6217000010041315715","88888888");
489 account_1_1.addBankCardList(bankcard_1_1_1);
490 account_1_1.addBankCardList(bankcard_1_1_2);
491 Account account_1_2 = new Account("3217000010041315715",10000,user_1_1,"中国建设银行","debit");
492 BankCard bankcard_1_2_1 = new BankCard("6217000010041315718","88888888");
493 account_1_2.addBankCardList(bankcard_1_2_1);
494 User user_1_2 = new User("郭靖");
495 Account account_1_3 = new Account("3217000010051320007",10000,user_1_2,"中国建设银行","debit");
496 BankCard bankcard_1_3_1 = new BankCard("6217000010051320007","88888888");
497 account_1_3.addBankCardList(bankcard_1_3_1);
498 User user_1_3 = new User("张三丰");
499 Account account_1_4 = new Account("3640000010045442002",10000,user_1_3,"中国建设银行","credits");
500 BankCard bankcard_1_4_1 = new BankCard("6640000010045442002","88888888");
501 BankCard bankcard_1_4_2 = new BankCard("6640000010045442003","88888888");
502 account_1_4.addBankCardList(bankcard_1_4_1);
503 account_1_4.addBankCardList(bankcard_1_4_2);
504 bank_1.addAccountList(account_1_1);
505 bank_1.addAccountList(account_1_2);
506 bank_1.addAccountList(account_1_3);
507 bank_1.addAccountList(account_1_4);
508
509 ATM atm_1_1 = new ATM(bank_1,"01");
510 ATM atm_1_2 = new ATM(bank_1,"02");
511 ATM atm_1_3 = new ATM(bank_1,"03");
512 ATM atm_1_4 = new ATM(bank_1,"04");
513 bank_1.addAtmList(atm_1_1);
514 bank_1.addAtmList(atm_1_2);
515 bank_1.addAtmList(atm_1_3);
516 bank_1.addAtmList(atm_1_4);
517
518
519 //bank_2
520 User user_2_1 = new User("张无忌");
521 Account account_2_1 = new Account("3222081502001312389",10000,user_2_1,"中国工商银行","debit");
522 BankCard bankcard_2_1_1 = new BankCard("6222081502001312389","88888888");
523 account_2_1.addBankCardList(bankcard_2_1_1);
524 Account account_2_2 = new Account("3222081502001312390",10000,user_2_1,"中国工商银行","debit");
525 BankCard bankcard_2_2_1 = new BankCard("6222081502001312390","88888888");
526 account_2_2.addBankCardList(bankcard_2_2_1);
527 Account account_2_3 = new Account("3222081502001312399",10000,user_2_1,"中国工商银行","debit");
528 BankCard bankcard_2_3_1 = new BankCard("6222081502001312399","88888888");
529 BankCard bankcard_2_3_2 = new BankCard("6222081502001312400","88888888");
530 account_2_3.addBankCardList(bankcard_2_3_1);
531 account_2_3.addBankCardList(bankcard_2_3_2);
532 User user_2_2 = new User("韦小宝");
533 Account account_2_4 = new Account("3222081502051320785",10000,user_2_2,"中国工商银行","debit");
534 BankCard bankcard_2_4_1 = new BankCard("6222081502051320785","88888888");
535 account_2_4.addBankCardList(bankcard_2_4_1);
536 Account account_2_5 = new Account("3222081502051320786",10000,user_2_2,"中国工商银行","debit");
537 BankCard bankcard_2_5_1 = new BankCard("6222081502051320786","88888888");
538 account_2_5.addBankCardList(bankcard_2_5_1);
539 User user_2_3 = new User("令狐冲");
540 Account account_2_6 = new Account("3640000010045441009",10000,user_2_3,"中国工商银行","credits");
541 BankCard bankcard_2_6_1 = new BankCard("6640000010045441009","88888888");
542 account_2_6.addBankCardList(bankcard_2_6_1);
543 bank_2.addAccountList(account_2_1);
544 bank_2.addAccountList(account_2_2);
545 bank_2.addAccountList(account_2_3);
546 bank_2.addAccountList(account_2_4);
547 bank_2.addAccountList(account_2_5);
548 bank_2.addAccountList(account_2_6);
549
550 ATM atm_2_1 = new ATM(bank_2,"05");
551 ATM atm_2_2 = new ATM(bank_2,"06");
552 bank_2.addAtmList(atm_2_1);
553 bank_2.addAtmList(atm_2_2);
554
555
556 User user_3_1 = new User("乔峰");
557 Account account_3_1 = new Account("3630000010033431001",10000,user_3_1,"中国农业银行","credits");
558 BankCard bankcard_3_1_1 = new BankCard("6630000010033431001","88888888");
559 account_3_1.addBankCardList(bankcard_3_1_1);
560 User user_3_2 = new User("洪七公");
561 Account account_3_2 = new Account("3630000010033431008",10000,user_3_2,"中国农业银行","credits");
562 BankCard bankcard_3_2_1 = new BankCard("6630000010033431008","88888888");
563 account_3_2.addBankCardList(bankcard_3_2_1);
564 bank_3.addAccountList(account_3_1);
565 bank_3.addAccountList(account_3_2);
566
567 ATM atm_3_1 = new ATM(bank_3,"07");
568 ATM atm_3_2 = new ATM(bank_3,"08");
569 ATM atm_3_3 = new ATM(bank_3,"09");
570 ATM atm_3_4 = new ATM(bank_3,"10");
571 ATM atm_3_5 = new ATM(bank_3,"11");
572 bank_3.addAtmList(atm_3_1);
573 bank_3.addAtmList(atm_3_2);
574 bank_3.addAtmList(atm_3_3);
575 bank_3.addAtmList(atm_3_4);
576 bank_3.addAtmList(atm_3_5);
577
578
579 return BF;
580 }
581 }
582 class Account {
583
584 private String bankName;
585 private String accountNum;
586 private double accountBalance;
587 private String type; //账户类型
588 private ArrayList<BankCard> bankCardList = new ArrayList<BankCard>();
589 private User user;
590
591 Account(String accountNum,double accountBalance,User user,String bank,String type){
592 this.accountBalance = accountBalance;
593 this.accountNum = accountNum;
594 this.user = user;
595 this.bankName = bank;
596 this.type = type;
597
598 }
599
600 public User getUser() {
601 return user;
602 }
603
604 public void setUser(User user) {
605 this.user = user;
606 }
607
608 public String getAccountNum() {
609 return accountNum;
610 }
611 public void setAccountNum(String accountNum) {
612 this.accountNum = accountNum;
613 }
614 public double getAccountBalance() {
615 return accountBalance;
616 }
617 public void setAccountBalance(double accountBalance) {
618 this.accountBalance = accountBalance;
619 }
620 public ArrayList<BankCard> getBankCardList() {
621 return bankCardList;
622 }
623 public void addBankCardList(BankCard bankCardmen) {
624 this.bankCardList.add(bankCardmen);
625 }
626
627 public String getBankName() {
628 return bankName;
629 }
630
631 public void setBankName(String bankName) {
632 this.bankName = bankName;
633 }
634
635 public String getType() {
636 return type;
637 }
638
639 public void setType(String type) {
640 this.type = type;
641 }
642
643
644
645
646 }
647 class User {
648
649 private String userName;
650
651 User(String userName){
652 this.userName = userName;
653 }
654
655 public String getUserName() {
656 return userName;
657 }
658
659 public void setUserName(String userName) {
660 this.userName = userName;
661 }
662
663 }
664 class Input {
665
666 static ArrayList<String> in_cmd() {
667 Scanner input = new Scanner(System.in);
668 ArrayList<String> cmd = new ArrayList<String>();
669 String temp;
670
671 temp = input.nextLine();
672 while(!temp.equals("#")) {
673 cmd.add(temp);
674 temp = input.nextLine();
675 }
676
677 return cmd;
678 }
679 }
680 class BankCard {
681
682 private String cardNum;
683 private String password;
684
685 BankCard(String cardNum,String password){
686 this.cardNum = cardNum;
687 this.password = password;
688 }
689
690 public String getCardNum() {
691 return cardNum;
692 }
693 public void setCardNum(String cardNum) {
694 this.cardNum = cardNum;
695 }
696 public String getPassword() {
697 return password;
698 }
699 public void setPassword(String password) {
700 this.password = password;
701 }
702
703
704 }
题集9
结合题集8和题集9进行一个名词与名词之间的分析。这应该是一个包含一个的关系,和之前的日期有些相似。首先是有中国银联之下拥有几个银行和透支取款手续费和最大透支额,银行拥有自己的几个ATM机和几个用户和跨行存取手续费,用户拥有自己的几个账号,账号分借记账号和贷款账号,账号拥有初始余额和几张卡。我就是通过这样一层一层的关系,将这几个名词都写成类,从最里面一层开始写,也就是卡,我这里认为卡号可以作为一个属性在账号中所以就没有写成类,账号类拥有卡组、账户号、账户余额和账户密码这几个属性题集9这里加了一个标记标记是借记账号还是贷款账号,用户类拥有姓名和账户数组这两个属性,银行类拥有ATM机组、用户数组、银行名称和跨行存取手续费这几个属性,中国银联类(在我的代码中是DataBase类)有透支手续费、最大透支限额和银行数组这几个属性,场景的初始化我就是在中国银联类中实现的。然后为了完成一个完整的场景需要进行的用户操作,我创建了一个DealData类引用中国银联类,使用其中的数据进行存期钱以及查询的操作。所有的数据处理都在这个类之中,这也就是Monitor图中所示复杂度最高的地方。该类可以判断用户是进行了什么操作,如果是取钱可以判断是否超过可取上限,取钱成功余额的扣减也在这个函数中实现。题目集9在取钱操作上有许多需要考虑的地方,下面给出的是题集9的类图。
踩坑心得
题集7中7-1写的是很顺的,写7-2时一开始未读清题以为最后输出的还是所有的图形和,后面提交到PTA测试点有错才发现最后要输出的不是简单的图形和而是同一类型的图形和比较出最大的输出,然后我是直接在7-1的代码上进行的修改的,当时没有考虑到这些问题导致7-2为了分开不同类型的图形再求和再比较最大值花了不少功夫。虽然最后这样做解决了测试点但是代码的复杂度也大幅度提高了。这就是一开始设计类的时候就没有想好,导致后面出现新问题没法简单解决,接下来的题集8题集9也是如此。
题集8我一开始还想偷小摸的省去账户这个类,我以为账户用不到。结果写完之后在测试时发现存取款会出现问题,因为我一开始下意识的认为是每张卡初始余额一万元,测试点出错才发现是账户余额一万元,账户下面的卡是共用这一万元钱的。这个时候就因为我没写账户这个类导致存取款怎么算钱变得很复杂,所以最后我还是重新写了这道题,将账户类加上去了。事实上也确实需要重写,如果不写账户类的话也就没法像题目给的要求那样模拟银行ATM机存取款了,因为银行肯定是有账户这种东西的。
题集9在我重写了题集8的类的情况下初始化挺容易的,所要做的修改并不多,但是题集9我也没能一次性过,而且还修改了好几次才全部通过测试点。题集9的难点我觉得在取钱这一操作上,题集9新增跨行取款手续费和透支取款手续费,要考到的方面其实很多。先说借记账户(不能超额透支的账户),首先取款不能够超过余额,其次考虑跨行取款,手续费加上要取的钱不能够超过余额。贷记账户(可以透支但有最大上限的账户),首先取款后不能够超过透支最大上限,其次透支手续费加上本次透支的钱不能超过最大上限,跨行取款还要考虑跨行手续费,再加上这个手续费不能够超过最大上限。这些情况都需要考虑到。
改进建议
题集7在我看来需要改进的地方就是每个图形都应该拥有一个类数组,这样在写7-2时就能够轻松很多,仍可以通过使用接口写排序函数,这样就可以直接使用对应类数组的sort()函数进行排序再showResult()即可按需输出就不必用switch...case...去分类去逐个相加。这样解决了求图形和最大值函数的循环过多问题,对照上面的Monitor图可知,少了switch复杂度就会降低,整个代码的复杂度也会降低,这样就达到了降低复杂度的目的,然后拥有类数组将来若是有什么其他有关不同类型图形的需求就可以通过取数组中的内容来实现,可实现开闭原则。
题集8我想银行卡应该也拿出来做一个类,而不是用一个数组简单解决,用一个类实现就可以实现一张卡可以拥有一些其他的私有属性,比如不仅账户有密码银行卡也有密码,这种情况银行卡作为类拥有私有属性密码和卡号就可以方便密码的设置、更改和比对等操作。
题集9除了上面题集8改进建议提到的银行卡作为一个类,还应该对数据处理的函数做处理。像题集9的输入是否合法是否能进行取钱操作中的计算透支手续费和跨行取款的手续费的计算可以写额外的函数,这样就可以减少代码的重复率,缩短代码长度,也能够更方便阅读。
总结
这次的三个题目集其实在写代码时并不难,难的是类图的设计,这三次实验类图都需要自己设计而不是像之前大题都会给好你类图,按照类图去做就好。而且这三次作业的几道题目又是呈一个递增的形式,场景和需求是递加的,这样让我意识到设计一个好的类真的很重要,只有考虑好设计好类图才能够写好代码,代码写起来顺手简单,要不然就是像我一开始写题集8一样最后的结果就是重写代码。所以我觉得类图设计方面还是需要进一步学习和研究。
这个学期的Java学习到此就结束了,老师教会了我很多。一开始觉得不怎么讲代码会不会不太好,因为当时学C语言都是按代码来讲的,后来发现Java作为面对对象的编程最重要的学会如何分析用户需求合理的设计类图,代码因为已经有了C语言和数据结构的基础其实Java语言也是类似的,甚至在许多地方Java的使用都更加方便,Java的函数更多更加方便。最后还学了有关Javafx再结合这个学期学的数据库,我感觉那些游戏大概就是通过代码连接数据库在这之上建立起来的,很有意思,期待进一步的学习。