본문 바로가기

언어/Java

Product 예제 2

※ 상품 수정, 삭제 기능 추가

public interface ProductDAOInterface {	
	/**
	 * 변경할 상품의 상품번호와 같은 상품을 저장소에서 찾아낸다
	 * 변경할 상품의 상품명 또는 가격으로 저장소 상품정보를 변경한다
	 * @param p 변경할 상품. 상품번호는 반드시 필요, 
	 * 					  p의 가격이 0이면 저장소 상품의 가격 변경안함,
	 * 					  p의 이름이 null이면 저장소 상품의 이름 변경안함
	 * @throws ModifyException; 변경할 상품이 없으면 예외발생한다
	 */
	void update(Product p) throws ModifyException;
	/**
	 * 상품을 삭제한다
	 * @param prodNo 상품번호
	 * @throws RemoveException; 삭제할 상품이 없으면 예외발생한다
	 */
	void delete(String prodNo) throws RemoveException;
}
public class ProductDAOArray implements ProductDAOInterface{
	private Product[] products = new Product[5];
	private int totalCnt;	// 상품수 0으로 자동 초기화

	public void delete(String prodNo) throws RemoveException {
		for(int i=0; i<totalCnt; i++) {
			if(products[i].getProdNo().equals(prodNo)) {
				for(int j=i; j<totalCnt -1; j++) {
					products[j] = products[j+1];
				}
				products[totalCnt - 1] = null;			
				totalCnt--;
				return;
			}
		}
			throw new RemoveException("삭제할 상품이 없습니다");
	}
	
	public void update(Product p) throws ModifyException {
		for(int i=0; i<totalCnt; i++) {
			if(products[i].getProdNo().equals(p.getProdNo())) {
				if(p.getProdPrice() != 0) {
					products[i].setProdPrice(p.getProdPrice());	
				} 
				if (p.getProdName() != null) {
					products[i].setProdName(p.getProdName());
				}
				return;
			} 
		}	
		throw new ModifyException("변경할 상품이 없습니다");
	}
}
public class ProductUser {
	void update() {
		System.out.println(">>상품 수정<<");
		System.out.print("수정할 상품번호를 입력하세요:");
		String prodNo = sc.nextLine();
		System.out.print("수정할 상품명을 입력하세요:");
		String prodName = sc.nextLine();
		System.out.print("수정할 상품가격을 입력하세요:");
		String prodPrice = sc.nextLine();
		
		if (prodName.equals("")){
			prodName = null;
		}
		
		Product p = new Product(prodNo, prodName,Integer.parseInt(prodPrice));
		try {
			dao.update(p);
		} catch (ModifyException e) {
			e.printStackTrace();
		}
			
	}
	
	void delete() {
		System.out.println(">>상품 삭제<<");
		System.out.print("삭제할 상품번호를 입력하세요:");
		String prodNo = sc.nextLine();
		try {
			dao.delete(prodNo);
		} catch (RemoveException e) {
			e.printStackTrace();
		}
	}
		
	public static void main(String[] args) {
		ProductUser user = new ProductUser();
//		user.sc.nextInt();//숫자값
//		user.sc.nextFloat();; //실수값
//		user.sc.next(); // 토큰단위로 읽음
//		user.sc.nextLine(); // 줄바꿈 단위로 읽음, 줄단위로 입력된 값을 반환해주는 메서드
		
		// 반복의 횟수, 반복 최종도달해야하는 값이 결정되있으면 for문 사용하는게 좋으나
		// 반복횟수가 정해져있지 않은 반복문은 while이 적절하다.
		while(true) {
			System.out.println("작업을 선택하세요: 상품전체목록-1, 상품번호로검색-2, 상품추가-3, 상품수정-4, 상품삭제-5, 종료-9;");
			String opt = user.sc.nextLine();
		
			if(opt.equals("1")) {
				user.findAll();
			} else if(opt.equals("2")) {
				user.findByProdNo();				
			} else if(opt.equals("3")) {
				user.add();
			} else if(opt.equals("4")) {
				user.update();
			} else if(opt.equals("5")) {
				user.delete();
			} else if(opt.equals("9")) {
				break;
			}
		}		
	}
}

'언어 > Java' 카테고리의 다른 글

ArrayList, HashSet, HashMap 예시  (0) 2023.08.08
Product 예제 3  (1) 2023.08.04
제네릭(element generic), ArrayList, HashSet 예시  (0) 2023.08.04
향상된 for문  (0) 2023.08.04
컬렉션 프레임워크(Java Collection Framework)  (0) 2023.08.04