우리는 의류 쇼핑몰 프로젝트를 하기로했음
기한 : 일주일
자바만 이용해서 콘솔 프로젝트 만들기
회사명 : freie (독일어로 자유를 의미하는 회사)
자유롭지만 프라이빗하게 회원이 가입된 경우에만 구입할 수 있게 나눔
일단 상품을 위한
카테고리테이블, 상품테이블
그리고 관리자 로그인을 위한 테이블
회원가입을 위한 테이블
카트테이블, 카트에서 주문 테이블, 주문에서 주문확정되는 컨펌테이블로 나눔
우선 관리자 부터 만듬
대분류를 나누기위해 카테고리에 cNumD를 넣어서 구분을 주고 cNum을 기본키로 엮어서 cproduct랑 연동되게 할것임
또한, cNum, pNum은 pk로 두기 위해 시퀀스를 돌려 구분을 줌
그 후 관리자 로그인해야 볼 수있어야 했으므로 관리자 테이블을 만들어줬음
핸드폰번호는 중복이 될 수 없으니까 pk로 넣어서 아이디관리를 할 수 있게끔 했음
대충 어떻게 돌아갈지 정해봄
그 후 메인 페이지를 만듬
package Shop;
import java.util.Scanner;
public class ShopMain {
public static void main(String[] args) {
int menu = -1;
Scanner sc = new Scanner(System.in);
ShopUtil util = new ShopUtil(sc);
AdminUtil aUtil = new AdminUtil(sc);
while (true) {
try {
System.out.println("…………………………………………………………");
System.out.println("1. 메인\t2. 관리자\t0.종료");
System.out.println("…………………………………………………………");
System.out.print("메뉴선택>>");
menu = sc.nextInt();
sc.nextLine();
} catch (Exception e) {
sc.nextLine();
System.err.println("잘못입력하셨습니다.");
System.err.println("1,2,0 중 선택해주세요.");
continue;
}
if (menu < 0 || menu > 2) {
System.err.println("잘못입력하셨습니다.");
System.err.println("1,2,0 중 선택해주세요.");
continue;
} else if (menu == 1) {
util.mainPage(sc);
} else if (menu == 2) {
aUtil.mainPage(sc);
} else {
System.out.println("다음에 또 이용해주세요");
System.out.println("…………………………………………………………");
break;
} // if
} // while
sc.close();
}// main
}// class
실제 돌아가는 모습
일단 회원가입을 하기위해 admin테이블을 보고 VO를 짰음
공통 모듈을 짜줌
static {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(0);
}
}
private static ShopDAO dao = null;
public ShopDAO() {
}
public static ShopDAO getInstance() {
if (dao == null)
dao = new ShopDAO();
return dao;
}
private Connection getConnection() {
Connection conn = null;
String url = "jdbc:oracle:thin:@localhost:1521:xe";
try {
conn = DriverManager.getConnection(url, "JAVADB", "1234");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
// 닫기 - pstmt, conn
private void close(PreparedStatement pstmt, Connection conn) {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 닫기 - rs, pstmt, conn
private void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
그 후 회원가입 DAO를 짬
// 회원가입(관리자)
public int insertAdmin(String phone, String password, String position) {
int result = 0;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
String sql = null;
sql = "insert into shop_admin values (?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, phone);
pstmt.setString(2, pwdEncrypt(password + phone));
pstmt.setString(3, position);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(pstmt, conn);
}
return result;
}
그후 유틸창을 짬
// 메인페이지
public void mainPage(Scanner sc) {
int menu = -1;
while (true) {
try {
System.out.println("…………………………………………………………");
System.out.println("1. 로그인");
System.out.println("2. 회원가입");
System.out.println("0. 이전화면");
System.out.println("…………………………………………………………");
System.out.print("메뉴선택>>");
menu = sc.nextInt();
sc.nextLine();
} catch (Exception e) {
sc.nextLine();
System.err.println("잘못입력하셨습니다.");
System.err.println("1,2,0 중 선택해주세요.");
continue;
}
if (menu < 0 || menu > 2) {
System.err.println("잘못입력하셨습니다.");
System.err.println("1,2,0 중 선택해주세요.");
continue;
} else if (menu == 1) {
int submenu = -1;
HashMap<String, String> map = dao.loginAdmin(sc);
if (map != null) {
phone = map.get("phone");
System.out.println();
System.out.println("………………………………………………");
System.out.println(" " + phone + "님");
System.out.println(" ♡환영합니다♡");
adminC(sc);
} else
System.err.println("아이디 또는 비밀번호가 일치하지 않습니다.");
} else if (menu == 2) {
adminJoin(sc);
} else {
System.out.println("이전화면으로 돌아갑니다");
break;
} // if
} // while
}// main
그 뒤 로그인이 가능하게 로그인을 해쉬맵에 담아서 짬
// 관리자 로그인 select
public ShopVO adminSelect(String phone, String pwd) {
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
ShopVO vo = null;
sql = "select * from shop_admin where phone=? and password=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, phone);
pstmt.setString(2, pwdEncrypt(pwd + phone));
rs = pstmt.executeQuery();
if (rs.next()) {// 읽은 튜플이 있는가?
vo = new ShopVO(rs.getString("phone"), rs.getString("position"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return vo;
}
에러처리해주고
// 상품/카테고리
public static void productC(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 상품/카테고리관리");
System.out.println("………………………………………………");
System.out.println("1. 카테고리");
System.out.println("2. 상품");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu = 0;
try {
System.out.print("메뉴선택>>");
ssMenu = sc.nextInt();
sc.nextLine();
if (ssMenu == 1) {
Category_0(sc);
break;
} else if (ssMenu == 2) {
Product_0(sc);
} else if (ssMenu == 0) {
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
} // ss if
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
sc.nextLine();
continue;
} // if
if (ssMenu < 0 || ssMenu > 2) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
continue;
} // ss if
} // wh(대)
}// 상품
// 카테고리관리
public static void Category_0(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 카테고리");
System.out.println("………………………………………………");
System.out.println("1. 등록");
System.out.println("2. 수정");
System.out.println("3. 삭제");
System.out.println("4. 조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu = 0;
try {
System.out.print("메뉴선택>>");
ssMenu = sc.nextInt();
sc.nextLine();
if (ssMenu == 1) {
String cName = null;
int cNumD = 0;
System.out.print("카테고리명>>");
cName = sc.nextLine();
System.out.println("0 남자 | 1 여자 | 2 공용");
System.out.print("대분류>>");
cNumD = sc.nextInt();
int result = dao.insertCategory(cName, cNumD);
if (result == 0) {
System.out.println(" 등록 실패");
break;
} else {
System.out.println("………………………………………………");
System.out.println("카테고리명 : [" + cName + "]");
System.out.println(" 등록 완료");
System.out.println("………………………………………………");
}
} else if (ssMenu == 2) {
while (true) {
int result = 0;
int cNum;
String cName = null;
int cNumd = -1;
System.out.print("수정할 순번>>");
cNum = sc.nextInt();
sc.nextLine();
System.out.print("수정할 이름>>");
cName = sc.nextLine();
if(cName.isEmpty()) cName=null;
System.out.println("0 남자 | 1 여자 | 2 공용");
System.out.println("수정할 대분류(분류를 안할경우 -값을 입력)>>");
cNumd = sc.nextInt();
sc.nextLine();
int result1 = dao.categoryUpdate(cNum, cName, cNumd);
if (result1 == 0) {
System.out.println(" 수정 실패");
System.out.println("………………………………………………");
break;
} else {
System.out.println("………………………………………………");
System.out.println("카테고리명 : [" + cName + "]");
System.out.println(" 수정 완료");
System.out.println("………………………………………………");
break;
}
}
} else if (ssMenu == 3) {
int cNum = 0;
while (true) {
System.out.print("삭제할 카테고리 번호>>");
try {
cNum = sc.nextInt();
sc.nextLine();
} catch (Exception e) {
// e.printStackTrace();
System.err.println("잘못입력하셨습니다. 카테고리 번호를 입력하세요");
continue;
}
int result = dao.deleteCategory(cNum);
if (result == 0) {
System.out.println(" 삭제 실패");
System.out.println("………………………………………………");
break;
} else {
System.out.println("………………………………………………");
System.out.println("카테고리명 : [" + cNum + "]");
System.out.println(" 삭제 완료");
System.out.println("………………………………………………");
break;
}
}
} else if (ssMenu == 4) {
searchC(sc);
} else if (ssMenu == 0) {
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
}
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,3,4,0 중 선택해주세요.");
sc.nextLine();
continue;
} // trycatch
if (ssMenu < 1 || ssMenu > 4) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,3,4,0 중 선택해주세요.");
continue;
}
} // wh 대
}// 카테고리 1
// 카테고리 조회2
public static void searchC(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 카테고리");
System.out.println("………………………………………………");
System.out.println("1. 전체조회");
System.out.println("2. 부분조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu1 = 0;
try {
System.out.print("메뉴선택>>");
ssMenu1 = sc.nextInt();
sc.nextLine();
if (ssMenu1 == 1) {
ArrayList<ShopVO> result = dao.selectCategory();
for (ShopVO vo : result)
System.out.println(vo.ctoString());
} else if (ssMenu1 == 2) {
String find = null;
String variable = null;
int sub = 0;
while (true) {
while (true) {
System.out.println("1 카테고리명 | 2 대분류 | 0 종료");
System.out.println("찾을카테고리타입>>");
sub = sc.nextInt();
sc.nextLine();
if (sub == 0)
break;
if (sub < 0 || sub > 2) {
System.err.println("범위가 잘못되었습니다. 다시입력해주세요");
continue;
}
try {
if (sub == 1)
variable = "cname";
else // if (sub == 2)
variable = "cnumd";
} catch (Exception e) {
System.err.println("잘못입력했습니다. 다시입력해주세요");
continue;
}
System.out.println("찾을 이름>>");
find = sc.nextLine();
if (find == null) {
System.err.println("잘못입력하셨습니다.찾을이름을 입력해주세요");
continue;
}
break;
} // while v
// System.out.println(variable);
ArrayList<ShopVO> resultd = dao.searchCategory(find, variable);
if (resultd != null)
{
int count =0;
for (ShopVO vo : resultd) {
System.out.print(vo.ctoString());
count++;}
System.out.println("검색결과 : "+count+"건");
}
break;
} // while
} else if (ssMenu1 == 0) {
System.out.println("이전화면으로 넘어갑니다.");
System.out.println("………………………………………………");
break;
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
continue;
} // if소
if (ssMenu1 < 0 || ssMenu1 > 2) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
continue;
} // if
} // wh(대)
}// 카테고리조회2
// 상품 관리
public static void Product_0(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 상품");
System.out.println("………………………………………………");
System.out.println("1. 등록");
System.out.println("2. 수정");
System.out.println("3. 삭제");
System.out.println("4. 조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu = 0;
try {
System.out.print("메뉴선택>>");
ssMenu = sc.nextInt();
sc.nextLine();
if (ssMenu == 1) {
int result = 0;
while (true) {
String pName;
System.out.print("상품명>>");
pName = sc.nextLine();
int wPrice;
System.out.print("도매가>>");
wPrice = sc.nextInt();
int margin;
System.out.print("이익률>>");
margin = sc.nextInt();
int qty;
System.out.print("수량>>");
qty = sc.nextInt();
int cNum;
ArrayList<ShopVO> resultd = dao.selectCategory();
for (ShopVO vo : resultd)
System.out.print(vo.ctoString());
System.out.print("분류코드>>");
cNum = sc.nextInt();
sc.nextLine();
result = dao.insertProduct(pName, wPrice, margin, qty, cNum);
if (result == 0) {
System.out.println(" 등록 실패");
System.out.println("………………………………………………");
continue;
} else {
System.out.println("………………………………………………");
System.out.println("상품명 : [" + pName + "]");
System.out.println(" 등록 완료");
System.out.println("………………………………………………");
break;
}
}
} else if (ssMenu == 2) {
int result = 0;
int pNum;
String pName=null;
int wPrice=-1, margin=-1, qty=-1, cNum=-1;
System.out.println("수정할 순번>>");
pNum = sc.nextInt();
sc.nextLine();
System.out.println("수정할 이름>>");
pName = sc.nextLine();
if(pName.isEmpty()) pName=null;
System.out.println("수정할 도매가(수정안할경우 -값 입력)>>");
wPrice = sc.nextInt();
sc.nextLine();
System.out.println("수정할 마진률(수정안할경우 -값 입력)>>");
margin = sc.nextInt();
sc.nextLine();
System.out.println("수정할 수량(수정안할경우 -값 입력)>>");
qty = sc.nextInt();
sc.nextLine();
ArrayList<ShopVO> resultd = dao.selectCategory();
for (ShopVO vo : resultd)
System.out.print(vo.ctoString());
System.out.println("수정할 상품분류코드(수정안할경우 -값 입력)>>");
cNum = sc.nextInt();
sc.nextLine();
result = dao.productUpdate(pNum, pName, wPrice, margin, qty, cNum);
if (result == 0) {
System.out.println(" 수정 실패");
System.out.println("………………………………………………");
continue;
} else {
System.out.println("………………………………………………");
System.out.println("상품명 : [" + pName + "]");
System.out.println(" 수정 완료");
System.out.println("………………………………………………");
break;
}
} else if (ssMenu == 3) {
int pNum = 0;
while (true) {
System.out.println("삭제할 상품 번호>>");
try {
pNum = sc.nextInt();
sc.nextLine();
} catch (Exception e) {
e.printStackTrace();
System.err.println("잘못입력하셨습니다. 상품 번호를 입력하세요");
continue;
}
int result = dao.deleteProduct(pNum);
if (result == 0) {
System.out.println(" 삭제 실패");
System.out.println("………………………………………………");
continue;
} else {
System.out.println("………………………………………………");
System.out.println("상품명 : [" + pNum + "]");
System.out.println(" 삭제 완료");
System.out.println("………………………………………………");
break;
}
}
} else if (ssMenu == 4) {
searchP(sc);
} else if (ssMenu == 0)
{
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
}
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,3,4,0 중 선택해주세요.");
sc.nextLine();
continue;
}
if (ssMenu < 1 || ssMenu > 4) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,3,4,0 중 선택해주세요.");
continue;
}
}
}
카테고리를 부분별로 나눠서 돌린다
//카테고리 등록
public int insertCategory(String cName, int cNumD) {
int result = 0;
// 1.드라이버 로딩 : 완료 - static블록에 정의
// 2.연결
Connection conn = this.getConnection();
// 3.쿼리문 만들기
String sql = "insert into categorytbl values (cnum_seq.nextval,?,?)";
// 4.PreparedStatement 객체 생성하기
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
// 5.?값설정하기
pstmt.setString(1, cName);
pstmt.setInt(2, cNumD);
// 쿼리문 전송 + 결과받기
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(pstmt, conn);
}
return result;
}
//카테고리 조회(전체)
public ArrayList<ShopVO> selectCategory() {
ArrayList<ShopVO> list = null;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from categorytbl";
ShopVO vo = null;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<ShopVO>();// Arraylist 객체 생성
do {
vo = new ShopVO(rs.getInt("cNum"), rs.getNString("cName"), rs.getInt("cNumD"));
list.add(vo);// ArrayList에 vo 객체 담기
} while (rs.next());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return list;
}
//카테고리 조회(부분)
public ArrayList<ShopVO> searchCategory(String findThing, String variable) {
ArrayList<ShopVO> list = null;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
ShopVO vo = null;
findThing= "%" + findThing + "%"; //
String sql = "select * from categorytbl where "+variable+" like ?";
// System.out.println(sql);
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, findThing);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<>();
do {
vo = new ShopVO(rs.getInt("cnum"),rs.getString("cname"),rs.getInt("cnumd"));
list.add(vo);
} while (rs.next());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return list;
}
//카테고리삭제
public int deleteCategory(int cNum) {
int result=0;
Connection conn=this.getConnection();
PreparedStatement pstmt=null;
String sql="delete from categorytbl where cnum=?";
try {
pstmt=conn.prepareStatement(sql);
//?채우기
pstmt.setInt(1, cNum);
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
//카테고리 수정 - 1이상 수정이 있는 경우 전달
public int categoryUpdate(int cNum, String cName, int cNumd) {
int result=0;
Connection conn=this.getConnection();
PreparedStatement pstmt=null;
StringBuilder sql=new StringBuilder("update categorytbl set ");
int cnt=0;//수정 필드(열) 개수
if(cName!=null) {
sql.append("cname=?,");
}
if(cNumd>=0) {
sql.append("cnumd=?,");
}
//마지막 , 없애고
sql=sql.delete(sql.length()-1, sql.length());
//where 이하 붙이기
sql.append(" where cnum=?");
try {
pstmt=conn.prepareStatement(sql.toString());
//?채우기
if(cName!=null) {
cnt++;
pstmt.setString(cnt, cName);
}
if(cNumd>=0) {
cnt++;
pstmt.setInt(cnt,cNumd);
}
//System.out.println(sql);
pstmt.setInt(++cnt, cNum);
if(cName!=null || cNumd>=0)
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
등록 수정 삭제를 만들어주고 상품도 똑같이 해주었다.
그다음 회원관리페이지
public static void adminM(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 회원 관리");
System.out.println("2. 매출 관리");
System.out.println("0. 종 료 ");
System.out.println("………………………………………………");
int ssMenu = 0;
try {
System.out.print("메뉴선택>>");
ssMenu = sc.nextInt();
sc.nextLine();
if (ssMenu == 1) {
Member_0(sc);
} else if (ssMenu == 2) {
sales_0(sc);
} else if (ssMenu == 0) {
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
}
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
sc.nextLine();
continue;
}
if (ssMenu < 0 || ssMenu > 2) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
sc.nextLine();
continue;
}
}
}
// 회원관리
public static void Member_0(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 회원관리");
System.out.println("………………………………………………");
System.out.println("1. 삭제");
System.out.println("2. 조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu = 0;
try {
System.out.print("메뉴선택>>");
ssMenu = sc.nextInt();
sc.nextLine();
if (ssMenu == 1) {
System.out.print("삭제할 아이디>>");
String id = sc.nextLine();
int result = dao.deleteMember(id);
if (result == 0) {
System.out.println(" 삭제 실패");
System.out.println("………………………………………………");
continue;
} else {
System.out.println("………………………………………………");
System.out.println("카테고리명 : [" + id + "]");
System.out.println(" 삭제 완료");
System.out.println("………………………………………………");
break;
}
} else if (ssMenu == 2) {
searchMM(sc);
} else if (ssMenu == 0) {
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
}
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
sc.nextLine();
continue;
}
if (ssMenu < 0 || ssMenu > 2) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
continue;
}
break;
}
}// m0
public static void searchMM(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 회원");
System.out.println("………………………………………………");
System.out.println("1. 전체조회");
System.out.println("2. 부분조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu1 = 0;
try {
System.out.print("메뉴선택>>");
ssMenu1 = sc.nextInt();
sc.nextLine();
if (ssMenu1 == 0) {
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
}
if (ssMenu1 == 1) {
int cnt=0;
ArrayList<ShopVO> result = dao.selectMember();
for (ShopVO vo : result) {
System.out.println(vo.member_toString());
cnt++;
}
System.out.println("검색결과 : "+cnt+"건");
} else if (ssMenu1 == 2) {
String find = null;
String variable = null;
int sub = 0;
while (true) {
System.out.println("1. 아이디 | 2. 전화번호 ");
System.out.print("찾을 맴버 타입>>");
sub = sc.nextInt();
sc.nextLine();
if (sub == 0)
break;
if (sub < 0 || sub > 2) {
System.err.println("범위가 잘못되었습니다. 다시입력해주세요");
continue;
}
try {
if (sub == 1) {
variable = "id";break;
}
else if (sub == 2) {
variable = "phone";break;}
} catch (Exception e) {
System.err.println("잘못입력했습니다. 다시입력해주세요");
continue;
}}
while(true) {
System.out.print("찾을 이름>>");
find = sc.nextLine();
if (find == null) {
System.err.println("잘못입력하셨습니다.찾을이름을 입력해주세요");
continue;
}
break;
}
ArrayList<ShopVO> resultd = dao.searchMember(find, variable);
int count = 0;
if (resultd != null)
if (sub == 1)
System.out.println("[ 아이디 : " + find + " ] 로 검색한 결과");
else if (sub == 2)
System.out.println("[ 이름 : " + find + " ] 로 검색한 결과");
for (ShopVO vo : resultd) {
count++;
System.out.print(vo.member_toString());
}
System.out.println("검색결과 : " + count + "건");
}
} catch (Exception e) {
//e.printStackTrace();
}
}
}
//매출관리
public static void sales_0(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("2. 매출관리");
System.out.println("………………………………………………");
System.out.println("1. 구매내역조회");
System.out.println("2. 전체매출조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu = 0;
try {
System.out.print("메뉴선택>>");
ssMenu = sc.nextInt();
sc.nextLine();
if (ssMenu == 1) {
searchS(sc);
} else if (ssMenu == 2) {
searchTS(sc);
} else if (ssMenu == 0) {
System.out.println("이전화면으로 넘어갑니다");
System.out.println("………………………………………………");
break;
}
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
sc.nextLine();
continue;
}
if (ssMenu < 1 || ssMenu > 2) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,0 중 선택해주세요.");
continue;
}
}
}
//구매내역조회
public static void searchS(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("1. 구매내역조회");
System.out.println("………………………………………………");
System.out.println("1. 전체조회");
System.out.println("2. 부분조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu1 = 0;
try {
System.out.print("메뉴선택>>");
ssMenu1 = sc.nextInt();
sc.nextLine();
if (ssMenu1 == 1) {
ArrayList<ShopVO> result = dao.selectSales();
if(result!=null) {
for (ShopVO vo : result)
System.out.println(vo.stoString());
}
} else if (ssMenu1 == 2) {
String find = null;
String variable = null;
int sub = 0;
while (true) {
System.out.println("1.주문번호 | 2. 제품명 | 3. 아이디 ");
System.out.print("찾을 맴버 타입(0.종료)>>");
sub = sc.nextInt();
sc.nextLine();
if (sub == 0)
break;
if (sub < 0 || sub > 3) {
System.out.println("범위가 잘못되었습니다. 다시입력해주세요");
continue;
}
try {
if (sub == 1) {
variable = "ordernum";}
else if (sub == 2) {
variable = "pname";}
else if (sub == 3) {
variable = "id";}
} catch (Exception e) {
System.err.println("잘못 입력하셨습니다. 다시 입력해주세요");
System.err.println("1,2,3,0 중 선택해주세요.");
continue;
}
System.out.print("찾을 이름>>");
find = sc.nextLine();
if (find == null) {
System.out.println("잘못입력하셨습니다.찾을이름을 입력해주세요");
continue;
}
break;
}
ArrayList<ShopVO> resultd = dao.searchSales(find, variable);
if (resultd != null) {
int count =0;
for (ShopVO vo : resultd) {
System.out.print(vo.stoString());
count++;
}
System.out.println("검색결과 : "+count+"건");
break;
}}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void searchTS(Scanner sc) {
while (true) {
System.out.println("………………………………………………");
System.out.println("2. 전체매출조회");
System.out.println("………………………………………………");
System.out.println("1. 총매출조회");
System.out.println("2. 카테고리별매출조회");
System.out.println("0. 이전화면");
System.out.println("………………………………………………");
int ssMenu1 = 0;
try {
System.out.print("메뉴선택>>");
ssMenu1 = sc.nextInt();
sc.nextLine();
if (ssMenu1 == 1) {
int tSum = dao.adminlist();
// System.out.println("--------------------------------------------------------------------------------------------");
// System.out.println("상품이름\t\t\t구매자\t수량\t판매금액\t총판매금액");
// System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("----------------------------------------------");
System.out.println("2023년도 총 매출 : " + tSum + " 원");
System.out.println("----------------------------------------------");
}
else if (ssMenu1 == 2) {
String find = null;
String variable = null;
int sub = 0;
System.out.print("찾을 이름을 쓰세요>>");
find = sc.nextLine();
if (find == null) {
System.out.println("잘못입력하셨습니다.찾을이름을 입력해주세요");
continue;
}
while (true) {
System.out.println("찾을 맴버 타입을 지정해주세요.");
System.out.println("1.아이디 | 2. 상품명 ");
System.out.print("메뉴선택>>");
sub = sc.nextInt();
sc.nextLine();
if (sub == 0)
break;
if (sub < 0 || sub > 2) {
System.out.println("범위가 잘못되었습니다. 다시입력해주세요");
continue;
}
try {
if (sub == 1) {
int tSum = dao.adminlist(find, "id");
System.out.println("----------------------------------------------");
System.out.println(find+"님의 총구매금액 : " + tSum + " 원");
System.out.println("----------------------------------------------");
}else if (sub == 2) {
int tSum = dao.adminlist(find, "pname");
System.out.println("----------------------------------------------");
System.out.println(find+"의 총판매금액 : " + tSum + " 원");
System.out.println("----------------------------------------------");
}} catch (Exception e) {
System.out.println("잘못입력했습니다. 다시입력해주세요");
continue;
}
// ArrayList<ShopVO> resultd = dao.checkSales(find,variable);
// if (resultd != null)
// for (ShopVO vo : resultd) {
// System.out.print(vo.toString());
// }
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
일단 유틸로 틀을 만들어주고
// 회원 조회(전체)
public ArrayList<ShopVO> selectMember() {
ArrayList<ShopVO> list = null;
Connection conn = dao.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from member";
ShopVO vo = null;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<ShopVO>();// Arraylist 객체 생성
do {
vo = new ShopVO(rs.getString("id"), rs.getString("phone"), rs.getString("address")
);
list.add(vo);// ArrayList에 vo 객체 담기
} while (rs.next());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);;
}
return list;
}
// 회원 조회(부분)
public ArrayList<ShopVO> searchMember(String findThing, String variable) throws SQLException {
ArrayList<ShopVO> list = null;
Connection conn = dao.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
ShopVO vo = null;
findThing = "%" + findThing + "%"; //
String sql = "select * from member where " + variable + " like ?";
// System.out.println(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, findThing);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<>();
do {
vo = new ShopVO(rs.getString("id"), rs.getString("phone"), rs.getString("address")
);
list.add(vo);
} while (rs.next());
}
this.close(rs,pstmt, conn);
return list;
}
// 회원삭제
public int deleteMember(String id) {
int result = 0;
Connection conn = dao.getConnection();
PreparedStatement pstmt = null;
String sql = "delete from shop_member where id=?";
try {
pstmt = conn.prepareStatement(sql);
// ?채우기
pstmt.setString(1, id);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
//구매내역전체조회
public ArrayList<ShopVO> selectSales() {
ArrayList<ShopVO> list = null;
Connection conn = dao.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from confirm";
ShopVO vo = null;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<ShopVO>();// Arraylist 객체 생성
do {
vo = new ShopVO(rs.getString("orderNum"), rs.getInt("pNum"), rs.getString("pName"),
rs.getInt("quantity"), rs.getString("id"),rs.getInt("sPrice"));
list.add(vo);// ArrayList에 vo 객체 담기
} while (rs.next());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return list;
}
// 구매내역 조회(부분)
public ArrayList<ShopVO> searchSales(String findThing, String variable) {
ArrayList<ShopVO> list = null;
Connection conn = dao.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
ShopVO vo = null;
findThing = "%" + findThing + "%"; //
String sql = "select * from confirm where " + variable + " like ?";
// System.out.println(sql);
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, findThing);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<>();
do {
vo = new ShopVO(rs.getString("orderNum"), rs.getInt("pNum"), rs.getString("pName"),
rs.getInt("quantity"), rs.getString("id"),rs.getInt("sPrice"));
list.add(vo);
} while (rs.next());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return list;
}
public int adminlist() {
ArrayList<ShopVO> list = new ArrayList<ShopVO>();
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
accountVO vo=null;
int sum=0;
int tSum=0;
int quantity=0;
int sPrice=0;
String sql = "select * from confirm ";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next() ) {
vo = new accountVO(rs.getInt("quantity"),rs.getInt("sPrice"));
quantity =vo.quantity;
sPrice=vo.sPrice;
sum = sPrice*quantity;
tSum = tSum+ sum;
}
//System.out.println(quantity);
//System.out.println(sPrice);
//System.out.println(tSum);
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return tSum;
}
public int adminlist(String find, String var) {
ArrayList<ShopVO> list = new ArrayList<ShopVO>();
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
accountVO vo=null;
int sum=0;
int tSum=0;
int quantity=0;
int sPrice=0;
String sql = "select * from confirm where "+var+" like ?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, find);
rs = pstmt.executeQuery();
while(rs.next() ) {
vo = new accountVO(rs.getInt("quantity"),rs.getInt("sPrice"));
quantity =vo.quantity;
sPrice=vo.sPrice;
sum = sPrice*quantity;
tSum = tSum+ sum;
}
//System.out.println(quantity);
//System.out.println(sPrice);
//System.out.println(tSum);
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return tSum;
}
// 폰체크
public String phoneCheck(String phone) throws SQLException {
ArrayList<ShopVO> list = null;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String id2 = null;
String sql = "select * from shop_admin where phone like ?";
// System.out.println(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, phone);
rs = pstmt.executeQuery();
if (rs.next())
id2 = rs.getString("phone");
this.close(rs, pstmt, conn);
return id2;
}
dao를 만들어주었다.
그리고 마진*매가를 입혀서 판매가를 만들고 그거에 제품구입수를 더해서 총 구매금액을 넣어서 그거 별로 띄울 수 있게 하였다.
아이디별로 구매내역을 볼 수있게 짰고,
아이디나 상품명으로 얼마나 팔렸는지도 알 수 있게끔 설계하였다.
구입페이지는 다음에 정리하도록하겠다
'🍺JAVA' 카테고리의 다른 글
[이클립스] 이클립스 글꼴 전체 설정하기 (0) | 2023.02.16 |
---|---|
[JAVA]콘솔프로젝트 - 쇼핑몰 2메인 구매 (0) | 2023.02.13 |
[JAVA] 끝말잇기게임 만들기 //동일단어 반복 (0) | 2023.01.11 |
[JAVA]로또게임만들기 lotto (0) | 2023.01.10 |
[JAVA] 스트링배열로 이름 입력받아 저장하기!! (0) | 2023.01.06 |
댓글