存取钱 小demo

package cn.wry.oop_test;

public class BankAccount {
    public static void main(String[] args) {
        SavingsAccount check = new SavingsAccount();
        check.setBalance(0);

        for (int i = 1; i <= 365; i++) {
            check.deposit(100);
            if (i % 30 == 0){
                check.earnMonthlyInterest();//每30天重置一次,计算一次利息+本金
                System.out.println(check.getBalance());
            }
        }

//        check.earnMonthlyInterest();
//        System.out.println(check.getBalance());//计算出利率+本金   以及重置书续费次数
//        check.deposit(100);
//        check.withdraw(100);
//        check.deposit(100);
//        System.out.println(check.getBalance());
//        check.withdraw(17);
        System.out.println(check.getBalance());

    }

    private double balance;//余额

    public BankAccount() {
    }

    public BankAccount(double balance) {
        this.balance = balance;
    }

    public void deposit(double amount) {//存钱
        balance += amount;

    }

    public void withdraw(double amount) {//取钱
        balance -= amount;

    }

    public double getBalance() {
        return balance;
    }

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

class checkAccount extends BankAccount {

    public checkAccount() {
    }

    public checkAccount(double balance) {
        super(balance);
    }

    //重写存取钱方法,每次存取减1
    @Override
    public void deposit(double amount) {
        super.deposit(amount - 1);
    }

    @Override
    public void withdraw(double amount) {
        super.withdraw(amount + 1);
    }
}

//每月产生利息类
class SavingsAccount extends BankAccount {
    private double rate = 0.1;
    private int count = 0;

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public SavingsAccount() {
    }

    public SavingsAccount(double balance) {
        super(balance);
    }

    @Override
    public void deposit(double amount) {
        count++;
        if (count <= 3) {
            super.deposit(amount);
        } else {
            super.deposit(amount - 1);
        }


    }

    @Override
    public void withdraw(double amount) {
        count++;
        if (count <= 3) {
            super.withdraw(amount);
        } else {
            super.withdraw(amount + 1);
        }

    }

    //每月重置次数,并计算利率
    public void earnMonthlyInterest() {
        count = 0;
        super.deposit(getBalance() * rate);//计算利率,拿到余额*利率
    }
}

 

posted @ 2021-09-11 19:00  搬砖丶  阅读(37)  评论(0)    收藏  举报