JAVA/수업

Coffee 메뉴 등록 CRUD

lavender1122 2024. 1. 5. 21:47

MainController_Coffee

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import service.CoffeeService;
import util.Print;
import util.ScanUtil;
import util.View_Coffee;
import view.CoffeePrint;
import vo.Coffeevo;

public class MainController_Coffee extends CoffeePrint { // 상속클래스라서 view 
	CoffeeService coffeeservice = CoffeeService.getInstance();
	static public Map<String, Object> sessionStorage = new HashMap<>();//static 있어서 계속 살아있음
	
	public static void main(String[] args) {
		new MainController_Coffee().start(); //start 실행
	}
	
}

start

private void start() {
		View_Coffee view = View_Coffee.MAIN; //처음시작 MAIN
		while (true) {
			switch (view) {
			case MAIN: //전체조회
				view = home(); 
				break;
			case MEMBER : //일반회원
				view = MEMBER(); 
				break;
			case ADMIN: //관리자
				view = admin(); 
				break;
			case ADMIN_LIST: 
				view = adminList(); 
				break;
			case ADMIN_COFFEE_INSERT: 
				view = adminInsert(); 
				break;
			case ADMIN_COFFEE_UPDATE: 
				view = adminUpdate(); 
				break;
			case ADMIN_COFFEE_DELETE: 
				view = adminDelete(); 
				break;
			default:
				break;
			}
			
		}
	}

adminUpdate

private View_Coffee adminUpdate() {
		System.out.println("커피 메뉴 수정");
		int coffeeNo =(int) sessionStorage.get("coffeeNo");
		
		System.out.println("1. 이름");
		System.out.println("2. 타입");
		System.out.println("3. 가격");
		System.out.println("4. 취소");
		
		int sel = ScanUtil.nextInt("메뉴 선택: ");
		if(sel == 4) return View_Coffee.ADMIN_LIST;
		List<Object> param = new ArrayList();
		if(sel == 1) {
			String name = ScanUtil.nextLine("변경될 이름 >>");
			param.add(name);
		}
		if(sel == 2) {
			String type = ScanUtil.nextLine("변경될 타입 >>");
			param.add(type);
		}
		if(sel == 3) {
			int price = ScanUtil.nextInt("변경될 가격 >>");
			param.add(price);
		}
		
		param.add(coffeeNo); // 업데이트 될 대상
		coffeeservice.updateCoffee(param, sel);
		
		return View_Coffee.ADMIN_LIST; //확인 차 list로 리턴
	}

adminDelete

private View_Coffee adminDelete() {
		System.out.println("커피 메뉴 삭제");
		return View_Coffee.ADMIN_LIST;
	}

adminInsert

private View_Coffee adminInsert() {
		System.out.println("커피 메뉴 등록");
		
		String name = ScanUtil.nextLine("커피 이름 >>");
		String type = ScanUtil.nextLine("커피 타입>>");
		int price = ScanUtil.nextInt("커피 가격 >>");
		
		List<Object> param = new ArrayList();
		param.add(name);
		param.add(type);
		param.add(price);
		
		coffeeservice.adminInsert(param);
		
		return View_Coffee.ADMIN_LIST; // 등록되면
	}

adminList

private View_Coffee adminList() {
		List<Coffeevo> coffeeList = coffeeservice.coffeeList(); // 메소드 호출
		for (Coffeevo coffeevo : coffeeList) {
			int no = coffeevo.getNo();
			String type = coffeevo.getType();
			String name = coffeevo.getName();
			int price = coffeevo.getPrice();
			System.out.println(no+  "\t" + coffeevo.getName()+"\t\t\t"+coffeevo.getType()+"\t"+coffeevo.getPrice());
		}
		
		int coffeeNo = 0;
		System.out.println("1. 커피메뉴 수정");
		System.out.println("2. 커피메뉴 삭제");
		System.out.println("3. 관리자 메뉴");
		int sel = ScanUtil.nextInt("메뉴 선택: ");
		switch (sel) {
		case 1:
			coffeeNo = ScanUtil.nextInt("커피 번호를 입력하세요");
			sessionStorage.put("coffeeNo", coffeeNo); //session(Map타입) 값 넣음
			return View_Coffee.ADMIN_COFFEE_UPDATE;
		case 2:
			coffeeNo = ScanUtil.nextInt("커피 번호를 입력하세요");
			sessionStorage.put("coffeeNo", coffeeNo);
			return View_Coffee.ADMIN_COFFEE_DELETE;
		case 3:
			return View_Coffee.MAIN;

		default:
			return View_Coffee.ADMIN_LIST;
		}
	}

admin

public View_Coffee admin() {
		System.out.println("1. 커피 메뉴 조회");
		System.out.println("2. 커피 메뉴 등록");
		System.out.println("3. 홈");
		int sel = ScanUtil.nextInt("메뉴 선택: ");
		switch (sel) {
		case 1:
			return View_Coffee.ADMIN_LIST;
		case 2:
			return View_Coffee.ADMIN_COFFEE_INSERT;
		case 3:
			return View_Coffee.MAIN;

		default://잘못 만들시
			return View_Coffee.ADMIN;
		}
	}

MEMBER

