Acount
public class Acount { private double balance; //余额 //带参构造器 public Acount(double initBalance) { this.balance = initBalance; } //查询余额 public double getBalance() { return balance; } //存钱 public void deposit(double amt) { if (amt > 0) { balance += amt; System.out.println("成功存入 " + amt); } else { System.out.println("存入金额非法"); } } //取钱 public void withdwa(double amt) { if (balance >= amt) { balance -= amt; System.out.println("成功取出 " + amt); } else { System.out.println("余额不足"); } }
Customer
public class Customer { private String name; //客户名 private Acount acount; //客户账户 public Customer(String name) { this.name = name; } public String getName() { return name; } public Acount getAcount() { return acount; } public void setAcount(Acount acount) { this.acount = acount; } }
Bank
public class Bank { private Customer[] customers; //存储多个客户 private int numberOfCustomer; //存储客户的个数 //构造器 public Bank() { customers = new Customer[10]; } /** * 将指定客户进行保存 * * @param name */ public void addCustomer(String name) { Customer cust = new Customer(name); customers[numberOfCustomer++] = cust; } /** * 获取客户列表个数 * * @return */ public int geiNumberOfCustomer() { return numberOfCustomer; } /** * 获取指定位置客户 * * @param index * @return */ public Customer getCuustomer(int index) { if (index < 0 || index >= numberOfCustomer) { return null; } else { return customers[index]; } } }
测试类
public class BankTest { public static void main(String[] args) { Bank bank = new Bank(); bank.addCustomer("令狐冲"); bank.addCustomer("依琳"); bank.getCuustomer(0).setAcount(new Acount(200)); bank.getCuustomer(0).getAcount().withdwa(30); System.out.println( "账号余额为 "+bank.getCuustomer(0).getAcount().getBalance()); } }标签:Customer,Acount,return,name,--,public,OOP,Java,amt From: https://www.cnblogs.com/wdh01/p/17308366.html