首页 > 其他分享 >训练集04-06

训练集04-06

时间:2023-04-28 17:35:22浏览次数:43  
标签:输出 06 04 训练 样例 new public 输入 String

目录

(1)前言

知识点 题量 难度
训练集04 HashSet、Array List、一维数组、 封装性、日期类使用 12道 第一题★★★其余题★
训练集05 正则表达式、字符串、聚合 9道 第六七题★★其余题★
训练集06 继承与多态、正则表达式、封装性 4道 第三题★★第四五题★★★其余题★

(2)设计与分析

训练集04:

7-2 有重复的数据

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

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

输入格式:

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

输出格式:

如果这些整数中存在重复的,就输出:

YES

否则,就输出:

NO

输入样例:

5
1 2 3 1 4

输出样例:

YES
import java.util.HashSet;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //HashSet来存储数据,保证数据不重复
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            int num = sc.nextInt();
            //如果set中已经有该数据,说明有重复
            if(set.contains(num)){
                System.out.println("YES");
                return;
            }
            set.add(num);
        }
        System.out.println("NO");
    }
        
}

7-3 去掉重复的数据

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

你要写一个程序来做这件事情,读入数据,检查是否有重复的数据。如果有,去掉所有重复的数字。最后按照输入顺序输出没有重复数字的数据。所有重复的数字只保留第一次出现的那份。

输入格式:

你的程序首先会读到一个正整数 n,1≤n≤100000。
然后是 n 个整数,这些整数的范围是 [1, 100000]。

输出格式:

在一行中按照输入顺序输出去除重复之后的数据。每两个数据之间有一个空格,行首尾不得有多余空格。

输入样例:

5
1 2 2 1 4

输出样例:

1 2 4
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashSet<Integer> set = new HashSet<>();
        //第一个数字一定输出 并且前面不含空格 其他数字输出前面含空格
        //保证末尾没有多余空格
        //如果在循环中判断i=0 则会超时 于是放在循环外 循环从1开始
        int num = sc.nextInt();
        set.add(num);
        System.out.print(num);

        for (int i = 1; i < n; i++) {
            num = sc.nextInt();
            if(set.contains(num)){
                continue;
            }

            set.add(num);
            System.out.print(" "+num);

        }
//        for (int x:set){
//
//            System.out.print("\n"+x);
//        }

    }
}

7-4 单词统计与排序

从键盘录入一段英文文本(句子之间的标点符号只包括“,”或“.”,单词之间、单词与标点之间都以" "分割。
要求:按照每个单词的长度高到低输出各个单词(重复单词只输出一次),如果单词长度相同,则按照单词的首字母顺序(不区分大小写,首字母相同的比较第二个字母,以此类推)升序输出。

输入格式:

一段英文文本。

输出格式:

按照题目要求输出的各个单词(每个单词一行)。

输入样例:

Hello, I am a student from China.

输出样例:

student
China
Hello
from
am
a
I
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String sentence = sc.nextLine();
        String[] words = sentence.split("[ ,.]+");//+消除中间许多匹配符号分割
        HashSet<String> set = new HashSet<>(Arrays.asList(words));//转换成set 去除重复数据
       ArrayList <String> list = new ArrayList<>(set); // 将set转换成list,方便排序
        Collections.sort(list, new Comparator<String>() { // 重写排序
            @Override
            public int compare(String s1, String s2) {
                if (s1.length() != s2.length()) { // 如果长度不相等,按照长度降序排序
                    return s2.length() - s1.length();
                } else { // 如果长度相等,按照字典序升序排序
                    return s1.compareToIgnoreCase(s2);
                }
            }
        });
        for (String s:list){
            System.out.println(s);
        }


    }
}

7-5 面向对象编程(封装性)

Student类具体要求如下:
私有成员变量:学号(sid,String类型),姓名(name,String类型),年龄(age,int类型),专业(major,String类型) 。
提供无参构造和有参构造方法。(注意:有参构造方法中需要对年龄大小进行判定)
普通成员方法:print(),输出格式为“学号:6020203100,姓名:王宝强,年龄:21,专业:计算机科学与技术”。
普通成员方法:提供setXxx和getXxx方法。(注意:setAge()方法中需要对年龄进行判定)
注意:
年龄age不大于0,则不进行赋值。
print()中的“:”和“,”为均为中文冒号和逗号。

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}

/* 请在这里填写答案 */

输入格式:

输出格式:

学号:6020203110,姓名:王宝强,年龄:21,专业:计算机科学与技术
学号:6020203119,姓名:张三丰,年龄:23,专业:软件工程

输入样例:

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

6020203110 王宝强 21 计算机科学与技术
6020203119 张三丰 23 软件工程

输出样例:

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

