OO题目集7~9作业总结

OO题目集7~9作业总结

1.前言

针对于PTA题目集7~9的作业,有如下分析:

题目集7的知识点、题量、难度:

7-1:图形卡片排序游戏:继承、多态的应用;ArrayList泛型的应用方法;Comparable接口及泛型的应用;单一职责原则的应用;“开-闭”原则的应用;

7-2:图形卡片分组游戏:沿袭图片卡片排序游戏,对卡片进行分组,考察继承与多态等知识点。

题目集7的难度:相对于我个人而言,对于图形卡片排序以及卡片分组游戏,感觉很难。需要用到继承与多态,提高代码的复用性,以及对Comparable接口及泛型需要了解透彻,明白单一职责原则以及开闭原则的应用。

题目集8的知识点、题量、难度:

7-1:ATM机类结构设计(一):设计ATM仿真系统,要求可实现存款、取款以及查询余额的功能;注意题目中的实体类关系尤其是一对多的组合关系,单一职责原则等。

题目集8的难度:多行输入多行输出,需要对输出进行存储,输入有误时之前正确的输入给予输出。

题目集9的知识点、题量、难度:

7-1:ATM机类结构设计(二):设计ATM仿真系统,要求含有借记卡贷记卡等银行卡,实现本行取款、跨行取款并收取手续费以及查询余额的功能;注意题目中的实体类关系尤其是一对多的组合关系,符合单一职责原则、合成复用原则以及开闭原则等。

2.设计与分析

题目集7的设计分析总结:

