Java 练习(银行取款测试)
作者:互联网
银行取款测试
Bank.java
public class Bank {
private Customer[] customers; //存放多个客户的数组
private int numberOfCustomers; //记录客户的个数
public Bank() {
customers = new Customer[10];
}
//添加客户
public void addCustomer(String f, String l) {
Customer cust = new Customer(f, l);
// customers[numberOfCustomers] = cust;
// numberOfCustomers++;
//或
customers[numberOfCustomers++] = cust;
}
//获取客户的个数
public int getNumOfCustomers() {
return numberOfCustomers;
}
//获取指定位置的客户
public Customer getCustomer(int index) {
if(index >= 0 && index < numberOfCustomers) {
return customers[index];
}
return null;
}
}
Account.java
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("余额不足");
}
}
}
Customer.java
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;
}
}
BankTest.java
public class BankTest {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addCustomer("Jane", "Smith");
bank.getCustomer(0).setAccount(new Account(2000));
bank.getCustomer(0).getAccount().withdraw(500);
bank.getCustomer(0).getAccount().getBalance();
double balance = bank.getCustomer(0).getAccount().getBalance();
System.out.println("客户, " + bank.getCustomer(0).getFirstName() + "的账户余额为: " + balance);
System.out.println("*********************************************");
bank.addCustomer("靖", "郭");
System.out.println("银行客户的个数为: " + bank.getNumOfCustomers());
}
}
运行结果:
标签:Customer,Account,Java,String,练习,bank,balance,public,取款 来源: https://www.cnblogs.com/klvchen/p/14372494.html