学号:6020203110,姓名:王宝强,年龄:21,专业:计算机科学与技术
学号:6020203119,姓名:张三丰,年龄:23,专业:软件工程
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //调用无参构造方法,并通过setter方法进行设值
        String sid1 = sc.next();
        String name1 = sc.next();
        int age1 = sc.nextInt();
        String major1 = sc.next();
        Student student1 = new Student();
        student1.setSid(sid1);
        student1.setName(name1);
        student1.setAge(age1);
        student1.setMajor(major1);
        //调用有参构造方法
        String sid2 = sc.next();
        String name2 = sc.next();
        int age2 = sc.nextInt();
        String major2 = sc.next();
        Student student2 = new Student(sid2, name2, age2, major2);
        //对学生student1和学生student2进行输出
        student1.print();
        student2.print();
    }
}

/* 请在这里填写答案 */




class Student {
        private String sid;
        private String name;
        private int age;
        private String major;
        //无参数构造
        public Student() {
        }
        //有参数构造
        public Student(String sid, String name, int age, String major) {
            this.sid = sid;
            this.name = name;
            if (age > 0) {
                this.age = age;
            }
            this.major = major;
        }
       
        public String getSid() {
            return sid;
        }
        public void setSid(String sid) {
            this.sid = sid;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            if (age > 0) {
                this.age = age;
            }
        }
        public String getMajor() {
            return major;
        }
        public void setMajor(String major) {
            this.major = major;
        }
    public void print() {
        System.out.printf("学号:%s,姓名:%s,年龄:%d,专业:%s\n",sid,name,age,major);
    }
}

7-6 GPS测绘中度分秒转换

在测绘中,获取经度和纬度信息的时候,可以是度分秒格式,也可以是小数点格式。例如一个北纬的纬度信息,30°41′52.37″ ,可以转换为 30.697881。

规则:
(1)度和分都是整数,秒可以含有小数。将用户输入的第一个整数,加上第二个整数除以60,再加上第三个浮点数除以3600,就是最终结果。

(2)在输出结果时,保留6位小数。

(3)题目假定用户输入的数据,一定是合法的数据。

输入格式:

三个数值,数之间用空格分开。

输出格式:

见输出样例。

输入样例:

两个整数后,跟上一个小数。数据之间用空格分开。三个数分别代表度、分、秒。

30 41 52.37

输出样例:

输出经纬度信息的小数点格式,保留6位小数。
注意等号的前后有且仅有一个空格,建议复制下面输出到你的代码中,再进行修改。

30°41′52.37″ = 30.697881
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        //找到三个字串
        int pos1=s1.indexOf(" ");
        int pos2=s1.indexOf(" ",pos1+1);
//        System.out.println(pos1);
//        System.out.println(pos2);
        int degree= Integer.parseInt(s1.substring(0,pos1));
        double minute=  Double.parseDouble(s1.substring(pos1+1,pos2));
        double second= Double.parseDouble(s1.substring(pos2+1));
//System.out.println(degree);
//System.out.println(s1.substring(pos1+1,pos2));
//System.out.println(s1.substring(pos2+1));
   System.out.printf("%d°%s′%s″ = %.6f\n",degree,s1.substring(pos1+1,pos2),s1.substring(pos2+1),degree+minute/60+second/3600);


    }
}

7-7 判断两个日期的先后,计算间隔天数、周数

从键盘输入两个日期,格式如:2022-06-18。判断两个日期的先后,并输出它们之间间隔的天数、周数(不足一周按0计算)。

预备知识:通过查询Java API文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。

输入格式:

输入两行,每行输入一个日期,日期格式如:2022-06-18

输出格式:

第一行输出:第一个日期比第二个日期更早(晚)
第二行输出:两个日期间隔XX天
第三行输出:两个日期间隔XX周

输入样例1:

2000-02-18
2000-03-15

输出样例1:

第一个日期比第二个日期更早
两个日期间隔26天
两个日期间隔3周

输入样例2:

2022-6-18
2022-6-1

输出样例2:

第一个日期比第二个日期更晚
两个日期间隔17天
两个日期间隔2周
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String dateStr1 = scanner.nextLine();
        String dateStr2 = scanner.nextLine();
        String[] date1 = dateStr1.split("-");
        String[] date2 = dateStr2.split("-");
        LocalDate localDate1 = LocalDate.of(Integer.parseInt(date1[0]), Integer.parseInt(date1[1]), Integer.parseInt(date1[2]));
        LocalDate localDate2 = LocalDate.of(Integer.parseInt(date2[0]), Integer.parseInt(date2[1]), Integer.parseInt(date2[2]));
        if (localDate1.isBefore(localDate2)) {
            System.out.println("第一个日期比第二个日期更早");
            System.out.println("两个日期间隔" + localDate1.until(localDate2, ChronoUnit.DAYS) + "天");
            System.out.println("两个日期间隔" + localDate1.until(localDate2, ChronoUnit.WEEKS) + "周");
        } else {
            System.out.println("第一个日期比第二个日期更晚");
            System.out.println("两个日期间隔" + localDate2.until(localDate1, ChronoUnit.DAYS) + "天");
            System.out.println("两个日期间隔" + localDate2.until(localDate1, ChronoUnit.WEEKS) + "周");
        }
    }
}

