首页 > 代码库 > 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()); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。