Java面向对象封装&继承练习题(银行账户继承树)

一道比较基础的练习题

1.Account类

/*账户类*/
public class Account {
    private long id; // 账户id
    private double balance; // 账户余额
    private String password; // 账户密码

    public Account() {
    }
    public Account(long id, String password) {
        this.id = id;
        this.password = password;
    }
    // 构造方法
    public Account(long id, double balance, String password) {
        this.id = id;
        this.balance = balance;
        if (password.length() == 6) {
            this.password = password;
        }else {
            System.out.println("密码必须是6位数!请重试~");
        }

    }

    // 封装
    public long getId() {
        return id;
    }

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

    public double getBalance() {
        return balance;
    }

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

    public String getPassword(String glymm) {
        //设一个管理员密码,ATM机器上默认有密码
        if (glymm.equals("fuckyoujava")){
            return password;
        }else {
            System.out.println("密码错误!已报警!");
            return null;
        }
    }

    //设置密码
    public void setPassword(String password) {
        if (password.length() == 6) {
            this.password = password;
        }else {
            System.out.println("密码必须是6位数!请重试~");
        }
    }

2.CreditAccount类

/*信用账户类*/
public class CreditAccount extends Account{
    private double creditLine; //信用额度

    public CreditAccount() {
        super();
    }
    public CreditAccount(long id, String password) {
        super(id, password);
    }

    //封装
    public double getCreditLine() {
        return creditLine;
    }

    public void setCreditLine(double creditLine) {
        this.creditLine = creditLine;
    }
}

3.SavingAccount类

/*储蓄账户类*/
public class SavingAccount extends Account{
    private double interestRate; //存款利率

    public SavingAccount(long id, String password) {
        super(id, password);
    }

    //封装
    public double getInterestRate() {
        return interestRate;
    }

    public void setInterestRate(double interestRate) {
        if (interestRate > 0 && interestRate < 0.1) {
            this.interestRate = interestRate;
        }else {
            System.out.println("利率要在10%以内!请重试~");
        }
    }
}

4.Bank类

public class Bank {
    // 开户方法
    public static Account openAccount(long id, String password, int type) {
        switch (type) {
            case 0:
                System.out.println("开户成功!您的账户id:" + id + ",密码:****** , 账户类型:" + "普通账户");
                return new Account(id, password);
            case 1:
                System.out.println("开户成功!您的账户id:" + id + ",密码:****** , 账户类型:" + "储蓄账户");
                return new SavingAccount(id, password);
            case 2:
                System.out.println("开户成功!您的账户id:" + id + ",密码:****** , 账户类型:" + "信用账户");
                return new CreditAccount(id, password);
            default:
                System.out.println("请输入0-2开户哦!");
                return null;
        }
    }

    // 存款
    public static double deposit(Account a, double amount) {
        int count = 5;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入银行密码:");
            String psw = sc.nextLine();
            if (a.getPassword("fuckyoujava").equals(psw)) {
                a.setBalance(a.getBalance() + amount);
                System.out.println("存款" + amount+"成功!您当前的账户余额:" + a.getBalance());
                return a.getBalance();
            } else {
                System.out.println("密码错误!请重试");
                count--;
                if (count == 0) {
                    System.out.println("密码错误次数过多,账户被锁定!请联系柜台人员!");
                    return -1;
                }
            }
        }
    }

    // 取款
    public static double withdraw(Account a, double amount) {
        int count = 5;
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入银行密码:");
            String psw = sc.nextLine();
            if (a.getPassword("fuckyoujava").equals(psw)) {
                if (a.getBalance() <= amount) {
                    a.setBalance(a.getBalance() - amount);
                    System.out.println("取款"+ amount +"成功!您当前的账户余额:" + a.getBalance());
                    return a.getBalance();
                }else {
                    System.out.println("余额不足,取款失败!");
                    return -1;
                }
            } else {
                System.out.println("密码错误!请重试");
                count--;
                if (count == 0) {
                    System.out.println("密码错误次数过多,账户被锁定!请联系柜台人员!");
                    return -1;
                }
            }
        }
    }

}
* --- Be Humble and Hungry ---*
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值