训练集05:

7-1 正则表达式训练-QQ号校验

校验键盘输入的 QQ 号是否合格,判定合格的条件如下:

  • 要求必须是 5-15 位;
  • 0 不能开头;
  • 必须都是数字;

输入格式:

在一行中输入一个字符串。

输出格式:

  • 如果合格,输出:“你输入的QQ号验证成功”;
  • 否则,输出:“你输入的QQ号验证失败”。

输入样例1:

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

1234567890

输出样例1:

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

你输入的QQ号验证成功

输入样例2:

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

123456789O

输出样例2:

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

你输入的QQ号验证失败
import java.util.*;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String number = sc.nextLine();
        //运用正则表达式判断
        if(Pattern.matches("^[1-9]\\d{4,14}", number)){
            System.out.println("你输入的QQ号验证成功");
            return;
        }
        System.out.println("你输入的QQ号验证失败");
    }
}

7-2 字符串训练-字符排序

对输入的字符串中的字符进行排序并输出。

输入格式:

在一行内输入一个字符串。

输出格式:

对该字符串内的字符进行排序后(按ASCII码进行升序排序)输出。

输入样例:

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

h!dy%2dh1

输出样例:

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

!%12ddhhy
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        //将字符串转换成字符数组 方便排序
        char[] chars = str.toCharArray();
        //默认按照字典序升序
        Arrays.sort(chars);
        System.out.println(chars);
    }
}

7-3 正则表达式训练-验证码校验

接受给定的字符串,判断该字符串是否属于验证码。验证码是由四位数字或者字母(包含大小写)组成的字符串。

输入格式:

在一行内输入一个字符串。

输出格式:

判断该字符串是否符合验证码规则,若是验证码,输出字符串是验证码,否则输出字符串不是验证码。

输入样例1:

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

123A

输出样例1:

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

123A属于验证码

输入样例2:

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

12?AD

输出样例2:

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

12?AD不属于验证码
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        if(str.matches("[A-Z a-z 0-9]{4}")){
            System.out.println(str + "属于验证码");
            return;
        }
        System.out.println(str + "不属于验证码");


    }
}

7-4 正则表达式训练-学号校验

对软件学院2020级同学学号进行校验,学号共八位,规则如下:

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

要求如下:

  • 只针对2020级
  • 其中软件工程专业班级分别为:20201117、61,物联网工程专业班级为202071202073,数据科学与大数据专业班级为202081~82
  • 每个班级学号后两位为01~40

输入格式:

在一行输入一个字符串。

输出格式:

若符合规则的学号,输出”正确“,若不符合,输出”错误“。

输入样例1:

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

20201536

输出样例1:

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

正确

输入样例2:

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

20201541

输出样例2:

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

错误
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        if(str.matches("2020(1[1-7]|61|7[1-3]|8[1-2])[0-3][1-9]|40")){
            System.out.println("正确");
            return;
        }
        System.out.println("错误");


    }
}

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

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

类图.jpg

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

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用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:

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

3 2014 2 14 2020 6 14

输出样例1:

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

2312

输入样例2:

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

2 1935 2 17 125340

输出样例2:

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

1591-12-17

输入样例3:

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

1 1999 3 28 6543

输出样例3:

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

2017-2-24

输入样例4:

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

0 2000 5 12 30

输出样例4:

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

Wrong Format

分析

与之前的题目思路一致,但是是换了聚合的形式

7-6 日期问题面向对象设计(聚合二)

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

类图.jpg

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

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数

注意:严禁使用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:值
    

输入样例1:

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

3 2014 2 14 2020 6 14

输出样例1:

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

The days between 2014-2-14 and 2020-6-14 are:2312

输入样例2:

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

2 1834 2 17 7821

输出样例2:

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

1834-2-17 previous 7821 days is:1812-9-19

输入样例3:

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

1 1999 3 28 6543

输出样例3:

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

1999-3-28 next 6543 days is:2017-2-24

输入样例4:

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

0 2000 5 12 30

输出样例4:

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

Wrong Format
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int flag =sc.nextInt();
        if (flag == 1){
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            int n =sc.nextInt();
            DateUtil myDateUtil = new DateUtil(year, month, day);
            if(!myDateUtil.checkInputValidity()){
                System.out.println("Wrong Format");
                return;
            }
            myDateUtil = myDateUtil.getNextDays(n);
            System.out.print(year + "-" + month + "-" + day + " " + "next " + n + " " + "days is:");
            System.out.println(myDateUtil.showDate());
        }
        else if(flag == 2){
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            int n=sc.nextInt();
            DateUtil myDateUtil =new DateUtil(year,month,day);
            if (!myDateUtil.checkInputValidity()) {
                System.out.println("Wrong Format");
                return;
            }
            System.out.print(year + "-" + month + "-" + day + " " + "previous " + n + " " + "days is:");
            System.out.println(myDateUtil.getPreviousNDays(n).showDate());

        } else if (flag == 3) {
            int year1 = sc.nextInt();
            int month1 = sc.nextInt();
            int day1 = sc.nextInt();
            int year2 = sc.nextInt();
            int month2 = sc.nextInt();
            int day2 = sc.nextInt();
            DateUtil myDateUtil1 =new DateUtil(year1,month1,day1);
            DateUtil myDateUtil2 =new DateUtil(year2,month2,day2);
            if (!myDateUtil1.checkInputValidity()||!myDateUtil2.checkInputValidity()) {
                System.out.println("Wrong Format");
                return;
            }
            System.out.print("The days between " + year1 + "-" + month1 + "-" + day1 + " " + "and" + " " + year2 + "-" + month2 + "-" + day2 + " are:");
            System.out.println(Math.abs(myDateUtil1.getDaysOfDate(myDateUtil2)));

        }else {
            System.out.println("Wrong Format");
        }


    }
}

