java程序设计实践

Java程序设计实践

Java训练集4~6总结与心得

训练集链接
前言: 本次训练集4~6主要考察了java基础知识的掌握,正则表达式的学习,类的聚合,继承与多态,主要更多是面对较大型的项目的解题思路,以及思考逻辑的锻炼,尤其是本次训练集六,在这方面的考察很多。
目录:

  • 训练集速析(主要分享解题心得)
  • 典例精析(详细的介绍设计以及分析过程)
  • 踩坑心得
  • 总结

训练集速析

训练集四

主要内容:对代码性能的优化,时间和空间上的考量。


题2:有重复的数据
在一大堆数据中找出重复的是一件经常要做的事情。现在,我们要处理许多整数,在这些整数中,可能存在重复的数据。

你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,输出“YES”这三个字母;如果没有,则输出“NO”。

输入格式:
你的程序首先会读到一个正整数n,n∈[1,100000],然后是n个整数。

输出格式:
如果这些整数中存在重复的,就输出:YES
否则,就输出:NO

此题的特点

  • 桶排序过不了
  • 用边插入边排序,内存超限
  • 排序一次,再循环查找,时间超限
  • 使用哈希表去重

故而:改善输入输出流是很关键的做法
对于输入输出流的探索:

  • 使用nextLine方法替换大部分的nextInt()方法,再将获取到的一行字符串拆分,变成整形。
    其中,nextLine对整行字符进行处理,检测是否有换行符,而当使用完nextInt方法后,并没有到下一行,如果此时使用nextLine方法,读取的依旧是同一行,这点要格外注意!

下面是使用哈希表去重的代码:

    public class Main{
        public static void main(String[]args){
            Scanner scanner=new Scanner(System.in);
            int inputNumber=scanner.nextInt();
            scanner.nextLine();//此处非常重要,因为上面一行还没读取完,如果直接用nextLine就会读到空字符串
            String string=scanner.nextLine();
            String []stringArray=string.split(" ");
            HashSet<Integer>hashSet=new HashSet<Integer>();
            for(int i=0;i<stringArray.length;i++){
                hashSet.add(Integer.parseInt(stringArray[i]));
            }
            if(hashSet.size()==stringArray.length)
                System.out.print("NO");
            else
                System.out.print("YES");
        }
    }

训练集五

主要内容:正则表达式练习,以及类的聚合。


题2.字符串训练-字符排序:
对输入的字符串中的字符进行排序并输出。

输入格式:
在一行内输入一个字符串。

输出格式:
对该字符串内的字符进行排序后(按ASCII码进行升序排序)输出。
解题方法:

  • 手写冒泡排序
  • 直接用Array自带的sort方法

代码如下:

public class Main{
public static void main(String []args){
    Scanner sc=new Scanner(System.in);
    String string=sc.nextLine();
    char[]chars=string.toCharArray();
    Arrays.sort(chars);
    for(char c:chars)
        System.out.print(c);
    }
}

拓展:

  • 区间排序
    sort(T[] a,int formIndex, int toIndex)

  • 自定义排序(此处为降序)

    1.使用Collections.reverseOrder()
    Integer[] arr2={1,4,3,6,7,9,11};
    Arrays.sort(arr2, Collections.reverseOrder());
    注意:不能用int这个类型了,要用Integer,不能使用基本类型。

    2.自定义比较器比较
    Integer[] arr2={1,4,3,6,7,9,11};
    Arrays.sort(arr2,new Comparator<Integer>(){
    public int compare(int o1,int o2){
    return o2-o1;
    })


题4.正则表达式训练-学号校验:
对软件学院2020级同学学号进行校验,学号共八位,规则如下:

1、2位:入学年份后两位,例如20年
3、4位:学院代码,软件学院代码为20
5位:方向代码,例如1为软件工程,7为物联网
6位:班级序号
7、8位:学号(序号)
要求如下:

只针对2020级
其中软件工程专业班级分别为:20201117、61,物联网工程专业班级为202071202073,数据科学与大数据专业班级为202081~82
每个班级学号后两位为01~40
输入格式:
在一行输入一个字符串。

输出格式:
若符合规则的学号,输出”正确“,若不符合,输出”错误“。
代码如下:

    public class Main{
    public static void main(String[]args){
        Scanner input= new Scanner(System.in);
        String string=input.nextLine();
        String regularExpression="^2020((1[1-7])|(61)|(7[1-3])|(8[1-2]))([1-3][1-9])|40&";
        if(string.matches(regularExpression))
            System.out.print("正确");
        else
            System.out.print("错误");
        }
    }

总结: 正则表达式中,[]最为常用,确定范围,{}确定个数,()用于确定不同情况,或者结合多种情况。


训练集六

主要内容:继承与多态练习,类的精确设计。


题2.继承与多态:
编写程序,实现图形类的继承,并定义相应类对象并进行测试。

类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积

类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积

类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积

注意:

每个类均有构造方法,
每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
输出的数值均保留两位小数,PI的取值使用Math.PI。
主类内的主方法中,主要实现两个功能(1-2):
从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积;
从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积;

主类内定义一个输出图形面积的方法,方法原型为:

public static void showArea(Shape shape);
各图形输出面积均要调用该方法(实现多态)

假如数据输入非法(包括圆、矩形对象的属性不大于0和输入选择值非1-2),系统输出Wrong Format

输入格式:
共四种合法输入

1 圆半径
2 矩形宽、长
输出格式:
按照以上需求提示依次输出

解析: 题目说的很清楚,这是一道典型的练习继承与多态的题目。
代码如下:

    import java.util.Scanner;
    public class Main {
    public static void main(String []args) {
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        Shape shape;
        switch (choice){
            case 1:
                double newRadius=input.nextDouble();
                if(newRadius<=0){
                    System.out.println("Wrong Format");
                    return;
                }
                shape=new Circle(newRadius);
                break;
            case 2:
                double newWidth=input.nextDouble(),newLength=input.nextDouble();
                if(newLength<=0||newWidth<=0){
                    System.out.println("Wrong Format");
                    return;
                }
                shape=new Rectangle(newWidth,newLength);
                break;
            default:
                System.out.println("Wrong Format");
                return;
        }
        shape.showShape();
        }
    }
    abstract class Shape {
        abstract void showShape();
        abstract double getArea();
    }
    class Circle extends Shape{
        private double radius;
        public double getArea(){
        return Math.PI*radius*radius;
        }

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

    public void showShape(){
        System.out.printf("%.2f\n",getArea());
        }

    public double getRadius() {
        return radius;
        }

    public void setRadius(double radius) {
        this.radius = radius;
        }
    }
    class Rectangle extends Shape{
        private double width;
        private double length;
        public void showShape(){
        System.out.printf("%.2f\n",getArea());
        }

        public double getWidth() {
            return width;
        }

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

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

        public double getLength() {
            return length;
        }

        public void setLength(double length) {
            this.length = length;
        }

        public double getArea(){
            return width*length;
        }
    }

典例精析


日期问题面向对象设计(聚合一)

参考题目7-2的要求,设计如下几个类:DateUtil、YearMonthDay,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

应用程序共测试三个功能:
求下n天
求前n天
求两个日期相差的天数

注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):

1 year month day n //测试输入日期的下n天
2 year month day n //测试输入日期的前n天
3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数

输出格式:
当输入有误时,输出格式如下:
Wrong Format
当第一个数字为1且输入均有效,输出格式如下:
year-month-day
当第一个数字为2且输入均有效,输出格式如下:
year-month-day
当第一个数字为3且输入均有效,输出格式如下:
天数值

下面将详细的介绍我的整个分析过程:

  1. 按照题目给出的类图,day继承了year,month,也就意味着每个day都有最全的信息 ,这个思想在训练集6的ATM机上也充分体现出来了。
  2. 与上次日期类设计不同的是,这次更充分体现了单一职责原则 ,尤其是每个year,month,day都自带了增减方法,所以计算间隔日期或计算前n天或后n天日期循环调用增减方法即可。
  3. isLeapYear方法,根据判断闰年的方法,返回相应的布尔值
  4. compareDates方法,按照年月日顺序分别比较即可。
  5. equalTwoDates方法,直接比较两个日期的年月日。
  6. showDate方法,这里值得注意的是,它返回的是String类型,所以要到主程序查看它是在什么情况下被调用的,以此写出合适的代码。
    下面是我的代码:
