OO第4-6次作业总结

前言:

在OO04次作业中,用到了Arrays、ArrayList、LinkedHashSet、字符串与字符串数组的转换、字符串数组里的方法、以及面向对象编程的封装性。

在OO05练习了运用正则表达式进行校验,学习并且巩固了正则表达式的相关知识点,还练习了根据类图去完成程序设计,运用的是类与类之间的聚合关系。

在OO06运用了继承及多态的知识点,还有根据类图提示设计程序,最主要的是最后两题有关ATM机的,结合生活实际,自己设计类图完成程序。

前两次的作业难度相对较低,最后一次的难度较高。是需要用心且花时间才可以做到全对的题目。

设计与分析:

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且输入均有效,输出格式如下:

    天数值

 这道题主要是考察聚合关系的运用,在月份类里有年份类这一属性,在日期类里有月分类这一属性,最后的DateUtil类里有日期类这一属性,日期这一类的题目已经做过很多次了,包括求下n天、前n天以及两个日期之间间隔的天数这些功能已经反反复复写过很多遍了,所以我认为这道题目只是让我们用更清楚的类图关系完善之前所写的代码,没有什么值得再进一步分析的。

 

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

 

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

类图.jpg

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

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

输入格式:

有三种输入方式(以输入的第一个数字划分[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:值

 

这道题目就是运用另一种类图的方法,分别写年、月、日类,然后再在DateUtil里用这些属性,其实都是大同小异的,也没有什么特别大的难度,无非就是细心耐心再把代码重构一遍。

 

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int table=in.nextInt() ;
        int year=in.nextInt() ;
        int month=in.nextInt() ;
        int day=in.nextInt() ;
        Year theYear=new Year(year);
        Month theMonth=new Month(month);
        Day theDay=new Day(day);
        DateUtil date = new DateUtil(theYear,theMonth,theDay);

        if(date.checkInputValidity()){
            if(table==1){
                int n=in.nextInt() ;
                System.out.print(date.showDate()+" next "+n+" days is:"+date.getNextNDays(n).showDate()+"\n");
            }
            else if(table==2){
                int n=in.nextInt() ;
                System.out.print(date.showDate()+" previous "+n+" days is:"+date.getPreviousNDays(n).showDate()+"\n");
            }
            else if(table==3){
                int anotherYear=in.nextInt() ;
                int anotherMonth=in.nextInt() ;
                int anotherDay=in.nextInt() ;
                Year theOtherYear=new Year(anotherYear);
                Month theOtherMonth=new Month(anotherMonth);
                Day theOtherDay=new Day(anotherDay);
                DateUtil OtherDate = new DateUtil(theOtherYear,theOtherMonth,theOtherDay);
                if(OtherDate.checkInputValidity()){
                    System.out.print("The days between "+date.showDate()+" and "+OtherDate.showDate()+" are:"+date.getDaysofDates(OtherDate)+"\n") ;
                }
                else{
                    System.out.print("Wrong Format\n") ;
                }
            }
            else{
                System.out.print("Wrong Format\n");
            }
        }
        else{
            System.out.print("Wrong Format\n");
        }
    }
}
class DateUtil{
    private Year year;
    private Month month;
    private Day day;
    private int[] mon_maxnum={31,28,31,30,31,30,31,31,30,31,30,31};

    public DateUtil() {
    }

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

    public Year getYear() {
        return year;
    }

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

    public Month getMonth() {
        return month;
    }

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

    public Day getDay() {
        return day;
    }

    public void setDay(Day day) {
        this.day = day;
    }
    public void setDayMin(){
        day.setValue(1);
    }

