생성자!!!
public CustomerService() { // AL - 기본 크기(length-저장할 데이터의 개수)를 정하지 않고 생성 customerList = new ArrayListCreate : 등록하는 코드(); } public CustomerService(int listSize) { // AL - 저장할 데이터의 개수를 매개변수로 받은 값을 이용해 생성. customerList = new ArrayList (listSize); }
/** * 고객을 등록하는 메소드. - 고객 id (id)는 중복될 수 없다. * - 등록하려는 고객의 id와 같은 id의 고객이 이미 등록된 경우 등록 처리 하지 않는다. * * @param customer * 등록할 고객 정보를 가진 Customer객체를 받을 매개변수. */ public void registerCustomer(Customer customer) { Customer cust = findCustomerById(customer.getId());// 등록할 고객의 id로 고객조회 if (cust != null) {// 이미 그 ID로 등록된 고객있다. return; } customerList.add(customer); }Retrieve : ID 한개 조회하기
// 조회 메소드들 /** * id로 고객을 조회하는 메소드 * * @param id * 조회할 고객의 ID * * @return customerList에서 조회한 고객객체. 찾는 고객이 없으면 null을 리턴한다. */ public Customer findCustomerById(String id) { for (Customer c : customerList) { if (id.equals(c.getId())) { return c; } } return null; }Retrieve : 이름 여러개 조회하기
/** * 이름으로 고객을 조회하는 메소드 * * @param name * 조회할 고객의 이름 * @return customerList에서 조회된 고객들을 담아 리턴할 ArrayList */ public ArrayListRetrieve : 마일리지 범위로 조회하기findCustomerByName(String name) { ArrayList list = new ArrayList ();// 찾은 고객을 담을 list for (Customer c : customerList) { if (name.equals(c.getName())) {// 이름이 같은 고객 조회 list.add(c); } } return list; }
/** * 마일리지 범위로 고객을 조회하는 메소드. startMileage <= 고객.mileage <=endMileage * * @param startMileage * 조회범위의 시작 마일리지값. * @param endMileage * 조회범위의 끝 마일리지값 * @return customerList에서 조회된 고객들을 담아 리턴할 ArrayList */ public ArrayListUpdate : 갱신하기(기준 : id)findCustomerByMileageRange(int startMileage, int endMileage) { ArrayList list = new ArrayList (); for (Customer c : customerList) { if (startMileage <= c.getMileage() && c.getMileage() <= endMileage) { list.add(c); } } return list; }
/** * 매개변수로 받은 고객과 같은 ID를 가진 고객정보를 찾아 변경처리. * - 수정하려는 고객의 ID가 없는 경우 처리를 진행하지 않는다. * @param newCust * 변경할 고객 정보 */ public void updateCustomerInfo(Customer newCust) { Customer cust = findCustomerById(newCust.getId()); if (cust == null) { return; } int idx = 0; for (Customer c : customerList) { if (c.getId().equals(newCust.getId())) {// 변경할 고객정보를 조회 customerList.set(idx, newCust); break; } idx++; } }Delete : 제거하기
//제거하는 메소드 - Delete public void removeCustomerById(String id) { for (Object c : customerList) { if (((Customer) c).getId().equals(id)) { customerList.remove(c); break; } } }출력하기 : Iterator
public void printCustomerList() { Iterator iter = customerList.iterator(); while(iter.hasNext()) { System.out.println(iter.next()); } }출력하기 : 향상된 for문
public void printCustomerList() { for (Object c : customerList) { System.out.println(c); } }아.... 쓰지 말아야하나....ㅠㅠ 제네릭스 쓰면 자꾸 이상하게 뜸 어쩌라는거...
반응형
'JAVA > Design Pattern' 카테고리의 다른 글
Singleton Design Pattern (0) | 2015.03.09 |
---|---|
[Design Pattern] #0. Value Object (0) | 2015.02.05 |