import java.util.Scanner;
public class Main {
    public static void main(String []args){
        Scanner input=new Scanner(System.in);
        int choice=input.nextInt();

        if (choice == 1) {
            int year = input.nextInt(), month = input.nextInt(), day = input.nextInt();
            int n = input.nextInt();
            DateUtil newDay = new DateUtil(year, month, day);
            if(!newDay.checkInputValid()){
                System.out.println("Wrong Format");
                return;
            }
            newDay = newDay.getNextDays(n);
            System.out.println(newDay.showDate());
        } else if (choice == 2) {
            int year = input.nextInt(), month = input.nextInt(), day = input.nextInt();
            int n = input.nextInt();
            DateUtil newDay = new DateUtil(year, month, day);
            if(!newDay.checkInputValid()){
                System.out.println("Wrong Format");
                return;
            }
            newDay = newDay.getPreviousDays(n);
            System.out.println(newDay.showDate());
        }else if(choice==3){
            int year = input.nextInt(), month = input.nextInt(), day = input.nextInt();
            DateUtil date1=new DateUtil(year,month,day);
            year = input.nextInt();
            month = input.nextInt();
            day = input.nextInt();
            DateUtil date2=new DateUtil(year,month,day);
             if(!date1.checkInputValid()||!date2.checkInputValid()){
                System.out.println("Wrong Format");
                return;
            }
            int result=Math.abs(date1.getDaysOfDate(date2));
            System.out.println(result);
        }
        else{
            System.out.println("Wrong Format");
        }
    }
}
//后续的代码,基础代码部分是直接用powerdesigner里的代码生成方法直接生成的,其它的是自己写的

/** @pdOid 008ca248-f052-41c7-8ab4-f0097a28490c */
class Year {
    /** @pdOid cab32b12-fc09-46c5-94d4-a4fff0eeb0bb */
    private int value;

    /** @pdOid 52f86562-b734-487c-9392-f412ba3bc5b0 */
    public int getValue() {
        return value;
    }

    /** @param newValue
     * @pdOid b5dfd31c-1d95-484d-b21c-6e39b2ae6165 */
    public void setValue(int newValue) {
        value = newValue;
    }

    /** @param value
     * @pdOid 923fa132-5694-4ba5-b8a0-275adf441e93 */
    public Year(int value) {
        // TODO: implement
        this.value=value;
    }

    /** @pdOid 115d4a6b-f4a9-49a0-bd5d-6054278a1cfe */
    public Year() {
        // TODO: implement
    }

    /** @pdOid a7ea8680-d57c-4bd1-9bc5-448baa0494e1 */
    //判断闰年
    public boolean isLeapYear() {
        // TODO: implement
        if(value%400==0||(value%100!=0&&value%4==0))
            return true;
        return false;
    }

    /** @pdOid 653e3cc6-cfa3-4499-8926-bd874dd8e865 */
    //判断年份是否合法
    public boolean validDate() {
        // TODO: implement
        if(value>=1900&&value<=2050)
            return true;
        return false;
    }

    /** @pdOid 90878d41-0d3b-4cda-8071-002a36137813 */
    //年份加1
    public void yearIncrement() {
        value++;
        // TODO: implement
    }

    /** @pdOid 245ae0c2-4797-4c37-b5b8-7b5612b9a2d4 */
    //年份减1
    public void yearReduction() {
        value--;
        // TODO: implement
    }

}
class Month {
    /** @pdOid 904c5967-9291-4c0f-8167-50e5129826fa */
    private int value;
    /** @pdOid 7dad321b-0e8e-4c47-bba8-508339f72ef1 */
    private Year year;

    /** @pdOid 328b0d20-170c-4094-931c-ecedf47ab757 */
    public Year getYear() {
        return year;
    }

    /** @param newYear
     * @pdOid 8ab92e34-f1d8-4c07-92a7-e321eb3e6bc2 */
    public void setYear(Year newYear) {
        year = newYear;
    }

    /** @pdOid 29e3cf10-6cbb-441f-8b36-3e4d345f2f5f */
    public int getValue() {
        return value;
    }

    /** @param newValue
     * @pdOid 03c80541-0a4b-42c1-bfb7-d75a9b90d3af */
    public void setValue(int newValue) {
        value = newValue;
    }

    /** @param monthValue
     * @param yearValue
     * @pdOid 07623df4-3232-4f2a-829c-1b55cf4cb308 */
    public Month(int monthValue, int yearValue) {
        // TODO: implement
        this.setYear(new Year(yearValue));
        this.value=monthValue;
    }

    /** @pdOid e0107e77-5f0a-4ba0-8f34-e9e806519147 */
    public Month() {
        // TODO: implement
    }

    /** @pdOid 0990f178-76cc-4233-9cc3-785421d1804a */
    //设置月份为1
    public void resetMin() {
        // TODO: implement
            value=1;
    }

    /** @pdOid b37b55b0-afc3-4b94-80cb-089bd3a9f935 */
    //设置月份为12
    public void resetMax() {
        // TODO: implement
            value=12;
    }

    /** @pdOid ddf6b0ca-0228-486f-858a-0f59baf26f68 */
    //判断月份是否合法
    public boolean validDate() {
        // TODO: implement
        if(value<=12&&value>=1)
            return true;
        return false;
    }

    /** @pdOid b6181b6a-cdac-46eb-9bfc-69a1ce54843d */
    //月份加1
    public void monthIncrement() {
        // TODO: implement
        value++;
    }

    /** @pdOid 7e4706be-5f28-4ea2-a286-a069235e75c2 */
    //月份减1
    public void monthReduction() {
        // TODO: implement
        value--;
    }
}
class Day {
    public int[] getMonthMaxnum() {
        return monthMaxnum;
    }

    /** @pdOid 54fa93c3-738d-495c-a460-07a6040464dd */
    private int[] monthMaxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    /** @pdOid 84b38aaa-1a5b-4f79-af26-b62e9bd371a1 */
    private Month month;
    /** @pdOid ad97c214-078b-4aaa-aa06-59d0906ef11c */
    private int value;

    /** @param yearValue
     * @param monthValue
     * @param dayValue
     * @pdOid f697ee5a-e7e8-4a3a-aac8-3719e397e680 */
    public Day(int yearValue, int monthValue, int dayValue) {
        // TODO: implement
        this.value=dayValue;
        this.setMonth(new Month(monthValue,yearValue));
    }

    /** @pdOid 2f4aa316-6081-4297-94e5-e4036b8bb9c5 */
    public Day() {
        // TODO: implement
    }

    /** @pdOid 6be2a02a-274d-4083-a259-f124eb53c57b */
    public Month getMonth() {
        return month;
    }

    /** @param newMonth
     * @pdOid 65488c72-54da-4933-8f71-546cb43c4f50 */
    public void setMonth(Month newMonth) {
        month = newMonth;
    }

    /** @pdOid 10e4fca9-1a35-4990-a9d4-a038ddcde3a0 */
    public int getValue() {
        return value;
    }

    /** @param newValue
     * @pdOid 0bd47bb1-974d-4046-9bbd-646c4d3381ec */
    public void setValue(int newValue) {
        value = newValue;
    }

    /** @pdOid 8d4f473b-6394-48dc-b2f3-d053abb2742f */
    //判断天数是否合法
    public boolean vaildDate() {
        // TODO: implement
        if(value>=1&&value<=monthMaxnum[month.getValue()])
            return true;
        return false;
    }
    //更新月份的天数
    public void resetMonthMaxMum(){
        if(getMonth().getYear().isLeapYear())
            monthMaxnum[2]=29;
        else
            monthMaxnum[2]=28;
    }

    /** @pdOid 0fc18803-cc22-4201-8d0c-06cd235c8bfc */
    //设置天数为1
    public void resetMin() {
        // TODO: implement
            value=1;
    }

    /** @pdOid 999fa37c-b810-459b-9dda-d22647bbaec4 */
    //天数初始化为该月最大值
    public void resetMax() {
        // TODO: implement
        this.value=monthMaxnum[month.getValue()];
    }

    /** @pdOid 22db36aa-765e-4d0d-9c15-10afc7220c5b */
    //天数加1
    public void dayIncrement() {
        value++;
        // TODO: implement
    }

    /** @pdOid 1946926e-a9e8-40d6-866c-edadb9ac266e */
    //天数减1
    public void dayReduction() {
        value--;
        // TODO: implement
    }


    /** @pdGenerated default setter
     * @param newMonth */

}
/** @pdOid a9dad2d1-a815-4771-aed4-9d84dd59d3dc */
class DateUtil {
        /**
         * @pdOid 0a225641-c932-41d7-a6b2-5ba736a0438c
         */
        private Day day;

        /**
         * @param year
         * @param month
         * @param day
         * @pdOid 2dcbab2e-cef0-4f20-8ffe-483a64b0388d
         */
        public DateUtil(int year, int month, int day) {
            // TODO: implement
            this.day = new Day(year, month, day);
        }

        /**
         * @pdOid 7c91ebc3-cd14-4afa-ba7f-f2e2d8542a82
         */
        public DateUtil() {
            // TODO: implement
        }

        /**
         * @pdOid 4c4157f8-8fdd-43e4-b425-28905e713b90
         */
        public Day getDay() {
            return day;
        }

