第三阶段Blog作业

Posted on 2021-06-20 13:22  妖怪写代码  阅读(72)  评论(0)    收藏  举报

l  设计与分析

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

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

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

输入格式:

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

输出格式:

  • 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边不能组成三角形),则输出Wrong Format
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为图形名称1:面积值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.Scanner;
import java.util.TreeSet;

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");
                System.exit(0);
            }
            list.add(num);
            num = input.nextInt();
        }
        DealCardList dealCardList = new DealCardList(list);
        if(!dealCardList.validate()){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
    }
}

class DealCardList{
    ArrayList<Card> cardList=new ArrayList<>();

    public DealCardList() {
    }

    public DealCardList(ArrayList<Integer> card) {
        for (Integer integer : card) {
            if (integer==0)break;
            switch (integer){
                case 1:
                    Card card1=new Card(new Circle(Main.input.nextDouble()));
                    card1.getShape().setShapeName("Circle");
                    cardList.add(card1);
                    break;
                case 2:
                    Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
                    card2.getShape().setShapeName("Rectangle");
                    cardList.add(card2);
                    break;
                case 3:
                    Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card3.getShape().setShapeName("Triangle");
                    cardList.add(card3);
                    break;
                case 4:
                    Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card4.getShape().setShapeName("Trapezoid");
                    cardList.add(card4);
                    break;
            }

        }
    }
    public boolean validate(){
        boolean ret=true;
        for (Card card : cardList) {
            if (!card.getShape().validate()){
                ret=false;
                break;
            }
        }
        return ret;
    }
    public void cardSort(){
        TreeSet<Card> cards = new TreeSet<>(cardList);
        for (Card card : cards) {
            System.out.print(card.getShape());
        }
    }
    public double getAllArea(){
        double sum=0;
        for (Card card : cardList) {
            sum+=card.getShape().getArea();
        }
        return sum;
    }
    public void showResult(){
        System.out.println("The original list:");
        for (Card card : cardList) {
            System.out.print(card.getShape());
        }
        System.out.println();
        System.out.println("The sorted list:");
        cardSort();
        System.out.println();
        System.out.printf("Sum of area:%.2f\n",getAllArea());
    }
}
class Card implements Comparable<Card>{
    private Shape shape;

    public Card() {
    }

    public Card(Shape shape) {
        this.shape = shape;
    }

    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    @Override
    public int compareTo(Card card) {
        return -(int)(shape.getArea()-card.getShape().getArea());
    }
}

abstract class Shape{
    private String shapeName;

    public Shape() {
    }

    public Shape(String shapeName) {
        this.shapeName = shapeName;
    }

    public String getShapeName() {
        return shapeName;
    }

    public void setShapeName(String shapeName) {
        this.shapeName = shapeName;
    }
    public abstract double getArea();
    public abstract boolean validate();

    
    @Override
    public String toString() {
        return getShapeName()+":"+String.format("%.2f ",getArea());
    }
}

class Circle extends Shape{
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI*radius*radius;
    }

    @Override
    public boolean validate() {
        return this.radius>0;
    }
}

class Rectangle extends Shape{
    private double width,height;

    public Rectangle() {
    }

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double getArea() {
        return height*width;
    }

    @Override
    public boolean validate() {
        return width>0&&height>0;
    }
}

class Triangle extends Shape{
    private double side1,side2,side3;

    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    @Override
    public double getArea() {
        double p=(side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }

    @Override
    public boolean validate() {
        boolean ret=true;
        if (!(side1>0&&side3>0&&side2>0))ret=false;
        else{
            if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1))ret=false;
        }
        return ret;
    }
}

class Trapezoid extends Shape{
    private double topSide,bottomSide,height;

    public Trapezoid() {
    }

    public Trapezoid(double topSide, double bottomSide, double height) {
        this.topSide = topSide;
        this.bottomSide = bottomSide;
        this.height = height;
    }

    @Override
    public double getArea() {
        return (topSide+bottomSide)*height/2;
    }

    @Override
    public boolean validate() {
        return topSide>0&&height>0&&bottomSide>0;
    }
    private static void insertionSort(int[] list) {
          for (int i = 1;i < list.length;i++)
            {
                int num = list[i];
                int j = i - 1;
                for (;j >= 0 && list[j] > num;j--)
                {
                    list[j + 1] = list[j];
                }
                list[j + 1] = num;
            }

         
            }
            
      private static void selectionSort(int[] list) {
          for(int j=0;j<list.length-1;j++)
            {
                for(int i=j+1;i<list.length;i++)
                {
                    if(list[j]>list[i])
                    {
                        int t=list[j];
                        list[j]=list[i];
                        list[i]=t;
                    }
                  }
            }
            
        }
      private static void bubbleSort(int[] list) {
          for(int j=0;j<list.length-1;j++)
            {
                for(int i=0;i<list.length-1-j;i++)
                {
                    if(list[i]>list[i+1])
                    {
                        int t=list[i];
                        list[i]=list[i+1];
                        list[i+1]=t;
                    }
                }
            }
            
        }

}
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

源代码如下:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

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");
                System.exit(0);
            }
            list.add(num);
            num = input.nextInt();
        }
        DealCardList dealCardList = new DealCardList(list);
        if(!dealCardList.validate()){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        dealCardList.showResult();
        input.close();
    }
}

class DealCardList{
    ArrayList<Card> cardList=new ArrayList<>();

    public DealCardList() {
    }

