第九章第七题(Account类)(Account class)

第九章第七题(Account类)(Account class)

  • 9.7(Account类)设计一个名为Account的类,他包括:

    • 为账号定义一个名为id的int类型私有数据域(默认值为0)标识账号。
    • 为账号定义一个名为balance的double类型私有数据域(默认值为0)表示余额。
    • 一个名为annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
    • 一个名为dateCreated的Date类型的私有数据域,存储账户的开户日期。
    • 一个用于创建默认账户的无参构造方法。
    • 一个用于创建具有指定id和初始余额的账户的构造方法。
    • id、balance和annualInterestRate的访问器方法和修改器方法。
    • dateCreated的访问器方法。
    • 一个名为getMonthlyInterstRate()的方法,返回月利率。
    • 一个名为getMonthlyInterst()的方法,返回月利息。
    • 一个名为writeDraw的方法,从账户提取指定余额。
    • 一个名为deposit的方法向指定账户存储指定额度。

    画出该类的UML图并实现这个类。
    提示:方法getMonthlyInterst()用于返回月利息,而不是利率。月利息是balance*monthly-InteresRate。monthlyInterestRate是annualInterestRate/12、注意,annualInterestRate是一个百分数,比如4.5%。你需要将其除以100.
    编写一个测试程序,创建一个账户ID为1122、余额为20000美元、年利率为4.5%的Account对象。使用withDraw方法取款2500美元,使用deposit方法存款3000美元,然后打印余额、月利息以及这个账户的开户日期。
    9.7(Account class)Design a class named account, which includes:

    • Define an int type private data field named ID (the default value is 0) to identify the account.
    • Define a double type private data field named balance for the account (the default value is 0) to represent the balance.
    • A double type private data field called annualinterestrate stores the current interest rate (the default is 0). Suppose all accounts have the same interest rate.
    • A private data field of type date named datecreated stores the account opening date.
    • A nonparametric construction method for creating default accounts.
    • A constructor for creating an account with a specified ID and an initial balance.
    • Accessor and modifier methods for ID, balance, and annualinterestrate.
    • Accessor method for datecreated.
    • A method called getmonthlyinterstrate() returns the monthly interest rate.
    • A method called getmonthlyinterst() returns the monthly interest.
    • A method called writedraw withdraws the specified balance from the account.
    • A method called deposit stores the specified amount to the specified account.

    Draw the UML diagram of the class and implement the class.
    Tip: the method getmonthlyinterst() is used to return monthly interest, not interest rate. Monthly interest. Monthly interest rate is annual interest rate / 12. Note that annual interest rate is a percentage, such as 4.5%. You need to divide it by 100
    Write a test program to create an account object with an account ID of 1122, a balance of $20000 and an annual interest rate of 4.5%. Withdraw $2500 using the withdraw method, deposit $3000 using the deposit method, and print the balance, monthly interest, and the account opening date.

  • 参考代码:

package chapter09;

import java.util.Date;

public class Code_07 {
    public static void main(String[] args){
        Account account = new Account(1122,20000);
        if(account.withDraw(2500))
            System.out.println("Success");
        else
            System.out.println("Fail");

        if(account.deposit(2500))
            System.out.println("Success");
        else
            System.out.println("Fail");

        System.out.println("balance:" + account.getBalance());
        System.out.println("monthly interest:" + account.getMonthlyInterestRate() * account.getBalance());
        System.out.println(account.getDate().toString());
    }
}
class Account{
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;

    public Account(){
        dateCreated = new Date();
        id = 0;
        balance = 0;
        annualInterestRate = 0;
    }

    public Account(int ID, double BALANCE){
        dateCreated = new Date();
        id = ID;
        balance = BALANCE;
        annualInterestRate = 0.045;
    }

    public int getId(){
        return id;
    }

    public void setId(int ID){
        id = ID;
    }

    public double getBalance(){
        return balance;
    }

    public void setBalance(double ba){
        balance = ba;
    }

    public double getAnnualInterestRate(){
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annual){
        annualInterestRate = annual;
    }

    public Date getDate(){
        return dateCreated;
    }

    public double getMonthlyInterestRate(){
        return annualInterestRate / 12.0;
    }

    public boolean withDraw(double money){
        if(balance >= money){
            balance -= money;
            return true;
        }
        else
            return false;
    }

    public boolean deposit(double money){
        if(money >= 0){
            balance += money;
            return true;
        }
        else
            return false;
    }
}

  • 结果显示:
Success
Success
balance:20000.0
monthly interest:75.0
Fri Oct 30 18:09:50 CST 2020

Process finished with exit code 0

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值