        /**
         * @param newDay
         * @pdOid 1a09fc61-af6e-4bd1-84ed-db3e351ad2a5
         */
        public void setDay(Day newDay) {
            day = newDay;
        }

        /**
         * @pdOid 747ece98-51e2-4ef8-b32c-2162c7b4d7b5
         */
        //判断输入是否合法
        public boolean checkInputValid() {
            // TODO: implement
            if ( day.getMonth().validDate()&&day.vaildDate() && day.getMonth().getYear().validDate())
                return true;
            return false;
        }

        /**
         * @param date
         * @pdOid ea1cb228-c37f-45c6-a462-0889078346c9
         */
        //判断两日期的大小关系
        public boolean compareDates(DateUtil date) {
            // TODO: implement
            if (this.day.getMonth().getYear().getValue() > date.day.getMonth().getYear().getValue())
                return true;
            else if (this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue()) {
                if (this.day.getMonth().getValue() > date.getDay().getMonth().getValue())
                    return true;
                else if (this.day.getMonth().getValue() == date.getDay().getMonth().getValue()) {
                    if (this.day.getValue() > this.day.getValue())
                        return true;
                }
            }
            return false;
        }

        /**
         * @param date
         * @pdOid 43f04624-a171-4a14-a745-aa42ca967914
         */
        //判断两日期是否相同
        public boolean equalTwoDates(DateUtil date) {
            // TODO: implement
            if (this.day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&this.day.getMonth().getValue()== date.day.getMonth().getValue()&&this.day.getValue()==date.day.getValue())
                return true;
            return false;
        }

        /**
         * @pdOid f82d8a95-c8f3-4aba-a383-e6532d69fc16
         */
        //展示日期
        public String showDate() {
            // TODO: implement
            String string = day.getMonth().getYear().getValue() + "-" + day.getMonth().getValue() + "-" + day.getValue();
            return string;
        }

        /**
         * @param n
         * @pdOid 67880c1d-48e9-4df3-8a0d-99129b1870f1
         */
        //求n天后的日期
        public DateUtil getNextDays(int n) {
            // TODO: implement
            while (n > 0) {
                day.resetMonthMaxMum();
                day.dayIncrement();
                if (day.getValue() > day.getMonthMaxnum()[day.getMonth().getValue()]) {
                    day.resetMin();
                    day.getMonth().monthIncrement();
                }
                if (day.getMonth().getValue() > 12) {
                    day.getMonth().resetMin();
                    day.getMonth().getYear().yearIncrement();
                }
                n--;
            }
            return this;
        }

        /**
         * @param n
         * @pdOid b5c6c420-4a49-4bba-b5d3-f1e5b6d80e98
         */
        //求n天前的日期
        public DateUtil getPreviousDays(int n) {
            // TODO: implement
            while (n > 0) {
                day.resetMonthMaxMum();
                day.dayReduction();
                if (day.getValue() < 1) {
                    day.getMonth().monthReduction();
                    //1.此处一定是设置在后,如果设置在前每月的天数会出错
                    day.resetMax();
                }
                if (day.getMonth().getValue() < 1) {
                    day.getMonth().getYear().yearReduction();
                    day.resetMonthMaxMum();
                    day.getMonth().resetMax();
                    day.resetMax();//这里也一定要重新设置天数,不然会把一个月的时间直接缩掉
                }
                //System.out.println(showDate());
                n--;
            }
            return this;
        }

        /**
         * @param date
         * @pdOid 4f584d4d-1621-4a0a-bcae-c924eb6e0f41
         */
        //求两日期的间隔天数
        public int getDaysOfDate(DateUtil date) {
            // TODO: implement
            int sum=0;//结果
            DateUtil bigDate=new DateUtil();
            DateUtil smallDate=new DateUtil();
            if (this.equalTwoDates(date))
                return 0;
            else {
                if (this.compareDates(date)) {
                    bigDate = this;
                    smallDate = date;
                } else {
                    bigDate = date;
                    smallDate = this;
                }
            }//判断两日期大小
            while(!bigDate.equalTwoDates(smallDate)){
                smallDate.day.resetMonthMaxMum();
                smallDate.day.dayIncrement();
                if (smallDate.day.getValue() > smallDate.day.getMonthMaxnum()[smallDate.day.getMonth().getValue()]) {
                    smallDate.day.resetMin();
                    smallDate.day.getMonth().monthIncrement();
                }
                if (smallDate.day.getMonth().getValue() > 12) {
                    smallDate.day.getMonth().resetMin();
                    smallDate.day.getMonth().getYear().yearIncrement();
                }
                sum++;
            }
            return sum;
        }
    }

日期问题面向对象设计(聚合二)
参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:

应用程序共测试三个功能:

求下n天
求前n天
求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)

输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):

1 year month day n //测试输入日期的下n天
2 year month day n //测试输入日期的前n天
3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
当输入有误时,输出格式如下:
Wrong Format
当第一个数字为1且输入均有效,输出格式如下:
year1-month1-day1 next n days is:year2-month2-day2
当第一个数字为2且输入均有效,输出格式如下:
year1-month1-day1 previous n days is:year2-month2-day2
当第一个数字为3且输入均有效,输出格式如下:
The days between year1-month1-day1 and year2-month2-day2 are:值

这题更上面一道要求相同,但类图不同,聚合的情况不同,这里所有的day,month,year,聚合入DayUtil中

故所有的计算方法要先等DayUtil中的属性设置好,再计算具体变化。
下面看代码:

import java.util.Scanner;

public class Main {
    public static void main(String []args){
        Scanner input=new Scanner(System.in);
        int choice=input.nextInt();

        if (choice == 1) {
            int year = input.nextInt(), month = input.nextInt(), day = input.nextInt();
            int n = input.nextInt();
            DateUtil newDay = new DateUtil(year, month, day);
            if(!newDay.checkInputValid()){
                System.out.println("Wrong Format");
                return;
            }
            DateUtil sourceDate=new DateUtil(year, month, day);
            newDay = newDay.getNextDays(n);
            System.out.println(sourceDate.showDate()+" next "+n+" days is:"+newDay.showDate());
        } else if (choice == 2) {
            int year = input.nextInt(), month = input.nextInt(), day = input.nextInt();
            int n = input.nextInt();
            DateUtil newDay = new DateUtil(year, month, day);
            if(!newDay.checkInputValid()){
                System.out.println("Wrong Format");
                return;
            }
            DateUtil sourceDate=new DateUtil(year, month, day);
            newDay = newDay.getPreviousDays(n);
            System.out.println(sourceDate.showDate()+" previous "+n+" days is:"+newDay.showDate());
        }else if(choice==3){
            int year = input.nextInt(), month = input.nextInt(), day = input.nextInt();
            DateUtil date1=new DateUtil(year,month,day);
            DateUtil resource1=new DateUtil(year,month,day);
            year = input.nextInt();
            month = input.nextInt();
            day = input.nextInt();
            DateUtil date2=new DateUtil(year,month,day);
            DateUtil resource2=new DateUtil(year,month,day);
             if(!date1.checkInputValid()||!date2.checkInputValid()){
                System.out.println("Wrong Format");
                return;
            }
            int result=Math.abs(date1.getDaysOfDate(date2));
            System.out.println("The days between "+resource1.showDate()+" and "+resource2.showDate()+" are:"+result);
        }
        else{
            System.out.println("Wrong Format");
        }
    }
}
//此处沿用了题5的部分代码,以及重新使用powerdesigner生成了一部分代码
class Day {

   private int value;

    public Day( int dayValue) {
        // TODO: implement
        this.value=dayValue;
    }

    /** @pdOid 2f4aa316-6081-4297-94e5-e4036b8bb9c5 */
    public Day() {
        // TODO: implement
    }

    /** @pdOid 10e4fca9-1a35-4990-a9d4-a038ddcde3a0 */
    public int getValue() {
        return value;
    }

    /** @param newValue
     * @pdOid 0bd47bb1-974d-4046-9bbd-646c4d3381ec */
    public void setValue(int newValue) {
        value = newValue;
    }

    /** @pdOid 22db36aa-765e-4d0d-9c15-10afc7220c5b */
    //天数加1
    public void dayIncrement() {
        value++;
        // TODO: implement
    }

    /** @pdOid 1946926e-a9e8-40d6-866c-edadb9ac266e */
    //天数减1
    public void dayReduction() {
        value--;
        // TODO: implement
    }


    /** @pdGenerated default setter
     * @param newMonth */

}
class Month {
    /** @pdOid 904c5967-9291-4c0f-8167-50e5129826fa */
    private int value;

    /** @pdOid 29e3cf10-6cbb-441f-8b36-3e4d345f2f5f */
    public int getValue() {
        return value;
    }

