반응형
커피 주문 시스템 프로젝트 진행
아래의 메뉴로 구성된 프로젝트를 구현한다.
일반 회원은 구현하지 않고 프로젝트를 진행하였다.
더보기
----------- 커피숍 -----------
1. 일반 회원
2. 관리자
>> 관리자
----------- 관리자 -----------
1. 커피 메뉴 조회
2. 커피 메뉴 등록
3. 홈
>> 커피 메뉴 조회
메뉴 전체 내역
------ 관리자 커피 메뉴 조회 ------
1. 커피 메뉴 수정
2. 커피 메뉴 삭제
3. 관리자 메뉴
>> 커피 메뉴 수정
커피 번호를 입력하세요
1. 이름
2. 타입
3. 가격
4. 취소
>> 커피 메뉴 등록
커피 메뉴 등록
1. 커피 이름 :
2. 커피 타입 :
3. 커피 가격 :
아래와 같이 DB 테이블과 데이터를 생성해놓는다.
최종적인 프로젝트 파일은 바로 하단에 접기로 기입해놓았다.
package controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import service.CoffeeService;
import util.ScanUtil;
import util.View;
import view.Print;
import vo.CoffeeVo;
public class MainController extends Print{
// sessionStorage은 하나 밖에 존재하지 않음
static public Map<String, Object> sessionStorage = new HashMap<>();
CoffeeService coffeeService = CoffeeService.getInstance();
public static void main(String[] args) {
new MainController().start();
}
private void start() {
View view = View.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;
}
}
}
// 회원 메뉴
private View member() {
return null;
}
// 메뉴 수정
private View adminUpdate() {
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.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.ADMIN_LIST;
}
// 메뉴 삭제
private View adminDelete() {
int no = ScanUtil.nextInt("선택할 커피 번호 >> ");
coffeeService.coffeeDelete(no);
System.out.println("삭제 되었습니다.\n");
return View.ADMIN_LIST;
}
// 메뉴 등록
private View adminInsert() {
System.out.println("커피 메뉴 등록");
String name = ScanUtil.nextLine("1. 커피 이름 : ");
String type = ScanUtil.nextLine("2. 커피 타입 : ");
int price = ScanUtil.nextInt("3. 커피 가격 : ");
List<Object> param = new ArrayList();
param.add(name);
param.add(type);
param.add(price);
coffeeService.coffeeInsert(param);
return View.ADMIN_LIST;
}
// 관리자 커피 메뉴 조회
private View adminList() {
List<CoffeeVo> coffeeList = coffeeService.coffeeList();
for(CoffeeVo coffeeVo : coffeeList) {
// int no = coffeeVo.getNo();
// String name = coffeeVo.getName();
// String type = coffeeVo.getType();
// int price = coffeeVo.getPrice();
// System.out.println(no + "\t" + name + "\t" + type + "\t" + price);
System.out.println(coffeeVo.getNo() + "\t" + coffeeVo.getName() + "\t" + coffeeVo.getType() + "\t" + coffeeVo.getPrice());
}
int coffeeNo = 0;
printAdminList();
int sel = ScanUtil.nextInt("메뉴 선택 : ");
switch (sel) {
case 1:
coffeeNo = ScanUtil.nextInt("커피 번호를 입력하세요");
sessionStorage.put("coffeeNo", coffeeNo);
return View.ADMIN_COFFEE_UPDATE;
case 2:
return View.ADMIN_COFFEE_DELETE;
case 3:
return View.ADMIN;
default:
return View.ADMIN_LIST;
}
}
// 관리자
private View admin() {
printAdmin();
int sel = ScanUtil.nextInt("메뉴 선택 : ");
switch (sel) {
case 1:
return View.ADMIN_LIST;
case 2:
return View.ADMIN_COFFEE_INSERT;
case 3:
return View.MAIN;
default:
return View.ADMIN;
}
}
// 홈
private View home() {
printHome();
int sel = ScanUtil.nextInt("메뉴 선택 : ");
switch (sel) {
case 1:
return View.MEMBER;
case 2:
return View.ADMIN;
default:
return View.MAIN;
}
}
}
package dao;
import java.util.List;
import java.util.Map;
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();
public List<CoffeeVo> coffeeList() {
String sql = "select *\r\n" +
"from java_coffee\r\n" +
"where delyn = 'N'";
// 최종 방법
return 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"); // 원래는 BigDecimal을 사용해야 함.
// 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);
// }
// 위의 방식을 줄인 방법1
// List<CoffeeVo> coffeeList = ConvertUtils.convertToList(list, CoffeeVo.class);
}
public void updateCoffee(List<Object> param, int sel) {
String format = "update java_coffee\r\n" +
"set %s = ?\r\n" +
"where no = ?\r\n"
+ "and delyn = 'N'";
String sql ="";
if(sel == 1) {
sql = String.format(format, "NAME");
}
if(sel == 2) {
sql = String.format(format, "TYPE");
}
if(sel == 3) {
sql = String.format(format, "PRICE");
}
jdbc.update(sql, param);
}
public void coffeeInsert(List<Object> param) {
String sql = "insert into java_coffee\r\n" +
"values (coffee_seq.nextval, ?, ?, ?, 'N')";
jdbc.update(sql, param);
}
public void coffeeDelete(int no) {
String sql = "update java_coffee\r\n" +
"set delyn = 'Y'\r\n" +
"where no = " + no;
jdbc.update(sql);
}
}
package service;
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();
public List<CoffeeVo> coffeeList() {
return dao.coffeeList();
}
public void updateCoffee(List<Object> param, int sel) {
dao.updateCoffee(param, sel);
}
public void coffeeInsert(List<Object> param) {
dao.coffeeInsert(param);
}
public void coffeeDelete(int no) {
dao.coffeeDelete(no);
}
}
package vo;
/*
* VO 데이터 옮기는 클래스 (MVC 패턴의 Model)
*/
public class CoffeeVo {
private int no;
private String type;
private String name;
private int price;
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 + "]";
}
}
package util;
public enum View {
MAIN, // 기본화면
ADMIN, // 관리자
MEMBER, // 회원 메뉴
MEMBER_LIST, // 일반회원 커피 메뉴 조회
MEMBER_ORDER_LIST, // 주문 내역 조회
MEMBER_ORDER, // 커피 주문
ADMIN_LIST, // 관리자 커피 메뉴 조회
ADMIN_COFFEE_INSERT, // 메뉴 등록
ADMIN_COFFEE_UPDATE, // 메뉴 수정
ADMIN_COFFEE_DELETE, // 메뉴 삭제
}
package view;
public class Print {
public void printVar() {
System.out.println("============================");
}
public void printLn(int num) {
for(int i=0; i<num; i++) System.out.println();
}
public void printHome() {
printVar();
System.out.println("----------- 커피숍 -----------");
System.out.println("1. 일반 회원");
System.out.println("2. 관리자");
printLn(4);
printVar();
}
public void printAdmin() {
printVar();
System.out.println("----------- 관리자 -----------");
System.out.println("1. 커피 메뉴 조회");
System.out.println("2. 커피 메뉴 등록");
System.out.println("3. 홈");
printLn(3);
printVar();
}
public void printAdminList() {
printVar();
System.out.println("------ 관리자 커피 메뉴 조회 ------");
System.out.println("1. 커피 메뉴 수정");
System.out.println("2. 커피 메뉴 삭제");
System.out.println("3. 관리자 메뉴");
printLn(3);
printVar();
}
}
TIP
다른 테이블 끼리의 컬럼 조인의 경우 아래와 같은 식으로 진행하면 된다.
반응형
'자바' 카테고리의 다른 글
[Java 초급] 27장 MVC 패턴5 : 게시판2 (0) | 2024.01.09 |
---|---|
[Java 초급] 26장 MVC 패턴4 : 도서 대여 시스템 (1) | 2024.01.08 |
[Java 초급] 24장 MVC 패턴2 : 게시판 만들기1 (0) | 2024.01.04 |
[Java 초급] 23장 MVC 패턴1, 회원가입 (2) | 2024.01.03 |
[Java 초급] 22.5장 테스트 (0) | 2024.01.02 |