class Year{
    private  int value;
    public  Year(){}
    public  Year(int value){
        this.value=value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
    public boolean isLeapYear(){
        if(value%400==0||value%4==0&&value%100!=0){
            return  true;
        }
        return  false;
    }
    public boolean validate(){
        if (value>=1900&&value<=2050){
            return  true;
        }
        return  false;
    }
    public  void yearIncrement(){
        value+=1;
    }
    public  void yearReduction(){
        value -= 1;
    }
}
class Month{
    private  int value ;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
    public Month(){}
    public Month(int  value) {
        this.value = value;

    }
    public  void resetMin(){
        value =1 ;
    }
    public  void resetMax(){
        value = 12;
    }
    public  boolean validate() {
        if(value>=1&&value<=12){
            return  true;
        }
        return  false;
    }
    public  void monthIncrement(){
        value+=1;
    }
    public  void monthReduction(){
        value -=1;
    }
}
class Day{
    private int value;

    public Day(){

    }
    public Day(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public void dayIncrement(){
        value ++;
    }

    public void dayReduction(){
        value --;
    }

}

class  DateUtil{
    private Day day;
    private Year year;
    private  Month month;
    int[] mon_maxnum = {0 , 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,0,0,0,0,};
    public  DateUtil(){

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

    public Day getDay() {
        return day;
    }

    public Year getYear() {
        return year;
    }

    public Month getMonth() {
        return month;
    }
    public void setDay(Day day) {
        this.day = day;
    }

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

    public void setMonth(Month month) {
        this.month = month;
    }
    public void setDayMin(){
        day.setValue(1);
    }
    public  void setDayMax(){
        day.setValue(mon_maxnum[month.getValue()]);
    }


    //校验输入数据合法性
    public  boolean checkInputValidity(){
        if(  year.getValue() <= 2020 && year.getValue() >= 1820 &&
                month.getValue() <= 12 && month.getValue() >= 1 &&
                day.getValue() >= 1 && day.getValue() <= mon_maxnum[month.getValue()]){
            return  true;
        }
        return  false;
    }
    //比较日期大小
    public boolean compareDates(DateUtil date){
        if (year.getValue() > date.year.getValue()){
            return true;
        }
        if(year.getValue()==date.year.getValue()){
            if(month.getValue()>date.month.getValue()){
                return  true;
            }
        }
        if(month.getValue()>date.month.getValue()){
            if (day.getValue()>date.day.getValue()){
                return true;
            }
        }
        return false;

    }
    //判断日期是否相等
    public boolean equalTwoDates(DateUtil date) {
        if( year.getValue() == date.year.getValue() && month.getValue() == date.month.getValue() && day.getValue() == date.day.getValue()){
            return true;
        }
        return false;
    }
    //日期格式化输出
    public String showDate(){
        return year.getValue() + "-" + month.getValue() + "-" + day.getValue();
    }
    //求下n天
    public  DateUtil getNextDays(int n){
        while (n>0){
            if(year.isLeapYear()) {
                mon_maxnum[2] = 29;
            }else {
                mon_maxnum[2] = 28;
            }
            day.dayIncrement();
            if(day.getValue()>mon_maxnum[month.getValue()]){
               month.monthIncrement();
               setDayMin();
            }
            if(month.getValue()>12){
               year.yearIncrement();
                month.setValue(1);
            }
            n--;

        }
        return new DateUtil(year.getValue(),month.getValue(),day.getValue());

    }

    //求前n天
    public  DateUtil getPreviousNDays(int n){
        while (n>0){
            if(year.isLeapYear()) {
                mon_maxnum[2] = 29;
            }else {
                mon_maxnum[2] = 28;
            }
            day.dayReduction();
            if(day.getValue()<1){

                month.monthReduction();
                if(month.getValue()<1){
                    year.yearReduction();
                    month.resetMax();

                }
                setDayMax();
            }

            n--;

        }
        return new DateUtil(year.getValue(),month.getValue(),day.getValue());

    }
    //求两日期之间天数
    public  int getDaysOfDate(DateUtil date){
        int count =0 ;
        DateUtil formerDate = new DateUtil();
        DateUtil latterDate = new DateUtil();
        if(this.equalTwoDates(date)){
            return  0;
        }
        if(this.compareDates(date)){//his >date
            latterDate = this;
            formerDate = date;
        }
        else{
            latterDate = date;
            formerDate = this;
        }

        while (!formerDate.equalTwoDates(latterDate)){
            if(formerDate.year.isLeapYear()){
                formerDate.mon_maxnum[2] = 29;
            }
            else{
                formerDate.mon_maxnum[2] = 28;
            }
            formerDate.day.dayIncrement();
            if(formerDate.day.getValue()>formerDate.mon_maxnum[formerDate.getMonth().getValue()]){
                formerDate.month.monthIncrement();

                formerDate.setDayMin();

            }
            if(formerDate.month.getValue()>12){

                formerDate.year.yearIncrement();

               formerDate.month.resetMin();
            }
            count++;
        }

        return  count;
    }
}

训练集06:

7-1 图形继承

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
  5. 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
  6. 注意:
  • 每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数

主方法内,主要实现四个功能(1-4):
从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积;
从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积;
从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积;
从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;

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

输入格式:

共四种合法输入