    /** @param newValue
     * @pdOid 03c80541-0a4b-42c1-bfb7-d75a9b90d3af */
    public void setValue(int newValue) {
        value = newValue;
    }
    public Month(int monthValue) {
        // TODO: implement
        this.value=monthValue;
    }

    /** @pdOid e0107e77-5f0a-4ba0-8f34-e9e806519147 */
    public Month() {
        // TODO: implement
    }

    /** @pdOid 0990f178-76cc-4233-9cc3-785421d1804a */
    //设置月份为1
    public void resetMin() {
        // TODO: implement
        value=1;
    }

    /** @pdOid b37b55b0-afc3-4b94-80cb-089bd3a9f935 */
    //设置月份为12
    public void resetMax() {
        // TODO: implement
        value=12;
    }

    /** @pdOid ddf6b0ca-0228-486f-858a-0f59baf26f68 */
    //判断月份是否合法
    public boolean validDate() {
        // TODO: implement
        if(value<=12&&value>=1)
            return true;
        return false;
    }

    /** @pdOid b6181b6a-cdac-46eb-9bfc-69a1ce54843d */
    //月份加1
    public void monthIncrement() {
        // TODO: implement
        value++;
    }

    /** @pdOid 7e4706be-5f28-4ea2-a286-a069235e75c2 */
    //月份减1
    public void monthReduction() {
        // TODO: implement
        value--;
    }
}
class Year {
    /** @pdOid cab32b12-fc09-46c5-94d4-a4fff0eeb0bb */
    private int value;

    /** @pdOid 52f86562-b734-487c-9392-f412ba3bc5b0 */
    public int getValue() {
        return value;
    }

    /** @param newValue
     * @pdOid b5dfd31c-1d95-484d-b21c-6e39b2ae6165 */
    public void setValue(int newValue) {
        value = newValue;
    }

    /** @param value
     * @pdOid 923fa132-5694-4ba5-b8a0-275adf441e93 */
    public Year(int value) {
        // TODO: implement
        this.value=value;
    }

    /** @pdOid 115d4a6b-f4a9-49a0-bd5d-6054278a1cfe */
    public Year() {
        // TODO: implement
    }

    /** @pdOid a7ea8680-d57c-4bd1-9bc5-448baa0494e1 */
    //判断闰年
    public boolean isLeapYear() {
        // TODO: implement
        if(value%400==0||(value%100!=0&&value%4==0))
            return true;
        return false;
    }

    /** @pdOid 653e3cc6-cfa3-4499-8926-bd874dd8e865 */
    //判断年份是否合法
    public boolean validDate() {
        // TODO: implement
        if(value>=1820&&value<=2020)
            return true;
        return false;
    }

    /** @pdOid 90878d41-0d3b-4cda-8071-002a36137813 */
    //年份加1
    public void yearIncrement() {
        value++;
        // TODO: implement
    }

    /** @pdOid 245ae0c2-4797-4c37-b5b8-7b5612b9a2d4 */
    //年份减1
    public void yearReduction() {
        value--;
        // TODO: implement
    }

}
class DateUtil {
        private Day day;
        private Month month;
        private int []monthMaxmum={0,31,28,31,30,31,30,31,31,30,31,30,31};
        public int[] getMonthMaxmum() {
            return monthMaxmum;
        }
        public void resetMonthMaxmum(){
            if(year.isLeapYear())
                monthMaxmum[2]=29;
            else
                monthMaxmum[2]=28;
        }
        public Month getMonth() {
            return month;
        }

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

        public Year getYear() {
            return year;
        }

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

        private Year year;


        public DateUtil(int year,int month,int day) {
            // TODO: implement
            this.day = new Day(day);
            this.month=new Month(month);
            this.year=new Year(year);
        }

        /**
         * @pdOid 7c91ebc3-cd14-4afa-ba7f-f2e2d8542a82
         */
        public DateUtil() {
            // TODO: implement
        }

        /**
         * @pdOid 4c4157f8-8fdd-43e4-b425-28905e713b90
         */
        public Day getDay() {
            return day;
        }

        /**
         * @param newDay
         * @pdOid 1a09fc61-af6e-4bd1-84ed-db3e351ad2a5
         */
        public void setDay(Day newDay) {
            day = newDay;
        }

        /**
         * @pdOid 747ece98-51e2-4ef8-b32c-2162c7b4d7b5
         */
        public boolean checkInputValid() {
           if(day.getValue()>=1&&day.getValue()<=monthMaxmum[month.getValue()]&&month.validDate()&&year.validDate())
               return true;
           return false;
        }
        /**
         * @param date
         * @pdOid ea1cb228-c37f-45c6-a462-0889078346c9
         */
        public boolean compareDates(DateUtil date) {
            // TODO: implement
            if (this.year.getValue() > date.year.getValue())
                return true;
            else if (this.year.getValue() == date.year.getValue()) {
                if (this.month.getValue() > date.month.getValue())
                    return true;
                else if (this.month.getValue() == date.month.getValue()) {
                    if (this.day.getValue() > this.day.getValue())
                        return true;
                }
            }
            return false;
        }

        /**
         * @param date
         * @pdOid 43f04624-a171-4a14-a745-aa42ca967914
         */
        public boolean equalTwoDates(DateUtil date) {
            // TODO: implement
            if (this.year.getValue()==date.year.getValue()&&this.month.getValue()== date.month.getValue()&&this.day.getValue()==date.day.getValue())
                return true;
            return false;
        }

        /**
         * @pdOid f82d8a95-c8f3-4aba-a383-e6532d69fc16
         */
        public String showDate() {
            // TODO: implement
            String string = year.getValue() + "-" + month.getValue() + "-" + day.getValue();
            return string;
        }

        public void resetDayMin(){
            day.setValue(1);
        }
        public void resetDayMax(){
            day.setValue(getMonthMaxmum()[month.getValue()]);
        }

        /**
         * @param n
         * @pdOid 67880c1d-48e9-4df3-8a0d-99129b1870f1
         */
        public DateUtil getNextDays(int n) {
            // TODO: implement
            while (n > 0) {
                resetMonthMaxmum();
                day.dayIncrement();
                if (day.getValue() > getMonthMaxmum()[month.getValue()]) {
                    resetDayMin();
                    month.monthIncrement();
                }
                if (month.getValue() > 12) {
                    month.resetMin();
                   year.yearIncrement();
                }
                n--;
            }
            return this;
        }

        /**
         * @param n
         * @pdOid b5c6c420-4a49-4bba-b5d3-f1e5b6d80e98
         */
        public DateUtil getPreviousDays(int n) {
            // TODO: implement
            while (n > 0) {
               resetMonthMaxmum();
                day.dayReduction();
                if (day.getValue() < 1) {
                  month.monthReduction();
                    //1.此处一定是设置在后,如果设置在前每月的天数会出错
                    //2.此处一定要重新
                    resetDayMax();
                }
                if (month.getValue() < 1) {
                    year.yearReduction();
                    resetMonthMaxmum();
                    month.resetMax();
                    resetDayMax();//这里也一定要重新设置天数,不然会把一个月的时间直接缩掉
                }
                //System.out.println(showDate());
                n--;
            }
            return this;
        }

        /**
         * @param date
         * @pdOid 4f584d4d-1621-4a0a-bcae-c924eb6e0f41
         */
        public int getDaysOfDate(DateUtil date) {
            // TODO: implement
            int sum=0;
            DateUtil bigDate=new DateUtil();
            DateUtil smallDate=new DateUtil();
            if (this.equalTwoDates(date))
                return 0;
            else {
                if (this.compareDates(date)) {
                    bigDate = this;
                    smallDate = date;
                } else {
                    bigDate = date;
                    smallDate = this;
                }
            }
            while(!bigDate.equalTwoDates(smallDate)){
                smallDate.resetMonthMaxmum();
                smallDate.day.dayIncrement();
                if (smallDate.day.getValue() > smallDate.getMonthMaxmum()[smallDate.month.getValue()]) {
                    smallDate.resetDayMin();
                    smallDate.month.monthIncrement();
                }
                if (smallDate.month.getValue() > 12) {
                    smallDate.month.resetMin();
                    smallDate.year.yearIncrement();
                }
                sum++;
            }
            return sum;
        }
    }

