栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

100121-AccountAndCheck练习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

100121-AccountAndCheck练习

AccountTest
package www.exer2;


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;


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;


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;


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;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/285504.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号