第三次blog作业

前言

习题7-1考察了类的继承、多态性使用方法以及接口的应用,习题7-2与7-1大体一致,而习题集8和习题集9考察java面向对象,通过java的知识实现ATM机的模拟系统。

题量只有1-2题,非常的少,但难度在依次提升。代码量也在不断的加大。

设计与分析

(1)习题集7-1

     类图如下

 

 

 

圈复杂度如下

 

 

 

从类图和圈复杂度上看,类与类之间的关系大部分的是继承关系,圈复杂度也只有6,表面这题大体上是不难度,但在这题的输入上面,需要用到动态数组,即ArrayList<Integer> list = new ArrayList<Integer>();,同时将字符串存入数组也是一个难题,这里用到list接口,如图

本题题目如下

7-1 图形卡片排序游戏 (40 分)

掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 2020-OO第07次作业-1指导书V1.0.pdf

输入格式:

  • 首先,在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如: 1 3 4 2 1 3 4 2 1 3 0
  • 然后根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
  • 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format。
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:

输出格式:

  1. 排序前的各图形类型及面积,格式为图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格;
  2. 排序后的各图形类型及面积,格式同排序前的输出;
  3. 所有图形的面积总和,格式为Sum of area:总面积值。

输入样例1:

在这里给出一组输入。例如:

1 5 3 2 0

输出样例1:

在这里给出相应的输出。例如:

Wrong Format

输入样例2:

在这里给出一组输入。例如:

4 2 1 3 0

3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5

输出样例2:

在这里给出相应的输出。例如:

The original list:

Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02

The sorted list:

Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14

Sum of area:106.91

输入样例3:

在这里给出一组输入。例如:

4 2 1 3 0

3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4

输出样例3:

在这里给出相应的输出。例如:

Wrong Format

 

本题代码如下

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
        public static Scanner input = new Scanner(System.in);
        public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<Integer>();
        int num = input.nextInt();
        while(num != 0){
        if(num < 0 || num > 4){
        System.out.println("Wrong Format");
        return;
        }
         list.add(num);
         num = input.nextInt();
        }
        DealCardList dealCardList = new DealCardList(list);
        if(!dealCardList.validate()){
        System.out.println("Wrong Format");
        return;
        }
        dealCardList.showResult();
        input.close();
        }
    }
class Card{
     Shape shape;
    Card(){
        
    }
    Card(Shape shape){
        this.shape=shape;
    }
    public Shape getShape() {
        return shape;
    }
    public void setShape(Shape Shape) {
        this.shape=shape;
    }
    
}
class DealCardList{
    ArrayList<Card> cardList=new ArrayList<Card>();
    DealCardList(){
        
    }
    DealCardList(ArrayList<Integer> list){
        for(int i=0;i<list.size();i++)
        {
switch(list.get(i)){
        case 1:
                double r=Main.input.nextDouble();
                Circle circle=new Circle(r);
                Card card=new Card(circle);
           card.getShape().setShapeName("Circle");
                cardList.add(card);    
    break;
            case 2:
                double a=Main.input.nextDouble();
                double b=Main.input.nextDouble();
                Rectangle rectangle=new Rectangle(a,b);
                Card card1=new Card(rectangle);
                card1.getShape().setShapeName("Rectangle");
                cardList.add(card1);
    break;
            case 3:
                double a1=Main.input.nextDouble();
                double b1=Main.input.nextDouble();
                double c1=Main.input.nextDouble();
                Triangle triangle=new Triangle(a1,b1,c1);
                Card card2=new Card(triangle);
                card2.getShape().setShapeName("Triangle");
                cardList.add(card2);
      break;
            case 4:
                double a2=Main.input.nextDouble();
                double b2=Main.input.nextDouble();
                double c2=Main.input.nextDouble();
                Traperoid traperoid=new Traperoid(a2,b2,c2);
                Card card3=new Card(traperoid);
                card3.getShape().setShapeName("Trapezoid");
                cardList.add(card3);
            break;
}
            }
    }
    public boolean validate() {
        for(int i=0;i<cardList.size();i++)
            {if(!cardList.get(i).getShape().vaildate())
                    return false;}
        return true;
    }
    public void cardSort() {
        for(int k=0;k<cardList.size();k++)
            for(int i=k+1;i<cardList.size();i++)
            {
                if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
                    Collections.swap(cardList, k, i);
            }
        
        
    }
    public double getAllArea() {
        double s=0;
        for(int i=0;i<cardList.size();i++)
        s=s+cardList.get(i).getShape().getArea();
        return s;
    }
    public void showResult() {
        System.out.println("The original list:");
        for(int i=0;i<cardList.size();i++)
            System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
        System.out.println();
        System.out.println("The sorted list:");
        cardSort();
        for(int i=0;i<cardList.size();i++)
        System.out.print(cardList.get(i).getShape().getShapeName()+":"+String.format("%.2f",cardList.get(i).getShape().getArea())+" ");
        System.out.println();
        System.out.println("Sum of area:"+String.format("%.2f",getAllArea()));
    }
}
 class Shape {
    private String shapeName;
    Shape(){
        
    }
    Shape(String shapeName){
        this.shapeName=shapeName;
    }
    public String getShapeName() {
        return shapeName;
    }
    public void setShapeName(String shapeName) {
        this.shapeName=shapeName;
    }
    public double getArea() {
        return 0.0;
    }
    public boolean vaildate() {
        return true;
    }
    
}
class Circle extends Shape{
    private double radius;
    Circle(){
        
    }
    Circle(double radius){
        this.radius=radius;
    }
    public double getArea() {
        return Math.PI*radius*radius;
    }
    public boolean vaildate() {
        return radius>0?true:false;
    }
}
class Rectangle extends Shape{
    private double width;
   private double length;
    Rectangle (double width,double length){
        this.width=width;
        this.length=length;
    }
    public double getArea() {
        return width*length;
    }
    public boolean vaildate() {
        return (width>0&&length>0)? true:false;
    }
    
}
class Triangle extends Shape{
     double side1;
   double side2;
double side3;
     Triangle(double side1,double side2,double side3){
         this.side1=side1;
         this.side2=side2;
         this.side3=side3;
     }
     public double getArea() {
         double c=(side1+side2+side3)/2;
         double s=Math.sqrt(c*(c-side1)*(c-side2)*(c-side3));
            return s;
        }
     public boolean vaildate() {
            return (side1+side2>side3&&side1+side3>side2&&side2+side3>side1)?true:false;
        }

    
}
class Traperoid extends Shape{
    private double topSide,bottomSide,height;
    Traperoid(){
        
    }
    Traperoid(double topSide,double bottomSide,double height){
        this.bottomSide=bottomSide;
        this.height=height;
        this.topSide=topSide;
    }
    public double getArea() {
        return (topSide+bottomSide)*height/2;
    }
    public boolean validate() {
        return (topSide>0&&bottomSide>0&&height>0)?true:false;
    }
}

 

 

(二)习题7-2