  • 1 圆半径
  • 2 矩形宽、长
  • 3 球半径
  • 4 立方体宽、长、高

输出格式:

按照以上需求提示依次输出

输入样例1:

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

1 1.0

输出样例1:

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

Constructing Shape
Constructing Circle
Circle's area:3.14

输入样例2:

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

4 3.6 2.1 0.01211

输出样例2:

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

Constructing Shape
Constructing Rectangle
Constructing Box
Box's surface area:15.26
Box's volume:0.09

输入样例3:

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

2 -2.3 5.110

输出样例2:

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

Wrong Format
import  java.util.*;
public class Main {
    public static void main(String[] args) {
        double radius ,width,length,height;
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                 radius = sc.nextDouble();
                if(radius < 0){
                    System.out.println("Wrong Format");
                    return;
                }
                Circle circle = new Circle(radius);
                System.out.printf("Circle's area:%.2f",circle.getArea());
                break;
            case 2:
               width = sc.nextDouble();
                 length = sc.nextDouble();
                if(width<0 || length <0 ){
                    System.out.println("Wrong Format");
                    return;
                }
                Rectangle rectangle = new Rectangle(width,length);
                System.out.printf("Rectangle's area:%.2f",rectangle.getArea());
                break;
            case 3:
                 radius = sc.nextDouble();
                if(radius < 0){
                    System.out.println("Wrong Format");
                    return;
                }
                Ball ball = new Ball(radius);
                System.out.printf("Ball's surface area:%.2f\n",ball.getArea());
                System.out.printf("Ball's volume:%.2f\n",ball.getVolume());
                break;
            case 4:
                width = sc.nextDouble();
                length = sc.nextDouble();
                height = sc.nextDouble();
                if (width<0||length<0||height<0){
                    System.out.println("Wrong Format");
                    return;
                }
                Box box = new Box(width,length,height);
                System.out.printf("Box's surface area:%.2f\n",box.getArea());
                System.out.printf("Box's volume:%.2f\n",box.getVolume());
                break;
            default:
                System.out.println("Wrong Format");
                break;


        }


    }
}

  class Shape {
    public Shape(){
        System.out.println("Constructing Shape");
    }
    public  double getArea(){return 0;}
}
  class Circle extends Shape {
    private double radius;

    public double getRadius() {
        return radius;
    }

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

    public  Circle(){
        System.out.println("Constructing Circle");
    }
    public  Circle(double radius){

        System.out.println("Constructing Circle");
        this.radius=radius;
    }

    @Override
    public double getArea() {
        return Math.PI*radius*radius;
    }
}
  class Rectangle extends  Shape {
    private double width;
    private double length;

    public double getWidth() {
        return width;
    }

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

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

    public double getLength() {
        return length;
    }

    public Rectangle(){
        System.out.println("Constructing Rectangle");
    }
    public Rectangle(double width,double length){
        System.out.println("Constructing Rectangle");
        this.length = length;
        this.width = width;
    }

    @Override
    public double getArea() {
        return width*length;
    }
}
   class Ball  extends Circle {
    public Ball(){
        System.out.println("Constructing Ball");


    }
    public  Ball(double radius){
        System.out.println("Constructing Ball");
        setRadius(radius);
    }



    @Override
    public double getArea() {
        return super.getArea()*4;
    }
    public double getVolume(){
        return 4.0/3*Math.PI*getRadius()*getRadius()*getRadius();
    }
}

   class Box extends Rectangle {
    private double height;
    public Box(){
        System.out.println("Constructing Box");

    }
    public Box(double width,double length,double height){
        System.out.println("Constructing Box");

        setWidth(width);
        setLength(length);
        this.height = height;
    }

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

    public double getHeight() {
        return height;
    }