    public DealCardList(ArrayList<Integer> card) {
        for (Integer integer : card) {
            if (integer==0)break;
            switch (integer){
                case 1:
                    Card card1=new Card(new Circle(Main.input.nextDouble()));
                    card1.getShape().setShapeName("Circle");
                    cardList.add(card1);
                    break;
                case 2:
                    Card card2=new Card(new Rectangle(Main.input.nextDouble(),Main.input.nextDouble()));
                    card2.getShape().setShapeName("Rectangle");
                    cardList.add(card2);
                    break;
                case 3:
                    Card card3=new Card(new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card3.getShape().setShapeName("Triangle");
                    cardList.add(card3);
                    break;
                case 4:
                    Card card4=new Card(new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble()));
                    card4.getShape().setShapeName("Trapezoid");
                    cardList.add(card4);
                    break;
            }

        }
    }
    public boolean validate(){
        boolean ret=true;
        for (Card card : cardList) {
            if (!card.getShape().validate()){
                ret=false;
                break;
            }
        }
        return ret;
    }
    public void cardSort(){
        TreeSet<Card> cards = new TreeSet<>(cardList);
        for (Card card : cards) {
            System.out.print(card.getShape());
        }
    }
    public double getAllArea(){
        double sum=0;
        for (Card card : cardList) {
            sum+=card.getShape().getArea();
        }
        return sum;
    }
    public void showResult(){
        System.out.println("The original list:");
        for (Card card : cardList) {
            System.out.print(card.getShape());
        }
        System.out.println();
        System.out.println("The sorted list:");
        cardSort();
        System.out.println();
        System.out.printf("Sum of area:%.2f\n",getAllArea());
    }
}
class Card implements Comparable<Card>{
    private Shape shape;

    public Card() {
    }

    public Card(Shape shape) {
        this.shape = shape;
    }

    public Shape getShape() {
        return shape;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    @Override
    public int compareTo(Card card) {
        return -(int)(shape.getArea()-card.getShape().getArea());
    }
}

abstract class Shape{
    private String shapeName;

    public Shape() {
    }

    public Shape(String shapeName) {
        this.shapeName = shapeName;
    }

    public String getShapeName() {
        return shapeName;
    }

    public void setShapeName(String shapeName) {
        this.shapeName = shapeName;
    }
    public abstract double getArea();
    public abstract boolean validate();

    
    @Override
    public String toString() {
        return getShapeName()+":"+String.format("%.2f ",getArea());
    }
}

class Circle extends Shape{
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    @Override
    public double getArea() {
        return Math.PI*radius*radius;
    }

    @Override
    public boolean validate() {
        return this.radius>0;
    }
}

class Rectangle extends Shape{
    private double width,height;

    public Rectangle() {
    }

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public double getArea() {
        return height*width;
    }

    @Override
    public boolean validate() {
        return width>0&&height>0;
    }
}

class Triangle extends Shape{
    private double side1,side2,side3;

    public Triangle() {
    }

    public Triangle(double side1, double side2, double side3) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    @Override
    public double getArea() {
        double p=(side1+side2+side3)/2;
        return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
    }

    @Override
    public boolean validate() {
        boolean ret=true;
        if (!(side1>0&&side3>0&&side2>0))ret=false;
        else{
            if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1))ret=false;
        }
        return ret;
    }
}

class Trapezoid extends Shape{
    private double topSide,bottomSide,height;

    public Trapezoid() {
    }

    public Trapezoid(double topSide, double bottomSide, double height) {
        this.topSide = topSide;
        this.bottomSide = bottomSide;
        this.height = height;
    }

    @Override
    public double getArea() {
        return (topSide+bottomSide)*height/2;
    }

    @Override
    public boolean validate() {
        return topSide>0&&height>0&&bottomSide>0;
    }
    private static void insertionSort(int[] list) {
          for (int i = 1;i < list.length;i++)
            {
                int num = list[i];
                int j = i - 1;
                for (;j >= 0 && list[j] > num;j--)
                {
                    list[j + 1] = list[j];
                }
                list[j + 1] = num;
            }

         
            }
            
      private static void selectionSort(int[] list) {
          for(int j=0;j<list.length-1;j++)
            {
                for(int i=j+1;i<list.length;i++)
                {
                    if(list[j]>list[i])
                    {
                        int t=list[j];
                        list[j]=list[i];
                        list[i]=t;
                    }
                  }
            }
            
        }
      private static void bubbleSort(int[] list) {
          for(int j=0;j<list.length-1;j++)
            {
                for(int i=0;i<list.length-1-j;i++)
                {
                    if(list[i]>list[i+1])
                    {
                        int t=list[i];
                        list[i]=list[i+1];
                        list[i+1]=t;
                    }
                }
            }
            
        }

}

        本两次习题集有着很强的递进关系,首先在习题集一中,题目的要求是让我们设计出程序使得完成对不同类的图片的面积进行排序和输出。在第二次的习题集中则让我们先将图形类进行分类,而后在分好类的图形组中进行各个图形面积的排序与输出

        题目一中我们使用ArrayList与否不影响题目的解决,但是题目集二我们必须使用Arraylist等集合框架才能分组储存几个图形类,之后再进行求面积和排序等工作

   解题思路:按部就班的用代码实现类图中规定的属性和方法,并通过不断的调试与分析,最终得出能通过所有测试点的代码提交即可。

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

 

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

