package com.feige.tools;
import com.feige.beans.Customer;
import java.util.Arrays;
public class CustomerData {
private static int SIZE = 10;
private static int COUNT = 0;
private static Customer[] CUSTOMERDATA = new Customer[SIZE];
// 保存用戶信息
public static void save( Customer customer) {
// 當(dāng)使用量count 和數(shù)組的最大容量size相等,空間用完了,擴(kuò)容為原大小的2倍
if ( COUNT == SIZE ) {
CUSTOMERDATA = Arrays.copyOf(CUSTOMERDATA , SIZE * 2) ;
SIZE *= 2 ;
}
for (int i = 0; i < CUSTOMERDATA.length; i++) {
if ( null == CUSTOMERDATA[i] ) {
CUSTOMERDATA[i] = customer;
COUNT++;
}
}
}
// 查詢客戶的信息
public static Customer get (String customerId ) {
for( Customer c : CUSTOMERDATA ) {
if ( customerId.equals(c.getCustomerId() ) ) {
return c;
}
}
return null;
}
// 重載查詢方法
public static Customer get(String customerId , String pwd ) {
/* for (int i = 0; i < CUSTOMERDATA.length; i++) {
if ( customerId.equals(CUSTOMERDATA[i].getCustomerId() ) && pwd.equals(CUSTOMERDATA[i].getPwd() ) ) {
return CUSTOMERDATA[i];
}
}
return null;*/
for ( Customer c : CUSTOMERDATA ) {
if ( customerId.equals(c.getCustomerId()) && pwd.equals(c.getPwd()) ) {
return c;
}
}
return null;
}
}