ATM机类结构设计(一)
1.作业目标
编写一个银行 ATM 机的模拟程序,能够完成用户的存款、取款以及查询余额功能。
2.作业要求
2.1 业务背景
(1)相关概念说明
⚫中国银联(China UnionPay):成立于 2002 年 3 月,是经国务院同意,中
国人民银行批准设立的中国银行卡联合组织,总部设于上海。主要负责建设和
运营全国统一的银行卡跨行信息交换网络、提供银行卡跨行信息交换相关的专
业化服务、管理和经营“银联”品牌、制定银行卡跨行交易业务规范和技术标
准。其包含多家银行机构。
⚫银行(Bank):是依法成立的经营货币信贷业务的金融机构,是商品货币经
济发展到一定阶段的产物。
⚫银行用户(User):能够在银行办理相应业务的用户。一个银行机构可以拥
有多个银行用户及多个银行账户,一个用户也可以在多个银行机构开立账户。
⚫银行账户(Account):客户在银行开立的存款账户、贷款账户、往来账户
的总称。一个银行用户可以拥有多个银行账户。
⚫银行卡(Card):经批准由商业银行(含邮政金融机构) 向社会发行的具有
消费信用、转账结算、存取现金等全部或部分功能的信用支付工具。一个银行
账户可以发行多张银行卡。
⚫ ATM(Automated Teller Machine),自动柜员机,因大部分用于取款,又
称自动取款机。它是一种高度精密的机电一体化装置,利用磁性代码卡或智能
卡实现金融交易的自助服务,代替银行柜面人员的工作。可提取现金、查询存
款余额、进行账户之间资金划拨、余额查询等工作。一般 ATM 机隶属于某一银
行机构。
(2)设计要求
尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设
计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进
行扩充和完善。
注意:为降低难度,本次作业限定银行卡均为借记卡(不能透支),且不允
许跨行办理相关业务(例如在中国建设银行的 ATM 机上对其他银行的账户进行操
作)。
2.2 程序功能需求
(1)实现功能
⚫ 基础数据的初始化;
⚫ 用户存款、取款及查询余额功能。
(2)初始化数据
表 1 银行相关数据
机构名称 银行名称 ATM 机编号
中国银联
中国建设银行
01
02
03
04
中国工商银行
05
06
表 2 用户相关数据
用户姓名 隶属银行 银行账号 初始余额 隶属卡号
杨过 中国建设银行 3217000010041315709 10000.00
6217000010041315709
6217000010041315715
3217000010041315715 10000.00 6217000010041315718
郭靖 中国建设银行 3217000010051320007 10000.00 6217000010051320007
张无忌 中国工商银行
3222081502001312389 10000.00 6222081502001312389
3222081502001312390 10000.00 6222081502001312390
3222081502001312399 10000.00
6222081502001312399
6222081502001312400
韦小宝 中国工商银行 3222081502051320785 10000.00 6222081502051320785
3222081502051320786 10000.00 6222081502051320786
注意:系统的测试用例均采用如上数据,且所有卡号密码默认为“88888888”。
(3)输入输出规则
1)输入规则
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业
务操作输入格式如下:
➢ 存款、取款功能输入数据格式
卡号 密码 ATM 机编号 金额(由一个或多个空格分隔)
其中,当金额大于 0 时,代表取款,否则代表存款。
➢ 查询余额功能输入数据格式
卡号
2)输出规则
①输入错误处理
 如果输入卡号不存在,则输出“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 编号]上存款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
④查询余额业务输出
¥[金额]
金额保留两位小数。

思路:
这道题,最大的特点就是长(还需要用文件专门写下题目),所以仔细思考类的设计是最重要的。
首先先给它浅浅的分下类:
中国银联——银行——用户——账户——银行卡
ATM——银行卡
这是两个主要关系链
对于第一个关系链:
具体逻辑:银行卡隶属于账户,账户隶属于用户,用户可以在某银行登记信息,银行之间的协议为银联
接着分析每个类应有的属性和方法

  • 银联:名字
  • 银行:名字,旗下的ATM机编号。
  • 用户:名字,在哪个银行开户的。
  • 账户:编号,隶属的用户,金额。
  • 银行卡:编号,隶属的账户。

对于ATM的类设计:

  • 由于ATM机只负责读取卡的编号,故ATM机要有Card属性
  • ATM要有查询余额方法
  • ATM要有存取款方法
  • ATM要存入所有卡的编号(卡列表)

由powerDesigner生成的类图如下:

别忘了还要初始化数据
代码如下:

import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String []args){
        Scanner input=new Scanner(System.in);
        AutomatedTellerMachine automatedTellerMachine=new AutomatedTellerMachine();
        //初始化数据
        ChinaUnionPay chinaUnionPay=new ChinaUnionPay("中国银联");
        Bank bank1=new Bank(chinaUnionPay);bank1.initBank1();
        Bank bank2=new Bank(chinaUnionPay);bank2.initBank2();
        User user1=new User();user1.user1Init(bank1);
        User user2=new User();user2.user2Init(bank1);
        User user3=new User();user3.user3Init(bank2);
        User user4=new User();user4.user4Init(bank2);
        Account user1Account1=new Account();user1Account1.yangGuoAccount1Init(user1);
        Account user1Account2=new Account();user1Account2.yangGuoAccount2Init(user1);
        Account user2Account1=new Account();user2Account1.guoJinAccount1Init(user2);
        Account user3Account1=new Account();user3Account1.zhangWuJiAccount1Init(user3);
        Account user3Account2=new Account();user3Account2.zhangWuJiAccount2Init(user3);
        Account user3Account3=new Account();user3Account3.zhangWuJiAccount3Init(user3);
        Account user4Account1=new Account();user4Account1.weiXiaoBaoAccount1Init(user4);
        Account user4Account2=new Account();user4Account2.weiXiaoBaoAccount2Init(user4);
        Card user1Account1Card1=new Card(user1Account1,"6217000010041315709");automatedTellerMachine.getAllCard().add(user1Account1Card1);
        Card user1Account1Card2=new Card(user1Account1,"6217000010041315715");automatedTellerMachine.getAllCard().add(user1Account1Card2);
        Card user1Account2Card1=new Card(user1Account2,"6217000010041315718");automatedTellerMachine.getAllCard().add(user1Account2Card1);
        Card user2Account1Card1=new Card(user2Account1,"6217000010051320007");automatedTellerMachine.getAllCard().add(user2Account1Card1);
        Card user3Account1Card1=new Card(user3Account1,"6222081502001312389");automatedTellerMachine.getAllCard().add(user3Account1Card1);
        Card user3Account2Card1=new Card(user3Account2,"6222081502001312390");automatedTellerMachine.getAllCard().add(user3Account2Card1);
        Card user3Account3Card1=new Card(user3Account3,"6222081502001312399");automatedTellerMachine.getAllCard().add(user3Account3Card1);
        Card user3Account3Card2=new Card(user3Account3,"6222081502001312400");automatedTellerMachine.getAllCard().add(user3Account3Card2);
        Card user4Account1Card1=new Card(user4Account1,"6222081502051320785");automatedTellerMachine.getAllCard().add(user4Account1Card1);
        Card user4Account2Card1=new Card(user4Account2,"6222081502051320786");automatedTellerMachine.getAllCard().add(user4Account2Card1);
        //输入
        StringBuilder stringBuilder=new StringBuilder();
        String temp=input.nextLine();
        if(temp.equals("#"))
        {
            return;
        }
        while(true){
            stringBuilder.append(temp);
            stringBuilder.append("xiongjiawei");
            temp=input.nextLine();
            if(temp.equals("#"))
                break;
        }
        String []strings=stringBuilder.toString().split("xiongjiawei");
        for(int i=0;i<strings.length;i++) {
            int pos=0;
            String[] strings1 = strings[i].split(" ");
            while(strings1[pos].equals(""))
                pos++;
            String cardId=strings1[pos++];
            if(!automatedTellerMachine.findCard(cardId)){
                System.out.println("Sorry,this card does not exist.");
                return;
            }
            if(strings1.length<=1){
                automatedTellerMachine.findBalance();
            }
            else {
                while(strings1[pos].equals(""))
                    pos++;
                String key = strings1[pos++];
                while(strings1[pos].equals(""))
                    pos++;
                String ATMid = strings1[pos++];
                while(strings1[pos].equals(""))
                    pos++;
                double changeMoney = Double.parseDouble(strings1[pos++]);
                String regex="0[1-6]";
                if(!ATMid.matches(regex)){
                    System.out.println("Sorry,the ATM's id is wrong.");
                    return;
                }
                else {
                    if (!key.equals("88888888")) {
                        System.out.println("Sorry,your password is wrong.");
                        return;
                    }
                    else{
                        automatedTellerMachine.setChangeMoney(changeMoney);
                        if(!automatedTellerMachine.checkYourBalance()){
                            System.out.println("Sorry,your account balance" +" is insufficient.");
                            return;
                        }
                        else {
                            if(!automatedTellerMachine.getCard().getAccount().getUser().getBank().getAutomatedTellerMachineIdArrayList().contains(ATMid)){
                                System.out.println("Sorry,cross-bank withdrawal is not supported.");
                                return;
                            }
                            else
                                automatedTellerMachine.changeYourMoney();
                                if(automatedTellerMachine.getChangeMoney()<0)
                                System.out.printf(automatedTellerMachine.getCard().getAccount().getUser().getName()+"在"+automatedTellerMachine.getCard().getAccount().getUser().getBank().getName()+"的"+ATMid+"号ATM机上存款¥%.2f\n",Math.abs(automatedTellerMachine.getChangeMoney()));
                                else{
                                    System.out.printf(automatedTellerMachine.getCard().getAccount().getUser().getName()+"在"+automatedTellerMachine.getCard().getAccount().getUser().getBank().getName()+"的"+ATMid+"号ATM机上取款¥%.2f\n",automatedTellerMachine.getChangeMoney());
                                }
                                System.out.printf("当前余额为¥%.2f\n",automatedTellerMachine.getCard().getAccount().getBalance());
                            }
                        }
                    }
                }
            }
        }
    }
 class ChinaUnionPay {
    private String name;
    ChinaUnionPay(String name){
        this.name=name;
    }
    ChinaUnionPay(){}
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class Bank {
    private ChinaUnionPay chinaUnionPay;
    private String name;
    private ArrayList<String> automatedTellerMachineIdArrayList;

    public ChinaUnionPay getChinaUnionPay() {
        return chinaUnionPay;
    }

    public void setChinaUnionPay(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
    }

    public ArrayList<String> getAutomatedTellerMachineIdArrayList() {
        return automatedTellerMachineIdArrayList;
    }

    public void setAutomatedTellerMachineIdArrayList(ArrayList<String> automatedTellerMachineIdArrayList) {
        this.automatedTellerMachineIdArrayList = automatedTellerMachineIdArrayList;
    }

    public String getName() {
        return name;
    }

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

    public Bank(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
        this.automatedTellerMachineIdArrayList=new ArrayList<>();
    }
    public void initBank1(){
        ArrayList<String>tempArrayList=new ArrayList<>();
        for(int i=1;i<=4;i++){
            String temp="0"+i;
            tempArrayList.add(temp);
        }
        this.automatedTellerMachineIdArrayList=tempArrayList;
        this.name="中国建设银行";
    }
    public void initBank2(){
        ArrayList<String>tempArrayList=new ArrayList<>();
        for(int i=5;i<=6;i++){
            String temp="0"+i;
            tempArrayList.add(temp);
        }
        this.automatedTellerMachineIdArrayList=tempArrayList;
        this.name="中国工商银行";
    }
}
class User {
    private Bank bank;
    private String name;

    public Bank getBank() {
        return bank;
    }

    public void setBank(Bank bank) {
        this.bank = bank;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void user1Init(Bank bank){
        this.name="杨过";
        this.bank=bank;
    }
    public void user2Init(Bank bank){
        this.name="郭靖";
        this.bank=bank;
    }
    public void user3Init(Bank bank){
        this.name="张无忌";
        this.bank=bank;
    }
    public void user4Init(Bank bank){
        this.name="韦小宝";
        this.bank=bank;
    }

}
class Account {
    private User user;
    private String id;
    private double balance;
    Account(){
        this.balance=10000.0;
    }
    private String key;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    public void yangGuoAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3217000010041315709";
    }
    public void yangGuoAccount2Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3217000010041315715";
    }
    public void guoJinAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3217000010051320007";
    }
    public void zhangWuJiAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502001312389";
    }
    public void zhangWuJiAccount2Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502001312390";
    }
    public void zhangWuJiAccount3Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502001312399";
    }
    public void weiXiaoBaoAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502051320785";
    }
    public void weiXiaoBaoAccount2Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502051320786";
    }

}
class Card {
    private Account account;
    private String id;

