练习1.
/*
* 写一个测试程序。
* (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
* 1000,annualInterestRate is 1.23%,balance is 1140.0
*/
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;
}
/*
* 在提款方法withdraw中,需要判断用户余额是否能够满足提款数额的要求
* 如果不能,应给出提示
*/
public void withdraw(double amount) {//取钱
if(balance < amount) {
System.out.println("余额不足,取款失败");
return;
}
balance -= amount;
System.out.println("成功取出:" + amount);
}
public void deposit(double amount) {//存钱
if(amount > 0) {
balance += amount;
System.out.println("成功存入:" + amount);
}
}
}
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 Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
public class CustomerTest {
public static void main(String[] args) {
Customer cust = new Customer("Jane", "Smith");
Account acct = new Account(1000, 2000, 0.0123);
cust.setAccount(acct);
cust.getAccount().deposit(100);
cust.getAccount().withdraw(960);
cust.getAccount().withdraw(2000);
System.out.println("Customer[" + cust.getLastName() +
"," + cust.getFirstName() + "] has a account : id is"
+ cust.getAccount().getId() + ",annualInterestRate is"
+ cust.getAccount().getAnnualInterestRate() * 100
+ "%,blanec is" + cust.getAccount().getBalance());
}
}
练习2.
public class Account {
private double balance;
public Account(double init_balance) {
this.balance = init_balance;
}
public double getBalance() {
return balance;
}
//存钱操作
public void deposit(double amt) {
if(amt > 0) {
balance += amt;
System.out.println("存钱成功");
}
}
//取钱操作
public void withdraw(double amt) {
if(balance >= amt) {
balance -= amt;
System.out.println("取钱成功");
}else {
System.out.println("余额不足");
}
}
}
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 Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
private Customer[] customers;//存放多个客户的数组
private int numberOfCustomer;//记录客户的个数
public bank() {
customers = new Customer[10];
}
//添加客户
public void addCustomer(String f,String l) {
Customer cust = new Customer(f, l);
// customers[numberOfCustomer] = cust;
// numberOfCustomer++;
//或:
customers[numberOfCustomer++] = cust;
}
//获取客户的个数
public int getNumOfCustomer() {
return numberOfCustomer;
}
//获取指定位置上的客户
public Customer getCustomer(int index) {
// return customers[index]; 可能报异常
if(index >= 0 && index < numberOfCustomer) {
return customers[index];
}
return null;
}
}
public class bankTest {
public static void main(String[] args) {
bank ban = new bank();
ban.addCustomer("Jane", "Smith");
//连续操作
ban.getCustomer(0).setAccount(new Account(2000));
ban.getCustomer(0).getAccount().withdraw(500);
double balance = ban.getCustomer(0).getAccount().getBalance();
System.out.println("客户:" + ban.getCustomer(0).getFirstName() +
"账户的余额为:" + balance);
System.out.println("***************************");
ban.addCustomer("万里", "杨");
System.out.println("银行客户的个数为:" + ban.getNumOfCustomer());
}
}