100121-AccountAndCheck练习

这篇博客展示了如何使用Java实现一个简单的银行账户类`Account`,包括存款、取款和计算月利息的功能。此外,还扩展了一个`CheckAccount`类,增加了透支功能。在`CheckAccountTest`中,演示了透支限额内的正常透支和超出限额的透支情况。
摘要由CSDN通过智能技术生成

AccountTest

package www.exer2;

/**
 * @param
 * @author rang
 * @date 2021/10/1
 * @desc
 */
public class Account {
    private int id;
    private double balance;
    private double annualInterestRate;

    public Account(int id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    public int getId() {
        return id;
    }

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

    public double getBalance() {
        return balance;
    }

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

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public double getMonthlyInterest(){
        return annualInterestRate/12;
    }

    public void withdraw (double amount){
        if (balance >= amount){
            balance -= amount;
        }else {
            System.out.println("余额不足");
        }
    }

    public void deposit(double amount){
        if (amount > 0){
            balance += amount;
        }
    }

}

package www.exer2;

/**
 * @param
 * @author rang
 * @date 2021/10/1
 * @desc
 */
public class AccountTest {

    public static void main(String[] args) {
        Account acct = new Account(1122, 20000, 0.045);
        acct.withdraw(30000);
        System.out.println("您的账户余额为: "+ acct.getBalance());
        acct.withdraw(2500);
        System.out.println("您的账户余额为: "+ acct.getBalance());
        acct.deposit(3000);
        System.out.println("您的账户余额为: "+ acct.getBalance());

        System.out.println("月利率为 :"+ (acct.getAnnualInterestRate())*100+"%");

    }
}

CheckAccountTest

package www.exer2;

/**
 * @param
 * @author rang
 * @date 2021/10/1
 * @desc
 */
public class CheckAccountTest {
    public static void main(String[] args) {
        CheckAccount acct = new CheckAccount(1122,20000,0.045,5000);
        acct.withdraw(5000);
        System.out.println("您的账户余额为 :" + acct.getBalance());
        System.out.println("您的可透支额度为 :" + acct.getOverdraft());
        acct.withdraw(18000);
        System.out.println("您的账户余额为 :" + acct.getBalance());
        System.out.println("您的可透支额度为 :" + acct.getOverdraft());
        acct.withdraw(3000);
        System.out.println("您的账户余额为 :" + acct.getBalance());
        System.out.println("您的可透支额度为 :" + acct.getOverdraft());

    }
}

package www.exer2;

/**
 * @param
 * @author rang
 * @date 2021/10/1
 * @desc
 */
public class CheckAccount extends Account {
    private double overdraft;
    public CheckAccount(int id, double balance, double annualInterestRate,double overdraft){
        super(id, balance, annualInterestRate);
        this.overdraft = overdraft;
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount){
            super.withdraw(amount);
        }else if (overdraft >= (amount - getBalance())){
            overdraft -= amount - getBalance() ;
            setBalance(0);
        }else {
            System.out.println("超过可透支限额");
        }
    }

    public double getOverdraft() {
        return overdraft;
    }

    public void setOverdraft(double overdraft) {
        this.overdraft = overdraft;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值