题目7-1代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 import java.util.Collections;
  4 
  5 public class Main {
  6     //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
  7     //使用Main.input.next…即可(避免采坑)
  8     public static Scanner input = new Scanner(System.in);
  9     public static void main(String[] args){
 10         ArrayList<Integer> list = new ArrayList<Integer>();
 11         int num = input.nextInt();
 12         while(num != 0){
 13             if(num < 0 || num > 4){
 14                 System.out.println("Wrong Format");
 15                 System.exit(0);
 16             }
 17             list.add(num);
 18             num = input.nextInt();
 19         }
 20         DealCardList dealCardList = new DealCardList(list);
 21         if(!dealCardList.validate()){
 22             System.out.println("Wrong Format");
 23             System.exit(0);
 24         }
 25         dealCardList.showResult();
 26         input.close();
 27     }
 28 }
 29 class DealCardList {
 30     private ArrayList<Card> cardList = new ArrayList<Card>();
 31     
 32     public DealCardList() {
 33         
 34     }
 35 
 36     public DealCardList(ArrayList<Integer> list) {
 37         Scanner input = new Scanner(System.in);
 38         ArrayList<Card> cardlist = new ArrayList<Card>();
 39         this.cardList = cardlist;
 40         for(int i = 0;i < list.size();i++){
 41             if(list.get(i) == 1){
 42                 Circle circle = new Circle(Main.input.nextDouble());
 43                 circle.setShapeName("Circle");
 44                 Card cardradius = new Card(circle);
 45                 cardlist.add(cardradius);
 46             }else if(list.get(i) == 2){
 47                 Rectangle rectangle = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
 48                 rectangle.setShapeName("Rectangle");
 49                 Card cardrectangle = new Card(rectangle);
 50                 cardlist.add(cardrectangle);
 51             }else if(list.get(i) == 3){
 52                 Triangle triangle = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
 53                 triangle.setShapeName("Triangle");
 54                 Card cardtriangle = new Card(triangle);
 55                 cardlist.add(cardtriangle);
 56             }else{
 57                 Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
 58                 trapezoid.setShapeName("Trapezoid");
 59                 Card cardtrapezoid = new Card(trapezoid);
 60                 cardlist.add(cardtrapezoid);
 61             }
 62         }
 63     }
 64 
 65     public boolean validate() {
 66         int flag = 1;
 67         for(int i = 0;i < cardList.size();i++) {
 68             if(cardList.get(i).getShape().validate() == false) {
 69                 flag = 0;
 70             }
 71         }
 72         if(flag == 0) {
 73             return false;
 74         }else {
 75             return true;
 76         }
 77     }
 78 
 79     public void showResult() {
 80         System.out.println("The original list:");
 81         for(int i = 0;i < cardList.size();i++) {
 82             System.out.printf(cardList.get(i).getShape().toString());
 83         }
 84         System.out.println("\nThe sorted list:");
 85         Collections.sort(cardList);
 86         for(int i = 0;i < cardList.size();i++) {
 87             System.out.printf(cardList.get(i).getShape().toString());
 88         }
 89         System.out.printf("\nSum of area:");
 90         double sum = 0;
 91         for(int i = 0;i < cardList.size();i++) {
 92             sum += cardList.get(i).getShape().getArea();
 93         }
 94         System.out.printf("%.2f",sum);
 95     }
 96 
 97 }
 98 abstract class Shape {
 99      private String shapeName;
100      public abstract double getArea();
101      public abstract boolean validate();
102      public abstract String toString();
103      public Shape(){
104          
105      }
106      public Shape(String shapeName){
107          this.shapeName = shapeName;
108      }
109     public String getShapeName() {
110         return shapeName;
111     }
112     public void setShapeName(String shapeName) {
113         this.shapeName = shapeName;
114     }
115     
116 }
117 class Card implements Comparable<Card>{
118     private Shape shape;
119 
120     public Card() {
121         
122     }
123 
124     public Card(Shape shape) {
125         this.shape = shape;
126     }
127 
128     public Shape getShape() {
129         return shape;
130     }
131 
132     public void setShape(Shape shape) {
133         this.shape = shape;
134     }
135     @Override
136     public int compareTo(Card card) {
137         if(this.shape.getArea() < card.shape.getArea()) {
138             return 1;
139         }else if(this.shape.getArea() > card.shape.getArea()) {
140             return -1;
141         }else {
142             return 0;
143         }
144     }
145 }
146 
147 class Circle extends Shape{
148     private double radius;
149     public Circle() {
150         
151     }
152     public Circle(double radius) {
153         this.radius = radius;
154     }
155 
156     public double getRadius() {
157         return radius;
158     }
159 
160     public void setRadius(double radius) {
161         this.radius = radius;
162     }
163 
164     @Override
165     public double getArea() {
166         return Math.PI*this.radius*this.radius;
167     }
168 
169     @Override
170     public boolean validate() {
171         if(this.radius < 0) {
172             return false;
173         }else {
174             return true;
175         }
176     }
177     @Override
178     public String toString() {
179         String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " ";
180         return str;
181     }
182     
183 }
184 class Rectangle extends Shape{
185     private double width;
186     private double length;
187     public Rectangle() {
188         
189     }
190     public Rectangle(double width, double length) {
191         this.width = width;
192         this.length = length;
193     }
194     public double getWidth() {
195         return width;
196     }
197     public void setWidth(double width) {
198         this.width = width;
199     }
200     public double getLength() {
201         return length;
202     }
203     public void setLength(double length) {
204         this.length = length;
205     }
206     @Override
207     public double getArea() {
208         return this.length * this.width;
209     }
210     @Override
211     public boolean validate() {
212         if(this.length < 0||this.width < 0)    {    
213             return false;
214         }else {
215             return true;
216         }
217     }
218     @Override
219     public String toString() {
220         String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " ";
221         return str;
222     }
223     
224 }
225 class Triangle extends Shape{
226     private double side1;
227     private double side2;
228     private double side3;
229     public Triangle() {
230         
231     }
232     public Triangle(double side1, double side2, double side3) {
233         this.side1 = side1;
234         this.side2 = side2;
235         this.side3 = side3;
236     }
237     public double getSide1() {
238         return side1;
239     }
240     public void setSide1(double side1) {
241         this.side1 = side1;
242     }
243     public double getSide2() {
244         return side2;
245     }
246     public void setSide2(double side2) {
247         this.side2 = side2;
248     }
249     public double getSide3() {
250         return side3;
251     }
252     public void setSide3(double side3) {
253         this.side3 = side3;
254     }
255     @Override
256     public double getArea() {
257         double p = (this.getSide1() + this.getSide2() + this.getSide3()) / 2;
258         double area = Math.sqrt(p * (p - this.getSide1()) * (p - this.getSide2()) * (p - this.getSide3()));
259         return area;
260     }
261     @Override
262     public boolean validate() {
263         if(this.side1 + this.side2 > this.side3&&this.side1 + this.side3 > this.side2&&this.side3 + this.side2 > this.side1) {
264             return true;
265         }else {
266             return false;
267         }
268     }
269     @Override
270     public String toString() {
271         String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " ";
272         return str;
273     }
274     
275 }
276 class Trapezoid extends  Shape{
277     private double topSide;
278     private double bottomSide;
279     private double height;
280     public Trapezoid() {
281         
282     }
283 
284     public Trapezoid(double topSide, double bottomSide, double height) {
285         this.topSide = topSide;
286         this.bottomSide = bottomSide;
287         this.height = height;
288     }
289     @Override
290     public double getArea() {
291         return (this.topSide + this.bottomSide) * this.height / 2;
292     }
293     @Override
294     public boolean validate() {
295         if(this.bottomSide < 0||this.height < 0||this.topSide < 0) {
296             return false;
297         }else {
298             return true;
299         }
300     }
301     @Override
302     public String toString() {
303         String str = this.getShapeName() + ":" + String.format("%.2f", getArea()) + " ";
304         return str;
305     }
306 }

 

 题目7-2的代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Collections;
  3 import java.util.Scanner;
  4 public class Main {
  5     //在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
  6     //使用Main.input.next…即可(避免采坑)
  7     public static Scanner input = new Scanner(System.in);
  8     public static void main(String[] args) {
  9         ArrayList<Integer> list = new ArrayList<Integer>();
 10         int num = input.nextInt();
 11         if(num==0) {
 12             System.out.println("Wrong Format");
 13             System.exit(0);
 14         }
 15         while(num != 0) {
 16             if(num < 0 || num > 4) {
 17                 System.out.println("Wrong Format");
 18                 System.exit(0);
 19             }
 20             list.add(num);
 21             num = input.nextInt();
 22         }
 23         DealCardList dealCardList = new DealCardList(list);
 24         if(!dealCardList.validate()) {
 25             System.out.println("Wrong Format");
 26             System.exit(0);
 27         }
 28         dealCardList.showResult();
 29         double maxarea = dealCardList.showByKinds();
 30         dealCardList.showsorted();
 31         System.out.println("\nThe max area:"+String.format("%.2f", maxarea));
 32         input.close();
 33     }
 34 }
 35 class DealCardList{
 36     ArrayList<Card> cardlist = new ArrayList<Card>();
 37     public DealCardList(){
 38         
 39     }
 40     public DealCardList(ArrayList<Integer> list) {
 41         
 42         Scanner input = new Scanner(System.in);
 43         ArrayList<Card> cardlist = new ArrayList<Card>();
 44         this.cardlist = cardlist;
 45         for(int i = 0;i < list.size();i++){
 46             if(list.get(i) == 1){
 47                 Circle circle = new Circle(Main.input.nextDouble());
 48                 circle.setShapeName("Circle");
 49                 Card cardradius = new Card(circle);
 50                 cardlist.add(cardradius);
 51             }else if(list.get(i) == 2){
 52                 Rectangle rectangle = new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
 53                 rectangle.setShapeName("Rectangle");
 54                 Card cardrectangle = new Card(rectangle);
 55                 cardlist.add(cardrectangle);
 56             }else if(list.get(i) == 3){
 57                 Triangle triangle = new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
 58                 triangle.setShapeName("Triangle");
 59                 Card cardtriangle = new Card(triangle);
 60                 cardlist.add(cardtriangle);
 61             }else{
 62                 Trapezoid trapezoid = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
 63                 trapezoid.setShapeName("Trapezoid");
 64                 Card cardtrapezoid = new Card(trapezoid);
 65                 cardlist.add(cardtrapezoid);
 66             }
 67         }
 68     }
 69     
 70     public boolean validate(){
 71         int flag = 1;
 72         for(int i = 0;i < cardlist.size();i++) {
 73             if(cardlist.get(i).getShape().validate() == false) {
 74                 flag = 0;
 75             }
 76         }
 77         if(flag == 0) {
 78             return false;
 79         }else {
 80             return true;
 81         }
 82     }
 83     public void cardSort(){
 84         Collections.sort(cardlist);
 85     }
 86     public void showResult(){
 87         System.out.println("The original list:");
 88         System.out.print("[");
 89         for(int i = 0;i < cardlist.size();i++) {
 90             System.out.print(cardlist.get(i).getShape().getShapeName()+":"+String.format("%.2f", cardlist.get(i).getShape().getArea())+" ");
 91         }
 92         System.out.print("]\n");
 93         
 94     }
 95     public double showByKinds(){
 96         System.out.println("The Separated List:");
 97         System.out.print("[");
 98         double max = 0,sum1 = 0,sum2 = 0,sum3 = 0,sum4 = 0;
 99         for(int i=0;i<cardlist.size();i++){
100             if(cardlist.get(i).getShape().getShapeName().equals("Circle")){
101                 System.out.print("Circle:"+String.format("%.2f", cardlist.get(i).getShape().getArea())+" ");
102                 sum1 += cardlist.get(i).getShape().getArea();
103             }
104         }
105         System.out.print("][");
106         for(int i = 0;i < cardlist.size();i++){
107             if(cardlist.get(i).getShape().getShapeName().equals("Rectangle")){
108                 System.out.print("Rectangle:" + String.format("%.2f", cardlist.get(i).getShape().getArea())+" ");
109                 sum2 += cardlist.get(i).getShape().getArea();
110             }
111         }
112         if(sum1 >= sum2) {
113             max = sum1;
114         }else{
115             max = sum2;
116         }
117         System.out.print("][");
118         for(int i = 0;i < cardlist.size();i++){
119             if(cardlist.get(i).getShape().getShapeName().equals("Triangle")){
120                 System.out.print("Triangle:" + String.format("%.2f", cardlist.get(i).getShape().getArea()) + " ");
121                 sum3 += cardlist.get(i).getShape().getArea();
122             }
123         }
124         if(sum3 > max) {
125             max = sum3;
126         }
127         System.out.print("][");
128         for(int i = 0;i<cardlist.size();i++){
129             if(cardlist.get(i).getShape().getShapeName().equals("Trapezoid")){
130                 sum4 += cardlist.get(i).getShape().getArea();
131                 System.out.print("Trapezoid:" + String.format("%.2f", cardlist.get(i).getShape().getArea()) + " ");
132             }
133         }
134         System.out.print("]");
135         if(sum4 > max) {
136             max = sum4;
137         }
138         return max;
139     }
140     public void showsorted(){
141         Collections.sort(cardlist);
142         System.out.println("\nThe Separated sorted List:");
143         System.out.print("[");
144         int i = 0;
145         for(;i < cardlist.size();i++){
146             if(cardlist.get(i).getShape().getShapeName().equals("Circle")) {
147                 System.out.print("Circle:" + String.format("%.2f", cardlist.get(i).getShape().getArea()) + " ");
148             }else {
149                 break;
150             }
151         }
152         System.out.print("][");
153         for(;i < cardlist.size();i++){
154             if(cardlist.get(i).getShape().getShapeName().equals("Rectangle")) {
155                 System.out.print("Rectangle:" + String.format("%.2f", cardlist.get(i).getShape().getArea()) + " ");
156             }else {
157                 break;
158             }
159         }
160         System.out.print("][");
161         for(;i < cardlist.size();i++){
162             if(cardlist.get(i).getShape().getShapeName().equals("Triangle")) {
163                 System.out.print("Triangle:" + String.format("%.2f", cardlist.get(i).getShape().getArea()) + " ");
164             }else {
165                 break;
166             }
167         }
168         System.out.print("][");
169         for(;i < cardlist.size();i++){
170             if(cardlist.get(i).getShape().getShapeName().equals("Trapezoid")) {
171                 System.out.print("Trapezoid:" + String.format("%.2f", cardlist.get(i).getShape().getArea()) + " ");
172             }else {
173                 break;
174             }
175         }
176         System.out.print("]");
177     }
178 }
179 abstract class Shape{
180     private String shapeName;
181     abstract public double getArea();
182     abstract public boolean validate();
183     public Shape(){
184         this.shapeName = new String();
185     }
186     public Shape(String shapeName){
187         this.shapeName=shapeName;
188     }
189     public String getShapeName() {
190         return shapeName;
191     }
192     public void setShapeName(String shapeName) {
193         this.shapeName = shapeName;
194     }
195 }
196 class Card implements Comparable<Card>{
197     private Shape shape;
198     public Card(){
199         
200     }
201     public Card(Shape shape){
202         this.shape=shape;
203     }
204     public Shape getShape() {
205         return shape;
206     }
207     public void setShape(Shape shape) {
208         this.shape = shape;
209     }
210     @Override
211     public int compareTo(Card card) {
212         int m,n;
213         if(this.shape.getShapeName().equals("Circle")) {
214             m = 1;
215         }else if(this.shape.getShapeName().equals("Rectangle")) {
216             m = 2;
217         }else if(this.shape.getShapeName().equals("Triangle")) {
218             m = 3;
219         }else {
220             m = 4;
221         }
222         if(card.shape.getShapeName().equals("Circle")) {
223             n = 1;
224         }else if(card.shape.getShapeName().equals("Rectangle")) {
225             n = 2;
226         }else if(card.shape.getShapeName().equals("Triangle")) {
227             n = 3;
228         }else {
229             n = 4;
230         }
231         if(m < n) {
232             return -1;
233         }else if(m == n){
234             if(this.shape.getArea()<card.getShape().getArea()) {
235                 return 1;
236             }else if(this.shape.getArea()>card.getShape().getArea()) {
237                 return -1;
238             }else {
239                 return 0;
240             }
241         }else {
242             return 1;
243         }
244     }
245 }
246 class Circle extends Shape{
247     private double radius;
248     public Circle() {
249         
250     }
251     public Circle(double radius) {
252         this.radius = radius;
253     }
254     public double getRadius() {
255         return radius;
256     }
257     public void setRadius(double radius) {
258         this.radius = radius;
259     }
260     public double getArea() {
261         return Math.PI * this.radius * this.radius;
262     }
263     @Override
264     public boolean validate() {
265         if(this.radius < 0) {
266             return false;
267         }else {
268             return true;
269         }
270     }
271 }
272 class Rectangle extends Shape{
273     private double width;
274     private double length;
275     public Rectangle() {
276         
277     }
278     public Rectangle(double width, double length) {
279         this.width = width;
280         this.length = length;
281     }
282     public double getWidth() {
283         return width;
284     }
285     public void setWidth(double width) {
286         this.width = width;
287     }
288     public double getLength() {
289         return length;
290     }
291     public void setLength(double length) {
292         this.length = length;
293     }
294     public double getArea() {
295         return this.length*this.width;
296     }
297     @Override
298     public boolean validate() {
299         if(this.length < 0||this.width < 0)    {    
300             return false;
301         }else {
302             return true;
303         }
304     }
305 }
306 class Triangle extends Shape{
307     private double side1;
308     private double side2;
309     private double side3;
310     public Triangle() {
311         
312     }
313     public Triangle(double side1, double side2, double side3) {
314         this.side1 = side1;
315         this.side2 = side2;
316         this.side3 = side3;
317     }
318     public double getSide1() {
319         return side1;
320     }
321     public void setSide1(double side1) {
322         this.side1 = side1;
323     }
324     public double getSide2() {
325         return side2;
326     }
327     public void setSide2(double side2) {
328         this.side2 = side2;
329     }
330     public double getSide3() {
331         return side3;
332     }
333     public void setSide3(double side3) {
334         this.side3 = side3;
335     }
336     public double getArea() {
337         double p = (this.getSide1() + this.getSide2() + this.getSide3()) / 2;
338         double area = Math.sqrt(p * (p - this.getSide1()) * (p - this.getSide2()) * (p - this.getSide3()));
339         return area;
340     }
341     @Override
342     public boolean validate(){
343         if(this.side1 + this.side2 > this.side3&&this.side1 + this.side3 > this.side2&&this.side3 + this.side2 > this.side1) {
344             return true;
345         }else {
346             return false;
347         }
348     }
349 }
350 class Trapezoid extends Shape{
351     private double topSide;
352     private double bottomSide;
353     private double height;
354     public Trapezoid() {
355         
356     }
357 
358     public Trapezoid(double topSide, double bottomSide, double height) {
359         this.topSide = topSide;
360         this.bottomSide = bottomSide;
361         this.height = height;
362     }
363     @Override
364     public double getArea() {
365         return (this.topSide + this.bottomSide) * this.height / 2;
366     }
367     @Override
368     public boolean validate() {
369         if(this.bottomSide < 0||this.height < 0||this.topSide < 0) {
370             return false;
371         }else {
372             return true;
373         }
374     }
375 }