    @Override
    public double getArea() {
        return 2*(height*getLength()+height*getWidth()+getWidth()*getLength());
    }
    public double getVolume(){
        return  getWidth()*height*getLength();
    }

}

7-2 继承与多态

编写程序,实现图形类的继承,并定义相应类对象并进行测试。

  1. 类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
  2. 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
  3. 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
  4. 注意:
  • 每个类均有构造方法,
  • 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
  • 输出的数值均保留两位小数,PI的取值使用Math.PI。

主类内的主方法中,主要实现两个功能(1-2):
从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积;
从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积;

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

public static void showArea(Shape shape);

各图形输出面积均要调用该方法(实现多态)

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

输入格式:

共四种合法输入

  • 1 圆半径
  • 2 矩形宽、长

输出格式:

按照以上需求提示依次输出

输入样例1:

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

1 1.0

输出样例1:

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

3.14

输入样例2:

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

2 -2.3 5.110

输出样例2:

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

Wrong Format
import  java.util.*;
public class Main {
    public static void main(String[] args) {
        double radius ,width,length,height;
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        switch (choice){
            case 1:
                 radius = sc.nextDouble();
                if(radius < 0){
                    System.out.println("Wrong Format");
                    return;
                }
                Circle circle = new Circle(radius);
                showArea(circle);
                break;
            case 2:
               width = sc.nextDouble();
                 length = sc.nextDouble();
                if(width<0 || length <0 ){
                    System.out.println("Wrong Format");
                    return;
                }
                Rectangle rectangle = new Rectangle(width,length);
                showArea(rectangle);
                break;

            default:
                System.out.println("Wrong Format");
                break;


        }
    }
    public static void showArea(Shape shape){
        System.out.printf("%.2f",shape.getArea());
    };

}
  class Shape {
    public Shape(){

    }
    public  double getArea(){return 0;}
}
  class Rectangle extends  Shape {
    private double width;
    private double length;

    public double getWidth() {
        return width;
    }

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

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

    public double getLength() {
        return length;
    }

    public Rectangle(){

    }
    public Rectangle(double width,double length){

        this.length = length;
        this.width = width;
    }

    @Override
    public double getArea() {
        return width*length;
    }
}
   class Circle extends Shape {
    private double radius;

    public double getRadius() {
        return radius;
    }

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

    public  Circle(){

    }
    public  Circle(double radius){


        this.radius=radius;
    }

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



7-3 水文数据校验及处理

使用Java中的字符串处理类以及正则表达式对输入字符串数据进行合法性校验及计算。(具体需求参见附件
2021-OO第04次作业-1指导书V1.0.pdf
)

输入格式:

假定分水口门的数据上报时是采用人工输入的方式,每一行代表一个整点时刻的分水数据,各数据之间采用“|”符号进行分隔,每次可以输入多条数据,直到遇到用户输入“exit”为止,每一行输入数据共包含五部分:测量时间、目标水位、实际水位、开度(包含目标开度和实际开度,以“/”分隔)、流量。
各数据格式要求如下:

  1. 测量时间:格式为“年/月/日 时:分”,其中年份取值范围为[1,9999],“月”与“日”为一位数时之前不加“0”,日期与时间之间有一个空格,“时”与“分”之间采用冒号分隔(英文半角),“时”为一位数时之前不加“0”,“分”始终保持两位,且始终为“00”。注意:“时”数必须是24小时进制中的偶数值。
  2. 目标水位、实际水位、流量:均为实型数,取值范围为[1,1000), 小数点后保留1-3位小数或无小数(也无小数点)
  3. 目标开度、实际开度:实型数,取值范围为[1,10),必须保留2位小数,两个开度之间用“/”分隔

输出格式:

  1. 对输入的数据进行有效性校验,其规则如前所述,如遇到不符合规则的数据,系统应能够给出错误提示,提示规则如下:
  • 如果每一行输入数据不是由“|”分隔的五部分,则输出:

    Wrong Format
    Data:输入的数据
    
  • 如果某一部分数据有误,则按如下方式显示:

    Row:行号,Column:列号Wrong Format
    Data:输入的数据
    

    其中,行号为输入数的行数(从1开始),列号为6个数据的序号(从1开始,最大为6,顺序参见输入数据结构说明)

  1. 由于人工输入数据可能存在疏忽,在每一个输入数据两端均可能存在多余的空格,程序应该能够自动过滤这些空格(不报错)。
  2. 如果用户未输入数据,则直接输出Max Actual Water Level和Total Water Flow的值即可(均为0)
  3. 若输入无误,则对数据进行如下处理:
  • 当实际开度的值大于目标开度时,程序给出如下警告:

    Row:1 GateOpening Warning
    
  • 求出所输入数据中的最大实际水位值(保留2位小数),输出格式如下:
    Max Actual Water Level:实际水位值

  • 根据每个整点时刻的瞬时流量求出所输入的所有时段的总流量(保留2位小数),其计算公式为(参见作业指导书):