    public Account getAccount() {
        return account;
    }

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

    public Card(Account account, String id) {
        this.account = account;
        this.id = id;
    }
    public Card(){}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
class AutomatedTellerMachine {
    private Card card;;

    AutomatedTellerMachine(){
        allCard=new ArrayList<>();
    }
    private ArrayList<Card>allCard;

    public ArrayList<Card> getAllCard() {
        return allCard;
    }

    public void setAllCard(ArrayList<Card> allCard) {
        this.allCard = allCard;
    }

    private String id;
    private String key;

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public double getChangeMoney() {
        return changeMoney;
    }

    public void setChangeMoney(double changeMoney) {
        this.changeMoney = changeMoney;
    }

    private double changeMoney;
    public boolean findCard(String cardId){
        for(Card tempCard:allCard){
            if(tempCard.getId().equals(cardId)) {
                this.card = tempCard;return true;
            }
        }
        return false;
    }
    public void findBalance(){
        System.out.printf("¥%.2f\n",card.getAccount().getBalance());
    }
    public boolean checkYourBalance(){
        if(card.getAccount().getBalance()-changeMoney<0)
            return false;
        return true;
    }
    public void changeYourMoney(){
        card.getAccount().setBalance(card.getAccount().getBalance()-changeMoney);
    }
}

ATM机类结构设计(二)
1.作业目标
编写一个银行 ATM 机的模拟程序,能够完成用户的取款以及查询余额功能。
2.作业要求
2.1 业务背景
(1)相关概念说明
⚫中国银联(China UnionPay):成立于 2002 年 3 月,是经国务院同意,中
国人民银行批准设立的中国银行卡联合组织,总部设于上海。主要负责建设和
运营全国统一的银行卡跨行信息交换网络、提供银行卡跨行信息交换相关的专
业化服务、管理和经营“银联”品牌、制定银行卡跨行交易业务规范和技术标
准。其包含多家银行机构。
⚫银行(Bank):是依法成立的经营货币信贷业务的金融机构,是商品货币经
济发展到一定阶段的产物。
⚫银行用户(User):能够在银行办理相应业务的用户。一个银行机构可以拥
有多个银行用户及多个银行账户,一个用户也可以在多个银行机构开立账户。
⚫银行账户(Account):客户在银行开立的存款账户、贷款账户、往来账户
的总称。银行账户分为借记账户和贷记账户两种,其中,借记账户不能够透支
取款,而贷记账户可以透支取款(可能需要支付手续费)。一个银行用户可以
拥有多个银行账户。
⚫银行卡(Card):经批准由商业银行(含邮政金融机构) 向社会发行的具有
消费信用、转账结算、存取现金等全部或部分功能的信用支付工具。根据账户
的不同,银行卡一般分为借记卡(针对借记账户)和信用卡(针对贷记账户)
两类。一个银行账户可以发行多张银行卡。
⚫ ATM(Automated Teller Machine),自动柜员机,因大部分用于取款,又
称自动取款机。它是一种高度精密的机电一体化装置,利用磁性代码卡或智能
卡实现金融交易的自助服务,代替银行柜面人员的工作。可提取现金、查询存
款余额、进行账户之间资金划拨、余额查询等工作。一般 ATM 机隶属于某一银
行机构。
(2)设计要求
尝试使用面向对象技术对银行用户在 ATM 机上进行相关操作的模拟系统设
计,上述的相关概念均要设计为实体类,业务(控制)类请自行根据实际需要进
行扩充和完善。
注意:本次作业中银行卡包含借记卡和信用卡两类,且允许跨行办理相关业
务(例如在中国建设银行的 ATM 机上使用中国工商银行的银行卡进行业务操作)。
2.2 程序功能需求
(1)实现功能
⚫ 基础数据的初始化;
⚫ 用户取款及查询余额功能。
(2)初始化数据
表 1 银行相关数据
机构名称 透支取款手续费
及透支最大限额 银行名称 跨行取款手续费 ATM 机编号
中国银联 取款金额的 5%
/50000 元
中国建设银行 取款金额的 2%
01
02
03
04
中国工商银行 取款金额的 3%
05
06
中国农业银行 取款金额的 4%
07
08
09
10
11
表 2 用户相关数据
用户姓名 隶属银行 银行账号 账号种类 隶属卡号
杨过 中国建设银行 3217000010041315709 借记账号 6217000010041315709
6217000010041315715
3217000010041315715 借记账号 6217000010041315718
郭靖 中国建设银行 3217000010051320007 借记账号 6217000010051320007
张无忌 中国工商银行
3222081502001312389 借记账号 6222081502001312389
3222081502001312390 借记账号 6222081502001312390
3222081502001312399 借记账号 6222081502001312399
6222081502001312400
韦小宝 中国工商银行 3222081502051320785 借记账号 6222081502051320785
3222081502051320786 借记账号 6222081502051320786
张三丰 中国建设银行 3640000010045442002 贷记账号 6640000010045442002
6640000010045442003
令狐冲 中国工商银行 3640000010045441009 贷记账号 6640000010045441009
乔峰 中国农业银行 3630000010033431001 贷记账号 6630000010033431001
洪七公 中国农业银行 3630000010033431008 贷记账号 6630000010033431008
(3)业务假定
1)系统的测试用例均采用如上数据,且所有卡号密码默认为“88888888”,
初始余额均为 10000 元。
2)手续费默认均从银行卡所对应的账户中自动扣除。
3)跨行业务手续费收取比例由 ATM 机隶属银行决定,例如,用户手持中国
工商银行的银行卡在中国建设银行的 ATM 上进行取款操作,则手续费收取比例为
中国建设银行的相应比例,按初始化数据中的规定为取款金额的 2%。
4)跨行查询余额不收取手续费。
5)透支取款手续费由中国银联统一规定为 5%,最大透支金额均为 50000 元。
(4)输入输出规则
1)输入规则
每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业
务操作输入格式如下:
➢ 取款功能输入数据格式
卡号 密码 ATM 机编号 金额(由一个或多个空格分隔)
➢ 查询余额功能输入数据格式
卡号
2)输出规则
①输入错误处理
 如果输入卡号不存在,则输出“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 编号]上取款¥[金额]