两道题目都是针对图形卡片进行操作,使用继承、多态等方法,ArrayList数组来存储数据,使用Comparable接口进行调用,代码符合单一职责原则以及开闭原则。

图形卡片排序游戏将卡片的面积进行依次排列;图形卡片分组游戏先将卡片的种类进行排列;然后每种类型的卡片进行面积排序。

 

题目集8和题目集9的设计分析总结:

题目集8(7-1)代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Query query = new Query();
  7         Money money = new Money();
  8         ArrayList<String> bank = new ArrayList<String>();
  9         ArrayList<String> user = new ArrayList<String>();
 10         ArrayList<String> result = new ArrayList<String>();
 11         ArrayList<String> information = new ArrayList<String>();
 12         for(int i = 1;i < 7;i++) {
 13             if(i < 5) {
 14                 bank.add("中国建设银行0" + i);
 15             }else {
 16                 bank.add("中国工商银行0" + i);
 17             }
 18         }
 19         user.add("杨过,中国建设银行,3217000010041315709,10000.00,6217000010041315709");
 20         user.add("杨过,中国建设银行,3217000010041315709,10000.00,6217000010041315715");
 21         user.add("杨过,中国建设银行,3217000010041315715,10000.00,6217000010041315718");
 22         user.add("郭靖,中国建设银行,3217000010051320007,10000.00,6217000010051320007");
 23         user.add("张无忌,中国工商银行,3222081502001312389,10000.00,6222081502001312389");
 24         user.add("张无忌,中国工商银行,3222081502001312390,10000.00,6222081502001312390");
 25         user.add("张无忌,中国工商银行,3222081502001312399,10000.00,6222081502001312399");
 26         user.add("张无忌,中国工商银行,3222081502001312399,10000.00,6222081502001312400");
 27         user.add("韦小宝,中国工商银行,3222081502051320785,10000.00,6222081502051320785");
 28         user.add("韦小宝,中国工商银行,3222081502051320786,10000.00,6222081502051320786");
 29         Scanner input = new Scanner(System.in);
 30         String str = "";
 31         while(!str.equals("#")){
 32             str=input.nextLine();
 33             information.add(str);
 34         }
 35         information.remove("#");
 36         for(int i = 0;i < information.size();i++) {
 37             
 38             if(information.get(i).length() < 19) {
 39                 result.add("Sorry,this card does not exist.");
 40                 print(result);
 41                 System.exit(0);
 42             }else if(information.get(i).length() == 19) {
 43                 String a = query.deposit(user,information.get(i));//剩余金额
 44                 if(a.equals("Sorry,this card does not exist.")) {
 45                     result.add(a);
 46                     print(result);
 47                     System.exit(0);
 48                 }else {
 49                     result.add("¥" + a);
 50                 }
 51                 
 52             }else {
 53                 String b = money.operation(bank,user,information.get(i));
 54                 if(b.equals("Sorry,this card does not exist.")) {
 55                     result.add(b);
 56                     print(result);
 57                     System.exit(0);
 58                 }else if(b.equals("Sorry,your password is wrong.")){
 59                     result.add(b);
 60                     print(result);
 61                     System.exit(0);
 62                 }else if(b.equals("Sorry,the ATM's id is wrong.")) {
 63                     result.add(b);
 64                     print(result);
 65                     System.exit(0);
 66                 }else if(b.equals("Sorry,your account balance is insufficient.")) {
 67                     result.add(b);
 68                     print(result);
 69                     System.exit(0);
 70                 }else if(b.equals("Sorry,cross-bank withdrawal is not supported.")){
 71                     result.add(b);
 72                     print(result);
 73                     System.exit(0);
 74                 }else {
 75                     result.add(b);
 76                     String aa[] = information.get(i).split("[ ]+");
 77                     String sum[] = new String[5];
 78                     for(int j = 0;j < 10;j++) {
 79                         sum = user.get(j).split(",");//分割用户,寻找卡号
 80                         if(aa[0].equals(sum[4])) {
 81                             for(int k = 0;k < 10;k++) {
 82                                 String sum1[] = user.get(k).split(",");//分割用户,寻找账号
 83                                 if(sum1[2].equals(sum[2])) {
 84                                     user.remove(k);
 85                                     user.add(sum1[0]+","+sum1[1]+","+sum[2]+","+sum[3]+","+sum1[4]);
 86                                     
 87                                 }
 88                             }
 89                         }
 90                     }
 91 //                    System.out.println(user);
 92                 }
 93                 
 94             }
 95             if(i == information.size()-1) {
 96                 print(result);
 97             }
 98         }
 99     }
