简单功能包括取钱和存钱
需要定义三个对象:account类,customer类,bank类
account类:
package test1;
//账户类
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("成功存入"+amt);
}
}
//取钱函数
public void withdraw(double amt){
if(balance>=amt&&amt>0){//判断余额是否大于所取出金额和所取金额是否大于零
balance-=amt;
System.out.println("成功取出"+amt);
}else{
System.out.println("取款数额有误或余额不足!!!");
}
}
}
customer类:
package test1;
public class Customer {
private String firstname;//名
private String lastname;//姓
private Account account;//定义银行类对象
public Customer(String firstname, String lastname) {//Customer赋名方法
this.firstname = firstname;
this.lastname = lastname;
}
public void setAccount(Account account) {
this.account = account;
}
public String getFirstname() {
return firstname;
}
public String getLastname() {
return lastname;
}
public Account getAccount() {
return account;
}
}
bank类:
package test1;
//银行类
public class Bank {
private Customer[] customers;//先定义customer类数组,用于保存多个客户
private int NumberOfCustomer;//定义用户数量,初始化为零
public Bank(){
customers =new Customer[10];
}
//将指定姓名的客户保存在银行的客户列表中
public void addCustomer(String firstname,String lastname){//增加客户
Customer cust=new Customer(firstname,lastname);//定义一个新的客户
customers[NumberOfCustomer++]=cust;//将客户存储在客户数数组内
}
//获取客户列表中存储的客户个数
public int getNumberOfCustomer(){
return NumberOfCustomer;
}
//获取指定指引位置上的客户
public Customer getCustomer(int index){
if(index<0||index>=NumberOfCustomer)return null;//如果数据错误则返回空地址
return customers[index];//如果数据正确则返回对应下标的数组数据
}
}
测试函数:
package test1;
public class BankTest {
public static void main(String[] args) {
Bank bank=new Bank();//新建一个银行类对象
bank.addCustomer("操","曹");//调用bank类中的addCustomer函数增加客户
bank.addCustomer("备","刘");
bank.getCustomer(0).setAccount(new Account(1000));//获取对应对象
bank.getCustomer(0).getAccount().withdraw(50);
System.out.println("账户余额为:"+bank.getCustomer(0).getAccount().getBalance());
}
}