类图如下

 

圈复杂度如下

 

 

 

该题与上一天的答题上没有发生任何变化,同样也是考虑掌握类的继承、多态性使用方法以及接口的应用。圈复杂度相比较于上一题有了一小点的提升。

题目如下

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

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

2021-OO第07次作业-2指导书V1.0.pdf

输入格式:

  • 在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:1 3 4 2 1 3 4 2 1 3 0
  • 根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
  • 如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出Wrong Format。
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:

输出格式:

  1. 排序前的各图形类型及面积,格式为[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
  2. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为The max area:面积值。

输入样例1:

在这里给出一组输入。例如:

1 5 3 2 0

输出样例1:

在这里给出相应的输出。例如:

Wrong Format

输入样例2:

在这里给出一组输入。例如:

4 2 1 3 0

3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5

输出样例2:

在这里给出相应的输出。例如:

The original list:

[Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 ]

The Separated List:

[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]

The Separated sorted List:

[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]

The max area:98.52

输入样例3:

在这里给出一组输入。例如:

2 1 2 1 1 3 3 4 4 1 1 1 2 1 0

2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65

输出样例3:

在这里给出相应的输出。例如:

The original list:

[Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]

The Separated List:

[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]

The Separated sorted List:

[Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]

The max area:1601.31

输入样例4:

在这里给出一组输入。例如:

1 1 3 0

6.5 12.54 3.6 5.3 6.4

输出样例4:

在这里给出相应的输出。例如:

The original list:

[Circle:132.73 Circle:494.02 Triangle:9.54 ]

The Separated List:

[Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]

The Separated sorted List:

[Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]

The max area:626.75

代码如下

 

  1 import java.util.ArrayList;
  2 import java.util.Arrays;
  3 import java.util.Collections;
  4 import java.util.Scanner;
  5 public class Main
  6 {
  7 public static Scanner input = new Scanner(System.in);
  8     public static void main(String[] args) 
  9     {
 10         ArrayList<Integer> list = new ArrayList<Integer>();
 11         int num = input.nextInt(); 
 12         
 13         while(num != 0)
 14         {    
 15             if(num < 0 || num > 4)
 16             {     
 17                 System.out.println("Wrong Format");   
 18                 System.exit(0);    
 19             }         
 20             list.add(num);          
 21             num = input.nextInt();  
 22         }   
 23         Kapianlist kapianlist = new Kapianlist(list);
 24         if(!kapianlist.check())
 25         {
 26             System.out.println("Wrong Format");
 27             System.exit(0);
 28         }
 29         kapianlist.showResult();
 30         input.close();
 31     }    
 32     public static boolean checkABC(double a,double b,double c)
 33     {
 34         return (a<0||b<0||c<0)?false:true;        
 35 }
 36 }
 37 class Shape 
 38 {
 39         private String kapianName;
 40         Shape()
 41         {
 42             
 43         }
 44         Shape(String kapianName)
 45         {
 46             this.kapianName=kapianName;
 47         }
 48         public String getKapianName() 
 49         {
 50             return kapianName;
 51         }
 52         public void setKapianName(String KapianName) 
 53         {
 54             this.kapianName=KapianName;
 55         }
 56         public double getArea() 
 57         {
 58             return 0.0;
 59         }
 60         public boolean check() 
 61         {
 62             return true;
 63         }
 64 }
 65 class Circle extends  Shape
 66 {
 67     private double radius;
 68     public Circle(double radius) 
 69     {
 70            this.radius = radius;
 71     }
 72     public double getR() 
 73     {
 74            return radius;
 75     }
 76     public void setR(double radius )
 77     {
 78            this.radius = radius;
 79     }
 80     public double getArea()
 81     {
 82         return Math.PI*radius*radius;
 83     }
 84     public  boolean check(double radius)
 85     {
 86         return(radius<=0)?false:true;
 87         
 88 }
 89 }
 90 class Rectangle extends  Shape
 91 {
 92     private double weith;
 93     private double length;
 94     public Rectangle()
 95     {
 96     }
 97     public Rectangle(double length,double weith)
 98     {
 99         this.weith=weith;
100         this.length=length;
101     }
102     public double getWidth() 
103     {
104         return weith;
105     }
106     public void setWidth(double weith)
107     {
108         this.weith = weith;
109     }
110     public double getLength() 
111     {
112         return length;
113     }
114     public void setLength(double length)
115     {
116         this.length = length;
117     }
118     public double getArea()
119     {
120         return length*weith;
121     }
122     public  boolean check(double weith,double l)
123     {
124         return(weith<0||length<0)?false:true;
125 }
126 }
127 class Triangle extends  Shape
128 {
129     private double x;
130     private double y;
131     private double z;
132     public Triangle()
133     {
134     }
135     public Triangle(double x,double y,double z)
136     {
137         this.x=x;
138         this.y=y;
139         this.z=z;
140     }
141     public double getArea()
142     {
143         double p=(x+y+z)/2;
144         return Math.sqrt(p*(p-x)*(p-y)*(p-z));
145     }
146     public  Boolean check(double x,double y,double z)
147     {
148         return (x+y<=z||x+z<=y&&y+z<=x||x<0&&y<0&&z<0)?false:true;
149     }
150 }
151 class Trapezoid extends  Shape
152 {
153     private double x;
154     private double y;
155     private double z;
156     public Trapezoid()
157     {
158     }
159     public Trapezoid(double x,double y,double z)
160     {
161         this.x=x;
162         this.y=y;
163         this.z=z;
164     }
165     public double getArea()
166     {
167         return (x+y)*z/2;
168     }
169     public  Boolean check(double x,double y,double z)
170     {
171         return (x<0||y<0&&z<0)?false:true;
172 }
173 }
174 
175 class Kapian
176 {
177     Shape Shape;
178     Kapian(Shape Shape)
179     {
180         this.Shape=Shape;
181     }
182     public Shape getShape() 
183     {
184         return Shape;
185     }
186     public void setShape(Shape Shape) 
187     {
188         this.Shape=Shape;
189     }
190 }
191 class Kapianlist//卡片属性
192 {
193     ArrayList<Kapian> kapianList=new ArrayList<Kapian>();
194     
195     public Kapianlist(){
196         
197     }
198     Kapianlist (ArrayList<Integer> list)
199     {
200         for(int i=0;i<list.size();i++)
201         {
202             switch(list.get(i)) {
203             case 1:
204                 double r=Main.input.nextDouble();
205                 Circle circle=new Circle(r);
206                 Kapian kapian=new Kapian(circle);
207                 kapian.getShape().setKapianName("Circle");
208                 kapianList.add(kapian);    
209                 break;
210             
211             case 2:
212             
213                 double w=Main.input.nextDouble();
214                 double l=Main.input.nextDouble();
215                 Rectangle rectangle=new Rectangle(w,l);
216                 Kapian kapian1=new Kapian(rectangle);
217                 kapian1.getShape().setKapianName("Rectangle");
218                 kapianList.add(kapian1);    
219                 
220                 break;
221             case 3:
222             
223                 double x=Main.input.nextDouble();
224                 double y=Main.input.nextDouble();
225                 double z=Main.input.nextDouble();
226                 Triangle triangle=new Triangle(x,y,z);
227                 Kapian kapian2=new Kapian(triangle);
228                 kapian2.getShape().setKapianName("Triangle");
229                 kapianList.add(kapian2);    
230                 
231                 break;
232             case 4:
233             
234                 double x1=Main.input.nextDouble();
235                 double y1=Main.input.nextDouble();
236                 double z1=Main.input.nextDouble();
237                 Trapezoid traperoid=new Trapezoid(x1,y1,z1);
238                 Kapian kapian3=new Kapian(traperoid);
239                 kapian3.getShape().setKapianName("Trapezoid");
240                 kapianList.add(kapian3);
241                 break;
242                 
243             }
244         }
245     }
246             public boolean check() 
247             {
248                 for(int i=0;i<kapianList.size();i++)
249                     {
250                         if(!kapianList.get(i).getShape().check())
251                         {
252                             return false;
253                         }
254                     }
255                 return true;
256             }
257             public void KapianSort() 
258             {
259                 for(int k=0;k<kapianList.size();k++)
260                 {
261                     for(int i=k+1;i<kapianList.size();i++)
262                     {
263                         if(kapianList.get(k).getShape().getArea()<kapianList.get(i).getShape().getArea())
264                             Collections.swap(kapianList, k, i);
265                     }
266                 
267                 }
268             }
269             public double getAllArea() 
270             {
271                 double s=0;
272                 for(int i=0;i<kapianList.size();i++)
273                     s=s+kapianList.get(i).getShape().getArea();
274                 return s;
275             }
276             public void showResult() 
277             {
278                 System.out.printf("The original list:\n");
279                 System.out.printf("[");
280                 double []a1 = new double[1000];
281                 double []a2 = new double[1000];
282                 double []a3 = new double[1000];
283                 double b = 0;
284                 double c = 0;
285                 double d = 0;
286                 double e = 0;
287                 for(int i=0;i<kapianList.size();i++)
288                 {
289                     System.out.print(kapianList.get(i).getShape().getKapianName()+":");
290                     System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
291                 }
292                 System.out.printf("]\n");
293                 System.out.printf("The Separated List:\n");
294                 System.out.printf("[");
295                 //KapianSort();
296                 for(int i=0;i<kapianList.size();i++)
297                 {
298                     if(kapianList.get(i).getShape().getKapianName()=="Circle") {
299                         System.out.print(kapianList.get(i).getShape().getKapianName()+":");
300                         System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
301                         b =b+ kapianList.get(i).getShape().getArea();
302                         
303                         
304                     }
305                     
306                 }
307                 System.out.printf("]");
308                 System.out.printf("[");
309                 //KapianSort();
310                 for(int i=0;i<kapianList.size();i++)
311                 {
312                     if(kapianList.get(i).getShape().getKapianName()=="Rectangle") {
313                         System.out.print(kapianList.get(i).getShape().getKapianName()+":");
314                         System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
315                         c=c+kapianList.get(i).getShape().getArea();
316                         
317                         
318                     }
319                     
320                 }
321                 System.out.printf("]");
322                 ;
323                 System.out.printf("[");
324                 //KapianSort();
325                 for(int i=0;i<kapianList.size();i++)
326                 {
327                     if(kapianList.get(i).getShape().getKapianName()=="Triangle") {
328                         System.out.print(kapianList.get(i).getShape().getKapianName()+":");
329                         System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
330                         d=d+kapianList.get(i).getShape().getArea();
331                         
332                         
333                     }
334                     
335                 }
336                 System.out.printf("]");
337                 System.out.printf("[");
338                 //KapianSort();
339                 for(int i=0;i<kapianList.size();i++)
340                 {
341                     if(kapianList.get(i).getShape().getKapianName()=="Trapezoid") {
342                         System.out.print(kapianList.get(i).getShape().getKapianName()+":");
343                         System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
344                     }
345                     
346                 }
347                 System.out.printf("]");
348                 
349                     System.out.printf("\n");
350                     System.out.printf("The Separated sorted List:\n");
351                     System.out.printf("[");
352                     KapianSort();
353                     for(int i=0;i<kapianList.size();i++)
354                     {
355                         if(kapianList.get(i).getShape().getKapianName()=="Circle") {
356                             System.out.print(kapianList.get(i).getShape().getKapianName()+":");
357                             System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
358                             
359                             
360                         }
361                         
362                     }
363                     System.out.printf("]");
364                     System.out.printf("[");
365                     //KapianSort();
366                     for(int i=0;i<kapianList.size();i++)
367                     {
368                         if(kapianList.get(i).getShape().getKapianName()=="Rectangle") {
369                             System.out.print(kapianList.get(i).getShape().getKapianName()+":");
370                             System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
371                             
372                             
373                         }
374                         
375                     }
376                     System.out.printf("]");
377                     
378                     System.out.printf("[");
379                     //KapianSort();
380                     for(int i=0;i<kapianList.size();i++)
381                     {
382                         if(kapianList.get(i).getShape().getKapianName()=="Triangle") {
383                             System.out.print(kapianList.get(i).getShape().getKapianName()+":");
384                             System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
385                             
386                             
387                         }
388                         
389                     }
390                     System.out.printf("]");
391                     System.out.printf("[");
392                     KapianSort();
393                     for(int i=0;i<kapianList.size();i++)
394                     {
395                         if(kapianList.get(i).getShape().getKapianName()=="Trapezoid") {
396                             System.out.print(kapianList.get(i).getShape().getKapianName()+":");
397                             System.out.printf("%.2f"+" ",kapianList.get(i).getShape().getArea());
398                             
399                         }
400                         
401                     }
402                     System.out.printf("]");
403                     
404                         System.out.printf("\n");
405                     
406                     
407                 double max;
408                 max = (b>c)?b:c;
409                 max = (max>d)?max:d;
410                 max = (max>e)?max:e;
411                 System.out.printf("The max area:%.2f\n",max);
412             }
413 
414 }

 

 

 

 

 

 

(三)习题集8

   类图如下

 

 

 

圈复杂度如下

 

 

 

这道题我的大体思路是先将用户所有的数据存入各个类当中,每个数据位于一个类当中,然后同过一个一个数据的必对,通过对账号的传参,比较输入的密码是否于已经存入的密码相匹配如下

 

 

 

但终究有几个测试点没过。输入用户的数据用到了动态数据,数据存入数组用到了正则表达式如下:

 

 

 

 

 

 

圈复杂度也不算太高,因为题目难度毕竟在这。

题目如下

7-1 ATM机类结构设计(一) (100 分)

设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf

输入格式:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 存款、取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。
  • 查询余额功能输入数据格式: 卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.。
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.。
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.。
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.。
  • 如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.。

②取款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③存款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

④查询余额业务输出

¥[金额]

金额保留两位小数。

输入样例1:

在这里给出一组输入。例如:

6222081502001312390 88888888 06 -500.00

#

输出样例1:

在这里给出相应的输出。例如:

张无忌在中国工商银行的06号ATM机上存款¥500.00

当前余额为¥10500.00

输入样例2:

在这里给出一组输入。例如:

6217000010041315709  88888888 02 3500.00

#

输出样例2:

在这里给出相应的输出。例如:

杨过在中国建设银行的02号ATM机上取款¥3500.00

当前余额为¥6500.00

输入样例3:

在这里给出一组输入。例如:

6217000010041315715

#

输出样例3:

在这里给出相应的输出。例如:

¥10000.00

输入样例4:

在这里给出一组输入。例如:

6222081502001312390 88888888 06 -500.00

6222081502051320786 88888888 06 1200.00

6217000010041315715 88888888 02 1500.00

6217000010041315709  88888888 02 3500.00

6217000010041315715

#

输出样例4:

在这里给出相应的输出。例如:

张无忌在中国工商银行的06号ATM机上存款¥500.00

当前余额为¥10500.00

韦小宝在中国工商银行的06号ATM机上取款¥1200.00

当前余额为¥8800.00

杨过在中国建设银行的02号ATM机上取款¥1500.00

当前余额为¥8500.00

杨过在中国建设银行的02号ATM机上取款¥3500.00

当前余额为¥5000.00

¥5000.00

代码如下

 

  1 import java.util.ArrayList;
  2 import java.util.List;
  3 import java.util.Scanner;
  4 public class Main{
  5 public static void main(String[] args) {
  6     Scanner in = new Scanner(System.in);
  7     List<String> list=new ArrayList<String>();
  8     String a1 ;
  9    
 10     while(!(a1=in.nextLine()).equals("#")) {
 11         list.add(a1);
 12         
 13 }
 14     for(int i=0;i<list.size();i++)
 15                {
 16                   String[] s = list.get(i).split("\\s+");
 17                   Card one = new Card(s);
 18                   if(one.dudge()) {
 19                       
 20                       if(s.length==1) {
 21                           Function forth = new Function();
 22                           System.out.printf("¥%.2f",Float.parseFloat(forth.money[one.getPlace()]));
 23                           continue;
 24                       }
 25                       code two = new code(one.getPlace(),s);
 26                       if(two.dudge()) {
 27                           ATM three = new ATM(one.getPlace(),s);
 28                           
 29                           if(three.dudge()) {
 30                               Function forth = new Function(one.getPlace(),s);
 31                               forth.kqk();
 32                               if(forth.dudge()) {
 33                                   name five = new name();
 34                                   System.out.print(five.Name[one.getPlace()]+"在"+five.bank[one.getPlace()]+"的"+s[2]+"号ATM机上");
 35                                   if(Float.parseFloat(s[3])>0) {
 36                                       System.out.printf("取款¥%.2f\n",Float.parseFloat(s[3]));
 37                                   }
 38                                   else {
 39                                       System.out.printf("存款¥%.2f\n",-Float.parseFloat(s[3]));
 40                                   }
 41                                   System.out.printf("当前余额为¥%.2f\n",forth.getSymoney());
 42                               }
 43                               else{
 44                                   System.out.println("Sorry,your account balance is insufficient.");
 45                               }
 46                           }
 47                           else {
 48                               System.out.println("Sorry,the ATM's id is wrong.");
 49                           }
 50                       }
 51                       else {
 52                           System.out.println("Sorry,your password is wrong.");
 53                       }
 54                   }
 55                   else {
 56                       System.out.println("Sorry,this card does not exist.");
 57                       continue;
 58                   }
 59                }
 60 }
 61 }
 62 
 63 class Card {
 64     String []ID = {"6217000010041315709","6217000010041315715","6217000010041315718","6217000010051320007"
 65                    ,"6222081502001312389","6222081502001312390","6222081502001312399",
 66                     "6222081502001312400","6222081502051320785","6222081502051320786"};
 67     private String id;
 68     private int place;
 69     
 70     public int getPlace() {
 71         return place;
 72     }
 73     public void setPlace(int place) {
 74         this.place = place;
 75     }
 76     public String getId() {
 77         return id;
 78     }
 79     public void setId(String id) {
 80         this.id = id;
 81     }
 82     public Card(String []id) {
 83         this.id = id[0];
 84     }
 85     
 86     
 87     public boolean dudge() {
 88         for(int i = 0;i<ID.length;i++) {
 89             if(ID[i].equals(id)) {
 90                 this.place=i;
 91                 
 92                 return true;
 93             }
 94             
 95         }
 96         return false;
 97     }
 98         }
 99  class code {
100     public String []code= {"88888888","88888888","88888888","88888888",
101             "88888888","88888888","88888888","88888888","88888888","88888888"};
102     private String Code;
103     private int place;
104     public code(int place, String []s) {
105         // TODO Auto-generated constructor stub
106         this.Code = s[1];
107         this.place = place;
108     }
109     public String getCode() {
110         return Code;
111     }
112     public void setCode(String code) {
113         this.Code = code;
114     }
115     public int getPlace() {
116         return place;
117     }
118     public void setPlace(int place) {
119         this.place = place;
120     }
121     public boolean dudge() {
122         if(code[place].equals(Code)) {
123             return true;
124         }
125         else {
126             return false;
127         }
128         }
129     }
130 class ATM {
131     private int place;
132     private String Number;
133     public int getPlace() {
134         return place;
135     }
136     public void setPlace(int place) {
137         this.place = place;
138     }
139     public String getNumber() {
140         return Number;
141     }
142     public void setNumber(String number) {
143         this.Number = number;
144     }
145     public String []number = {"01","02","03","04","05","06","07","08","09"};
146     public ATM(int place, String[] s) {
147         // TODO Auto-generated constructor stub
148         this.place = place;
149         this.Number = s[2];
150     }
151     public boolean dudge() {
152 //        if((place<5&&Number!="01"&&Number!="02"&&Number!="03"&&Number!="04")||(place>4&&Number!="05"&&Number!="06"&&Number!="07"&&Number!="08"&&Number!="08")) {
153 //            return false;
154 //        }
155 //        else {
156 //            return true;
157 //        }
158         for(int i = 0;i<number.length;i++) {
159             if(Number.equals(number[i])) {
160                 return true;
161             }
162         }
163         
164             return false;
165         
166     }
167 
168 }
169  class Function {
170     private int place;
171     private String Money;
172     private float symoney;
173     public int getPlace() {
174         return place;
175     }
176      Function() {
177         // TpublicODO Auto-generated constructor stub
178     }
179     public void setPlace(int place) {
180         this.place = place;
181     }
182     public String getMoney() {
183         return Money;
184     }
185     public void setMoney(String money) {
186         Money = money;
187     }
188     public Function(int place, String[] s) {
189         // TODO Auto-generated constructor stub
190         this.place = place;
191         Money = s[3];
192     }
193     String []money = {"10000","10000","10000","10000","10000","10000","10000","10000","10000"};
194     public boolean dudge() {
195         float a = Float.parseFloat(money[place]);
196         float b =Float.parseFloat(Money);
197         if(a<b) {
198             return false;
199         }
200         else {
201             return true;
202         }
203     }
204     public void kqk() {
205         // TODO Auto-generated method stub
206         float a = Float.parseFloat(money[place]);
207         float b =Float.parseFloat(Money);
208         this.symoney=a-b;
209         
210     }
211     public float getSymoney() {
212         return symoney;
213     }
214     public void setSymoney(int symoney) {
215         this.symoney = symoney;
216     }
217 
218 }
219 class name extends Bank {
220      String []Name= {"杨过","杨过","杨过","郭靖","张无忌","张无忌","张无忌","张无忌","韦小宝","韦小宝"};
221     
222 }
223 class Bank {
224   String []bank= {"中国建设银行","中国建设银行","中国建设银行","中国建设银行","中国工商银行","中国工商银行","中国工商银行","中国工商银行","中国工商银行","中国工商银行","中国工商银行"};
225 }

 

 

 

(四)题目集9

类图如下

 

 

 

圈复杂如下

 

 

 

单从圈复杂度上看,圈复杂度是要第一点的,按理说是要高一点的,但这是在老师给的代码下改进的,上一题的代码是我自己写的,所以低一点很正常。这题主要是把卡号分成了两种,借记卡和贷记卡,所以我们在输入的时候就要加入,如下

 

 

 

直接加入卡号以变在之后判断是否可用透支,如下

 

 

 

与上一题比较,只需要改变两个类即可。在Main类输入卡的类型,withdraw类设计算法,判断金额的变化。

题目如下

7-1 ATM机类结构设计(二) (100 分)

设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf

输入格式:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔)
  • 查询余额功能输入数据格式: 卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.。
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.。
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.。
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.。

