码迷,mamicode.com
首页 > 编程语言 > 详细

java面向对象练习---银行业务模拟(数组+面向对象)

时间:2018-08-04 23:18:07      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:面向对象   class   ges   添加用户   sys   证件   成功   整型   proc   

  • 编程实现:
    a) 用户类:账号(int)、姓名(String)、密码(int:6位数字)、地址(String)、存款余额(double)、注册时间(date)、开户行(与银行的名称对应,String)
    b) 地址类:国家(String)、省份(String)、街道(String)、门牌号(String)
    c) 银行类:能存储100个用户的库([ ])、本银行名称(比如:中国工商银行的昌平支行,String)
    i. 银行功能
  • 添加用户(传入参数:用户,返回值:整型值(1:成功,2:用户已存在,3:用户库已满))
    a) 业务逻辑:先检查该用户的账号在库里是否存在。若不存在则在用户库里添加一个该用户并返回代号1,若存在则返回代号2。另外在添加用户的时候检测用户库是否已注册满,若已满则返回代号3
  • 存钱(传入值:用户的账号,存取的金额。返回值:布尔类型值)
    a) 业务逻辑:先根据传入的账号信息查询用户库里是否有该用户。若没有则返回false,若有,则将该用户的金额存进去。
  • 取钱(传入值:用户的账号,用户密码,取钱金额。返回值:整型值(0:正常,1:账号不存在,2:密码不对,3:钱不够))
    a) 业务逻辑:先根据账号信息来查询该用户是否存在,若不存在,则返回代号1,若存在,则继续判断密码是否正确,若不正确,则返回代号2。若账号密码都正确,则继续判断当前用户的金额是否满足要取出的钱,若不满足,则返回代号3,若满足,则将该用户的金额减去。
  • 转账(传入值:转出的账号,转入的账号,转出账号的密码,转出的金额。返回值:0:正常,1:账号不对,2密码不对,3钱不够)
    a) 业务逻辑:先查询用户库是否存在转出和转入的账号,若不存在则返回代号,1,若账号都存在则继续判断转出账号的密码是否正确,若不正确,则返回2,若正确则继续判断要转出的金额是否足够,若不够则返回3,否则正常转出,转出的账号金额要相对应的减少,转入的金额相对应的增加。
  • 查询账户功能(传入值:账号,账号密码,返回值:空)
    a) 业务逻辑:先根据账号判断用户库是否存在该用户,若不存在则打印提示信息:该不存在。否则继续判断密码是否正确。若不正确则打印相对应的错误信息。若账号和密码都正确,则将该用户的信息都打印出来,比如:当前账号:xxxx,密码:xxxxxx,余额:xxxx元,用户居住地址:xxxxxxxxxxxxx,当前账户的开户行:xxxxxxxxxx.
    d) 界面类:在执行该入口程序时,就打印银行业务选择菜单:比如:
    i. 技术分享图片
    ii. 然后就开始处理各种输入操作,直到业务处理完成!
  • 答案:
    1、地址类

    class Address{
    private String country;//国家
    private String province;//省份
    private String street;//街道
    private String id;//门排号

    public Address(){}
    public Address(String country,String province,String street,String id){
        this.country = country;
        this.province = province;
        this.street = street;
        this.id = id;
    }
    
    public void setCountry(String country){
        this.country = country;
    }
    
    public String getCountry(){
        return this.country;
    }
    
    public void setProvince(String province){
        this.province = province;
    }
    
    public String getProvince(){
        return this.province;
    }
    
    public void setStreet(String street){
        this.street = street;
    }
    
    public String getStreet(){
        return this.street;
    }
    
    public void setId(String id){
        this.id = id;
    }
    
    public String getId(){
        return this.id;
    }

    }

    2、用户类

    class User{
    private int code;//账号
    private String name;//姓名
    private int password;//密码
    private Address address;//地址
    private double money;//存款余额
    private Date date;//注册时间
    private String openBankName;//开户行

    public User(){}
    
    public User(int code,String name,int password,Address address,double money,Date date,String openBankName){
        this.code = code;
        this.name = name;
        this.password = password;
        this.address = address;
        this.money = money;
        this.date = date;
        this.openBankName = openBankName;
    }
    
    public void setCode(int code){
        this.code = code;
    }
    
    public int getCode(){
        return this.code;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return this.name;
    }
    
    public void setPassword(int password){
        this.password = password;
    }
    
    public int getPassword(){
        return this.password;
    }
    
    public void setAddress(Address address){
        this.address = address;
    }
    
    public Address getAddress(){
        return this.address;
    }
    
    public void  setMoney(double money){
        this.money = money;
    }
    
    public double getMoney(){
        return this.money;
    }
    
    public void setDate(Date date){
        this.date = date;
    }
    
    public Date getDate(){
        return this.date;
    }
    
    public void setOpenBankName(String openBankName){
        this.openBankName = openBankName;
    }
    
    public String getOpenBankName(){
        return this.openBankName;
    }

    }

    3、银行类

    class Bank{
    private String bankName ;

    static User[] users =  new User[10];
    
    public Bank(){}
    
    public Bank(String bankName){
        this.bankName = bankName;
    }
    
    public void setBankName(String bankName){
        this.bankName = bankName;
    }
    
    public String getBankName(){
        return this.bankName;
    }
    
    /**
        检查用户是否存在
    */
    public boolean checkUser(int code){
        for (User u : users){
            if (u != null){
                if (u.getCode() == code){
                    return true;
                }
            }
        }
        return false;
    }
    
    /**
        检查用户是否已满
    */
    public boolean isFullUser(){
        for (User user : users){
            if (user == null){
                return false;
            }
        }
        return true;
    }
    
    /**
        检查用户和密码是否正确
    */
    public boolean checkUserAndPassword(int userCode, int password){
        if (checkUser(userCode)){
            for (User u : users){
                if (u.getCode() == userCode){
                    if (u.getPassword() == password){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    /**
        检查钱是否够
    */
    public boolean checkMoney(int userCode,double money){
        for (User u : users){
            if (u.getCode() == userCode){
                if (u.getMoney() >= money){
                    return true;
                }else{
                    break;
                }
            }
        }
        return false;
    }
    
    /**
        添加用户的方法
    */
    public int  addUser(User user){
        if (checkUser(user.getCode())){
            return 2; 
        }else if (isFullUser()){
            return 3;
        }else{
            for (int i = 0;i < users.length ; i++){
                if (users[i] == null){
                    users[i] = user;
                    return 1;

    // break;
    }
    }
    }
    return 1;
    }

    /**
        存钱方法
    */
    public boolean saveMoney(int userCode,double money){
        if (!checkUser(userCode)){
            return false;
        }else{
            for (User u : users){
                if (u.getCode() == userCode){
                    u.setMoney(u.getMoney() + money);
                }
            }
        }
        return true;
    }
    
    /**
        取钱方法
    */
    public int takeMoney(int userCode,int password,double takeMoney){
        if (!checkUser(userCode)){
            return 1;
        }else if (!checkUserAndPassword(userCode,password)){
            return 2;
        }else if (!checkMoney(userCode,takeMoney)){
            return 3;
        }else{
            for (User u : users){
                if (u.getCode() == userCode){
                    u.setMoney(u.getMoney() - takeMoney);
                    break;
                }
            }
        }
        return 0;
    }
    
    /**
        转账方法
    */
    public int transferMoney(int outCode,int inCode,int outpassword,double outMoney){
        if (!checkUser(outCode) && checkUser(inCode)){
            return 1;
        }else if (!checkUserAndPassword(outCode,outpassword)){
            return 2;
        }else if (!checkMoney(outCode,outMoney)){
            return 3;
        }else{
            saveMoney(inCode,outMoney);
            takeMoney(outCode,outpassword,outMoney);
        }
        return 0;
    }
    
    /**
        查询账户
    */
    public void selectUser(int userCode,int password){
        if (!checkUser(userCode)){
            System.out.println("该用户不存在!");
        }else if (!checkUserAndPassword(userCode,password)){
            System.out.println("密码错误!");
        }else{
            User u = null;
            Address address = null;
            for (User user : users){
                if (user.getCode() == userCode){
                    u = user;
                    address = u.getAddress();
                    break;
                }
            }   
            System.out.println("当前账户为:\t" + u.getName() + "\n账号为:" + userCode 
                + "\n密码:" + password  + "\n余额:" + u.getMoney() 
                + "\n用户当前居住地址:\t" +  address.getCountry() + address.getProvince() + "市" 
                + address.getStreet() + address.getId() + "号!");
        }
    }

    }

    4、页面模拟类

    class Page{
    public static void welcomePage(){
    System.out.println("**");
    System.out.println(" 中国工商银行 ");
    System.out.println(" 账户管理系统 ");
    System.out.println(" V1.0 ");
    System.out.println("**");
    System.out.println(" 1.开户 ");
    System.out.println(" 2.存钱 ");
    System.out.println(" 3.取钱 ");
    System.out.println(" 4.转账 ");
    System.out.println(" 5.查询 ");
    System.out.println(" 6.bye! ");
    System.out.println("**");
    }
    }

    5、具体的测试类
    import java.util.Date;
    import java.util.Scanner;
    public class Demo{
    private static Scanner sc = new Scanner(System.in);
    private static Bank bank = new Bank("中国工商银行沙河支行");
    public static void main(String[] args){
    Page.welcomePage();
    System.out.print("请输入业务编号:");
    int num = sc.nextInt();
    if (num == 1){
    System.out.print("请输入账户号:");
    int code = sc.nextInt();
    System.out.print("\n请输入用户名:");
    String name = sc.next();
    System.out.print("\n请输入密码:");
    int password = sc.nextInt();
    System.out.print("\n请输入输入居住的地址,首先输入国家:");
    String country = sc.next();
    System.out.print("\n请输入省份:");
    String province = sc.next();
    System.out.print("\n请输入街道:");
    String street = sc.next();
    System.out.print("\n请输入门牌号:");
    String id = sc.next();
    System.out.print("\n请输入您的账户的初始余额:");
    double money = sc.nextDouble();

            Address address = new Address(country,province,street,id);
            User user = new User(code,name,password,address,money,new Date(),bank.getBankName());
            int n = bank.addUser(user);
            if (n == 2){
                System.out.println("对不起,您输入的账户已存在,无法开户,请带好证件到营业厅窗口办理!");
            }else if (n == 3){
                System.out.println("对不起,当前银行的用户数已满,请到其他银行办理业务!");
            }else if (n == 1){
                System.out.println("恭喜你,开户成功下面是您的开户信息:");
                bank.selectUser(code,password);
            }
        }else if (num == 2){
    
        }else if (num == 3){
    
        }else if (num == 4){
    
        }else if (num == 5){
    
        }else if (num == 6){
            System.out.println("Bye~~");
            System.exit(0);
        }
    }

    }

    java面向对象练习---银行业务模拟(数组+面向对象)

    标签:面向对象   class   ges   添加用户   sys   证件   成功   整型   proc   

    原文地址:http://blog.51cto.com/lyinginsun/2154804

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!