Java、账户类Account的子类


————————————————————————————————————————————
                Account
————————————————————————————————————————————
-id: int
-balance: double
+annualInterestRate:double
-dateCreated: Date
————————————————————————————————————————————
+Account()
+Account(id: int, balance: double)
+withdraw(amount: double): boolean
+deposit(amount: double): boolean
+toString(): String
+getId(): int
+setId(id: int): void
+getBalance(): double
+setBalance(balance: double): void
+getAnnualInterestRate(): double
+setAnnualInterestRate(annualInterestRate:
           double): void
+getDateCreated(): Date
—————————————————————————————————————————————
package pack1;

import java.util.Date;

public class Account {
    private int id; //账号
    private double balance; //余额
    public static double annualInterestRate;    //年利率
    private Date dateCreated = new Date();  //开户时间

    public Account() {
    }

    /**带指定账号、余额的构造方法*/
    public Account(int id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    /**取款*/
    public boolean withdraw(double amount) {
        if(amount < 0 || amount > balance)
            return false;
        balance -= amount;
        return true;
    }

    /**存款*/
    public boolean deposit(double amount) {
        if (amount < 0) return false;
        balance += amount;
        return true;
    }

    @Override   /**返回账号、余额、开户日期的字符串*/
    public String toString() {
        return "Id: " + id + "\nBalance: " + balance + "\nDate created: " + dateCreated;
    }

    /**返回账号*/
    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 static double getAnnualInterestRate() {
        return annualInterestRate;
    }

    /**设置年利率的静态方法*/
    public static void setAnnualInterestRate(double annualInterestRate) {
        Account.annualInterestRate = annualInterestRate;
    }

    /**返回开户日期*/
    public Date getDateCreated() {
        return dateCreated;
    }
}


————————————————————————————————————
        CheckingAccount
————————————————————————————————————
-limit: double__
————————————————————————————————————
+CheckingAccount()
+CheckingAccount(id: int, balance: double)
+getLimit(): double___
+setLimit(limit: double): void__
+toString(): String
————————————————————————————————————
package pack1;

public class CheckingAccount extends Account{
    private static double limit;    //透支限定额

    public CheckingAccount() {
    }

    /**带指定账号、余额的构造方法*/
    public CheckingAccount(int id, double balance) {
        super(id, balance);
    }

    /**返回透支限定额的静态方法*/
    public static double getLimit() {
        return limit;
    }

    /**设置透支限定额的静态方法*/
    public static void setLimit(double limit) {
        CheckingAccount.limit = limit;
    }

    @Override   /**返回父类字符串、透支限定额的字符串*/
    public String toString() {
        return super.toString() + "\nLimit: " + limit;
    }
}



——————————————————————————————
        SavingAccount
——————————————————————————————
+SavingAccount()
+SavingAccount(id: int, balance: double)
——————————————————————————————

package pack1;

public class SavingAccount extends Account {
    public SavingAccount() {
    }

    public SavingAccount(int id, double balance) {
        super(id, balance);
    }
}




package pack1;

public class TestAccount {
    public static void main(String[] args) {
        Account account = new Account(1, 1000);
        CheckingAccount checkingAccount = new CheckingAccount(2, 12000);
        CheckingAccount.setLimit(200);
        CheckingAccount.setAnnualInterestRate(0.25);
        SavingAccount savingAccount = new SavingAccount(3, 2000);

        System.out.println(account + "\n");
        System.out.println(checkingAccount + "\n");
        System.out.println(savingAccount);
    }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值