目标
写一个名为 Account 的类模拟账户。该类的属性和方法如下图所示。
该类包括的属性:
- 账号 id
- 余额 balance
- 年利率 annualInterestRate
- 包含的方法
- 访问器方法(getter 和 setter方法)
- 取款方法 withdraw()
- 存款方法 deposit()。
Account类
public Account (int id, double balance, double annualInterestRate )
private int id
private double balance
private 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 中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
Customer类
private String firstName
private String lastName
private Account account
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 属性。
测试程序
(1) 创建一个 Customer ,名字叫 Jane Smith, 他有一个账号为 1000,余额为 2000 元,年利率为 1.23% 的账户。
(2) 对 Jane Smith 操作。
- 存入 100 元,再取出 960 元。
- 再取出 2000 元。
- 打印出 Jane Smith 的基本信息
实现代码参考
Account
package pers.zyx.accountcustomer;
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 double getBalance(){
return balance;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setId( int id){
this.id = id;
}
public void setBalance(double balance){
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public void withdraw (double amount){
if(balance < amount){
System.out.println("余额不足,取款失败");
}else{
balance -= amount;
System.out.println("成功取出:" + amount);
}
}
public void deposit (double amount){
if(amount > 0){
balance += amount;
System.out.println("成功存入:" + amount);
}
}
}
Customer
package pers.zyx.accountcustomer;
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 Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
测试程序
package pers.zyx.accountcustomer;
public class CustomerTest {
public static void main(String[] args) {
Customer JaneSmith = new Customer("Jane","Smith");
Account acc = new Account(1000, 2000,0.0123);
JaneSmith.setAccount(acc);
JaneSmith.getAccount().deposit(100);
JaneSmith.getAccount().withdraw(960);
JaneSmith.getAccount().withdraw(2000);
System.out.println("Customer + " + "[" + JaneSmith.getLastName() + "," + JaneSmith.getFirstName() + "] " +
"has a account : id is " + JaneSmith.getAccount().getId() + " ,annualInterestRate is " + JaneSmith.getAccount().getAnnualInterestRate() +
" balance is " + JaneSmith.getAccount().getBalance());
}
}
成功存入:100.0
成功取出:960.0
余额不足,取款失败
Customer + [Smith,Jane] has a account : id is 1000 ,annualInterestRate is 0.0123 balance is 1140.0
备注
-
<br />