设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf(https://images.ptausercontent.com/93fc7ad6-5e85-445a-a759-5790d0baab28.pdf)

输入格式:

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

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

输出格式:

①输入错误处理

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

②取款业务输出

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

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

当前余额为¥[金额]

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

③存款业务输出

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

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

当前余额为¥[金额]

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

④查询余额业务输出

¥[金额]

金额保留两位小数。

输入样例1:

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

6222081502001312390 88888888 06 -500.00
#
 

输出样例1:

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

张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
 

输入样例2:

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

6217000010041315709  88888888 02 3500.00
#
 

输出样例2:

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

杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥6500.00
 

输入样例3:

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

6217000010041315715
#
 

输出样例3:

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

¥10000.00
 

输入样例4:

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

6222081502001312390 88888888 06 -500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709  88888888 02 3500.00
6217000010041315715
#
 

输出样例4:

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

张无忌在中国工商银行的06号ATM机上存款¥500.00
当前余额为¥10500.00
韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
¥5000.00

源代码如下:

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

class Bank
{
    String bankname;
    ArrayList<String> ATMList;
    public Bank(String bankname,ArrayList<String> ATMList)
    {
        this.bankname=bankname;
        this.ATMList=ATMList;
    }

}
class Account
{
    private String name;
    ArrayList<Bank> banklist;
    Bank bank;
    private String account;
    private String password;
    private double balance;
    private  ArrayList<String> cardList;
    public Account(String name, ArrayList<Bank> banklist,Bank bank,String account,String password,double balance,
                   ArrayList<String> cardList)
    {
        this.bank=bank;
        this.banklist=banklist;
        this.name=name;
        this.account=account;
        this.password=password;
        this.balance=balance;
        this.cardList=cardList;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getAccount() {
        return account;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setCardList(ArrayList<String> cardList) {
        this.cardList = cardList;
    }

    public ArrayList<String> getCardList() {
        return cardList;
    }

}
class Check2 {
    ArrayList<Account> accountList;
    String card;

    public Check2(ArrayList<Account> accountList, String card) {
        this.accountList = accountList;
        this.card = card;
    }

    public boolean check() {
        int flag = 0;
        for (int j = 0; j < accountList.size(); j++) {
            for (int i = 0; i < accountList.get(j).getCardList().size(); i++) {
                if (card.compareTo(accountList.get(j).getCardList().get(i)) == 0)//卡号校验
                {
                    flag = 1;
                    break;
                }
            }
            if(flag==1)
                break;
        }
        if(flag==1)
            return true;
            else
        {
            System.out.println("Sorry,this card does not exist.");
            return false;

        }
    }
}
class Check1
{
    ArrayList<Account> accountList;
    String card;
    String password;
    String number;
    double money;
    public Check1(ArrayList<Account> accountList,String card, String password,String number,double money)
    {
       this.accountList=accountList;
       this.card=card;
       this.password=password;
       this.number=number;
       this.money=money;
    }
    public boolean check()
    {
        int flag=0;
        int t = 0;
        //System.out.println(accountList.size());
        for(int j=0;j<accountList.size();j++) {
            for (int i = 0; i < accountList.get(j).getCardList().size(); i++) {
                if (card.compareTo(accountList.get(j).getCardList().get(i)) == 0)//卡号校验
                {
                    flag = 1;
                    t=j;
                    break;

                }
            }
            if(flag==1)
                break;
        }
        if(flag==1)
        {
             //password=password.replaceAll(" ","");
            if(password.compareTo(accountList.get(t).getPassword())==0)//密码校验
            {
                flag=2;
            }
            else {
                System.out.println("Sorry,your password is wrong.");
                return false;
            }

        }
        else
        {
            System.out.println("Sorry,this card does not exist.");
            return false;

        }

        if(flag==2)
        {
            for(int j=0;j<accountList.get(t).banklist.size();j++) {
                for (int i = 0; i < accountList.get(t).banklist.get(j).ATMList.size(); i++)     //ATM 机编号校验
                    if (number.compareTo(accountList.get(t).banklist.get(j).ATMList.get(i)) == 0) {
                        flag = 3;
                        break;
                    }
            }
        }
        if(flag==3)
        {
            if(money<=accountList.get(t).getBalance())   //金额校验
            {
                flag=4;
            }
            else {
                System.out.println("Sorry,your account balance is insufficient.");
                return false;
            }

        }
        else
        {
            System.out.println("Sorry,the ATM's id is wrong.");
            return false;

        }
        if(flag==4)
        {
            for(int i=0;i<accountList.get(t).bank.ATMList.size();i++) {
                        if (number.compareTo(accountList.get(t).bank.ATMList.get(i)) == 0) {
                            flag = 5;
                            break;
                    }
                }
        }
        if(flag!=5) {
            System.out.println("Sorry,cross-bank withdrawal is not supported.");
            return false;
        }
            else
                return true;
        }

}
class Show
{
    ArrayList<Account> accountList;
    String card;
    public Show(ArrayList<Account> accountList,String card)
    {
        this.accountList=accountList;
        this.card=card;
    }
    public void show()
    {
        DecimalFormat df = new DecimalFormat("#.00");
        int t=0;
        for(int j=0;j<accountList.size();j++) {
            for (int i = 0; i < accountList.get(j).getCardList().size(); i++) {
                if (card.compareTo(accountList.get(j).getCardList().get(i)) == 0)//卡号校验
                {
                    t=j;
                    break;

                }
            }
        }
        System.out.println("¥"+df.format(accountList.get(t).getBalance()));
    }

}
class Access
{
    ArrayList<Account> accountList;
    String card;
    String password;
    String number;
    double money;
    public Access(ArrayList<Account> accountList,String card, String password,String number,double money)
    {
        this.password=password;
        this.number=number;
        this.card=card;
        this.accountList=accountList;
        this.money=money;
    }

    public void access()
    {
        int t=0;
        for(int j=0;j<accountList.size();j++) {
            for (int i = 0; i < accountList.get(j).getCardList().size(); i++) {
                if (card.compareTo(accountList.get(j).getCardList().get(i)) == 0)//卡号校验
                {
                    t=j;
                    break;

                }
            }
        }
        if(money>=0)//取款
        {
            accountList.get(t).setBalance(accountList.get(t).getBalance()-money);
        }
        else//存款
        {
            accountList.get(t).setBalance(accountList.get(t).getBalance()-money);
        }
    }
    public void show()
    {
        DecimalFormat df = new DecimalFormat("#.00");
        int t=0;
        for(int j=0;j<accountList.size();j++) {
            for (int i = 0; i < accountList.get(j).getCardList().size(); i++) {
                if (card.compareTo(accountList.get(j).getCardList().get(i)) == 0)//卡号校验
                {
                    t=j;
                    break;

                }
            }
        }
        if(money>=0)//取款
        {
            System.out.println(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上取款¥"+df.format(money));
            if(accountList.get(t).getBalance()==0)
                System.out.println("当前余额为¥0.00");
            else
                System.out.println("当前余额为¥"+df.format(accountList.get(t).getBalance()));
        }
        else
        {
            money=-money;
            System.out.println(accountList.get(t).getName()+"在"+accountList.get(t).bank.bankname+"的"+number+"号ATM机上存款¥"+df.format(money));
            System.out.println("当前余额为¥"+df.format(accountList.get(t).getBalance()));
        }
    }
}



public class Main {
    public static void main(String[] args) {
        ArrayList<String> ATMList1 = new ArrayList<String>();
        ATMList1.add("01");
        ATMList1.add("02");
        ATMList1.add("03");
        ATMList1.add("04");
        Bank bankjian = new Bank("中国建设银行", ATMList1);
        ArrayList<String> ATMList2 = new ArrayList<String>();
        ATMList2.add("05");
        ATMList2.add("06");
        Bank bankgong = new Bank("中国工商银行", ATMList2);

        ArrayList<Bank> bankList = new ArrayList<Bank>();
        bankList.add(bankjian);
        bankList.add(bankgong);

        ArrayList<String> cardList1 = new ArrayList<String>();
        cardList1.add("6217000010041315709");
        cardList1.add("6217000010041315715");
        Account account1 = new Account("杨过", bankList, bankjian, "3217000010041315709", "88888888", 10000.00, cardList1);

        ArrayList<String> cardList2 = new ArrayList<String>();
        cardList2.add("6217000010041315718");
        Account account2 = new Account("杨过", bankList, bankjian, "3217000010041315715", "88888888", 10000.00, cardList2);

        ArrayList<String> cardList3 = new ArrayList<String>();
        cardList3.add("6217000010051320007");
        Account account3 = new Account("郭靖", bankList, bankjian, "3217000010051320007", "88888888", 10000.00, cardList3);

        ArrayList<String> cardList4 = new ArrayList<String>();
        cardList4.add("6222081502001312389");
        Account account4 = new Account("张无忌", bankList, bankgong, "3222081502001312389", "88888888", 10000.00, cardList4);

        ArrayList<String> cardList5 = new ArrayList<String>();
        cardList5.add("6222081502001312390");
        Account account5 = new Account("张无忌", bankList, bankgong, "3222081502001312390", "88888888", 10000.00, cardList5);

        ArrayList<String> cardList6 = new ArrayList<String>();
        cardList6.add("6222081502001312399");
        cardList6.add("6222081502001312400");
        Account account6 = new Account("张无忌", bankList, bankgong, "3222081502001312399", "88888888", 10000.00, cardList6);

        ArrayList<String> cardList7 = new ArrayList<String>();
        cardList7.add("6222081502051320785");
        Account account7 = new Account("韦小宝", bankList, bankgong, "3222081502051320785", "88888888", 10000.00, cardList7);

        ArrayList<String> cardList8 = new ArrayList<String>();
        cardList8.add("6222081502051320786");
        Account account8 = new Account("韦小宝", bankList, bankgong, "3222081502051320786", "88888888", 10000.00, cardList8);

        ArrayList<Account> accountList = new ArrayList<Account>();
        accountList.add(account1);
        accountList.add(account2);
        accountList.add(account3);
        accountList.add(account4);
        accountList.add(account5);
        accountList.add(account6);
        accountList.add(account7);
        accountList.add(account8);
        Scanner in = new Scanner(System.in);
        //卡号 密码 ATM 机编号 金额
        String abc = " ";
        Check1 check1 = null;
        Check2 check2 = null;
        abc=in.nextLine();
        while(abc.compareTo("#")!=0) {
            String[] shuju=abc.split(" ");
            ArrayList<String> list = new ArrayList<String>();
            for(int i =0;i<shuju.length;i++)
            {
                if(shuju[i].compareTo("")==0)
                {
                    for(int j=i;j< shuju.length-1;j++)
                    {
                        shuju[j]=shuju[j+1];
                    }
                }
            }
            for(int i=0;i<shuju.length;i++)
            {
                if(shuju[i].compareTo("")==0)
                    break;
                else
                list.add(shuju[i]);
            }

            if(list.size()!=1) {
                //System.out.println("ok");
                double money = Double.valueOf(shuju[3]);
                check1 = new Check1(accountList, list.get(0), list.get(1), list.get(2), money);
                if (check1.check()) {
                    Access access = new Access(accountList, list.get(0), list.get(1), list.get(2), money);
                    access.access();
                    access.show();
                }
            }
            else if(list.size()==1)
            {
                check2 = new Check2(accountList,list.get(0));
                if(check2.check())
                {
                    Show show= new Show(accountList, list.get(0));
                    show.show();
                }

            }
            abc=in.nextLine();
        }
    }
    private static void insertionSort(int[] list) {
          for (int i = 1;i < list.length;i++)
            {
                int num = list[i];
                int j = i - 1;
                for (;j >= 0 && list[j] > num;j--)
                {
                    list[j + 1] = list[j];
                }
                list[j + 1] = num;
            }

         
            }
            
      private static void selectionSort(int[] list) {
          for(int j=0;j<list.length-1;j++)
            {
                for(int i=j+1;i<list.length;i++)
                {
                    if(list[j]>list[i])
                    {
                        int t=list[j];
                        list[j]=list[i];
                        list[i]=t;
                    }
                  }
            }
            
        }
      private static void bubbleSort(int[] list) {
          for(int j=0;j<list.length-1;j++)
            {
                for(int i=0;i<list.length-1-j;i++)
                {
                    if(list[i]>list[i+1])
                    {
                        int t=list[i];
                        list[i]=list[i+1];
                        list[i+1]=t;
                    }
                }
            }
            
        }
class DateUtil {
    private int year;
    private int month;
    private int day;

    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public DateUtil(){}

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }
}
}

本题要求编写一个银行ATM机的模拟程序能够完成用户的存款、取款以及查询余额功能,需要分别设计银行类Bank,用户类User,卡类card,银联类UnionPay,机器类ATM,账户类Account之后设计好他们之间类与类之间的关系,其中以关联为主。

 

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

 

设计ATM仿真系统,具体要求参见作业说明。 OO作业9-1题目说明.pdf,在作业说明中我们可以得知本次的设计需要我们根据账户的不同,银行卡一般分为借记卡针对借记账户和信用卡针对贷记账户两类其中信用卡可以透支消费,但是透支消费会额外扣除账户里的金额

此外,还有跨行取款这一新操作,也是需要额外收费的业务

 

输入格式:

 

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

 

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

 

输出格式:

 

①输入错误处理

 

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

 

②取款业务输出

 

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

 

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

 

当前余额为¥[金额]

 

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

 

③查询余额业务输出

 

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

 

金额保留两位小数。

 

输入样例1:

 

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

 

6222081502001312390 88888888 06 500.00
#

 

 

 

输出样例1:

 

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

 

业务:取款 张无忌在中国工商银行的06号ATM机上取款¥500.00
当前余额为¥9500.00

 

 

 

输入样例2:

 

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

 

6217000010041315709  88888888 06 3500.00
#

 

 

 

输出样例2:

 

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

 

业务:取款 杨过在中国工商银行的06号ATM机上取款¥3500.00
当前余额为¥6395.00

 

 

 

输入样例3:

 

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

 

6217000010041315715
#

 

 

 

输出样例3:

 

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

 

业务:查询余额 ¥10000.00

 

 

 

输入样例4:

 

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

 

6222081502001312390 88888888 01 500.00
6222081502051320786 88888888 06 1200.00
6217000010041315715 88888888 02 1500.00
6217000010041315709  88888888 02 3500.00
6217000010041315715
#

 

 

 

输出样例4:

 

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

 

业务:取款 张无忌在中国建设银行的01号ATM机上取款¥500.00
当前余额为¥9490.00
业务:取款 韦小宝在中国工商银行的06号ATM机上取款¥1200.00
当前余额为¥8800.00
业务:取款 杨过在中国建设银行的02号ATM机上取款¥1500.00
当前余额为¥8500.00
业务:取款 杨过在中国建设银行的02号ATM机上取款¥3500.00
当前余额为¥5000.00
业务:查询余额 ¥5000.00

 

 

 

输入样例5:

 

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

 

6640000010045442002 88888888 09 3000
6640000010045442002 88888888 06 8000
6640000010045442003 88888888 01 10000
6640000010045442002
#

 

 

 

输出样例5:

 

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

 

业务:取款 张三丰在中国农业银行的09号ATM机上取款¥3000.00
当前余额为¥6880.00
业务:取款 张三丰在中国工商银行的06号ATM机上取款¥8000.00
当前余额为¥-1416.00
业务:取款 张三丰在中国建设银行的01号ATM机上取款¥10000.00
当前余额为¥-11916.00
业务:查询余额 ¥-11916.00

 

源代码如下:

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
class Bank
{
    String bankname;
    ArrayList < String > ATMList;
    public Bank(String bankname, ArrayList < String > ATMList)
    {
        this.bankname = bankname;
        this.ATMList = ATMList;
    }
}
class Account
{
    private String name;
    ArrayList < Bank > banklist;
    Bank bank;
    private String account;
    private String type;
    private String password;
    private double balance;
    private ArrayList < String > cardList;
    public Account(String name, ArrayList < Bank > banklist, Bank bank, String account, String type, String password, double balance, ArrayList < String > cardList)
    {
        this.bank = bank;
        this.banklist = banklist;
        this.name = name;
        this.account = account;
        this.password = password;
        this.balance = balance;
        this.cardList = cardList;
        this.type = type;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
    public void setAccount(String account)
    {
        this.account = account;
    }
    public String getAccount()
    {
        return account;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getPassword()
    {
        return password;
    }
    public void setBalance(double balance)
    {
        this.balance = balance;
    }
    public double getBalance()
    {
        return balance;
    }
    public void setCardList(ArrayList < String > cardList)
    {
        this.cardList = cardList;
    }
    public ArrayList < String > getCardList()
    {
        return cardList;
    }
    public void setType(String type)
    {
        this.type = type;
    }
    public String getType()
    {
        return type;
    }
}
class Check2
{
    ArrayList < Account > accountList;
    String card;
    public Check2(ArrayList < Account > accountList, String card)
    {
        this.accountList = accountList;
        this.card = card;
    }
    public boolean check()
    {
        int flag = 0;
        for(int j = 0; j < accountList.size(); j++)
        {
            for(int i = 0; i < accountList.get(j).getCardList().size(); i++)
            {
                if(card.compareTo(accountList.get(j).getCardList().get(i)) == 0) //卡号校验
                {
                    flag = 1;
                    break;
                }
            }
            if(flag == 1) break;
        }
        if(flag == 1) return true;
        else
        {
            System.out.println("Sorry,this card does not exist.");
            return false;
        }
    }
}
class Check1
{
    ArrayList < Account > accountList;
    String card;
    String password;
    String number;
    double money;
    public Check1(ArrayList < Account > accountList, String card, String password, String number, double money)
    {
        this.accountList = accountList;
        this.card = card;
        this.password = password;
        this.number = number;
        this.money = money;
    }
    public boolean check()
    {
        int flag = 0;
        int t = 0;
        //System.out.println(accountList.size());
        for(int j = 0; j < accountList.size(); j++)
        {
            for(int i = 0; i < accountList.get(j).getCardList().size(); i++)
            {
                if(card.compareTo(accountList.get(j).getCardList().get(i)) == 0) //卡号校验
                {
                    flag = 1;
                    t = j;
                    break;
                }
            }
            if(flag == 1) break;
        }
        if(flag == 1)
        {
            //password=password.replaceAll(" ","");
            if(password.compareTo(accountList.get(t).getPassword()) == 0) //密码校验
            {
                flag = 2;
            }
            else
            {
                System.out.println("Sorry,your password is wrong.");
                return false;
            }
        }
        else
        {
            System.out.println("Sorry,this card does not exist.");
            return false;
        }
        if(flag == 2)
        {
            for(int j = 0; j < accountList.get(t).banklist.size(); j++)
            {
                for(int i = 0; i < accountList.get(t).banklist.get(j).ATMList.size(); i++) //ATM 机编号校验
                    if(number.compareTo(accountList.get(t).banklist.get(j).ATMList.get(i)) == 0)
                {
                    flag = 3;
                    break;
                }
            }
        }
        if(flag == 3)
        {
            if(accountList.get(t).getType().compareTo("借记账号") == 0)
            {
                if(money <= accountList.get(t).getBalance()) //金额校验
                {
                    flag = 4;
                }
                else
                {
                    System.out.println("Sorry,your account balance is insufficient.");
                    return false;
                }
            }
            else if(accountList.get(t).getType().compareTo("贷记账号") == 0)
            {
                if(money <= accountList.get(t).getBalance() + 50000) //金额校验
                {
                    flag = 4;
                }
                else
                {
                    System.out.println("Sorry,your account balance is insufficient.");
                    return false;
                }
            }
        }
        else
        {
            System.out.println("Sorry,the ATM's id is wrong.");
            return false;
        }
        return true;
    }
}
class Show
{
    ArrayList < Account > accountList;
    String card;
    public Show(ArrayList < Account > accountList, String card)
    {
        this.accountList = accountList;
        this.card = card;
    }
    public void show()
    {
        DecimalFormat df = new DecimalFormat("#.00");
        int t = 0;
        for(int j = 0; j < accountList.size(); j++)
        {
            for(int i = 0; i < accountList.get(j).getCardList().size(); i++)
            {
                if(card.compareTo(accountList.get(j).getCardList().get(i)) == 0) //卡号校验
                {
                    t = j;
                    break;
                }
            }
        }
        System.out.println("业务:查询余额" + " " + "¥" + df.format(accountList.get(t).getBalance()));
    }
}
class Access
{
    DecimalFormat df = new DecimalFormat("#.00");
    ArrayList < Account > accountList;
    ArrayList < Bank > bankList;
    String name = null;
    String card;
    String password;
    String number;
    double money;
    int t = 0;
    int m = 0;
    public Access(ArrayList < Account > accountList, ArrayList < Bank > bankList, String card, String password, String number, double money)
    {
        this.password = password;
        this.number = number;
        this.card = card;
        this.accountList = accountList;
        this.money = money;
        this.bankList = bankList;
    }
    public void access()
    {
        for(int j = 0; j < accountList.size(); j++)
        {
            for(int i = 0; i < accountList.get(j).getCardList().size(); i++)
            {
                if(card.compareTo(accountList.get(j).getCardList().get(i)) == 0) //卡号校验
                {
                    t = j;
                    break;
                }
            }
        }
        int flag = 0;
        for(int i = 0; i < accountList.get(t).bank.ATMList.size(); i++)
        {
            if(number.compareTo(accountList.get(t).bank.ATMList.get(i)) == 0)
            {
                flag = 1; //不跨行
                name = accountList.get(t).bank.bankname;
                break;
            }
        }
        if(flag != 1) //跨行
        {
            for(int i = 0; i < bankList.size(); i++)
            {
                for(int j = 0; j < bankList.get(i).ATMList.size(); j++)
                {
                    if(number.compareTo(bankList.get(i).ATMList.get(j)) == 0)
                    {
                        name = bankList.get(i).bankname;
                        flag = 2;
                        break;
                    }
                }
                if(flag == 2) break;
            }
        }
        if(accountList.get(t).getType().compareTo("借记账号") == 0)
        {
            if(flag == 1)
            { //不跨行
                if(money >= 0) //取款
                {
                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                }
                else //存款
                {
                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                }
            }
            if(flag == 2) //跨行
            {
                if(name.compareTo("中国建设银行") == 0)
                {
                    if(money >= 0) //取款
                    {
                        if(accountList.get(t).getBalance() - money - money * 0.02 < 0)
                        {
                            System.out.println("Sorry,your account balance is insufficient.");
                            m = 1;
                        }
                        else
                        {
                            accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.02);
                        }
                    }
                    else //存款
                    {
                        accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                    }
                }
                else if(name.compareTo("中国工商银行") == 0)
                {
                    if(money >= 0) //取款
                    {
                        if(accountList.get(t).getBalance() - money - money * 0.03 < 0)
                        {
                            System.out.println("Sorry,your account balance is insufficient.");
                            m = 1;
                        }
                        else
                        {
                            accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.03);
                        }
                    }
                    else //存款
                    {
                        accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                    }
                }
                else if(name.compareTo("中国农业银行") == 0)
                {
                    if(money >= 0) //取款
                    {
                        if(accountList.get(t).getBalance() - money - money * 0.04 < 0)
                        {
                            System.out.println("Sorry,your account balance is insufficient.");
                            m = 1;
                        }
                        else
                        {
                            accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.04);
                        }
                    }
                    else //存款
                    {
                        accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                    }
                }
            }
        }
        else if(accountList.get(t).getType().compareTo("贷记账号") == 0)
        {
            if(flag == 1)
            { //不跨行
                if(money >= 0) //取款
                {
                    if(money <= accountList.get(t).getBalance())
                    { //未透支
                        if(accountList.get(t).getBalance() - money < -50000)
                        {
                            System.out.println("Sorry,your account balance is insufficient.");
                            m = 1;
                        }
                        else
                        {
                            accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                        }
                    }
                    else
                    { //透支
                        if(accountList.get(t).getBalance() >= 0)
                        {
                            if(accountList.get(t).getBalance() - money - (money - accountList.get(t).getBalance()) * 0.05 < -50000)
                            {
                                System.out.println("Sorry,your account balance is insufficient.");
                                m = 1;
                            }
                            else
                            {
                                accountList.get(t).setBalance(accountList.get(t).getBalance() - money - (money - accountList.get(t).getBalance()) * 0.05);
                            }
                        }
                        else
                        {
                            if(accountList.get(t).getBalance() - money - (money * 0.05) < -50000)
                            {
                                System.out.println("Sorry,your account balance is insufficient.");
                                m = 1;
                            }
                            else
                            {
                                accountList.get(t).setBalance(accountList.get(t).getBalance() - money - (money * 0.05));
                            }
                        }
                    }
                }
                else //存款
                {
                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                }
            }
            if(flag == 2) //跨行
            {
                if(name.compareTo("中国建设银行") == 0)
                {
                    if(money >= 0) //取款
                    {
                        if(money <= accountList.get(t).getBalance())
                        { //未透支
                            if(accountList.get(t).getBalance() - money - money * 0.02 < 0)
                            {
                                accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.02 - (money - accountList.get(t).getBalance()) * 0.05);
                            }
                            else accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.02);
                        }
                        else
                        { //透支
                            if(accountList.get(t).getBalance() >= 0)
                            {
                                if(accountList.get(t).getBalance() - money - money * 0.02 - (money - accountList.get(t).getBalance()) * 0.05 < -50000)
                                {
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    m = 1;
                                }
                                else
                                {
                                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.02 - (money - accountList.get(t).getBalance()) * 0.05);
                                }
                            }
                            else
                            {
                                if(accountList.get(t).getBalance() - money - money * 0.02 - (money * 0.05) < -50000)
                                {
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    m = 1;
                                }
                                else
                                {
                                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.02 - (money * 0.05));
                                }
                            }
                        }
                    }
                    else //存款
                    {
                        accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                    }
                }
                if(name.compareTo("中国工商银行") == 0)
                {
                    if(money >= 0) //取款
                    {
                        if(money <= accountList.get(t).getBalance())
                        { //未透支
                            if(accountList.get(t).getBalance() - money - money * 0.03 < 0)
                            {
                                accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.03 - (money - accountList.get(t).getBalance()) * 0.05);
                            }
                            else accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.03);
                        }
                        else
                        { //透支
                            if(accountList.get(t).getBalance() >= 0)
                                if(accountList.get(t).getBalance() - money - money * 0.03 - (money - accountList.get(t).getBalance()) * 0.05 < -50000)
                                {
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    m = 1;
                                }
                                else
                                {
                                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.03 - (money - accountList.get(t).getBalance()) * 0.05);
                                }
                            else
                            {
                                if(accountList.get(t).getBalance() - money - money * 0.03 - (money * 0.05) < -50000)
                                {
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    m = 1;
                                }
                                else
                                {
                                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.03 - (money * 0.05));
                                }
                            }
                        }
                    }
                    else //存款
                    {
                        accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                    }
                }
                if(name.compareTo("中国农业银行") == 0)
                {
                    if(money >= 0) //取款
                    {
                        if(money <= accountList.get(t).getBalance())
                        { //未透支
                            if(accountList.get(t).getBalance() - money - money * 0.04 < 0)
                            {
                                accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.04 - (money - accountList.get(t).getBalance()) * 0.05);
                            }
                            else accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.04);
                        }
                        else
                        { //透支
                            if(accountList.get(t).getBalance() >= 0)
                                if(accountList.get(t).getBalance() - money - money * 0.04 - (money - accountList.get(t).getBalance()) * 0.05 < -50000)
                                {
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    m = 1;
                                }
                                else
                                {
                                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.04 - (money - accountList.get(t).getBalance()) * 0.05);
                                }
                            else
                            {
                                if(accountList.get(t).getBalance() - money - money * 0.04 - (money * 0.05) < -50000)
                                {
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    m = 1;
                                }
                                else
                                {
                                    accountList.get(t).setBalance(accountList.get(t).getBalance() - money - money * 0.04 - (money * 0.05));
                                }
                            }
                        }
                    }
                    else //存款
                    {
                        accountList.get(t).setBalance(accountList.get(t).getBalance() - money);
                    }
                }
            }
        }
    }
    public void show()
    {
        if(m == 0)
        {
            if(money >= 0) //取款
            {
                System.out.println("业务:取款" + " " + accountList.get(t).getName() + "在" + name + "的" + number + "号ATM机上取款¥" + df.format(money));
                if(accountList.get(t).getBalance() == 0) System.out.println("当前余额为¥0.00");
                else System.out.println("当前余额为¥" + df.format(accountList.get(t).getBalance()));
            }
            else
            {
                money = -money;
                System.out.println("业务:取款" + " " + accountList.get(t).getName() + "在" + name + "的" + number + "号ATM机上存款¥" + df.format(money));
                System.out.println("当前余额为¥" + df.format(accountList.get(t).getBalance()));
            }
        }
    }
}
public class Main
{
    public static void main(String[] args)
    {
        ArrayList < String > ATMList1 = new ArrayList < String > ();
        ATMList1.add("01");
        ATMList1.add("02");
        ATMList1.add("03");
        ATMList1.add("04");
        Bank bankjian = new Bank("中国建设银行", ATMList1);
        ArrayList < String > ATMList2 = new ArrayList < String > ();
        ATMList2.add("05");
        ATMList2.add("06");
        Bank bankgong = new Bank("中国工商银行", ATMList2);
        ArrayList < String > ATMList3 = new ArrayList < String > ();
        ATMList3.add("07");
        ATMList3.add("08");
        ATMList3.add("09");
        ATMList3.add("10");
        ATMList3.add("11");
        Bank banknong = new Bank("中国农业银行", ATMList3);
        ArrayList < Bank > bankList = new ArrayList < Bank > ();
        bankList.add(bankjian);
        bankList.add(bankgong);
        bankList.add(banknong);
        ArrayList < String > cardList1 = new ArrayList < String > ();
        cardList1.add("6217000010041315709");
        cardList1.add("6217000010041315715");
        Account account1 = new Account("杨过", bankList, bankjian, "3217000010041315709", "借记账号", "88888888", 10000.00, cardList1);
        ArrayList < String > cardList2 = new ArrayList < String > ();
        cardList2.add("6217000010041315718");
        Account account2 = new Account("杨过", bankList, bankjian, "3217000010041315715", "借记账号", "88888888", 10000.00, cardList2);
        ArrayList < String > cardList3 = new ArrayList < String > ();
        cardList3.add("6217000010051320007");
        Account account3 = new Account("郭靖", bankList, bankjian, "3217000010051320007", "借记账号", "88888888", 10000.00, cardList3);
        ArrayList < String > cardList4 = new ArrayList < String > ();
        cardList4.add("6222081502001312389");
        Account account4 = new Account("张无忌", bankList, bankgong, "3222081502001312389", "借记账号", "88888888", 10000.00, cardList4);
        ArrayList < String > cardList5 = new ArrayList < String > ();
        cardList5.add("6222081502001312390");
        Account account5 = new Account("张无忌", bankList, bankgong, "3222081502001312390", "借记账号", "88888888", 10000.00, cardList5);
        ArrayList < String > cardList6 = new ArrayList < String > ();
        cardList6.add("6222081502001312399");
        cardList6.add("6222081502001312400");
        Account account6 = new Account("张无忌", bankList, bankgong, "3222081502001312399", "借记账号", "88888888", 10000.00, cardList6);
        ArrayList < String > cardList7 = new ArrayList < String > ();
        cardList7.add("6222081502051320785");
        Account account7 = new Account("韦小宝", bankList, bankgong, "3222081502051320785", "借记账号", "88888888", 10000.00, cardList7);
        ArrayList < String > cardList8 = new ArrayList < String > ();
        cardList8.add("6222081502051320786");
        Account account8 = new Account("韦小宝", bankList, bankgong, "3222081502051320786", "借记账号", "88888888", 10000.00, cardList8);
        ArrayList < String > cardList9 = new ArrayList < String > ();
        cardList9.add("6640000010045442002");
        cardList9.add("6640000010045442003");
        Account account9 = new Account("张三丰", bankList, bankjian, "3640000010045442002", "贷记账号", "88888888", 10000.00, cardList9);
        ArrayList < String > cardList10 = new ArrayList < String > ();
        cardList10.add("6640000010045441009");
        Account account10 = new Account("令狐冲", bankList, bankgong, "3640000010045441009", "贷记账号", "88888888", 10000.00, cardList10);
        ArrayList < String > cardList11 = new ArrayList < String > ();
        cardList11.add("6630000010033431001");
        Account account11 = new Account("乔峰", bankList, banknong, "3630000010033431001", "贷记账号", "88888888", 10000.00, cardList11);
        ArrayList < String > cardList12 = new ArrayList < String > ();
        cardList12.add("6630000010033431008");
        Account account12 = new Account("洪七公", bankList, banknong, "3630000010033431008", "贷记账号", "88888888", 10000.00, cardList12);
        ArrayList < Account > accountList = new ArrayList < Account > ();
        accountList.add(account1);
        accountList.add(account2);
        accountList.add(account3);
        accountList.add(account4);
        accountList.add(account5);
        accountList.add(account6);
        accountList.add(account7);
        accountList.add(account8);
        accountList.add(account9);
        accountList.add(account10);
        accountList.add(account11);
        accountList.add(account12);
        Scanner in = new Scanner(System.in);
        //卡号 密码 ATM 机编号 金额
        String abc = " ";
        Check1 check1 = null;
        Check2 check2 = null;
        abc = in .nextLine();
        while(abc.compareTo("#") != 0)
        {
            String[] shuju = abc.split(" ");
            ArrayList < String > list = new ArrayList < String > ();
            for(int i = 0; i < shuju.length; i++)
            {
                if(shuju[i].compareTo("") == 0)
                {
                    for(int j = i; j < shuju.length - 1; j++)
                    {
                        shuju[j] = shuju[j + 1];
                    }
                }
            }
            for(int i = 0; i < shuju.length; i++)
            {
                if(shuju[i].compareTo("") == 0) break;
                else list.add(shuju[i]);
            }
            if(list.size() != 1)
            {
                //System.out.println("ok");
                double money = Double.valueOf(shuju[3]);
                check1 = new Check1(accountList, list.get(0), list.get(1), list.get(2), money);
                if(check1.check())
                {
                    Access access = new Access(accountList, bankList, list.get(0), list.get(1), list.get(2), money);
                    access.access();
                    access.show();
                }
            }
            else if(list.size() == 1)
            {
                check2 = new Check2(accountList, list.get(0));
                if(check2.check())
                {
                    Show show = new Show(accountList, list.get(0));
                    show.show();
                }
            }
            abc = in .nextLine();
        }
    }
}

