https://hyejin283.tistory.com/299
[JAVA]콘솔프로젝트 - 쇼핑몰 1 큰틀짜기(큰틀, 관리자모드)
우리는 의류 쇼핑몰 프로젝트를 하기로했음 기한 : 일주일 자바만 이용해서 콘솔 프로젝트 만들기 회사명 : freie (독일어로 자유를 의미하는 회사) 자유롭지만 프라이빗하게 회원이 가입된 경우
hyejin283.tistory.com
일단 큰틀짜는거와 관리자는 위에 설명을 해놓았다.
일단 메인에서 회원가입과 로그인이 되게끔 틀을 짜보았다.
// 메인페이지
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) {
login(sc);
} else if (menu == 2) {
memeberJoin(sc);
} else {
System.out.println("이전화면으로 돌아갑니다");
break;
} // if
} // while
}// main
회원가입
일단 vo를 만듬 그후 테이블을 만들어 주었다
비밀번호는 암호화를 위해 길게써주었다. 그 후 아이디 체크 및 정규식을 입혀 회원가입을 좀 더 디테일하게 신경써보았따.
여기서 패턴은 정규식 패턴을 썼다.
// 일치하면 true, 일치하지 않으면 false
public boolean idCheck(String id) {
boolean chk = false;
// 영문, 숫자 조합 (8~20 자리)
match = Pattern.compile(pattern6).matcher(id);
if (match.find()) {
chk = true;
}
return chk;
}
public boolean passwordCheck(String password) {
boolean chk = false;
// 영문, 숫자, 특수문자 조합 (10~20 자리)
match = Pattern.compile(pattern1).matcher(password);
if (match.find()) {// 패턴에 맞는지 확인
chk = true;
}
return chk;
}
public boolean mobileCheck(String phone) {
boolean chk = false;
String pattern = "^01(?:0|1|[6-9])-\\d{4}-\\d{4}$";
match = Pattern.compile(pattern).matcher(phone);
if (match.find()) {// 패턴에 맞는지 확인
chk = true;
}
return chk;
}
public String passwordRegularExpressionChk(String newpassword, String oldpassword, String userId) {
boolean chk = false;
// 특수문자, 영문, 숫자 조합 (8~20 자리)
match = Pattern.compile(pattern1).matcher(newpassword);
if (match.find()) {
chk = true;
}
if (chk) {
// 같은 문자 4자리
if (samepassword(newpassword)) {
return "동일 문자, 숫자를 4자리 이상 사용할 수 없습니다.";
}
// 연속 문자 4자리
if (continuouspassword(newpassword)) {
return "연속된 숫자를 4자리 이상 사용할 수 없습니다.";
}
// 이전 비밀번호
if (newpassword.equals(oldpassword)) {
return "이전 비밀번호와 동일합니다.";
}
// 아이디와 동일 문자 4자리
if (sameId(newpassword, userId)) {
return "아이디와 동일문자 4자리 이상 사용할 수 없습니다.";
}
} else {
return "영문대소문자/숫자/특수문자 3종류 이상 조합 최소 8자리";
}
return "";
}
public boolean samepassword(String password) {
match = Pattern.compile(pattern5).matcher(password);
return match.find() ? true : false;
}
/**
* 연속 문자, 숫자 4자리 체크
*
* @param password
* @return
*/
public boolean continuouspassword(String password) {
int o = 0;
int d = 0;
int p = 0;
int n = 0;
int limit = 4;
for (int i = 0; i < password.length(); i++) {
char tempVal = password.charAt(i);
if (i > 0 && (p = o - tempVal) > -2 && (n = p == d ? n + 1 : 0) > limit - 3) {
return true;
}
d = p;
o = tempVal;
}
return false;
}
/**
* 아이디와 동일 문자 4자리 체크
*
* @param password
* @param id
* @return
*/
public boolean sameId(String password, String id) {
for (int i = 0; i < password.length() - 3; i++) {
if (id.contains(password.substring(i, i + 4))) {
return true;
}
}
return false;
}
// 회원가입
public void memeberJoin(Scanner sc) {
int menu = -1;
id = null;
String password = null, passwordCheck = null, address = null, phone = null;
int result = -1;
while (true) {
try {
System.out.println("\t회원가입창");
System.out.println("…………………………………………………………");
System.out.println("영문자, 숫자 조합(8~20자리)");
System.out.print("아이디>>");
id = sc.nextLine().trim();
if (id.length() == 0)
break;
if (!idCheck(id)) {
System.err.println("부적합한 사용자 ID입니다. 영문자, 숫자 조합(8~20자리)");
continue;
}
String idCheck = null;
idCheck = dao.idCheck(id);
// System.out.println(idCheck);
// 2. 아이디 중복 조회 select 메소드를 DAO만들기 memberSelect(String id)
if (id.equals(idCheck)) {
System.err.println("같은 ID가 존재합니다.");
continue;
}
while (true) {
System.out.println("영문자, 숫자 조합(8~20자리)");
System.out.print("비밀번호>>");
password = sc.nextLine();
// 1. 패턴체크
if (!passwordCheck(password)) {
System.err.println("부적합한 비밀번호 형식 입니다. 영문자,특수문자,숫자조합(8~20자리)");
continue;
}
System.out.print("비밀번호확인>>");
passwordCheck = sc.nextLine();
if (!password.equals(passwordCheck)) {
System.err.println("비밀번호가 일치하지 않습니다");
System.err.println("다시입력해주세요");
continue;
} else
break;
} // 비번wh
while (true) {
System.out.print("주소>>");
address = sc.nextLine().trim();
if (address.length() == 0) {
System.err.println("주소는 필수 항목 입니다.");
continue;
}
break;
}
while (true) {
System.out.println("전화번호형식 000-0000-0000");
System.out.print("전화번호>>");
phone = sc.nextLine().trim();
// 1. 패턴체크
if (!mobileCheck(phone)) {
System.err.println("전화번호 형식이 틀립니다. 000-0000-0000");
continue;
}
if (phone.length() == 0) {
System.err.println("전화번호는 필수 항목 입니다.");
continue;
}
result = dao.insertMember(id, password, address, phone);
break;
}
// System.out.println(result);
if (result == 1) {
System.out.println("…………………………………………………………");
System.out.println(" 회원가입정보안내");
ArrayList<ShopVO> list = dao.select("shop_member", id, "id");
for (ShopVO vo1 : list) {
System.out.println(list.get(0).member_toString());
}
break;
} else
System.err.println("회원가입실패");
} catch (Exception e) {
e.printStackTrace();
}
} // while
}// memberJoin
로그인
// 회원로그인
public ShopVO memberSelect(String id, String pwd) {
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
ShopVO vo = null;
sql = "select * from shop_member where id=? and password=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, pwdEncrypt(pwd + id));
rs = pstmt.executeQuery();
if (rs.next()) {// 읽은 튜플이 있는가?
vo = new ShopVO(rs.getString("id"), rs.getString("address"), rs.getString("phone"), rs.getInt("mno"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return vo;
}
로그인을 돌리고 해시맵에 확인하게끔해서 id를 불러온 후 띄우는 작업을 하였따.
// 로그인
public void login(Scanner sc) {
int menu = -1;
HashMap<String, String> map = dao.login(sc);
if (map != null) {
id = map.get("id");
System.out.println("…………………………………………………………");
System.out.println(id + "님 로그인");
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;
}
cnt = 0;
if (menu < 0 || menu > 2) {
System.err.println("잘못입력하셨습니다.");
System.err.println("1,2,0 중 선택해주세요.");
continue;
} else if (menu == 1) {
productSelect(sc);
} else if (menu == 2) {
ArrayList<ShopVO> list = dao.select("carttbl", id, "id");
for (ShopVO vo : list) {
System.out.println(vo.cart());
cnt++;
}
System.out.println("검색결과 : " + cnt + "건");
} else {
System.out.println("이전화면으로 돌아갑니다");
break;
} // if
}
} else
System.err.println("아이디 또는 비밀번호가 일치하지 않습니다.");
}
로그인이 되기 위해 해시맵에 담아 다음화면을 띄우게하였다.
vo를 만들고 dao를만들어주었다.
//상품찾기(all)
public ArrayList<ShopVO> selectAllProduct(){
ArrayList<ShopVO> list = null;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
ShopVO vo= null;
String sql="select * from cProducttbl";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next()) {
list = new ArrayList<ShopVO>();
do {
vo = new ShopVO(rs.getInt("pNum"),rs.getString("pName"),
rs.getInt("wPrice"),rs.getInt("margin"),
rs.getInt("qty"),rs.getInt("cNum"));
list.add(vo);
}while(rs.next());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.close(rs, pstmt, conn);
}
return list;
}
//카트에 넣기
public int insertcart(String id,int pNum, int quantity) {
int result = 0;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
String sql = null;
sql = "insert into carttbl";
sql += " values(cartnum_seq.nextVal,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setInt(2, pNum);
pstmt.setInt(3, quantity);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
//구매넣기
public int insertOrder(int pNum, int quantity, String id, String addr,
String phone) {
int result = 0;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
String sql = null;
sql = "insert into ordertbl";
sql += " values(onum_seq.nextVal,?,?,?,?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, pNum);
pstmt.setInt(2, quantity);
pstmt.setString(3, id);
pstmt.setString(4, addr);
pstmt.setString(5, phone);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
//구매내역띄우기
public ArrayList<ShopVO> selectPh(String id) {
ArrayList<ShopVO> list = null;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
ShopVO vo = null;
String sql = "select o.oNum, o.pNum, c.pname, o.quantity,"
+ "o.id, o.addr, o.phone from ordertbl o"
+ "join cproducttbl c on o.pnum=c.pnum where id= ?";
// System.out.println(sql);
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
list = new ArrayList<ShopVO>();
vo = new ShopVO(rs.getInt("oNum"),rs.getInt("pNum"),
rs.getString("pname"),
rs.getInt("quantity"),rs.getString("id"),
rs.getString("addr"),rs.getString("phone"));
list.add(vo);
System.out.println(vo.order_toSTring());
} while (rs.next());
} catch (SQLException e) {
//e.printStackTrace();
} finally {
this.close(rs, pstmt, conn);
}
return list;
}
//삭제
public int delete(String table, String findThing, String variable) {
int result=0;
Connection conn=this.getConnection();
PreparedStatement pstmt=null;
String sql="delete from "+table+" where "+variable+"=?";
try {
pstmt=conn.prepareStatement(sql);
//?채우기
pstmt.setString(1, findThing);
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
//카테고리 수정 - 1이상 수정이 있는 경우 전달
public int qtyUpdate(int pNum, int Squantity) {
int result=0;
Connection conn=this.getConnection();
PreparedStatement pstmt=null;
String sql="update cproducttbl set qty = ? where pNum =? ";
int cnt=0;//수정 필드(열) 개수
try {
pstmt=conn.prepareStatement(sql.toString());
pstmt.setInt(1, Squantity);
pstmt.setInt(2, pNum);
result=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
//구매확정
public int insertConfirm(int oNum,int pNum, String pName, int quantity,
String id, int sPrice) {
int result = 0;
Connection conn = this.getConnection();
PreparedStatement pstmt = null;
String sql = null;
sql = "insert into confirm values(?,?,?,?,?,?,sysdate)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, oNum);
pstmt.setInt(2, pNum);
pstmt.setString(3, pName);
pstmt.setInt(4, quantity);
pstmt.setString(5, id);
pstmt.setInt(6, sPrice);
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
this.close(pstmt, conn);
return result;
}
그 후 유틸로 연결연결
// 상품조회
public void productSelect(Scanner sc) {
int menu = -1;
while (true) {
try {
System.out.println("…………………………………………………………");
System.out.println("1. 전체조회");
System.out.println("2. 상품명으로 조회");
System.out.println("3. 대분류로 조회");
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,3,0 중 선택해주세요.");
continue;
}
if (menu < 0 || menu > 3) {
System.err.println("잘못입력하셨습니다.");
System.err.println("1,2,3,0 중 선택해주세요.");
continue;
} else if (menu == 1) {
cnt = 0;
ArrayList<ShopVO> list = dao.selectAllProduct();
for (ShopVO vo : list) {
System.out.println(vo.productSelect());
cnt++;
}
System.out.println("검색결과 : " + cnt + "건");
System.out.print("원하는 제품번호>>");
pNum = sc.nextInt();
sc.nextLine();
String pNum1 = pNum + "";
ArrayList<ShopVO> list1 = dao.select("cproducttbl", pNum1, "pNum");
cnt = 0;
if (list1 == null)
System.out.println(pNum + "상품이 없습니다");
else
for (ShopVO vo : list1) {
System.out.println(vo.productSelect());
pNum = vo.getpNum();
qty = vo.getQty();
}
purchase(sc);
} else if (menu == 2) {
String find = null;
System.out.print("상품명>>");
find = sc.nextLine();
ArrayList<ShopVO> list = dao.select("cproducttbl", find, "pname");
if (list == null)
System.out.println(find + "상품이 없습니다");
else
for (ShopVO vo : list) {
System.out.println(vo.productSelect());
pNum = vo.getpNum();
qty = vo.getQty();
}
purchase(sc);
} else if (menu == 3) {
String find = null;
System.out.print("대분류조회>>");
find = sc.nextLine();
cnt = 0;
ArrayList<ShopVO> list = dao.select("cproducttbl", find, "cnum");
if (list == null)
System.out.println(find + "상품이 없습니다");
else
for (ShopVO vo : list) {
System.out.println(vo.productSelect());
pNum = vo.getpNum();
qty = vo.getQty();
cnt++;
}
System.out.println("검색결과 : " + cnt + "건");
purchase(sc);
} else {
System.out.println("이전화면으로 돌아갑니다");
break;
} // if
} // while
}
public void purchase(Scanner sc) {
int menu = -1;
while (true) {
try {
System.out.println("1. 장바구니에 넣기 2. 구매하기 0. 이전화면");
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 result = -1;
while (true) {
System.out.print("담을 수량>>");
quantity = sc.nextInt();
sc.nextLine();
sQuantity = qty - quantity;
if (sQuantity >= 0) {
result = dao.insertcart(id, pNum, quantity);
if (result == 1) {
System.out.println("장바구니 담기 성공!!");
System.out.println("…………………………………………………………");
order(sc);
break;
} else
System.out.println("장바구니 담기 실패");
break;
} else {
System.out.println("재고가 부족합니다");
continue;
}
}
} else if (menu == 2) {
sQuantity = qty - quantity;
if (sQuantity >= 0) {
System.out.print("구매 수량>>");
quantity = sc.nextInt();
sc.nextLine();
order(sc);
} else {
System.out.println("재고가 부족합니다");
continue;
}
} else {
System.out.println("이전화면으로 돌아갑니다");
break;
} // if
} // while
}
public void order(Scanner sc) {
int result = -1;
String purchaseCheck = null;
while (true) {
System.out.print("구매하시겠습니까?(y/n)>>");
purchaseCheck = sc.nextLine();
if (purchaseCheck.equals("Y") || purchaseCheck.equals("y")) {
while (true) {
System.out.print("배송할주소입력>>");
addr = sc.nextLine();
if (addr.length() == 0) {
System.err.println("배송할 주소는 필수입력값입니다");
continue;
}
break;
}
while (true) {
System.out.print("전화번호입력>>");
phone = sc.nextLine();
// 1. 패턴체크
if (!mobileCheck(phone)) {
System.err.println("전화번호 형식이 틀립니다. 000-0000-0000");
continue;
}
if (addr.length() == 0) {
System.err.println("전화번호는 필수입력값입니다");
continue;
}
break;
}
} else {
System.out.println("이전화면으로 돌아갑니다");
}
result = dao.insertOrder(pNum, quantity, id, addr, phone);
if (result == 1) {
String pNum1 = pNum + "";
// ArrayList<ShopVO> list =dao.selectPh(id);
// for(ShopVO vo : list) {
// System.out.println(vo.order_toSTring());
// }
String pName = null;
int wPrice = 0;
int margin = 0;
int oNum = 0;
ArrayList<ShopVO> list2 = dao.select("ordertbl", id, "id");
for (ShopVO vo : list2) {
oNum = vo.oNum;
}
dao.delete("ordertbl", id, "id");
dao.delete("carttbl", id, "id");
dao.qtyUpdate(pNum, sQuantity);
ArrayList<ShopVO> list = dao.select("cproducttbl", pNum1, "pNum");
for (ShopVO vo : list) {
pName = vo.pName;
wPrice = vo.wPrice;
margin = vo.margin;
}
int sPrice = wPrice * ((margin / 100) + 1);
// System.out.println(oNum+" pnum"+pNum+" pNa"+pName+
// "주문"+quantity+"id"+id+"dd"+sPrice);
dao.insertConfirm(oNum, pNum, pName, quantity, id, sPrice);
System.out.println("구매완료");
productSelect(sc);
break;
} else
System.out.println("구매실패");
break;
}
} // 그매
우선 카트에 아이디와 제품번호 그리고 구매수량이 들어가게 해가지고 그걸 구매하면 없어지는 쿼리를 짜보았다.
그래서 구매가 확정되면 오더테이블에 있는 것들이 지워지고 확정테이블로 넘어가게끔 해서 그걸 관리자모드에서 볼 수 있게 짜보았다.
컴펌테이블이랑 프로덕트 테이블을 조인해서 투스트링으로 띄우고싶었는데...
아쉽게 실패했다..ㅠ.ㅠ(맴찢..)
그리고 장바구니에 넣고 새로 배송할주소와 전번을 입력하게 한 후 그게 다 맞다면 수량이 감소하고, 구매완료를 띄워서 confirm에 들어 가게 하였다.
완료!!
'🍺JAVA' 카테고리의 다른 글
[JAVA] Deprecated 어노테이션의 의미 (0) | 2023.07.11 |
---|---|
[이클립스] 이클립스 글꼴 전체 설정하기 (0) | 2023.02.16 |
[JAVA]콘솔프로젝트 - 쇼핑몰 1 큰틀짜기(큰틀, 관리자모드) (0) | 2023.02.13 |
[JAVA] 끝말잇기게임 만들기 //동일단어 반복 (0) | 2023.01.11 |
[JAVA]로또게임만들기 lotto (0) | 2023.01.10 |
댓글