public class IDNotFoundException extends Exception{
public IDNotFoundException(){}
public IDNotFoundException(String message){
super(message);
}
}
public class SameIDException extends Exception {
public SameIDException(){}
public SameIDException(String message){
super(message);
}
}
public class ProductManagerService {
private ArrayList productList;
public ProductManagerService(){
productList = new ArrayList();
}
private boolean isNull(Object obj){
return obj == null;
}
//추가
public void addProduct(ProductDTO productDTO)
throws NullPointerException, SameIDException{
if(isNull(productDTO)){
throw new NullPointerException("Argument로 null이 들어왔습니다.");
}
//같은 ID 가 있는 지 비교.
/*ProductDTO pdto = searchProductById(productDTO.getProductId());
if(!isNull(pdto)){
System.out.println("이미 등록된 제품 ID입니다. ID를 바꿔주세요");
return;
}
*/
for(int idx=0; idx < productList.size(); idx++){
ProductDTO pto = (ProductDTO)productList.get(idx);
//((ProductDTO)productList.get(idx)).getProductId()
if(productDTO.getProductId().equals(pto.getProductId())){
throw new SameIDException("이미 등록된 제품 ID입니다.");
}
}
productList.add(productDTO);
}
public void printProductList(){
for(Object obj:productList){
System.out.println(obj);
}
/*
for(int idx = 0; idx<productList.size(); idx++){
System.out.println(productList.get(idx));
}
*/
}
//id로 제품을 조회해서 return
public ProductDTO searchProductById(String productId){
ProductDTO returnValue = null;
for(int idx=0; idx<productList.size(); idx++){
if(((ProductDTO)productList.get(idx)).getProductId().equals(productId)){
returnValue = (ProductDTO)productList.get(idx);
break;
}
}
return returnValue;
}
//수정 처리
public void modifyProductInfo(ProductDTO productDTO)
throws NullPointerException, IDNotFoundException{
if(isNull(productDTO)){
throw new NullPointerException("Null인 데이터로 값을 변경 할 수 없습니다.");
}
ProductDTO pto = searchProductById(productDTO.getProductId());
if(pto == null){
throw new IDNotFoundException("수정할 제품 정보가 없습니다.");
}
for(int idx = 0; idx < productList.size(); idx++){
if(productDTO.getProductId().equals(((ProductDTO)productList.get(idx)).getProductId())){
productList.set(idx, productDTO);
return;
}
}
}
//삭제
public void removeProductById(String productId)
throws NullPointerException, IDNotFoundException{
if(isNull(productId)){
throw new NullPointerException("삭제할 ID는 null이면 안됩니다.");
}
// for(int idx=0; idx < productList.size(); idx++){
// if(productId.equals(((ProductDTO)productList.get(idx)).getProductId())){
// productList.remove(idx);
// break;
// }
// }
ProductDTO pto = searchProductById(productId);
if(pto == null){
throw new IDNotFoundException("삭제하려는 제품이 없습니다.");
}
productList.remove(pto);
}
public ArrayList searchProductsByMaker(String productMaker){
ArrayList list = new ArrayList();
for(int idx = 0; idx < productList.size(); idx++){
if(productMaker.equals(((ProductDTO)productList.get(idx)).getProductMaker())){
list.add(productList.get(idx));
}
}
return list;
}
}
public class TestProductManager{
public static void main(String[] args){
Television tv1 = new Television();
Television tv2 = new Television("tv-222", "파브-TV", 1000000, "삼성", "LCD 티비", false, false, true);
DVDPlayer dvd1 = new DVDPlayer("dvd-111", "Bluelay-one", 120000, "소니", "최신형 블루레이 플레이어", "C", true);
DVDPlayer dvd2 = new DVDPlayer("dvd-222", "dvd-one", 50000, "LG", "DVD-RW 플레이어", "C", false);
//관리자인 ProductManagerService객체 생성
ProductManagerService pm = new ProductManagerService();
try{
pm.addProduct(tv1);
}catch(Exception e){
System.out.println("추가중 오류 발생");
}
try{
pm.addProduct(tv2);
}catch(NullPointerException ne){
System.out.println("추가중 오류 발생");
}catch(SameIDException se){
tv2.setProductId("aaaaaa");
try {
pm.addProduct(tv2);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (SameIDException e) {
e.printStackTrace();
}
}