当前余额为¥[金额]
其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。
③查询余额业务输出
业务:查询余额 ¥[金额]
金额保留两位小数。

这道题跟上道题最大的区别就是增加了借贷功能,且可跨行
面对上面的需求,我们可以:

  • 在账户和银行卡类设计中增加type属性,以区分借贷/借记。
  • 把跨行的限制取消掉
  • 扣亿点点细节
    类图如下:

代码如下:

import java.util.Scanner;
import java.util.ArrayList;
public class Main {
    public static void main(String []args){
        Scanner input=new Scanner(System.in);
        AutomatedTellerMachine automatedTellerMachine=new AutomatedTellerMachine();
        //初始化数据
        ChinaUnionPay chinaUnionPay=new ChinaUnionPay("中国银联");
        Bank bank1=new Bank(chinaUnionPay);bank1.initBank1();
        Bank bank2=new Bank(chinaUnionPay);bank2.initBank2();
        Bank bank3=new Bank(chinaUnionPay);bank3.initBank3();
        //用户
        User user1=new User();user1.user1Init(bank1);
        User user2=new User();user2.user2Init(bank1);
        User user3=new User();user3.user3Init(bank2);
        User user4=new User();user4.user4Init(bank2);
        User user5=new User();user5.user5Init(bank1);
        User user6=new User();user6.user6Init(bank2);
        User user7=new User();user7.user7Init(bank3);
        User user8=new User();user8.user8Init(bank3);
        //账户
        Account user1Account1 =new Account();
        user1Account1.yangGuoAccount1Init(user1);
        Account user1Account2 =new Account();
        user1Account2.yangGuoAccount2Init(user1);
        Account user2Account1 =new Account();
        user2Account1.guoJinAccount1Init(user2);
        Account user3Account1 =new Account();
        user3Account1.zhangWuJiAccount1Init(user3);
        Account user3Account2 =new Account();
        user3Account2.zhangWuJiAccount2Init(user3);
        Account user3Account3 =new Account();
        user3Account3.zhangWuJiAccount3Init(user3);
        Account user4Account1 =new Account();
        user4Account1.weiXiaoBaoAccount1Init(user4);
        Account user4Account2 =new Account();
        user4Account2.weiXiaoBaoAccount2Init(user4);
        Account user5Account1 =new Account();
        user5Account1.zhangSanFengAccount1Init(user5);
        Account user6Account1 =new Account();
        user6Account1.lingHuChongAccount1Init(user6);
        Account user7Account1 =new Account();
        user7Account1.qiaoFengAccount1Init(user7);
        Account user8Account1 =new Account();
        user8Account1.HongQiGongAccount1Init(user8);

        //银行卡
        Card user1Account1Card1 =new Card(user1Account1,"6217000010041315709");automatedTellerMachine.getAllCard().add(user1Account1Card1);
        Card user1Account1Card2 =new Card(user1Account1,"6217000010041315715");automatedTellerMachine.getAllCard().add(user1Account1Card2);
        Card user1Account2Card1 =new Card(user1Account2,"6217000010041315718");automatedTellerMachine.getAllCard().add(user1Account2Card1);
        Card user2Account1Card1 =new Card(user2Account1,"6217000010051320007");automatedTellerMachine.getAllCard().add(user2Account1Card1);
        Card user3Account1Card1 =new Card(user3Account1,"6222081502001312389");automatedTellerMachine.getAllCard().add(user3Account1Card1);
        Card user3Account2Card1 =new Card(user3Account2,"6222081502001312390");automatedTellerMachine.getAllCard().add(user3Account2Card1);
        Card user3Account3Card1 =new Card(user3Account3,"6222081502001312399");automatedTellerMachine.getAllCard().add(user3Account3Card1);
        Card user3Account3Card2 =new Card(user3Account3,"6222081502001312400");automatedTellerMachine.getAllCard().add(user3Account3Card2);
        Card user4Account1Card1 =new Card(user4Account1,"6222081502051320785");automatedTellerMachine.getAllCard().add(user4Account1Card1);
        Card user4Account2Card1 =new Card(user4Account2,"6222081502051320786");automatedTellerMachine.getAllCard().add(user4Account2Card1);
        Card user5Account1Card1 =new Card(1,user5Account1,"6640000010045442002");automatedTellerMachine.getAllCard().add(user5Account1Card1);
        Card user5Account1Card2 =new Card(1,user5Account1,"6640000010045442003");automatedTellerMachine.getAllCard().add(user5Account1Card2);
        Card user6Account1Card1 =new Card(1,user6Account1,"6640000010045441009");automatedTellerMachine.getAllCard().add(user6Account1Card1);
        Card user7Account1Card1 =new Card(1,user7Account1,"6630000010033431001");automatedTellerMachine.getAllCard().add(user7Account1Card1);
        Card user8Account1Card1 =new Card(1,user8Account1,"6630000010033431008");automatedTellerMachine.getAllCard().add(user8Account1Card1);
        //输入
        StringBuilder stringBuilder=new StringBuilder();
        String temp=input.nextLine();
        if(temp.equals("#"))
        {
            return;
        }
        while(true){
            stringBuilder.append(temp);
            stringBuilder.append("xiongjiawei");
            temp=input.nextLine();
            if(temp.equals("#"))
                break;
        }
        String []strings=stringBuilder.toString().split("xiongjiawei");
        for(int i=0;i<strings.length;i++) {
            int pos=0;
            String[] strings1 = strings[i].split(" ");
            while(strings1[pos].equals(""))
                pos++;
            String cardId=strings1[pos++];
            if(!automatedTellerMachine.findCard(cardId)){
                System.out.println("Sorry,this card does not exist.");
                return;
            }
            if(strings1.length<=1){
                automatedTellerMachine.findBalance();
            }
            else {
                while(strings1[pos].equals(""))
                    pos++;
                String key = strings1[pos++];
                while(strings1[pos].equals(""))
                    pos++;
                String ATMid = strings1[pos++];
                while(strings1[pos].equals(""))
                    pos++;
                double changeMoney = Double.parseDouble(strings1[pos++]);
                String regex="0[1-9]|1[0-1]";
                if(!ATMid.matches(regex)){
                    System.out.println("Sorry,the ATM's id is wrong.");
                    return;
                }
                else {
                    if (!key.equals("88888888")) {
                        System.out.println("Sorry,your password is wrong.");
                        return;
                    }
                    else{
                        automatedTellerMachine.setChangeMoney(changeMoney);
                        automatedTellerMachine.setId(ATMid);
                       
                        if(automatedTellerMachine.getCard().getType()==0&&!automatedTellerMachine.checkYourBalance()){
                            System.out.println("Sorry,your account balance is insufficient.");
                            return;
                        }
                        else {
                            if(automatedTellerMachine.getCard().getType()==1&&!automatedTellerMachine.checkYourBalance()) {
                                double value;
                                if(automatedTellerMachine.getCard().getAccount().getBalance()>0)
                                    value= (automatedTellerMachine.getChangeMoney()-automatedTellerMachine.getCard().getAccount().getBalance())*0.05;
                                else{
                                    value=automatedTellerMachine.getChangeMoney()*0.05;
                                }
                                if(automatedTellerMachine.getCard().getAccount().getBalance()-automatedTellerMachine.getChangeMoney()<-50000){
                                    System.out.println("Sorry,your account balance is insufficient.");
                                    return;
                                }
                           
                                automatedTellerMachine.getCard().getAccount().setBalance(automatedTellerMachine.getCard().getAccount().getBalance()-value);
                            }
                                automatedTellerMachine.changeYourMoney();
                                Bank nowBank=automatedTellerMachine.getCard().getAccount().getUser().getBank();
                                if(automatedTellerMachine.getChangeMoney()<0)
                                System.out.printf("业务:存款 "+automatedTellerMachine.getCard().getAccount().getUser().getName()+"在"+nowBank.getName()+"的"+ATMid+"号ATM机上存款¥%.2f\n",Math.abs(automatedTellerMachine.getChangeMoney()));
                                else{
                                    if(!automatedTellerMachine.getCard().getAccount().getUser().getBank().getAutomatedTellerMachineIdArrayList().contains(automatedTellerMachine.getId())){
                                        double rax=1;
                                        for(Card tempCard:automatedTellerMachine.getAllCard()){
                                            if(tempCard.getAccount().getUser().getBank().getAutomatedTellerMachineIdArrayList().contains(automatedTellerMachine.getId())){
                                                rax=tempCard.getAccount().getUser().getBank().getInterbankWithdrawalFees();
                                                nowBank=tempCard.getAccount().getUser().getBank();
                                                break;
                                            }
                                        }
                                        if(automatedTellerMachine.getCard().getAccount().getBalance()-rax* automatedTellerMachine.getChangeMoney()<0&&automatedTellerMachine.getCard().getType()==0){
                                            System.out.println("Sorry,your account balance is insufficient.");
                                            return;
                                        }
                                        automatedTellerMachine.getCard().getAccount().setBalance(automatedTellerMachine.getCard().getAccount().getBalance()-rax* automatedTellerMachine.getChangeMoney());
                                    }
                                    System.out.printf("业务:取款 "+automatedTellerMachine.getCard().getAccount().getUser().getName()+"在"+nowBank.getName()+"的"+ATMid+"号ATM机上取款¥%.2f\n",automatedTellerMachine.getChangeMoney());
                                }
                                System.out.printf("当前余额为¥%.2f\n",automatedTellerMachine.getCard().getAccount().getBalance());
                            }
                        }
                    }
                }
            }
        }
    }
