首页 > 代码库 > JAVA之银行系统1

JAVA之银行系统1

实现银行系统的账目,增加金额,与减少金额。

当支付金额小于实际金额时,返回假。当增加金额成功时,为真。

public class Account {
    private double balance;
    public double getBalance() {
        return balance;
    }
    public Account(double balance) {
        super();
        this.balance = balance;
    }
    public boolean deposit(double amt){
        balance += amt;
        return true;
    }
    public boolean withdraw(double amt){
        if(balance < amt){
            return false;
        }
        balance -=amt;
        return true;
    }
}

实现银行系统客户类,并包含了账目类,引用同一片内存区域

public class Customer {
    private String firstName;
    private String lastName;
    private Account account;
    public Customer(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
    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 Bank {
    private Customer []customers;
    private int numberOfCustomers;
    //初始化customer数组
    public Bank()
    {
        customers=new Customer[100];
    }
    //依次添加客户
    public void addCustomer(String f,String l){
        Customer customer=new Customer(f,l);
        customers[numberOfCustomers]=customer;
        numberOfCustomers++;
               
    }
    public int getNumberOfCustomers() {
        return numberOfCustomers;
    }
    public Customer getCustomer(int index){
        return customers[index];
    }
}

依次添加客户信息,并读出

public static void main(String[] args) {
        Bank bank=new Bank();
        bank.addCustomer("Jane", "Smith");
        bank.addCustomer("Owen", "Bryant");
        bank.addCustomer("Tim", "Soley");
        bank.addCustomer("Maria", "Soley");
        for(int i=0;i<bank.getNumberOfCustomers();i++)
        {
            Customer customer=bank.getCustomer(i);
            System.out.println("Customer ["+(i+1)+"]"+
                    "is "+customer.getLastName()+
                    ","+customer.getFirstName());
              
        }
    }