题目:
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元。
打印出Jane Smith 的基本信息
成功存入 :100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0
----------------------------------------------------------------------------------------------
-
package com.troubleshooting.bank5;
-
-
//写一个名为Account的类模拟账户
-
public class Account {
-
private int id;
-
private double balance;
-
private double annualInterestRate;
-
-
public Account(int id, double balance, double annualInterestRate) {
-
super();
-
this.id = id;
-
this.balance = balance;
-
this.annualInterestRate = annualInterestRate;
-
}
-
-
// public Account(int id, double balance, double 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;
-
}
-
-
// 在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求,如果不能,应给出提示。
-
public void withdraw(double amount) {
-
if (balance >= amount) {
-
balance -= amount;
-
System.out.println("成功取出:" + amount);
-
} else {
-
System.out.println("余额不足,取款失败");
-
}
-
}
-
-
public void deposit(double amount) {
-
balance += amount;
-
System.out.println("成功存入:" + amount);
-
}
-
}
-
-
package com.troubleshooting.bank5;
-
-
public class Customer {
-
private String firstName;
-
private String lastName;
-
private Account account;
-
-
// public Customer(String f, String l) {
-
// firstName = f;
-
// lastName = l;
-
// }
-
-
public Customer(String f, String l) {
-
super();
-
this.firstName = f;
-
this.lastName = l;
-
-
}
-
-
public String getFirstName() {
-
return firstName;
-
}
-
-
public void setFirstName(String firstName) {
-
this.firstName = firstName;
-
}
-
-
public String getLastName() {
-
return lastName;
-
}
-
-
public void setLastName(String lastName) {
-
this.lastName = lastName;
-
}
-
-
public Account getAccount() {
-
return account;
-
}
-
-
public void setAccount(Account account) {
-
this.account = account;
-
}
-
-
}
-
-
package com.troubleshooting.testbank1;
-
/*
-
* 创建一个Customer ,名字叫 Jane Smith,
-
* 他有一个账号为1000,余额为2000元,年利率为 1.23% 的账户,对Jane Smith操作。
-
* 存入 100 元,再取出960元。再取出2000元。
-
* 打印出Jane Smith 的基本信息
-
* 成功存入 :100.0
-
* 成功取出:960.0
-
* 余额不足,取款失败
-
* Customer [Smith, Jane] has a account: id is 1000, annualInterestRate is 1.23%, balance is 1140.0
-
-
*
-
*/
-
-
import com.troubleshooting.bank5.*;
-
-
public class TestCustomer {
-
public static void main(String[] args) {
-
Customer cust = new Customer("Jane", "Smith");
-
Account account = new Account(1000, 2000, 0.0123);
-
cust.setAccount(account);
-
account.deposit(100);
-
account.withdraw(960);
-
account.withdraw(2000);
-
System.out.println("Customer [" + cust.getLastName() + "," + cust.getFirstName() + "] has a acount: id is"
-
+ account.getId() + ",annualInterestRate is " + account.getAnnualInterestRate() * 100 + "%"
-
+ ", balance is " + account.getBalance());
-
}
-
-
}
显示结果:
成功存入:100.0
成功取出:960.0
余额不足,取款失败
Customer [Smith,Jane] has a acount: id is1000,annualInterestRate is 1.23%, balance is 1140.0
阅读(3302) | 评论(0) | 转发(0) |