class ChinaUnionPay {
    private String name;
    ChinaUnionPay(String name){
        this.name=name;
    }
    ChinaUnionPay(){}
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
class Bank {
    private ChinaUnionPay chinaUnionPay;
    private String name;
    private double interbankWithdrawalFees;
    private ArrayList<String> automatedTellerMachineIdArrayList;

    public ChinaUnionPay getChinaUnionPay() {
        return chinaUnionPay;
    }

    public void setChinaUnionPay(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
    }

    public ArrayList<String> getAutomatedTellerMachineIdArrayList() {
        return automatedTellerMachineIdArrayList;
    }

    public void setAutomatedTellerMachineIdArrayList(ArrayList<String> automatedTellerMachineIdArrayList) {
        this.automatedTellerMachineIdArrayList = automatedTellerMachineIdArrayList;
    }

    public String getName() {
        return name;
    }

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

    public double getInterbankWithdrawalFees() {
        return interbankWithdrawalFees;
    }

    public void setInterbankWithdrawalFees(double interbankWithdrawalFees) {
        this.interbankWithdrawalFees = interbankWithdrawalFees;
    }

    public Bank(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
        this.automatedTellerMachineIdArrayList=new ArrayList<>();
    }
    public void initBank1(){
        ArrayList<String>tempArrayList=new ArrayList<>();
        for(int i=1;i<=4;i++){
            String temp="0"+i;
            tempArrayList.add(temp);
        }
        this.automatedTellerMachineIdArrayList=tempArrayList;
        this.name="中国建设银行";
        this.interbankWithdrawalFees=0.02;
    }
    public void initBank2(){
        ArrayList<String>tempArrayList=new ArrayList<>();
        for(int i=5;i<=6;i++){
            String temp="0"+i;
            tempArrayList.add(temp);
        }
        this.automatedTellerMachineIdArrayList=tempArrayList;
        this.name="中国工商银行";
        this.interbankWithdrawalFees=0.03;
    }
    public void initBank3(){
        ArrayList<String>tempArrayList=new ArrayList<>();
        for(int i=7;i<=9;i++){
            String temp="0"+i;
            tempArrayList.add(temp);
        }
        for(int i=0;i<=1;i++){
            String temp="1"+i;
            tempArrayList.add(temp);
        }
        this.automatedTellerMachineIdArrayList=tempArrayList;
        this.name="中国农业银行";
        this.interbankWithdrawalFees=0.04;
    }
}
class User {
    private Bank bank;
    private String name;

    public Bank getBank() {
        return bank;
    }

    public void setBank(Bank bank) {
        this.bank = bank;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void user1Init(Bank bank){
        this.name="杨过";
        this.bank=bank;
    }
    public void user2Init(Bank bank){
        this.name="郭靖";
        this.bank=bank;
    }
    public void user3Init(Bank bank){
        this.name="张无忌";
        this.bank=bank;
    }
    public void user4Init(Bank bank){
        this.name="韦小宝";
        this.bank=bank;
    }
    public void user5Init(Bank bank){
        this.name="张三丰";
        this.bank=bank;
    }
    public void user6Init(Bank bank){
        this.name="令狐冲";
        this.bank=bank;
    }
    public void user7Init(Bank bank){
        this.name="乔峰";
        this.bank=bank;
    }
    public void user8Init(Bank bank){
        this.name="洪七公";
        this.bank=bank;
    }
}
class Account {
    private User user;
    private String id;
    private int type;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    private double balance;
    Account(){
        this.balance=10000.0;
        type=0;
    }
    private String key;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    public void yangGuoAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3217000010041315709";
    }
    public void yangGuoAccount2Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3217000010041315715";
    }
    public void guoJinAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3217000010051320007";
    }
    public void zhangWuJiAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502001312389";
    }
    public void zhangWuJiAccount2Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502001312390";
    }
    public void zhangWuJiAccount3Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502001312399";
    }
    public void weiXiaoBaoAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502051320785";
    }
    public void weiXiaoBaoAccount2Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3222081502051320786";
    }
    public void zhangSanFengAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3640000010045442002";
        this.type=1;
    }
    public void lingHuChongAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3640000010045441009";
        this.type=1;
    }
    public void qiaoFengAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3630000010033431001";
        this.type=1;
    }
    public void HongQiGongAccount1Init(User user){
        this.user=user;
        this.key="88888888";
        this.id="3630000010033431008";
        this.type=1;
    }
}
class Card {
    private Account account;
    private String id;
    private int type;
    public Account getAccount() {
        return account;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

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

    public Card(Account account, String id) {
        this.account = account;
        this.id = id;
        this.type=0;
    }
    public Card(int type,Account account, String id) {
        this.account = account;
        this.id = id;
        this.type=type;
    }
    public Card(){}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
class AutomatedTellerMachine {
    private Card card;;

    AutomatedTellerMachine(){
        allCard =new ArrayList<>();
    }
    private ArrayList<Card> allCard;

    public ArrayList<Card> getAllCard() {
        return allCard;
    }

    public void setAllCard(ArrayList<Card> allCard) {
        this.allCard = allCard;
    }

    private String id;
    private String key;

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public double getChangeMoney() {
        return changeMoney;
    }

    public void setChangeMoney(double changeMoney) {
        this.changeMoney = changeMoney;
    }

    private double changeMoney;
    public boolean findCard(String cardId){
        for(Card tempCard : allCard){
            if(tempCard.getId().equals(cardId)) {
                this.card = tempCard;return true;
            }
        }
        return false;
    }
    public void findBalance(){
        System.out.printf("业务:查询余额 ¥%.2f\n", card.getAccount().getBalance());
    }
    public boolean checkYourBalance(){
        if(card.getAccount().getBalance()-changeMoney<0)
            return false;
        return true;
    }
    public void changeYourMoney(){
        card.getAccount().setBalance(card.getAccount().getBalance()-changeMoney);
    }
}

踩坑心得

  • 印象最深的就是训练集六的ATM机
    最开始的整个类设计全是反过来的,银联包含银行列表,银行包含用户列表,用户包含账户列表,账户包含银行卡列表。这种思路下每进行下一步都是举步维艰,虽然它的逻辑比较清晰,但是当结合ATM机存钱款或查余额时,就会出现各种各样的逻辑BUG,不知道折磨了我多久......

    还有跨行和透支的极限值问题,没有想清楚再写,慢慢调试浪费了很多不应该的时间,很多判断方法一定得先想清楚顺序,顺序不一样会产生天翻地覆的结果。
  • 训练集六的水文数据处理
    有一个测试点是专门考察有没有去前置空格和尾部空格的,我调试了很久才成功,只能说吃亏吃麻了。而我也知道了java中去前置零的快速方法:
    string0=string0.trim();

总结

在这三次训练集中:
我的收获:

  • 能够灵活的使用正则表达式(真香)
  • 巩固了继承与多态的相关知识
  • 对IDEA的调试能力进一步提升
  • 理解题目的能力更加强大了

近期的目标:

  • 学习Collections的各种API使用
  • 做一些关于设计模式的心得笔记
posted @   AI-xiong  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示