②取款业务输出

输出共两行,格式分别为:

业务:取款 [用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③查询余额业务输出

业务:查询余额 ¥[金额]

金额保留两位小数。

输入样例1:

在这里给出一组输入。例如:

6222081502001312390 88888888 06 500.00

#

输出样例1:

在这里给出相应的输出。例如:

业务:取款 张无忌在中国工商银行的06号ATM机上取款¥500.00

当前余额为¥9500.00

输入样例2:

在这里给出一组输入。例如:

6217000010041315709  88888888 06 3500.00

#

输出样例2:

在这里给出相应的输出。例如:

业务:取款 杨过在中国工商银行的06号ATM机上取款¥3500.00

当前余额为¥6395.00

输入样例3:

在这里给出一组输入。例如:

6217000010041315715

#

输出样例3:

在这里给出相应的输出。例如:

业务:查询余额 ¥10000.00

输入样例4:

在这里给出一组输入。例如:

6222081502001312390 88888888 01 500.00

6222081502051320786 88888888 06 1200.00

6217000010041315715 88888888 02 1500.00

6217000010041315709  88888888 02 3500.00

6217000010041315715

#

输出样例4:

在这里给出相应的输出。例如:

业务:取款 张无忌在中国建设银行的01号ATM机上取款¥500.00

当前余额为¥9490.00

业务:取款 韦小宝在中国工商银行的06号ATM机上取款¥1200.00

当前余额为¥8800.00

业务:取款 杨过在中国建设银行的02号ATM机上取款¥1500.00

当前余额为¥8500.00

业务:取款 杨过在中国建设银行的02号ATM机上取款¥3500.00

当前余额为¥5000.00

业务:查询余额 ¥5000.00

输入样例5:

在这里给出一组输入。例如:

6640000010045442002 88888888 09 3000

6640000010045442002 88888888 06 8000

6640000010045442003 88888888 01 10000

6640000010045442002

#

输出样例5:

在这里给出相应的输出。例如:

业务:取款 张三丰在中国农业银行的09号ATM机上取款¥3000.00

当前余额为¥6880.00

业务:取款 张三丰在中国工商银行的06号ATM机上取款¥8000.00

当前余额为¥-1416.00

业务:取款 张三丰在中国建设银行的01号ATM机上取款¥10000.00

当前余额为¥-11916.00

业务:查询余额 ¥-11916.00

代码如下

 

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 import java.util.Iterator;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 public class Main {
  8        public static void main(String[] args) {
  9         Scanner input = new Scanner(System.in);
 10         
 11         UnionPay unionPay = new UnionPay();       
 12         
 13         Bank ccb = new Bank("1001","中国建设银行");
 14         Bank icbc = new Bank("1002","中国工商银行");
 15         Bank zgry = new Bank("1003","中国农业银行");
 16         unionPay.addBank(ccb);
 17         unionPay.addBank(icbc);
 18         unionPay.addBank(zgry);
 19         
 20         ATM aTM1 = new ATM("01",ccb);
 21         ATM aTM2 = new ATM("02",ccb);
 22         ATM aTM3 = new ATM("03",ccb);
 23         ATM aTM4 = new ATM("04",ccb);
 24         ATM aTM5 = new ATM("05",icbc);
 25         ATM aTM6 = new ATM("06",icbc);
 26         ATM aTM7 = new ATM("07",zgry);
 27         ATM aTM8 = new ATM("08",zgry);
 28         ATM aTM9 = new ATM("09",zgry);
 29         ATM aTM10 = new ATM("10",zgry);
 30         ATM aTM11 = new ATM("11",zgry);
 31         
 32         ccb.addATM(aTM1);
 33         ccb.addATM(aTM2);
 34         ccb.addATM(aTM3);
 35         ccb.addATM(aTM4);
 36         icbc.addATM(aTM5);
 37         icbc.addATM(aTM6);
 38         zgry.addATM(aTM7);
 39         zgry.addATM(aTM8);
 40         zgry.addATM(aTM9);
 41         zgry.addATM(aTM10);
 42         zgry.addATM(aTM11);
 43         
 44         User Yangguo = new User("360101200102122324","杨过","13856223254");
 45         User Guojing = new User("360101200012302552","郭靖","13800021124");
 46         User Zhangwuji = new User("360502199805163221","张无忌","13952110011");
 47         User Weixiaobao = new User("360201200513243326","韦小宝","13025996587");
 48         User Zhangsanfeng = new User("360202200212067116","张三丰","15279077461");
 49         User Linghucong = new User("360202200212067117","令狐聪","15279077462");
 50         User Qiaofeng = new User("360202200212067118","乔峰","15279077463");
 51         User Hongqigong = new User("360202200212067119","洪七公","15279077464");
 52         
 53         Account ccbAcc1 = new Account("3217000010041315709",10000.00,Yangguo,ccb,"借记");
 54         Account ccbAcc2 = new Account("3217000010041315715",10000.00,Yangguo,ccb,"借记");
 55         Account ccbAcc3 = new Account("3217000010051320007",10000.00,Guojing,ccb,"借记");
 56         Account ccbAcc4 = new Account("3640000010045442002",10000.00,Zhangsanfeng,ccb,"贷记");
 57         Account icbcAcc1 = new Account("3222081502001312389",10000.00,Zhangwuji,icbc,"借记");
 58         Account icbcAcc2 = new Account("3222081502001312390",10000.00,Zhangwuji,icbc,"借记");
 59         Account icbcAcc3 = new Account("3222081502001312399",10000.00,Zhangwuji,icbc,"借记");
 60         Account icbcAcc4 = new Account("3222081502051320785",10000.00,Weixiaobao,icbc,"借记");
 61         Account icbcAcc5 = new Account("3222081502051320786",10000.00,Weixiaobao,icbc,"借记");
 62         Account icbcAcc6 = new Account("3640000010045441009",10000.00,Linghucong,icbc,"贷记");
 63         Account zgryAcc1 = new Account("3630000010033431001",10000.00,Qiaofeng,zgry,"贷记");
 64         Account zgryAcc2 = new Account("3630000010033431008",10000.00,Hongqigong,zgry,"贷记");
 65         
 66         
 67         ccb.addAccount(ccbAcc1);
 68         ccb.addAccount(ccbAcc2);
 69         ccb.addAccount(ccbAcc3);
 70         ccb.addAccount(ccbAcc4);
 71         icbc.addAccount(icbcAcc1);
 72         icbc.addAccount(icbcAcc2);
 73         icbc.addAccount(icbcAcc3);
 74         icbc.addAccount(icbcAcc4);
 75         icbc.addAccount(icbcAcc5);
 76         icbc.addAccount(icbcAcc6);
 77         zgry.addAccount(zgryAcc1);
 78         zgry.addAccount(zgryAcc2);
 79         
 80     
 81         
 82         Yangguo.addAccount(ccbAcc1);
 83         Yangguo.addAccount(ccbAcc2);
 84         Guojing.addAccount(ccbAcc3);
 85         Zhangsanfeng.addAccount(ccbAcc4);
 86         Zhangwuji.addAccount(icbcAcc1);
 87         Zhangwuji.addAccount(icbcAcc2);
 88         Zhangwuji.addAccount(icbcAcc3);
 89         Weixiaobao.addAccount(icbcAcc4);
 90         Weixiaobao.addAccount(icbcAcc5);
 91         Linghucong.addAccount(icbcAcc6);
 92         Qiaofeng.addAccount(zgryAcc1);
 93         Hongqigong.addAccount(zgryAcc2);
 94         
 95         
 96         
 97         Card ccbCard1 = new Card("6217000010041315709","88888888",ccbAcc1);
 98         Card ccbCard2 = new Card("6217000010041315715","88888888",ccbAcc1);
 99         Card ccbCard3 = new Card("6217000010041315718","88888888",ccbAcc2);
100         Card ccbCard4 = new Card("6217000010051320007","88888888",ccbAcc3);
101         Card icbcCard5 = new Card("6222081502001312389","88888888",icbcAcc1);
102         Card icbcCard6 = new Card("6222081502001312390","88888888",icbcAcc2);
103         Card icbcCard7 = new Card("6222081502001312399","88888888",icbcAcc3);
104         Card icbcCard8 = new Card("6222081502001312400","88888888",icbcAcc3);
105         Card icbcCard9 = new Card("6222081502051320785","88888888",icbcAcc4);
106         Card icbcCard10 = new Card("6222081502051320786","88888888",icbcAcc5);
107         Card ccbCard11 = new Card("6640000010045442002","88888888",ccbAcc4);
108         Card ccbCard12 = new Card("6640000010045442003","88888888",ccbAcc4);
109         Card icbcCard13 = new Card("6640000010045441009","88888888",icbcAcc6);
110         Card zgryCard14 = new Card("6630000010033431001","88888888",zgryAcc1);
111         Card zgryCard15 = new Card("6630000010033431008","88888888",zgryAcc2);
112         
113         ccbAcc1.addCard(ccbCard1);
114         ccbAcc1.addCard(ccbCard2);
115         ccbAcc2.addCard(ccbCard3);
116         ccbAcc3.addCard(ccbCard4);
117         ccbAcc4.addCard(ccbCard11);
118         ccbAcc4.addCard(ccbCard12);
119         icbcAcc1.addCard(icbcCard5);
120         icbcAcc2.addCard(icbcCard6);
121         icbcAcc3.addCard(icbcCard7);
122         icbcAcc3.addCard(icbcCard8);
123         icbcAcc4.addCard(icbcCard9);
124         icbcAcc5.addCard(icbcCard10);
125         icbcAcc6.addCard(icbcCard13);
126         zgryAcc1.addCard(zgryCard14);
127         zgryAcc2.addCard(zgryCard15);
128         
129         StringBuilder sb = new StringBuilder();
130         
131         String data;
132         
133         while(!((data = input.nextLine()).equals("#"))) {
134             sb.append(data + "\n");
135         }        
136         
137         String[] dt = sb.toString().split("\n");
138         for(int i = 0; i < dt.length; i++) {
139             String[] dataLine = dt[i].toString().split("\\s+");
140             
141             if(dataLine.length == 1) {
142                 GetBalance gb = new GetBalance(unionPay);
143                 System.out.print("业务:查询余额 ");
144                 System.out.println(String.format("¥%.2f", gb.getBalance(dataLine[0])));
145             }else {
146                 Withdraw wd = new Withdraw(unionPay,dataLine[0],dataLine[1],dataLine[2],Double.parseDouble(dataLine[3]));
147                 wd.withdraw();
148             }
149         }       
150     }
151 }
152 
153    class Bank {
154     private String bankNO;
155     private String bankName;
156     private ArrayList<Account> accountList = new ArrayList<Account>();
157     private ArrayList<ATM> ATMList = new ArrayList<ATM>();
158 
159     public Bank() {
160         super();
161         // TODO Auto-generated constructor stub
162     }
163 
164     public Bank(String bankNO, String bankName) {
165         super();
166         this.bankNO = bankNO;
167         this.bankName = bankName;
168     }
169 
170     public String getBankNO() {
171         return bankNO;
172     }
173 
174     public void setBankNO(String bankNO) {
175         this.bankNO = bankNO;
176     }
177 
178     public String getBankName() {
179         return bankName;
180     }
181 
182     public void setBankName(String bankName) {
183         this.bankName = bankName;
184     }
185 
186     public void addAccount(Account account) {
187         this.accountList.add(account);
188     }
189 
190     public void removeAccount(Account account) {
191         this.accountList.remove(account);
192     }
193     
194     public void addATM(ATM aTM) {
195         this.ATMList.add(aTM);
196     }
197     
198     public void removeATM(ATM aTM) {
199         this.ATMList.remove(aTM);
200     }
201 
202     public ArrayList<Account> getAccountList() {
203         return accountList;
204     }
205 
206     public void setAccountList(ArrayList<Account> accountList) {
207         this.accountList = accountList;
208     }
209 
210     public ArrayList<ATM> getATMList() {
211         return ATMList;
212     }
213 
214     public void setATMList(ArrayList<ATM> aTMList) {
215         ATMList = aTMList;
216     }
217 }
218 
219 class User {
220     private String ID;
221     private String name;
222     private String Phone;
223     ArrayList<Account> list = new ArrayList<Account>();
224 
225     public User() {
226         super();
227         // TODO Auto-generated constructor stub
228     }    
229 
230     public User(String iD, String name, String phone) {
231         super();
232         ID = iD;
233         this.name = name;
234         Phone = phone;
235     }
236 
237     public String getID() {
238         return ID;
239     }
240 
241     public void setID(String iD) {
242         ID = iD;
243     }
244 
245     public String getPhone() {
246         return Phone;
247     }
248 
249     public void setPhone(String phone) {
250         Phone = phone;
251     }    
252 
253     public String getName() {
254         return name;
255     }
256 
257     public void setName(String name) {
258         this.name = name;
259     }
260 
261     public void addAccount(Account account) {
262         this.list.add(account);
263     }
264 
265     public void removeAccount(Account account) {
266         this.list.remove(account);
267     }
268 }
269 
270 class Account {
271     private String accountNO;
272     private double balance = 0;
273     private User user = null;
274     private Bank bank = null;
275     private String kind;
276     private static ArrayList<Card> list = new ArrayList<Card>();
277 
278     public Account() {
279         super();
280         // TODO Auto-generated constructor stub
281     }   
282 
283     public Account(String accountNO, double balance, User user, Bank bank,String kind) {
284         super();
285         this.accountNO = accountNO;
286         this.balance = balance;
287         this.user = user;
288         this.bank = bank;
289         this.kind = kind;
290     }
291 
292     public String getKind() {
293         return kind;
294     }
295 
296     public void setKind(String kind) {
297         this.kind = kind;
298     }
299 
300     public void addCard(Card card) {
301         list.add(card);
302     }
303 
304     public void removeCard(Card card) {
305         list.remove(card);
306     }
307 
308     public double getBalance() {
309         return balance;
310     }
311 
312     public void setBalance(double balance) {
313         this.balance = balance;
314     }
315 
316     public String getAccountNO() {
317         return accountNO;
318     }
319 
320     public void setAccountNO(String accountNO) {
321         this.accountNO = accountNO;
322     }
323 
324     public User getUser() {
325         return user;
326     }
327 
328     public void setUser(User user) {
329         this.user = user;
330     }
331 
332     public Bank getBank() {
333         return bank;
334     }
335 
336     public void setBank(Bank bank) {
337         this.bank = bank;
338     }
339 
340     public ArrayList<Card> getList() {
341         return list;
342     }
343 
344     public void setList(ArrayList<Card> list) {
345         Account.list = list;
346     }
347     
348     public static Account getAmountbyCardNO(String cardNO) {
349         Iterator<Card> cardItr = Account.list.iterator();
350         Card card = null;
351         
352         while(cardItr.hasNext()) {
353             card = cardItr.next();
354             if(card.getCardNO().equals(cardNO)) {
355                 return card.getAccount();
356             }
357         }
358         
359         return null;
360     }
361 }
362 
363 class Card {
364     private String cardNO;
365     private String cardPassword;
366     private Account account = null;
367 
368     public Card() {
369         super();
370         // TODO Auto-generated constructor stub
371     }
372 
373     public Card(String cardNO, String cardPassword, Account account) {
374         super();
375         this.cardNO = cardNO;
376         this.cardPassword = cardPassword;
377         this.account = account;
378     }
379 
380     public String getCardNO() {
381         return cardNO;
382     }
383 
384     public void setCardNO(String cardNO) {
385         this.cardNO = cardNO;
386     }
387 
388     public String getCardPassword() {
389         return cardPassword;
390     }
391 
392     public Account getAccount() {
393         return account;
394     }
395 
396     public void setAccount(Account account) {
397         this.account = account;
398     }
399 
400     public void setCardPassword(String cardPassword) {
401         this.cardPassword = cardPassword;
402     }
403 
404     public boolean checkCard() {
405         Pattern p = Pattern.compile("\\d{16}+");
406         Matcher m = p.matcher(this.cardNO);
407         return m.matches();
408     }
409 
410     public boolean checkPassword(String password) {
411         return this.cardPassword.equals(password);
412     }    
413 }
414 
415 class ATM {
416     private String ATMID;
417     private Bank bank = null;
418 
419     public ATM() {
420         super();
421         // TODO Auto-generated constructor stub
422     }   
423 
424     public ATM(String aTMID, Bank bank) {
425         super();
426         ATMID = aTMID;
427         this.bank = bank;
428     }
429 
430     public String getATMID() {
431         return ATMID;
432     }
433 
434     public void setATMID(String aTMID) {
435         ATMID = aTMID;
436     }
437 
438     public Bank getBank() {
439         return bank;
440     }
441 
442     public void setBank(Bank bank) {
443         this.bank = bank;
444     }
445 }
446 
447 class Withdraw {
448     private UnionPay unionPay;
449     private String cardNO;
450     private String cardPassword;
451     private String ATMID;
452     private double amount;
453     String a;
454     double b=0;
455     public Withdraw() {
456         super();
457         // TODO Auto-generated constructor stub
458     }
459     
460 
461     public Withdraw(UnionPay unionPay, String cardNO, String cardPassword, String aTMID, double amount) {
462         super();
463         this.unionPay = unionPay;
464         this.cardNO = cardNO;
465         this.cardPassword = cardPassword;
466         ATMID = aTMID;
467         this.amount = amount;
468     }
469     
470     public Withdraw(String cardNO, String cardPassword, String aTMID, double amount) {
471         super();
472         this.cardNO = cardNO;
473         this.cardPassword = cardPassword;
474         ATMID = aTMID;
475         this.amount = amount;
476     }
477 
478 
479     public void withdraw() {
480         /**
481          * 校验该卡是否存在
482          */        
483         
484         Card card = ValidateData.getCardbyCardNO(unionPay, cardNO);
485         if(card == null) {
486             System.out.println("Sorry,this card does not exist.");
487             System.exit(0);
488         }
489         
490         /**
491          * 校验ATM是否存在
492          */
493         ATM aTM = ValidateData.getATMbyATMID(unionPay, ATMID);
494         if(aTM == null) {
495             System.out.println("Sorry,the ATM's id is wrong.");
496             System.exit(0);
497         }
498         
499         Account account = Account.getAmountbyCardNO(cardNO);
500         double balance = account.getBalance();
501         
502         /**
503          * 校验卡密码是否正确
504          */
505         if(!card.getCardPassword().equals(cardPassword)) {
506             System.out.println("Sorry,your password is wrong.");
507             System.exit(0);
508         }
509         
510         /**
511          * 校验取款金额是否大于余额
512          */
513         if ((amount > balance)&& (account.getKind().equals("借记"))){
514             System.out.println("Sorry,your account balance is insufficient.");
515             System.exit(0);
516         }
517         
518         /**
519          * 校验是否为跨行取款
520          */
521         if (account.getBank().getBankNO() != aTM.getBank().getBankNO()) {
522             
523             
524             switch(aTM.getBank().getBankNO()) 
525             {
526             case "1001":b=amount*0.02;
527                           a="中国建设银行";
528                           
529                         break;
530             case "1002":b=amount*0.03;
531                          a="中国工商银行";
532                         
533                         break;
534             case "1003":b=amount*0.04;
535                         a="中国农业银行";
536                         break;
537             }
538         }
539 
540         if (account.getKind().equals("贷记")) {
541             if(account.getBalance()>=amount) {
542                 account.setBalance(account.getBalance()-amount-b);
543             }
544             else if(account.getBalance()<amount&&(account.getBalance()-amount)>-50000&&account.getBalance()>=0) {
545                 account.setBalance(account.getBalance()+(account.getBalance()-amount)*0.05-amount-b);
546             }
547             else if(account.getBalance()<0&&(account.getBalance()-amount)>-50000) {
548                 account.setBalance(account.getBalance()-b-amount*1.05);
549             }
550             else {
551                 System.out.println("Sorry,your account balance is insufficient.");
552                 System.exit(0);
553             }
554             }
555         else {
556             if(account.getBalance()>=amount+b) {
557                 account.setBalance(account.getBalance()-amount-b);
558             }
559             else {
560                 System.out.println("Sorry,your account balance is insufficient.");
561                 System.exit(0);
562             }
563         }
564         switch(aTM.getBank().getBankNO()) 
565         {
566         case "1001":
567                       a="中国建设银行";
568                     break;
569         case "1002":
570                      a="中国工商银行";
571                     
572                     break;
573         case "1003":
574                     a="中国农业银行";
575                     break;
576         }
577                 
578         if(amount >= 0) {
579             showResult(account,1,a);
580         }else {
581             showResult(account,0,a);
582         }
583         b = 0;
584         
585     }
586     
587     public void showResult(Account account,int flag,String a) {
588         String type = "";
589         if(flag == 1) {
590             type = "取款";            
591         }else {
592             type = "存款";
593             amount *= -1;
594         }
595         
596         String userName = account.getUser().getName();
597         String bankName = account.getBank().getBankName();
598         System.out.println("业务:"+type+" "+userName + "在" +
599                 a+ "的" + ATMID + "号ATM机上" + type + String.format("¥%.2f", amount));
600                 System.out.println("当前余额为" + String.format("¥%.2f", account.getBalance()));
601     }
602 }
603 
604 class ValidateData {
605     /**
606      * 校验卡号是否存在
607      * @param unionPay
608      * @param cardNO
609      * @return
610      */
611     public static Card getCardbyCardNO(UnionPay unionPay,String cardNO) {
612         Card card = null;
613         Iterator<Bank> bankItr = unionPay.getBankList().iterator();
614         
615         while(bankItr.hasNext()) {
616             ArrayList<Account> accountList = bankItr.next().getAccountList();
617             Iterator<Account> accountItr = accountList.iterator();
618             while(accountItr.hasNext()) {
619                 ArrayList<Card> cardList = accountItr.next().getList();
620                 Iterator<Card> cardItr = cardList.iterator();
621                 while(cardItr.hasNext()) {
622                     card = cardItr.next();
623                     if(card.getCardNO().equals(cardNO)) {
624                         return card;
625                     }
626                 }
627             }
628         }
629         return null;
630     }
631     
632     /**
633      * 校验ATM ID是否存在
634      * @param unionPay
635      * @param ATMID
636      * @return
637      */
638     public static ATM getATMbyATMID(UnionPay unionPay,String ATMID) {
639         Iterator<Bank> bankItr = unionPay.getBankList().iterator();
640         Bank bank = null;
641         ATM aTM = null;
642         
643         while(bankItr.hasNext()) {
644             bank = bankItr.next();
645             Iterator<ATM> aTMItr = bank.getATMList().iterator();    
646             
647             while(aTMItr.hasNext()) {
648                 aTM = aTMItr.next();
649                 if(aTM.getATMID().equals(ATMID)) {
650                     return aTM;
651                 }
652             }            
653         }        
654         return null;    
655     }
656 }
657 
658 class UnionPay {
659     private ArrayList<Bank> bankList = new ArrayList<Bank>();
660 
661     public UnionPay() {
662         super();
663         // TODO Auto-generated constructor stub
664     }
665 
666     public UnionPay(ArrayList<Bank> bankList) {
667         super();
668         this.bankList = bankList;
669     }
670 
671     public ArrayList<Bank> getBankList() {
672         return bankList;
673     }
674 
675     public void setBankList(ArrayList<Bank> bankList) {
676         this.bankList = bankList;
677     }
678     
679     public void addBank(Bank bank) {
680         this.bankList.add(bank);
681     }
682     
683     public void removeBank(Bank bank) {
684         this.bankList.remove(bank);
685     }    
686 }
687 
688 class GetBalance {
689     private UnionPay unionPay;
690     
691     public GetBalance() {
692         super();
693         // TODO Auto-generated constructor stub
694     }
695 
696     public GetBalance(UnionPay unionPay) {
697         super();
698         this.unionPay = unionPay;
699     }
700 
701     public double getBalance(String cardNO) {
702         return ValidateData.getCardbyCardNO(unionPay, cardNO).getAccount().getBalance();
703     }
704 }

 

 

采坑心得

1.在习题集7-1中一开始我并没有打算用动态数组,而是用一个字符串数组,但由于无法得知要输入的数据有多大,所以得到的分基本没有,但后来改成了

才好了很多,通过list.add()方法可轻松将数据存入。

 

 

 

  1. 在对习题集8的输入中,总出现了数据丢失去的情况。后面发现是我的正则表达式用的不最缺,由于我不知道数据间到底有几个空格,但却被我认为式一个空额,所以结果一直错

开始代码是

 

 

 

 

后改为

 

 

 

  1. 在对习题集9的算法计算中,我对银行的输出一直都是中国建设银行,这是在前面我就将数据默认,后面没有对其修改,最后改为

直接将判断的数据存入一个字符串中,后输出。

改进建议

  1. 由于题目量非常的大,所以我不应该直接上手,应该先对题目进行分析
  2. 对于抽象类的理解不足,后续应该继续改进。
  3. 正则表达式不能很灵活的运用,要多记住。

总结

这几次作业是本学期的最后几次,所以难度都比较大,需要花费大量的时间来完成,所以应该拥有调整自己的心态。另外后面两题考察的都很全面,考察了本课程的最重要的东西,面向对象,要更加认真的完成大作业了。

posted @ 2021-06-20 21:44  把你印在脑子里  阅读(75)  评论(0)    收藏  举报