    public void setDayMax(){
        if(year.isLeapYear()){
            mon_maxnum[1]=29;
        }
        else{
            mon_maxnum[1]=28;
        }
        day.setValue(mon_maxnum[month.getValue()-1]);
    }
    public boolean checkInputValidity(){
        if(year.validate()){
            if(month.validate()){
                if(day.getValue()<=mon_maxnum[month.getValue()-1]&&day.getValue()>=1){
                    return true;
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }
    public DateUtil getNextNDays(int n){
        //首先判断闰年从而确定二月份天数
        for(int i=0;i<n;i++){
            if(year.isLeapYear()){
                mon_maxnum[1]=29;
            }
            else{
                mon_maxnum [1]=28;
            }
            //当日期为一年的最后一天
            if(day.getValue()==mon_maxnum[month.getValue()-1]&&month.getValue()==12){
                year.yearIncrement();
                month.resetMin();
                setDayMin();

            }
            //当日期为除12月的月份的最后一天
            else if(day.getValue()==mon_maxnum[month.getValue()-1]&&month.getValue()!=12){
                month.monthIncrement() ;
                setDayMin();
            }
            else{
                day.dayIncrement();
            }
        }
        DateUtil NextNDate=new DateUtil(year,month,day);
        return NextNDate ;
    }
    public DateUtil getPreviousNDays(int n){
        for(int i=0;i<n;i++){
            if(year.isLeapYear()){
                mon_maxnum[1]=29;
            }
            else{
                mon_maxnum [1]=28;
            }
            if(day.getValue()==1&&month.getValue()==1){
                year.yearReduction() ;
                month.resetMax() ;
                setDayMax();
            }
            else if(day.getValue()==1&&month.getValue()!=1){
                month.monthReduction() ;
                setDayMax(); ;
            }
            else{
                day.dayReduction();
            }
        }
        DateUtil PreviousNDate=new DateUtil(year,month, day);
        return PreviousNDate ;
    }
    public boolean compareDates(DateUtil date){  //比较两个日期的大小
        //首先年份比较
        if(this.year.getValue()>date.year.getValue()){
            return true;
        }
        //若年份相等则比较月份
        else if(this.year.getValue()==date.year.getValue()&&this.month.getValue()>date.month.getValue()){
            return true;
        }
        //若年份和月份相等则比较日期
        else if(this.month.getValue()==date.month.getValue()&&this.month.getValue()==date.month.getValue()&&day.getValue()>date.day.getValue() ){
            return true;
        }
        //除了以上三种情况返回true其他全为false
        else{
            return false;
        }
    }
    public boolean equalTwoDates(DateUtil date){
        if(this.year.getValue()==date.year.getValue()&&this.month.getValue()==date.month.getValue()&&day.getValue()==date.day.getValue() ){
            return true;
        }
        else{
            return false;
        }
    }
    public int getDaysofDates(DateUtil date) {
        int n = 0;//n为相隔天数
        DateUtil previousDate=new DateUtil();
        DateUtil lateDate= new DateUtil() ;
        if(compareDates(date) ){
            lateDate.setDay(day);
            lateDate.setMonth(month);
            lateDate.setYear(year) ;
            previousDate=date ;
        }
        else{
            lateDate=date;
            previousDate.setDay(day);
            previousDate.setMonth(month);
            previousDate.setYear(year) ;
        }
        while(!previousDate.equalTwoDates(lateDate)){
            n++;
            previousDate=previousDate.getNextNDays(1);
        }
        return n;
    }
    public String showDate(){
        String printDate=String.format(year.getValue()+"-"+month.getValue()+"-"+day.getValue());
        return  printDate;
    }
}
class Year{
    private int value;

    public Year() {
    }

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

    public int getValue() {
        return value;
    }

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

    public boolean isLeapYear(){  //判断是否为闰年
        if(this.value%4==0&&this.value%100!=0||this.value%400==0){
            return true;
        }
        else {
            return false ;
        }
    }
    public boolean validate(){  //校验数据合法性
        if(this.value>=1820&&this.value<=2020){
            return true;
        }
        else{
            return false ;
        }
    }
    public void yearIncrement(){  //年份增1
        this.value +=1;
    }
    public void yearReduction(){  //年份减1
        this.value -=1;
    }
}
class Month {
    private int value;

    public Month() {
    }

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

    public int getValue() {
        return value;
    }

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

    public void resetMin(){  //月份复位(1)
        this.value =1;
    }
    public void resetMax(){  //月份复位(12)
        this.value =12;
    }
    public boolean validate(){ //判断月份是否合法,合法区间为[1-12]
         if(this.value>=1&&this.value<=12){
             return true;
         }
         else{
             return false;
         }
    }
    public void monthIncrement(){  //月份增1
        this.value +=1;
    }
    public void monthReduction(){  //月份减1
        this.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(){  //日期增1
        this.value+=1;
    }
    public void dayReduction(){  //日期减1
        this.value-=1;
    }
}

 

 

 

OO06 7-4:

在OO06里的最后两道题目中,中国银联、银行、银行用户、银行账户、银行卡以及ATM这六个实体类是题目要求设计的,除了这六个类,我还设计了DealOperate(处理操作),CheckOperate(检查操作合法性),BusinessProcessing(业务办理)这三个类,处理操作类是把输入的很多条操作按回车符号分割成一条一条的操作,分别传进检查操作合法性类检查是否输入错误处理,检查操作合法性分为五个部分,检查所输入的卡号、所输入的ATM机编号、所输入的银行卡密码、判断取款金额是否大于账户余额以及检测是否为跨行取款。在检查完所有操作的信息均无误后输出存款和取款业务。

关于基础数据的初始化,我是根据如下包含关系:在本题目中中国银联包含了中国建设银行和中国工商银行,不同银行所对应的ATM机编号不同,并且包含的用户也不同,在某个银行下的用户可以拥有一个或多个银行账号,每个银行账号下又对应一张或多张银行卡。题目设定账户的初始余额为10000.00,这道题目稍微复杂一点的就是基础信息的传入还用类与类之间的调用关系。

下面是我用powerDesign反向工程生成的类图:

 代码如下:

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

public class Main {
    public static void main(String[] args) {
        ChinaUnionPay chinaUnionPay=putInformation();
        Scanner in=new Scanner(System.in);
        StringBuilder sb=new StringBuilder();
        String operate=in.nextLine();
        while (!operate.equals("#")){
            sb.append(operate+"\n");
            operate=in.nextLine();
        }
        DealOperate dealOperate=new DealOperate(chinaUnionPay,sb);
        dealOperate.getDealOperateResult();
    }
    public static ChinaUnionPay putInformation(){
        Card card1=new Card("6217000010041315709");
        Card card2=new Card("6217000010041315715");
        Card card3=new Card("6217000010041315718");
        Card card4=new Card("6217000010051320007");
        Card card5=new Card("6222081502001312389");
        Card card6=new Card("6222081502001312390");
        Card card7=new Card("6222081502001312399");
        Card card8=new Card("6222081502001312400");
        Card card9=new Card("6222081502051320785");
        Card card10=new Card("6222081502051320786");
        Card[] cards1={card1,card2};
        Card[] cards2={card3};
        Card[] cards3={card4};
        Card[] cards4={card5};
        Card[] cards5={card6};
        Card[] cards6={card7,card8};
        Card[] cards7={card9};
        Card[] cards8={card10};
        Account account1=new Account("3217000010041315709",cards1);
        Account account2=new Account("3217000010041315715",cards2);
        Account account3=new Account("3217000010051320007",cards3);
        Account account4=new Account("3222081502001312389",cards4);
        Account account5=new Account("3222081502001312390",cards5);
        Account account6=new Account("3222081502001312399",cards6);
        Account account7=new Account("3222081502051320785",cards7);
        Account account8=new Account("3222081502051320786",cards8);
        Account[] accounts1={account1,account2};
        Account[] accounts2={account3};
        Account[] accounts3={account4,account5,account6};
        Account[] accounts4={account7,account8};
        User user1=new User("杨过",accounts1);
        User user2=new User("郭靖",accounts2);
        User user3=new User("张无忌",accounts3);
        User user4=new User("韦小宝",accounts4);
        User[] users1={user1,user2};
        User[] users2={user3,user4};
        ATM atm1=new ATM("01");
        ATM atm2=new ATM("02");
        ATM atm3=new ATM("03");
        ATM atm4=new ATM("04");
        ATM atm5=new ATM("05");
        ATM atm6=new ATM("06");
        ATM[] atms1={atm1,atm2,atm3,atm4};
        ATM[] atms2={atm5,atm6};
        Bank bank1=new Bank("中国建设银行",users1,atms1);
        Bank bank2=new Bank("中国工商银行",users2,atms2);
        Bank[] banks={bank1,bank2};
        ChinaUnionPay chinaUnionPay=new ChinaUnionPay(banks);
        return chinaUnionPay;
    }
}
class DealOperate{
    private ChinaUnionPay chinaUnionPay; //系统内的所有信息
    private  StringBuilder sb;  //存储所有输入信息

    public DealOperate() {
    }

    public DealOperate(ChinaUnionPay chinaUnionPay, StringBuilder sb) {
        this.chinaUnionPay = chinaUnionPay;
        this.sb = sb;
    }

    public StringBuilder getSb() {
        return sb;
    }

    public void setSb(StringBuilder sb) {
        this.sb = sb;
    }
    public void getDealOperateResult(){  //获取处理信息结果
        String Operate=sb.toString();
        String[] operate=Operate.split("\n");
        for(int i=0;i<operate.length;i++){
            CheckOperate checkOperate=new CheckOperate(operate[i], this.chinaUnionPay);
            if(!checkOperate.validateOperate()){
                return;
            }
            else{
                String data[]=operate[i].split(" ");
                ArrayList<String> list=new ArrayList<>();
                for(int j=0;j<data.length;j++){
                    list.add(data[j]);
                    list.remove("");
                }
                if(list.size()==4){
                    BusinessProcessing businessProcessing1=new BusinessProcessing(this.chinaUnionPay,list.get(2));
                    businessProcessing1.changeBalance(list.get(0), Double.parseDouble(list.get(3)));
                }
                else if(list.size()==1){
                    BusinessProcessing businessProcessing2=new BusinessProcessing(this.chinaUnionPay);
                    Account account=businessProcessing2.QueryAccount(list.get(0));
                    System.out.println("¥"+String.format("%.2f", account.getBalance()));
                }
            }
        }
    }
}
class BusinessProcessing{  //业务办理
    private ChinaUnionPay chinaUnionPay;
    private String ATMnumber;

    public BusinessProcessing() {
    }

    public BusinessProcessing(ChinaUnionPay chinaUnionPay, String ATMnumber) {
        this.chinaUnionPay = chinaUnionPay;
        this.ATMnumber = ATMnumber;
    }

    public BusinessProcessing(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
    }

    public String getATMnumber() {
        return ATMnumber;
    }

    public void setATMnumber(String ATMnumber) {
        this.ATMnumber = ATMnumber;
    }
    public Account QueryAccount(String cardNumber){
        Account account=null;
        OUT:for(int i=0;i<chinaUnionPay.getBanks().length;i++){
            for(int j=0;j<chinaUnionPay.getBanks()[i].getUsers().length;j++){
                for(int k=0;k<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts().length;k++){
                    for(int l=0;l<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards().length;l++){
                        if(chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards()[l].getCardNumber().equals(cardNumber)){
                            account=chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k];
                            break OUT;
                        }
                    }
                }
            }
        }
        return account;
    }
    public User QueryUser(String cardNumber){
        User user=new User();
        OUT:for(int i=0;i<chinaUnionPay.getBanks().length;i++){
            for(int j=0;j<chinaUnionPay.getBanks()[i].getUsers().length;j++){
                for(int k=0;k<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts().length;k++){
                    for(int l=0;l<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards().length;l++){
                        if(chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards()[l].getCardNumber().equals(cardNumber)){
                            user=chinaUnionPay.getBanks()[i].getUsers()[j];
                            break OUT;
                        }
                    }
                }
            }
        }
        return user;
    }
    public Bank QueryBank(String cardNumber){
        Bank bank=new Bank();
        OUT:for(int i=0;i<chinaUnionPay.getBanks().length;i++){
            for(int j=0;j<chinaUnionPay.getBanks()[i].getUsers().length;j++){
                for(int k=0;k<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts().length;k++){
                    for(int l=0;l<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards().length;l++){
                        if(chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards()[l].getCardNumber().equals(cardNumber)){
                            bank=chinaUnionPay.getBanks()[i];
                            break OUT;
                        }
                    }
                }
            }
        }
        return bank;
    }
    public void changeBalance(String cardNumber, double money){ //存钱或取钱
        Account account=QueryAccount(cardNumber);
        User user=QueryUser(cardNumber);
        Bank bank=QueryBank(cardNumber);
        account.setBalance(account.getBalance()-money);
        if(money>0){
            System.out.println(user.getName()+"在"+ bank.getName()+"的"+ATMnumber+"号ATM机上取款¥"+String.format("%.2f",money)+"\n"+"当前余额为¥"+String.format("%.2f", account.getBalance()));
        }
        else{
            System.out.println(user.getName()+"在"+ bank.getName()+"的"+ATMnumber+"号ATM机上存款¥"+String.format("%.2f",-money)+"\n"+"当前余额为¥"+String.format("%.2f", account.getBalance()));
        }
    }
}
class CheckOperate{
    private String operate;  //一条操作信息
    private ChinaUnionPay chinaUnionPay;
    public CheckOperate() {
    }

    public CheckOperate(String operate, ChinaUnionPay chinaUnionPay) {
        this.operate = operate;
        this.chinaUnionPay = chinaUnionPay;
    }

    public CheckOperate(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
    }

    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public boolean validateOperate(){
        int business=0;
        String data[]=operate.split(" ");
        ArrayList<String> list=new ArrayList<>();
        for(int i=0;i<data.length;i++){
            list.add(data[i]);
            list.remove("");
        }
        Account account=validateCardNumber(list.get(0));
        BusinessProcessing businessProcessing=new BusinessProcessing(this.chinaUnionPay);
        Bank bank=businessProcessing.QueryBank(list.get(0));
        if(account==null){
            System.out.println("Sorry,this card does not exist.");
            return false;
        }
        if(list.size()==4){
            business=1;
        }
        else if(list.size()==1){
            business=2;
        }
        if(business==1){
            if(!validateATM(list.get(2))){
                System.out.println("Sorry,the ATM's id is wrong.");
                return false;
            }
            if(!validatePassword(list.get(1))){
                System.out.println("Sorry,your password is wrong.");
                return false;
            }
            if(!validateGetMoney(Double.parseDouble(list.get(3)), account)){
                System.out.println("Sorry,your account balance is insufficient.");
                return false;
            }
            boolean key=true;
            if(list.get(2).equals("01")||list.get(2).equals("02")||list.get(2).equals("03")||list.get(2).equals("04")&&bank.getName().equals("中国建设银行")){
                key=true;
            }
            else if(list.get(2).equals("05")||list.get(2).equals("06") && bank.getName().equals("中国工商银行")){
                key=true;
            }
            else{
                key=false;
            }
            if(key==false){
                System.out.println("Sorry,cross-bank withdrawal is not supported.");
                return false;
            }
        }
        return true;
    }
    public Account validateCardNumber(String cardNumber){
        BusinessProcessing businessProcessing=new BusinessProcessing(this.chinaUnionPay);
        Account account=businessProcessing.QueryAccount(cardNumber);
        return account;
    }
    public boolean validateATM(String ATM){
        if(ATM.matches("0[1-6]")){
            return true;
        }
        else return false;
    }
    public boolean validatePassword(String password){
        if(password.matches("88888888")){
            return true;
        }
        else return false;
    }
    public boolean validateGetMoney(double getMoney, Account account){
        if(getMoney> account.getBalance()){
            return false;
        }
        else return true;
    }
}
class Card{
    private String cardNumber;
    private String password="88888888";
    public Card() {
    }

    public Card(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
}
class Account{
    private String accountNumber;
    private Card[] cards;
    private double balance=10000.00;

    public Account() {
    }

    public Account(String accountNumber, Card[] cards) {
        this.accountNumber = accountNumber;
        this.cards = cards;
    }

    public Card[] getCards() {
        return cards;
    }

    public void setCards(Card[] cards) {
        this.cards = cards;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}
class User{
    private String name;
    private Account[] accounts;

    public User() {
    }

    public User(String name, Account[] accounts) {
        this.name = name;
        this.accounts = accounts;
    }

    public String getName() {
        return name;
    }

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

    public Account[] getAccounts() {
        return accounts;
    }

    public void setAccounts(Account[] accounts) {
        this.accounts = accounts;
    }
}
class Bank{
    private String name;
    private User[] users;
    private ATM[] atms;
    public Bank() {
    }

    public Bank(String name, User[] users, ATM[] atms) {
        this.name = name;
        this.users = users;
        this.atms = atms;
    }

    public String getName() {
        return name;
    }

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

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }
}
class ChinaUnionPay{
    private Bank[] banks;

    public ChinaUnionPay() {
    }

    public ChinaUnionPay(Bank[] banks) {
        this.banks = banks;
    }

    public Bank[] getBanks() {
        return banks;
    }

    public void setBanks(Bank[] banks) {
        this.banks = banks;
    }
}
class ATM{
    private String num;

    public ATM() {
    }

    public ATM(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

 

OO06 7-5:

这题在上一题的基础上增加了一个银行和多个用户,且允许跨行取款,但要求收取手续费,并且不同银行对应的跨行取款手续费不同,账号被分为借记账号和贷记账号,其中贷记账号允许透支取款,但也是要收取相应的手续费(不同银行透支取款所对应的手续费相同),并且有透支最大限额为50000元。这道题目更加困难的点在于贷记账号可以透支,并且透支取款和跨行取款需要收取相应的手续费,这样判断操作合法性的话就需要考虑所取金额加上相应的手续费与账户余额之间的大小关系,还有一点值得注意的是对题目的理解,借贷记账户跨行收取的费用是不管透支与否,所取的金额全部要收取收费,透支部分的手续费另算的。如果没有很好地理解题目的意思是没有办法编写出正确的程序的。

同样是反向工程生成的类图:

代码如下:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        PutInformation putInformation=new PutInformation();
        ChinaUnionPay chinaUnionPay=putInformation.putInformation();
        Scanner in=new Scanner(System.in);
        StringBuilder sb=new StringBuilder();
        String operate=in.nextLine();
        while (!operate.equals("#")){
            sb.append(operate+"\n");
            operate=in.nextLine();
        }A
        DealOperate dealOperate=new DealOperate(chinaUnionPay,sb);
        dealOperate.getDealOperateResult();
    }
}
class PutInformation{ //录入信息
    public ChinaUnionPay putInformation(){
        //银行卡号
        Card card1=new Card("6217000010041315709");
        Card card2=new Card("6217000010041315715");
        Card card3=new Card("6217000010041315718");
        Card card4=new Card("6217000010051320007");
        Card card5=new Card("6222081502001312389");
        Card card6=new Card("6222081502001312390");
        Card card7=new Card("6222081502001312399");
        Card card8=new Card("6222081502001312400");
        Card card9=new Card("6222081502051320785");
        Card card10=new Card("6222081502051320786");
        Card card11=new Card("6640000010045442002");
        Card card12=new Card("6640000010045442003");
        Card card13=new Card("6640000010045441009");
        Card card14=new Card("6630000010033431001");
        Card card15=new Card("6630000010033431008");
        Card[] cards1={card1,card2};
        Card[] cards2={card3};
        Card[] cards3={card4};
        Card[] cards4={card5};
        Card[] cards5={card6};
        Card[] cards6={card7,card8};
        Card[] cards7={card9};
        Card[] cards8={card10};
        Card[] cards9={card11,card12};
        Card[] cards10={card13};
        Card[] cards11={card14};
        Card[] cards12={card15};
        //银行账户
        Account account1=new Account("3217000010041315709",cards1,true);
        Account account2=new Account("3217000010041315715",cards2,true);
        Account account3=new Account("3217000010051320007",cards3,true);
        Account account4=new Account("3222081502001312389",cards4,true);
        Account account5=new Account("3222081502001312390",cards5,true);
        Account account6=new Account("3222081502001312399",cards6,true);
        Account account7=new Account("3222081502051320785",cards7,true);
        Account account8=new Account("3222081502051320786",cards8,true);
        Account account9=new Account("3640000010045442002",cards9,false);
        Account account10=new Account("3640000010045441009",cards10,false);
        Account account11=new Account("3630000010033431001",cards11,false);
        Account account12=new Account("3630000010033431008",cards12,false);
        Account[] accounts1={account1,account2};
        Account[] accounts2={account3};
        Account[] accounts3={account4,account5,account6};
        Account[] accounts4={account7,account8};
        Account[] accounts5={account9};
        Account[] accounts6={account10};
        Account[] accounts7={account11};
        Account[] accounts8={account12};
        //用户
        User user1=new User("杨过",accounts1);
        User user2=new User("郭靖",accounts2);
        User user3=new User("张无忌",accounts3);
        User user4=new User("韦小宝",accounts4);
        User user5=new User("张三丰",accounts5);
        User user6=new User("令狐冲",accounts6);
        User user7=new User("乔峰",accounts7);
        User user8=new User("洪七公",accounts8);
        User[] users1={user1,user2,user5};
        User[] users2={user3,user4,user6};
        User[] users3={user7,user8};
        //ATM机
        ATM atm1=new ATM("01");
        ATM atm2=new ATM("02");
        ATM atm3=new ATM("03");
        ATM atm4=new ATM("04");
        ATM atm5=new ATM("05");
        ATM atm6=new ATM("06");
        ATM atm7=new ATM("07");
        ATM atm8=new ATM("08");
        ATM atm9=new ATM("09");
        ATM atm10=new ATM("10");
        ATM atm11=new ATM("11");
        ATM[] atms1={atm1,atm2,atm3,atm4};
        ATM[] atms2={atm5,atm6};
        ATM[] atms3={atm7,atm8,atm9,atm10,atm11};
        Bank bank1=new Bank("中国建设银行",users1,atms1);
        Bank bank2=new Bank("中国工商银行",users2,atms2);
        Bank bank3=new Bank("中国农业银行",users3,atms3);
        Bank[] banks={bank1,bank2,bank3};
        ChinaUnionPay chinaUnionPay=new ChinaUnionPay(banks);
        return chinaUnionPay;
    }
}
class DealOperate{
    private ChinaUnionPay chinaUnionPay; //系统内的所有信息
    private  StringBuilder sb;  //存储所有输入信息

    public DealOperate() {
    }

    public DealOperate(ChinaUnionPay chinaUnionPay, StringBuilder sb) {
        this.chinaUnionPay = chinaUnionPay;
        this.sb = sb;
    }

    public StringBuilder getSb() {
        return sb;
    }

    public void setSb(StringBuilder sb) {
        this.sb = sb;
    }
    public void getDealOperateResult(){  //获取处理信息结果
        String Operate=sb.toString();
        String[] operate=Operate.split("\n");
        for(int i=0;i<operate.length;i++){
            CheckOperate checkOperate=new CheckOperate(operate[i], this.chinaUnionPay);
            if(!checkOperate.validateOperate()){
                return;
            }
            else{
                String data[]=operate[i].split(" ");
                ArrayList<String> list=new ArrayList<>();
                for(int j=0;j<data.length;j++){
                    list.add(data[j]);
                    list.remove("");
                }
                if(list.size()==4){
                    BusinessProcessing businessProcessing1=new BusinessProcessing(this.chinaUnionPay,list.get(2));
                    Account account=businessProcessing1.QueryAccount(list.get(0));
                    businessProcessing1.changeBalance(list.get(0), Double.parseDouble(list.get(3)));
                }
                else if(list.size()==1){
                    BusinessProcessing businessProcessing2=new BusinessProcessing(this.chinaUnionPay);
                    Account account=businessProcessing2.QueryAccount(list.get(0));
                    System.out.println("业务:查询余额 ¥"+String.format("%.2f", account.getBalance()));
                }
            }
        }
    }
}
class CheckOperate{
    private String operate;  //一条操作信息
    private ChinaUnionPay chinaUnionPay;
    public CheckOperate() {
    }

    public CheckOperate(String operate, ChinaUnionPay chinaUnionPay) {
        this.operate = operate;
        this.chinaUnionPay = chinaUnionPay;
    }

    public CheckOperate(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
    }

    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public boolean validateOperate(){
        int business=0;
        String data[]=operate.split(" ");
        ArrayList<String> list=new ArrayList<>();
        for(int i=0;i<data.length;i++){
            list.add(data[i]);
            list.remove("");
        }
        Account account=validateCardNumber(list.get(0));
        BusinessProcessing businessProcessing=new BusinessProcessing(this.chinaUnionPay);
        Bank bank=businessProcessing.QueryBank(list.get(0));
        if(account==null){
            System.out.println("Sorry,this card does not exist.");
            return false;
        }
        if(list.size()==4){
            business=1;
        }
        else if(list.size()==1){
            business=2;
        }
        if(business==1){
            if(!validateATM(list.get(2))){
                System.out.println("Sorry,the ATM's id is wrong.");
                return false;
            }
            if(!validatePassword(list.get(1))){
                System.out.println("Sorry,your password is wrong.");
                return false;
            }
            if(!validateGetMoney(Double.parseDouble(list.get(3)), list.get(0),list.get(2))){
                System.out.println("Sorry,your account balance is insufficient.");
                return false;
            }
        }
        return true;
    }
    public Account validateCardNumber(String cardNumber){
        BusinessProcessing businessProcessing=new BusinessProcessing(this.chinaUnionPay);
        Account account=businessProcessing.QueryAccount(cardNumber);
        return account;
    }
    public boolean validateATM(String ATM){
        if(ATM.matches("((0[1-9])|1[0-1])")){
            return true;
        }
        else return false;
    }
    public boolean validatePassword(String password){
        if(password.matches("88888888")){
            return true;
        }
        else return false;
    }
    public boolean validateGetMoney(double getMoney, String cardNumber,String ATMnumber){
        BusinessProcessing businessProcessing=new BusinessProcessing(chinaUnionPay,ATMnumber);
        Account account=businessProcessing.QueryAccount(cardNumber);
        double commission=businessProcessing.Commission(getMoney,cardNumber);
        double overdraftFee=businessProcessing.OverdraftFee(getMoney,cardNumber);
        if(account.isDebitAccount()){
            if(businessProcessing.OverDraw(getMoney,commission,cardNumber)){
                return false;
            }
            else return true;
        }
        else{
            if(account.getBalance()-getMoney-commission-overdraftFee<-50000){
                return false;
            }
            else return true;
        }

    }
}
class BusinessProcessing{  //业务办理
    private ChinaUnionPay chinaUnionPay;
    private String ATMnumber;

    public BusinessProcessing() {
    }

    public BusinessProcessing(ChinaUnionPay chinaUnionPay, String ATMnumber) {
        this.chinaUnionPay = chinaUnionPay;
        this.ATMnumber = ATMnumber;
    }

    public BusinessProcessing(ChinaUnionPay chinaUnionPay) {
        this.chinaUnionPay = chinaUnionPay;
    }

    public String getATMnumber() {
        return ATMnumber;
    }

    public void setATMnumber(String ATMnumber) {
        this.ATMnumber = ATMnumber;
    }
    public Account QueryAccount(String cardNumber){
        Account account=null;
        OUT:for(int i=0;i<chinaUnionPay.getBanks().length;i++){
            for(int j=0;j<chinaUnionPay.getBanks()[i].getUsers().length;j++){
                for(int k=0;k<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts().length;k++){
                    for(int l=0;l<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards().length;l++){
                        if(chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards()[l].getCardNumber().equals(cardNumber)){
                            account=chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k];
                            break OUT;
                        }
                    }
                }
            }
        }
        return account;
    }
    public User QueryUser(String cardNumber){
        User user=new User();
        OUT:for(int i=0;i<chinaUnionPay.getBanks().length;i++){
            for(int j=0;j<chinaUnionPay.getBanks()[i].getUsers().length;j++){
                for(int k=0;k<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts().length;k++){
                    for(int l=0;l<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards().length;l++){
                        if(chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards()[l].getCardNumber().equals(cardNumber)){
                            user=chinaUnionPay.getBanks()[i].getUsers()[j];
                            break OUT;
                        }
                    }
                }
            }
        }
        return user;
    }
    public Bank QueryBank(String cardNumber){
        Bank bank=new Bank();
        OUT:for(int i=0;i<chinaUnionPay.getBanks().length;i++){
            for(int j=0;j<chinaUnionPay.getBanks()[i].getUsers().length;j++){
                for(int k=0;k<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts().length;k++){
                    for(int l=0;l<chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards().length;l++){
                        if(chinaUnionPay.getBanks()[i].getUsers()[j].getAccounts()[k].getCards()[l].getCardNumber().equals(cardNumber)){
                            bank=chinaUnionPay.getBanks()[i];
                            break OUT;
                        }
                    }
                }
            }
        }
        return bank;
    }
    public boolean InterBank(String cardNumber){  //判断是否跨行办理业务
        Bank bank=QueryBank(cardNumber);
        for(int i=0;i<bank.getAtms().length;i++){
            if(bank.getAtms()[i].getNum().equals(ATMnumber)){
                return false;  //通过遍历,在改卡号所在的银行找到对应的ATM机编号则非跨行办理业务
            }
        }
        return true;
    }
    public boolean OverDraw(double money,double interbankCommission,String cardNumber){  //判断是否透支
        Account account=QueryAccount(cardNumber);
        if(account.getBalance()-money-interbankCommission<0){  //账户余额减去取款再减去跨行办理业务的手续费(先不管透支取款的手续费)
            return true;
        }
        else return false;
    }
    public double OverdraftFee(double money,String cardNumber){  //透支取款手续费用
        Account account=QueryAccount(cardNumber);
        double OverdraftCommission=0;  //透支取款手续费
        double InterbankCommission=Commission(money,cardNumber);
        //分两种情况
        //第一种是账户里还有余额但是所取金额大于账户余额
        if(!account.isDebitAccount()){
            if(account.getBalance()>0){
                if(OverDraw(money,InterbankCommission,cardNumber)){
                    OverdraftCommission=-(account.getBalance()-money)*(5.0/100.0);
                }
                else OverdraftCommission=0;
            }
            //第二种是账户里已经没有余额了
            else{
                OverdraftCommission=money*(5.0/100.0);
            }
        }
        else{
            OverdraftCommission=0;
        }
//        if(!account.isDebitAccount()){
//            if(!InterBank(money,cardNumber)){  //如果没有跨行办理业务
//                if(!OverDraw(money,InterbankCommission,cardNumber)){  //如果没有透支
//                    OverdraftCommission=0;
//                }
//                else{  //如果透支了
//                    OverdraftCommission=money*(5.0/100.0);
//                }
//            }
//            else{
//                if(!OverDraw(money,InterbankCommission,cardNumber)){  //如果没有透支
//                    OverdraftCommission=0;
//                }
//                else{  //如果透支了
//                    OverdraftCommission=-(account.getBalance()-money)*(5.0/100.0);
//                }
//            }
//        }
        return OverdraftCommission;
    }
    public double Commission(double money,String cardNumber){  //计算跨行手续费用
        double InterbankCommission=0;  //跨行办理业务的手续费
        if(!InterBank(cardNumber)){  //如果没有跨行办理业务
            InterbankCommission=0;
        }
        else{
            //如果是在中国建设银行取款,则手续费为取款金额的2%
            if(ATMnumber.equals("01")||ATMnumber.equals("02")||ATMnumber.equals("03")||ATMnumber.equals("04")){
                InterbankCommission=money*(2.0/100.0);
            }
            //如果是在中国工商银行取款,则手续费为取款金额的3%
            else if(ATMnumber.equals("05")||ATMnumber.equals("06")){
                InterbankCommission=money*(3.0/100.0);
            }
            //如果是在中国农业银行取款,则手续费为取款金额的4%
            else if(ATMnumber.equals("07")||ATMnumber.equals("08")||ATMnumber.equals("09")||ATMnumber.equals("10")||ATMnumber.equals("11")){
                InterbankCommission=money*(4.0/100.0);
            }
        }
        return InterbankCommission;
    }
    public void changeBalance(String cardNumber, double money){ //取钱或者透支(计算手续费用)
        Account account=QueryAccount(cardNumber);
        User user=QueryUser(cardNumber);
        String bankName=null;
        if(ATMnumber.equals("01")||ATMnumber.equals("02")||ATMnumber.equals("03")||ATMnumber.equals("04")){
            bankName="中国建设银行";
        }
        if(ATMnumber.equals("05")||ATMnumber.equals("06")){
            bankName="中国工商银行";
        }
        if (ATMnumber.equals("07")||ATMnumber.equals("08")||ATMnumber.equals("09")||ATMnumber.equals("10")||ATMnumber.equals("11")){
            bankName="中国农业银行";
        }
        double InterbankCommission=Commission(money,cardNumber);
        double OverdraftCommission=OverdraftFee(money,cardNumber);
        account.setBalance(account.getBalance()-money-InterbankCommission-OverdraftCommission);
        System.out.println("业务:取款 "+user.getName()+"在"+bankName+"的"+ATMnumber+"号ATM机上取款¥"+String.format("%.2f",money)+"\n当前余额为¥"+String.format("%.2f",account.getBalance()));
    }
}
class Card{
    private String cardNumber;
    private String password="88888888";
    public Card() {
    }

    public Card(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
}
class Account{
    private String accountNumber;
    private Card[] cards;
    private boolean debitAccount;
    private double balance=10000.00;

    public Account() {
    }

    public Account(String accountNumber, Card[] cards, boolean debitAccount) {
        this.accountNumber = accountNumber;
        this.cards = cards;
        this.debitAccount = debitAccount;
    }

    public Card[] getCards() {
        return cards;
    }

    public void setCards(Card[] cards) {
        this.cards = cards;
    }

    public double getBalance() {
        return balance;
    }

    public boolean isDebitAccount() {
        return debitAccount;
    }

    public void setDebitAccount(boolean debitAccount) {
        this.debitAccount = debitAccount;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}
class User{
    private String name;
    private Account[] accounts;

    public User() {
    }

    public User(String name, Account[] accounts) {
        this.name = name;
        this.accounts = accounts;
    }

    public String getName() {
        return name;
    }

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

    public Account[] getAccounts() {
        return accounts;
    }

    public void setAccounts(Account[] accounts) {
        this.accounts = accounts;
    }
}
class Bank{
    private String name;
    private User[] users;
    private ATM[] atms;
    public Bank() {
    }

    public Bank(String name, User[] users, ATM[] atms) {
        this.name = name;
        this.users = users;
        this.atms = atms;
    }

    public String getName() {
        return name;
    }

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

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    public ATM[] getAtms() {
        return atms;
    }

    public void setAtms(ATM[] atms) {
        this.atms = atms;
    }
}
class ChinaUnionPay{
    private Bank[] banks;

    public ChinaUnionPay() {
    }

    public ChinaUnionPay(Bank[] banks) {
        this.banks = banks;
    }

    public Bank[] getBanks() {
        return banks;
    }

    public void setBanks(Bank[] banks) {
        this.banks = banks;
    }
}
class ATM{
    private String num;

    public ATM() {
    }

    public ATM(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

 

踩坑心得:

训练集6的最后两道题目一开始没有仔细看题目,没有注意到题目上所介绍的六个名词是要做成实体类的,是等到第四题完全写完通过测试点后才看到,于是相当于这局重开了,所以说一定一定要仔细看题目。还有就是看到题目别着急下手,先好好构思一下,不能急于求成,而且这一急也可能成不了。类与类之间的关系和设计还是做的不够好,感觉还是没能满足老师上课讲的那些原则,希望自己有一天也可以写出美丽代码而不是答辩代码。

改进建议:

注释啊注释,总是忘记写注释,没有注释的代码就是依托答辩啊,是过了很久返回去看自己都不知道在写什么的程度,长长记性吧。还有就是还是不能做到先画类图再写代码,这个能力不知道怎么去锻炼,或者说后面反向工程生成的类图和自己事先所画的根本不沾边。然后就是类与类之间功能的调用耦合度有点高,下次试着用老师上课所讲的设计模式去解题吧。

总结:

总的来说,我认为我的代码能力还是有那么一点点点点的进步的,至少不会是拿到题目手足无措了,虽然代码不够美丽但是我会越战越勇的!

 

posted @ 2023-04-30 21:09  一戴饼干  阅读(12)  评论(0编辑  收藏  举报