    $$p = \sum_{n=1}^N2*60*60*Flow$$
    

输出格式如下:

Total Water Flow:总流量值

输入样例1:

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

2015/8/2 4:00|133.8400|133.070|1.11/1.21|75.780
2015/8/2 6:00|133.840|133.080|11.11/1.11|72.8a0
2015/8/2 8:00|133.830|133.070|1.11/1.11|73.890
2015/8/2 10:00|133.820|133.080|1.11/1.11|74.380
exit

输出样例1:

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

Row:1,Column:2Wrong Format
Data:2015/8/2 4:00|133.8400|133.070|1.11/1.21|75.780
Row:2,Column:4Wrong Format
Row:2,Column:6Wrong Format
Data:2015/8/2 6:00|133.840|133.080|11.11/1.11|72.8a0

输入样例2:

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

2015/8/5 2:00|133.800|133.080|1.11/1.11|73.870
2015/8/5 4:00|133.800|133.070|1.11/1.11|73.330
2015/8/5 6:00|133.830|133.110|1.11/1.21|70.610
2015/8/5 8:00|133.860|133.140|1.11/1.11|73.430
2015/8/5 10:00|133.91|133.15|1.11/1.11|73.06
2015/8/5 12:00|133.900|133.110|1.16/1.11|75.460
2015/8/5 14:00|133.920|133.140|1.16/1.11|77.030
2015/8/5 16:00|133.92|133.16|1.16/1.91|79.4
2015/8/5 18:00|133.940|133.170|1.16/1.11|76.810
2015/8/5 20:00|133.94|133.19|1.16/1.11|74.53
2015/8/5 22:00|133.930|133.200|1.16/1.11|74.400
2015/8/6 0:00|133.930|133.200|1.16/1.11|73.150
2015/8/6 2:00|133.930|133.180|1.16/1.11|74.830
2015/8/6 4:00|133.910|133.180|1.16/1.11|    73.270
exit

输出样例2:

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

Row:3 GateOpening Warning
Row:8 GateOpening Warning
Max Actual Water Level:133.20
Total Water Flow:7510896.00
import  java.util.*;
public class Main {
    public  static boolean Flag= true;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String date ;
        double sumOfWaterFlow =0;
        double MaxActualWaterLevel =0;
        int countRow =0;
        while (true){
            date = sc.nextLine();
            if(date.equals("exit")){
                break;
            }
            countRow++;
            if(date.length()==0){
                continue;
            }
            String [] words = date.trim().split("\\|");
            if(!checkWords(words)){
                System.out.println("Wrong Format");
                System.out.println("Data:"+date);
                continue;
            }
            if(cheakEverySingleWord(words,countRow,date)){
                sumOfWaterFlow +=Double.valueOf(words[4])* 60 * 60 * 2;
                MaxActualWaterLevel =Math.max(MaxActualWaterLevel,Double.valueOf(words[2]));


            }


        }
        if(Flag){
            System.out.printf("Max Actual Water Level:%.2f\n",MaxActualWaterLevel);
            System.out.printf("Total Water Flow:%.2f\n",sumOfWaterFlow);

        }


    }

    public static boolean checkWords(String[] words){
        if(words.length!=5) {
            return false;
        }
        return true;
    }
    public static boolean cheakEverySingleWord(String[] words,int countRow,String data){
        boolean flag = true;
        //去除空格
        for(int i=0;i<=4;i++){
            words[i]=words[i].trim();
        }
        //检查日期
        String regex="(([1-9][0-9]{0,3})/(((1[0-2]|[1-9]))/([1-2][0-9]|30|[1-9])) ([02468]|1[02468]|2[02]|):00)";
        if(words[0].matches(regex)==false) {
            System.out.printf("Row:%d,Column:1Wrong Format\n",countRow);
            flag=false;
        }
        //检查水位

        String water ="([1-9][0-9]{0,2})(.[0-9]{1,3})";

        if(!words[1].matches(water)){System.out.printf("Row:%d,Column:2Wrong Format\n",countRow);flag=false;}

        if(!words[2].matches(water)){System.out.printf("Row:%d,Column:3Wrong Format\n",countRow);flag=false;}
        //检查开度
        String regex2="(([1-9])((.[0-9]{2}))|)";
        String[] towParts= words[3].split("\\/");
        if(words[3].contains("/")==false)
        {
            System.out.printf("Row:%d,Column:4Wrong Format\n",countRow);
            flag=false;
            System.out.printf("Row:%d,Column:5Wrong Format\n",countRow);
            flag=false;
        }
        else{
            //String[] temp1= s[3].split("\\/");
            if(towParts[0].matches(regex2)==false) {
                System.out.printf("Row:%d,Column:4Wrong Format\n",countRow);//第四个//中间分两个
                flag=false;
            }
            if(towParts[1].matches(regex2)==false) {
                System.out.printf("Row:%d,Column:5Wrong Format\n",countRow);//第四个//中间分两个
                flag=false;
            }
        }
        //检查流量
        if(!words[4].matches(water)){System.out.printf("Row:%d,Column:6Wrong Format\n",countRow);flag=false;}

        //开度警告
        if(flag) {
            if (Double.valueOf(towParts[0]) < Double.valueOf(towParts[1])) {
                System.out.printf("Row:%d GateOpening Warning\n", countRow);
            }
        }
        if(flag==false){ System.out.println("Data:"+data);
        Flag=false;}
        return flag;


    }
}

7-4 ATM机类结构设计(一)

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

输入格式:

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

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

输出格式:

①输入错误处理

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

②取款业务输出

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

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

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

③存款业务输出

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

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

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

④查询余额业务输出

¥[金额]

金额保留两位小数。

输入样例1:

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

6222081502001312390 88888888 06 -500.00
#

输出样例1:

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

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

输入样例2:

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

6217000010041315709  88888888 02 3500.00
#

输出样例2:

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

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

输入样例3:

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

6217000010041315715
#

输出样例3:

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

¥10000.00

输入样例4:

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

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

输出样例4:

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

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

类图

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);
    }
}