public View_Coffee MEMBER() {
		System.out.println("1. 커피 메뉴 조회");
		System.out.println("2. 주문 내역 조회");
		int sel = ScanUtil.nextInt("선택 : >>");
		//커피 메뉴 조회 전체 보여야 함
		adminList();
		//주문 내역 조회  선택함 
		//sql에서 데이터 받아와아햠 select		
		
		return null;
	}

home

private View_Coffee home() {
		//출력
		printHome(); // 상속클래스 넣어야함!!
		int sel = ScanUtil.nextInt(" 선택 : "); // 안내문구 안쓰면 실행 안됨
		switch (sel) {
		case 1:
			return View_Coffee.MEMBER;
		case 2:
			return View_Coffee.ADMIN;
	
		default:
			return View_Coffee.MAIN;
		}
	}
}

CoffeeService

import java.util.List;

import dao.CoffeeDao;
import vo.Coffeevo;

public class CoffeeService {
	//싱글톤
	private static CoffeeService instance =null;
		
		private CoffeeService() {
			
		}
		
		public static CoffeeService getInstance() {
			if(instance == null) {
				instance = new CoffeeService();
			}
			return instance;
		}
		CoffeeDao dao = CoffeeDao.getInstance(); // dao 연결

coffeeList

public List<Coffeevo> coffeeList(){
			return dao.coffeeList();
		}

updateCoffee

public void updateCoffee(List<Object> param, int sel) {
			dao.updateCoffee(param, sel);
		}

adminInsert

public void adminInsert (List<Object> param) {
			dao.adminInsert(param);
		}
}

CoffeeDao

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import util.ConvertUtils;
import util.JDBCUtil;
import vo.Coffeevo;

public class CoffeeDao {
	private static CoffeeDao instance =null;
		
		private CoffeeDao() {
			
		}
		
		public static CoffeeDao getInstance() {
			if(instance == null) {
				instance = new CoffeeDao();
			}
			return instance;
		}
		
	JDBCUtil jdbc = JDBCUtil.getInstance(); // dao 만들면 처음 하는것
	
}

coffeeList

JDBCUtil jdbc = JDBCUtil.getInstance(); // dao 만들면 처음 하는것
	
	public List<Coffeevo> coffeeList() {
		String sql = "SELECT * FROM JAVA_COFFEE";
		
		return jdbc.selectList(sql,Coffeevo.class);
//		List<Coffeevo> coffeeList = jdbc.selectList(sql,Coffeevo.class);
//		List<Coffeevo> coffeeList = new ArrayList();
//		for (Map<String,Object> map : list) {
//			Coffeevo coffee = new Coffeevoㄴ();
//			int no = (int) map.get("NO");
//			String name = (String) map.get("NAME");
//			String type = (String) map.get("TYPE");
//			int price = (int) map.get("PRICE");
//			
//			coffee.setNo(no);
//			coffee.setName(name);
//			coffee.setType(type);
//			coffee.setPrice(price);
//			
//			coffeeList.add(coffee);
//		}
		//자동 변환
//		List<Coffeevo> coffeeList =  ConvertUtils.convertToList(list, Coffeevo.class);
	}

updateCoffee

public void updateCoffee(List<Object> param, int sel) {
		String format ="UPDATE JAVA_COFFEE\r\n" + 
				" SET %s = ?\r\n" + 
				" WHERE NO =?";
		String sql = "";
		if(sel == 1) {
			sql = String.format(format, "NAME"); //format 형식:format변수명에 있는 %s에
		}
		if(sel == 2) {
			sql = String.format(format, "TYPE");
		}
		if(sel == 3) {
			sql = String.format(format, "PRICE");
		}
		  jdbc.update(sql,param);
	}

adminInsert

public void adminInsert (List<Object> param) {
		String sql ="INSERT INTO JAVA_COFFEE \r\n" + 
				"    VALUES(COFFE_SEQ. NEXTVAL,?,?,?)";
		 jdbc.update(sql,param);
	}
}

Coffeevo

import java.util.Date;

/*
 * VO 데이터 옮기는 클래스
 */

public class Coffeevo {
	//오라클에 있는 타입 변수명 명시
	private int no;  // 오라클 : number  자바: int
	private String type;
	private String name;
	private int price;
	
	//getting Setting
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Coffeevo [no=" + no + ", type=" + type + ", name=" + name + ", price=" + price + "]";
	}
}

View_Coffee

public enum View_Coffee { // 화면이동 페이지
	MAIN,					// 기본화면
	ADMIN,					//관리자 메뉴
	MEMBER,					//회원메뉴
	MEMBER_LIST,			//일반 회원 커피 메뉴조회
	MEMEBER_ORDER_LIST,		//주문 내역 조회
	MEMBER_ORDER,			//커피 주문
	ADMIN_LIST,				// 관리자 커피 메뉴조회
	ADMIN_COFFEE_INSERT,	//메뉴등록
	ADMIN_COFFEE_UPDATE,	//메뉴 수정
	ADMIN_COFFEE_DELETE		//메뉴 삭제
}

CoffeePrint

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

public class CoffeePrint {
	
	public void printVar() {
		System.out.println("(╯°□°)╯︵ 🖥️(╯°□°)╯︵🖥️(╯°□°)╯︵🖥️(╯°□°)╯︵ 🖥️");
	}
	public void printHome() {
		printVar();
		System.out.println("1. 일반회원");
		System.out.println("2. 관리자");
		printVar();
	}
}