class DateUtil {
    private int year;
    private int month;
    private int day;

    public DateUtil(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public DateUtil(){}

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }
}

第二次的设计只需要我们对第一次的账户类进行修改,而后将atm中的取款操作进行改进使其实现透支取款和跨行取款的业务

解题思路:按部就班的用代码实现类图中规定的属性和方法,并通过不断的调试与分析,最终得出能通过所有测试点的代码提交即可。

踩坑心得

子类继承父类,如果重写了父类构造方法中包含的方法(如此次测试的init()方法),一定要注意,在子类中声明的变量(如此次测试的sonStr1和sonStr2),即使初始化了,仍会是默认值(此次为String类型,所以为null),如果子类声明的变量是和父类同名的变量,会以父类的为准。子类继承父类,只有重写方法有效,重写属性无效。在子类声明的变量,无论是否初始化了,在父类的构造方法阶段,都不能拿到子类声明的变量属性,只有执行到子类的构造方法,才能得到。因此一定要注意是不是做好空处理,不然就是崩溃问题

总结

通过以上的编程练习,进行了对JAVA语言的进一步学习。众所周知面向对象编程的三大核心是:封装,继承,多态。现在我对这三者都有所了解和实践,对其有一定的掌握,在面向对象的程序设计上又有了新的进步。所谓继承,即子类与父类,继承的设计可以极大的提高我们代码的复用性。而多态提高了代码的维护性(继承保证);提高了代码的扩展性