7-5 ATM机类结构设计(二)

设计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.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);
    }
}

(3)踩坑心得

训练集04

7-2 7-3 不使用HashSet 容易超时 时候后在循环中,7 -3如果在循环中每次判断 i==0 则会超时 于是放在循环外 循环从1开始

(4)改进建议

(5)总结

经过这三次题目集的练习,学会了

1.使用HashSet 只存单一元素,可以用来去重,

2.ArrayList 重载compare函数 自定义排序,

3.for-each循环 遍历输出,

4.正则表达式 用来校验

5.对于封装性,继承与多态,聚合的进一步熟悉与掌握

标签:输出,06,04,训练,样例,new,public,输入,String
From: https://www.cnblogs.com/dsyyyyyyyy/p/17362750.html

相关文章

  • ubuntu18.04下 python虚拟环境安装
    #1.安装sudopipinstallvirtualenvsudopipinstallvirtualenvwrapper#2.很容易遇到的bug问题#安装完虚拟环境后,如果提示找不到mkvirtualenv命令,须配置环境变量#在这里配置环境变量时第2)步需要确定virtualenvwrapper的安装目录piplist#查看已安装的包pips......
  • KubeSphere 社区双周报 | 杭州站 Meetup 议题征集中 | 2023.04.14-04.27
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.04.14-2023.04.27。贡献者名单新晋KubeSphereCon......
  • Ubuntu 22.04 SSH the RSA key isn't working since upgrading from 20.04
    Ubuntu22.04SSHtheRSAkeyisn'tworkingsinceupgradingfrom20.04UpuntillastweekIwasrunningUbuntu20.04happily,andthenovertheweekenddecidedtobackeverythingupandinstall22.04.I'vehadacoupleofteethingissueswhichI&#......
  • 第八届河南省赛 zzuoj 10407: B.最大岛屿
    10407:B.最大岛屿TimeLimit:1SecMemoryLimit:128MBSubmit:29Solved:17[Submit][Status][WebBoard]Description神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等。加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要......
  • 第八届河南省赛 zzuoj 10409: D.引水工程 (最小生成树)
    10409:D.引水工程TimeLimit: 2Sec  MemoryLimit: 128MBSubmit: 111  Solved: 40[Submit][Status][WebBoard]Description南水北调工程是优化水资源配置、促进区域协调发展的基础性工程,是新中国成立以来投资额最大、涉及面最广的战略性工程,事关......
  • 第八届河南省赛 zzuoj 10411: F.Distribution (模拟)水
    10411:F.DistributionTimeLimit: 1Sec  MemoryLimit: 128MBSubmit: 10  Solved: 7[Submit][Status][WebBoard]DescriptionOneday,WangandDongintheDubaidesertexpedition,discoveredanancientcastle.Fortunately,theyfound......
  • [20230425]CBO cost与行迁移关系.txt
    [20230425]CBOcost与行迁移关系.txt--//一般现在很少使用analyzetable分析表,如果出现大量行迁移是否考虑看看是否考虑cbocost成本.--//测试参考链接:--//https://richardfoote.wordpress.com/2023/03/21/cbo-costing-plans-with-migrated-rows-part-i-ignoreland/--//https://......
  • NC50428 数星星 Stars
    题目链接题目题目描述天空中有一些星星,这些星星都在不同的位置,每个星星有个坐标。如果一个星星的左下方(包含正左和正下)有k颗星星,就说这颗星星是k级的。例如,上图中星星5是3级的(1,2,4在它左下),星星2,4是1级的。例图中有1个0级,2个1级,1个2级,1个3级的星星。给定星星的位置,输出各级......
  • 产品原型20-20230427
           ......
  • JAVA面向对象程序设计_PTA题目集04-06总结分析
    前言:JAVA_BLOG_PTA题目集4-6_总结分析 题目集四:知识点:大体如预备知识,即:通过查询JavaAPI文档,了解Scanner类中nextLine()等方法、String类中split()等方法、Integer类中parseInt()等方法的用法,了解LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,了解Chro......