100 
101     private static void print(ArrayList<String> result) {
102         // TODO Auto-generated method stub
103         for(int i = 0;i < result.size();i++) {
104             System.out.println(result.get(i));
105         }
106     }
107     
108 }
109 
110 
111 
112 class Query {
113     
114     public String deposit(ArrayList<String> user, String information) {
115         // TODO Auto-generated method stub
116         String str[] = new String[5];
117         for(int i = 0;i < 10;i++) {
118             str = user.get(i).split(",");
119             if(information.equals(str[4])) {
120                 return str[3];
121             }
122         }
123         return "Sorry,this card does not exist.";
124     }
125 
126 }
127 
128  class Money {
129 
130     public String operation(ArrayList<String> bank, ArrayList<String> user, String information) {
131         // TODO Auto-generated method stub
132         String str[] = information.split("[ ]+");
133         String a[] = new String[5];
134         String b[] = new String[2];
135         boolean flag = false;//卡号标志位,判断卡号是否存在
136         boolean flagatm = false;//ATM校验
137         for(int i = 0;i < 10;i++) {
138             a = user.get(i).split(",");//分割用户,寻找卡号
139 //            System.out.println(a[4]);
140             if(str[0].equals(a[4])) {
141                 flag = true;
142             }
143         }
144         for(int j = 0;j < 6;j++) {
145             b = bank.get(j).split("0");
146             if(str[2].equals("0" + b[1])) {
147                 flagatm = true;
148             }
149         }
150         if(flag == false) {
151             return "Sorry,this card does not exist.";
152         }
153         if(!str[1].equals("88888888")) {
154             return "Sorry,your password is wrong.";
155         }
156         if(flagatm == false) {
157             return "Sorry,the ATM's id is wrong.";
158         }
159         
160         //跨银行取款
161         if(str[2].equals("01")||str[2].equals("02")||str[2].equals("03")||str[2].equals("04")) {
162             for(int i = 0;i < 10;i++) {
163                 a = user.get(i).split(",");//分割用户,寻找卡号
164                 if(str[0].equals(a[4])) {
165                     if(!a[1].equals("中国建设银行")) {
166                         return "Sorry,cross-bank withdrawal is not supported.";
167                     }
168                 }
169             }
170         }
171         if(str[2].equals("05")||str[2].equals("06")) {
172             for(int i = 0;i < 10;i++) {
173                 a = user.get(i).split(",");//分割用户,寻找卡号
174                 if(str[0].equals(a[4])) {
175                     if(!a[1].equals("中国工商银行")) {
176                         return "Sorry,cross-bank withdrawal is not supported.";
177                     }
178                 }
179             }
180         }
181         
182         double money = Double.parseDouble(str[3]);
183         if(money > 0) {//取款
184             for(int i = 0;i < 10;i++) {
185                 a = user.get(i).split(",");//分割用户,寻找卡号
186                 if(str[0].equals(a[4])) {
187                     double nowmoney = Double.parseDouble(a[3]);
188                     if(nowmoney < money) {
189                         return "Sorry,your account balance is insufficient.";
190                     }else {
191                         nowmoney = nowmoney-money;
192                         
193                         user.remove(i);//除去旧的数据
194                         user.add(a[0]+","+a[1]+","+a[2]+","+String.format("%.2f", nowmoney)+","+a[4]);//取款后更新数据
195                         return a[0] + "在" + a[1] + "的" + str[2] + "号ATM机上取款¥" + String.format("%.2f", money) + "\n当前余额为¥" + String.format("%.2f", nowmoney);
196                     }
197                 }
198             }
199         }else {
200             for(int i = 0;i < 10;i++) {
201                 a = user.get(i).split(",");//分割用户,寻找卡号
202                 if(str[0].equals(a[4])) {
203                     double nowmoney = Double.parseDouble(a[3]);
204                     nowmoney = nowmoney-money;
205                     
206                     user.remove(i);//除去旧的数据
207                     user.add(a[0]+","+a[1]+","+a[2]+","+String.format("%.2f", nowmoney)+","+a[4]);//取款后更新数据
208                     return a[0] + "在" + a[1] + "的" + str[2] + "号ATM机上存款¥" + String.format("%.2f", -money) + "\n当前余额为¥" + String.format("%.2f", nowmoney);
209                 }
210             }
211         }
212         return null;
213     }
214 
215 }

 

 题目集9(7-1)代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Query query = new Query();
  7         Money money = new Money();
  8         ArrayList<String> bank = new ArrayList<String>();
  9         ArrayList<String> user = new ArrayList<String>();
 10         ArrayList<String> result = new ArrayList<String>();
 11         ArrayList<String> information = new ArrayList<String>();
 12         //存储银行ATM数据
 13         for(int i = 1;i < 12;i++) {    
 14             if(i < 5) {
 15                 bank.add("中国建设银行0" + i);
 16             }else if(i<7){
 17                 bank.add("中国工商银行0" + i);
 18             }else if(i<10) {
 19                 bank.add("中国农业银行0" + i);
 20             }else {
 21                 bank.add("中国农业银行" + i);
 22             }
 23         }
 24         //存储用户数据
 25         user.add("杨过,中国建设银行,3217000010041315709,10000.00,借记账号,6217000010041315709");
 26         user.add("杨过,中国建设银行,3217000010041315709,10000.00,借记账号,6217000010041315715");
 27         user.add("杨过,中国建设银行,3217000010041315715,10000.00,借记账号,6217000010041315718");
 28         user.add("郭靖,中国建设银行,3217000010051320007,10000.00,借记账号,6217000010051320007");
 29         user.add("张无忌,中国工商银行,3222081502001312389,10000.00,借记账号,6222081502001312389");
 30         user.add("张无忌,中国工商银行,3222081502001312390,10000.00,借记账号,6222081502001312390");
 31         user.add("张无忌,中国工商银行,3222081502001312399,10000.00,借记账号,6222081502001312399");
 32         user.add("张无忌,中国工商银行,3222081502001312399,10000.00,借记账号,6222081502001312400");
 33         user.add("韦小宝,中国工商银行,3222081502051320785,10000.00,借记账号,6222081502051320785");
 34         user.add("韦小宝,中国工商银行,3222081502051320786,10000.00,借记账号,6222081502051320786");
 35         user.add("张三丰,中国建设银行,3640000010045442002,10000.00,贷记账号,6640000010045442002");
 36         user.add("张三丰,中国建设银行,3640000010045442002,10000.00,贷记账号,6640000010045442003");
 37         user.add("令狐冲,中国工商银行,3640000010045441009,10000.00,贷记账号,6640000010045441009");
 38         user.add("乔峰,中国农业银行,3630000010033431001,10000.00,贷记账号,6630000010033431001");
 39         user.add("洪七公,中国农业银行,3630000010033431008,10000.00,贷记账号,6630000010033431008");
 40         
 41         Scanner input = new Scanner(System.in);
 42         String str = "";
 43         //多行输入,遇#结束输入
 44         while(!str.equals("#")){
 45             str=input.nextLine();
 46             information.add(str);
 47         }
 48         information.remove("#");//除去ArrayList中最后输入的#
 49         for(int i = 0;i < information.size();i++) {
 50             
 51             if(information.get(i).length() < 19) {        //卡号长度不足,无法查询剩余金额
 52                 result.add("Sorry,this card does not exist.");
 53                 print(result);
 54                 System.exit(0);
 55             }else if(information.get(i).length() == 19) {        //查询剩余金额
 56                 String a = query.deposit(user,information.get(i));
 57                 if(a.equals("Sorry,this card does not exist.")) {
 58                     result.add(a);
 59                     print(result);
 60                     System.exit(0);
 61                 }else {
 62                     result.add("业务:查询余额 ¥" + a);
 63                 }
 64                 
 65             }else {
 66                 String b = money.operation(bank,user,information.get(i));        //对余额进行操作
 67                 if(b.equals("Sorry,this card does not exist.")) {                //卡号有误,在数组中找不到对应的卡号
 68                     end(b,result);
 69                 }else if(b.equals("Sorry,your password is wrong.")){            //密码错误
 70                     end(b,result);
 71                 }else if(b.equals("Sorry,the ATM's id is wrong.")) {            //ATM编号不存在
 72                     end(b,result);
 73                 }else if(b.equals("Sorry,your account balance is insufficient.")) {            //取款金额大于账户余额
 74                     end(b,result);
 75                 }else if(b.equals("0Sorry,your account balance is insufficient.")) {            //取款金额大于账户余额
 76                     b=b.substring(1, b.length());
 77                     result.add(b);
 78                     continue;
 79                 }else {
 80                     result.add("业务:取款 "+b);                    //正常存取款
 81                     String aa[] = information.get(i).split("[ ]+");                    //单人多卡存取款,需要更新账户的余额
 82                     String sum[] = new String[6];
 83                     for(int j = 0;j < 15;j++) {
 84                         sum = user.get(j).split(",");//分割用户,寻找卡号
 85                         if(aa[0].equals(sum[5])) {
 86                             for(int k = 0;k < 15;k++) {
 87                                 String sum1[] = user.get(k).split(",");//分割用户,寻找账号
 88                                 if(sum1[2].equals(sum[2])) {
 89                                     user.remove(k);
 90                                     user.add(sum1[0]+","+sum1[1]+","+sum[2]+","+sum[3]+","+sum1[4]+","+sum1[5]);
 91                                     
 92                                 }
 93                             }
 94                         }
 95                     }
 96 //                    System.out.println(user);
 97                 }
 98                 
 99             }
100 //             if(i == information.size()-1) {
101 //                 print(result);
102 //             }
103         }
104         print(result);
105     }
106     private static void end(String end, ArrayList<String> result) {
107         // TODO Auto-generated method stub
108         result.add(end);
109         print(result);
110         System.exit(0);
111     }
112     private static void print(ArrayList<String> result) {
113         // TODO Auto-generated method stub
114         for(int i = 0;i < result.size();i++) {
115             System.out.println(result.get(i));
116         }
117     }
118     
119 }
120  class Money {
121 
122     public String operation(ArrayList<String> bank, ArrayList<String> user, String information) {
123         // TODO Auto-generated method stub
124         String str[] = information.split("[ ]+");
125         String a[] = new String[6];        //存储user表的信息
126         String b[] = new String[2];        //存储ATM的信息
127         boolean flag = false;//卡号标志位,判断卡号是否存在
128         boolean flagatm = false;//ATM校验
129         for(int i = 0;i < 15;i++) {            //判断卡号是否存在,flag
130             a = user.get(i).split(",");//分割用户,寻找卡号
131             if(str[0].equals(a[5])) {
132                 flag = true;
133             }
134         }
135         for(int j = 0;j < 11;j++) {        //判断ATM是否存在
136             b = bank.get(j).split("行");
137             if(str[2].equals(b[1])) {
138                 flagatm = true;
139             }
140         }
141         if(flag == false) {
142             return "Sorry,this card does not exist.";
143         }
144         if(!str[1].equals("88888888")) {    //密码错误
145             return "Sorry,your password is wrong.";
146         }
147         if(flagatm == false) {
148             return "Sorry,the ATM's id is wrong.";
149         }
150         
151         //取款
152         if(str[2].equals("01")||str[2].equals("02")||str[2].equals("03")||str[2].equals("04")) {
153             for(int i = 0;i < 15;i++) {
154                 a = user.get(i).split(",");//分割用户,寻找卡号
155                 if(str[0].equals(a[5])) {
156                     if(a[1].equals("中国建设银行")) {
157                         //本银行取款,无手续费
158                         return withdraw(str,user,0,"中国建设银行");
159                     }else if(a[1].equals("中国工商银行")) {
160                         //跨行取款,手续费3%
161                         return withdraw(str,user,0.02,"中国建设银行");
162                     }else {
163                         //跨行取款,手续费4%
164                         return withdraw(str,user,0.02,"中国建设银行");
165                     }
166                 }
167             }
168         }else if(str[2].equals("05")||str[2].equals("06")) {
169             for(int i = 0;i < 15;i++) {
170                 a = user.get(i).split(",");//分割用户,寻找卡号
171                 if(str[0].equals(a[5])) {
172                     if(a[1].equals("中国建设银行")) {
173                         //跨行取款,手续费2%
174                         return withdraw(str,user,0.03,"中国工商银行");
175                     }else if(a[1].equals("中国工商银行")) {
176                         //本银行取款,无手续费
177                         return withdraw(str,user,0,"中国工商银行");
178                     }else {
179                         //跨行取款,手续费4%
180                         return withdraw(str,user,0.03,"中国工商银行");
181                     }
182                 }
183             }
184         }else {
185             for(int i = 0;i < 15;i++) {
186                 a = user.get(i).split(",");//分割用户,寻找卡号
187                 if(str[0].equals(a[5])) {
188                     if(a[1].equals("中国建设银行")) {
189                         //跨行取款,手续费2%
190                         return withdraw(str,user,0.04,"中国农业银行");
191                     }else if(a[1].equals("中国工商银行")) {
192                         //跨行取款,手续费3%
193                         return withdraw(str,user,0.04,"中国农业银行");
194                     }else {
195                         //本银行取款,无手续费
196                         return withdraw(str,user,0,"中国农业银行");
197                     }
198                 }
199             }
200         }
201         
202         return null;
203     }
204 
205     public String withdraw(String[] str, ArrayList<String> user, double rate, String bankname) {
206         // TODO Auto-generated method stub
207         String a[] = new String[6];
208         double money = Double.parseDouble(str[3]);
209         for(int i = 0;i < 15;i++) {
210             a = user.get(i).split(",");//分割用户,寻找卡号
211             if(str[0].equals(a[5])) {
212                 double nowmoney = Double.parseDouble(a[3]);
213                 if(nowmoney < money+rate) {                        //取款金额大于账户余额
214                     if(a[4].equals("借记账号")) {
215                         return "Sorry,your account balance is insufficient.";
216                     }else {
217                         if(nowmoney < 0) {
218                             rate = money*0.05;
219                         }else {
220                             rate = money*rate+(money-nowmoney)*0.05;
221                         }
222                         nowmoney = nowmoney-money-rate;
223                         if((-nowmoney)>=50000) {
224                             return "Sorry,your account balance is insufficient.";
225                         }
226                         user.remove(i);        //除去旧的数据
227                         user.add(a[0]+","+a[1]+","+a[2]+","+String.format("%.2f", nowmoney)+","+a[4]+","+a[5]);//取款后更新数据
228                         return a[0] + "在" + bankname + "的" + str[2] + "号ATM机上取款¥" + String.format("%.2f", money) + "\n当前余额为¥" + String.format("%.2f", nowmoney);
229                     }
230                 }else {
231                     rate = money*rate;
232                     nowmoney = nowmoney-money-rate;
233                     
234                     user.remove(i);        //除去旧的数据
235                     user.add(a[0]+","+a[1]+","+a[2]+","+String.format("%.2f", nowmoney)+","+a[4]+","+a[5]);//取款后更新数据
236                     return a[0] + "在" + bankname + "的" + str[2] + "号ATM机上取款¥" + String.format("%.2f", money) + "\n当前余额为¥" + String.format("%.2f", nowmoney);
237                 }
238             }
239         }
240         return null;
241     }
242 
243 }
244 
245  class Query {
246     
247     public String deposit(ArrayList<String> user, String information) {
248         // TODO Auto-generated method stub
249         String str[] = new String[6];
250         for(int i = 0;i < 15;i++) {
251             str = user.get(i).split(",");
252             if(information.equals(str[5])) {
253                 return str[3];
254             }
255         }
256         return "Sorry,this card does not exist.";
257     }
258 
259 }

 

 针对ATM机,模拟存取款及查询功能。多个实体类,银行、用户等多种信息。用户信息有多个信息组合而成。设计阶段对实体类要做到单一职责原则以及开闭原则,不能缺少规定的实体类。

3.踩坑心得

针对于题目集9,有一个测试点一直过不去,不知道是什么原因。多次透支取款,最后超出透支额度的测试点一直过不去,但是我自己运行完全没问题,不清楚pta的测试机制,导致有时候代码无法通过测试点。

4.改进意见

对于题目集8与题目集9,需要使用多个实体类。不能简单使用ArrayList数组存储实体类的数据,这样对于之后提取数据以及修改数据会有一个缺陷。

5.总结

本阶段的三次题目集,让我了解了继承、多态的应用,对于ArrayList数组的使用方法也有了一个简单的了解,明白了单一职责原则、开闭原则以及合成复用原则等知识。但是自己的代码编程能力还是不足,需要继续加油提高自己的代码编程能力。

 

posted @ 2021-06-19 20:52  Imauselessp!  阅读(45)  评论(0)    收藏  举报