银行存取款模型
public class Test {
public static void main ( String[ ] args) {
Bank bank = new Bank ( ) ;
bank. addCustomer ( "John" , "Yan" ) ;
bank. addCustomer ( "Match" , "shun" ) ;
bank. addCustomer ( "Jane" , "zhang" ) ;
bank. getCustomer ( 0 ) . setAccount ( new Account ( 6540000 ) ) ;
bank. getCustomer ( 9 ) . setAccount ( new Account ( 40000 ) ) ;
bank. getCustomer ( 0 ) . getAccount ( ) . deposit ( 150 ) ;
bank. showInfo ( ) ;
bank. getCustomer ( 0 ) . getAccount ( ) . withdraw ( 50 ) ;
bank. find ( "Match555" ) ;
}
}
class Bank {
private Customer[ ] customers;
private int numberOfCustomer;
private Customer def = new Customer ( "" , "" ) ;
public void addCustomer ( String f, String l) {
Customer c = new Customer ( f, l) ;
if ( customers == null ) {
customers = new Customer [ 1 ] ;
customers[ 0 ] = c;
numberOfCustomer++ ;
} else {
Customer[ ] cs = new Customer [ numberOfCustomer + 1 ] ;
for ( int i = 0 ; i < customers. length; i++ ) {
cs[ i] = customers[ i] ;
}
cs[ numberOfCustomer] = c;
customers = cs;
numberOfCustomer++ ;
}
}
public int getNumOfCustomer ( ) {
return numberOfCustomer;
}
public Customer getCustomer ( int index) {
if ( index < numberOfCustomer && index >= 0 ) {
Customer customer = customers[ index] ;
return customer;
}
System. out. println ( "没有对应的用户" ) ;
return def;
}
public void showInfo ( ) {
if ( customers == null ) {
System. out. println ( "暂时没有用户信息" ) ;
System. out. println ( "---------------------------------------------" ) ;
return ;
}
for ( int i = 0 ; i < customers. length; i++ ) {
System. out. print ( i + ": " ) ;
customers[ i] . show ( ) ;
}
System. out. println ( "---------------------------------------------" ) ;
}
public void find ( String firstName) {
if ( customers != null ) {
for ( int i = 0 ; i < customers. length; i++ ) {
if ( customers[ i] . getFirstName ( ) == firstName) {
System. out. println ( " 找到用户 " ) ;
customers[ i] . show ( ) ;
System. out. println ( "---------------------------------------------" ) ;
break ;
}
if ( i == customers. length - 1 ) {
System. out. println ( "暂无用户信息" ) ;
}
}
} else {
System. out. println ( "暂无用户信息" ) ;
}
}
}
class Customer {
private String firstName;
private String lastName;
private Account account;
public Customer ( String f, String l) {
firstName = f;
lastName = l;
}
public String getFirstName ( ) {
return firstName;
}
public String getLastName ( ) {
return lastName;
}
public Account getAccount ( ) {
return account;
}
public void setAccount ( Account account) {
if ( this . account != null ) {
System. out. println ( "该用户已经存在账户了,创建失败" ) ;
return ;
}
this . account = account;
}
public void show ( ) {
if ( this . account != null ) {
System. out. println ( "Customer [firstName=" + firstName + ", lastName=" + lastName + ", balance=" + this . account. getBalance ( ) + "]" ) ;
} else {
System. out. println ( "Customer [firstName=" + firstName + ", lastName=" + lastName + ", 还未创建账户]" ) ;
}
}
}
class Account {
private double balance;
public Account ( double balance) {
this . balance = balance;
}
public double getBalance ( ) {
return balance;
}
public void deposit ( double amt) {
if ( amt >= 0 ) {
balance += amt;
System. out. println ( "成功存入:" + amt + "余额为:" + balance) ;
System. out. println ( "---------------------------------------------" ) ;
} else {
System. out. println ( "操作失败" ) ;
}
}
public void withdraw ( double amt) {
if ( balance < amt) {
System. out. println ( "余额不足,取款失败" ) ;
System. out. println ( "---------------------------------------------" ) ;
} else {
balance -= amt;
System. out. println ( "成功取款:" + amt + "余额为:" + balance) ;
System. out. println ( "---------------------------------------------" ) ;
}
}
}