模拟账户:写一个名为 Account 的类模拟账户。**该类的属性和方法如下图所示。该类包括的属性: 账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器

1、写一个名为 Account 的类模拟账户.该类的属性和方法如下图所示。该类包括的属性:
账号 id,余额 balance,年利率 annualInterestRate;包含的方法:访问器方法(getter 和 setter
方法),取款方法 withdraw(),存款方法 deposit()。
Account
private int id
private double balance
private double annualInterestRate
public Account (int id, double balance, double annualInterestRate )
public int getId()
public double getBalance()
public double getAnnualInterestRate()
public void setId( int id)
public void setBalance(double balance)
public void setAnnualInterestRate(double annualInterestRate)
public void withdraw (double amount)//取钱
public void deposit (double amount)//存钱
提示:在提款方法 withdraw 中,需要判断用户余额是否能够满足提款数额的要求,如果不
能,应给出提示。
2. 创建 Customer 类。
Customer
private String firstName
private String lastName
private Account account
public Customer(String f,String l)
public String getFirstName()
public String getLastName()
public Account getAccount()
public void setAccount(Account account)
a. 声明三个私有对象属性:firstName、lastName 和 account。
b. 声明一个公有构造器,这个构造器带有两个代表对象属性的参数(f 和 l)
c. 声明两个公有存取器来访问该对象属性,方法 getFirstName 和 getLastName 返回相应的属
性。
d. 声明 setAccount 方法来对 account 属性赋值。
e. 声明 getAccount 方法以获取 account 属性。
3.写一个测试程序。
(1) 创建一个 Customer ,名字叫 Jane Smith, 他有一个账号为 1000,余额为 2000 元,
年利率为 1.23% 的账户。
(2) 对 Jane Smith 操作。
存入 100 元,再取出 960 元。再取出 2000 元。

成功存入 :100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0

//1、写一个名为 Account 的类模拟账户。
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 void withdraw(double amount){
     //存钱
        this.balance+=amount;
        System.out.println("存入: "+amount+"\t"+"目前余额为:"+balance);
    }
    public void deposit(double amount){
     //取钱
        if (this.balance<amount){
     
            System.out.println("余额不足!");
            return;
        }
        this.balance-=amount;
        System.out.println("取出: "+amount+"\t"+"目前余额为:"+balance);
    }
}
//2. 创建 Customer 类。
public class Customer {
     
    private String firstName;
    private String lastName;
    private Account account;


    public Customer(String f,String l){
     
        this.firstName=f;
        this.lastName=l;

    }
    public String getFirstName() {
     
        return firstName;
    }

    public String getLastName() {
     
        return lastName;
    }

    public void setAccount(Account account) {
     
        this.account = account;
    }

    public Account getAccount() {
     
        return account;
    }

}
//3.测试程序。
public class Test {
     
    public static void main(String[] args) {
     
        Customer customer=new Customer("Jane","smith");
        Account account=new Account(1000,2000,0.0123);
        customer.setAccount(account);
           customer.getAccount().withdraw(100);
           customer.getAccount().deposit(960);
           customer.getAccount().deposit(2000);
//        account.withdraw(2565);//不涉及名称所以可以空account调用
//        account.deposit(2565);//不涉及名称所以可以空account调用
        System.out.println("Customer ["+customer.getFirstName()+","+ customer.getLastName()+"]"+"has a account: id is "+account.getId()+","+" annualInterestRate is "+account.getAnnualInterestRate()*100+"%"+",balance is "
        +account.getBalance());
        }


    }

测试成功截图:

你可能感兴趣的:(java,java)