package market;
import java.util.Scanner;
//배열까지 완료
public class Welcome {
// 온라인 서점 프로젝트 생성하기 사용자,관리자 정보 관리 및 관리자 로그인 기능 만들기
// 메인 메뉴 만들기 도서 정보 파일 저장 및 읽어오기 장바구니 도서 상품, 주문 처리 개선하기 메뉴 선택 및 장바구니 예외 처리하기
// 메인 메뉴 선택하기 > 도서 목록 표시하기 > 장바구니에 항목 추가하기 > 주문 처리하기
// 메뉴 정보 표시 및 종료하기 장바구니 항목 삭제 및 비우기
// 메뉴별 메서드 만들기 화면 GUI 구성하기 > 윈도우 기반 온라인 서점 만들기
static final int NUM_BOOK = 3;
static final int NUM_ITEM = 7;
static CartItem[] mCartItem = new CartItem[NUM_BOOK];
static int mCartCount = 0;
public static void main(String[] args) {
String[][] book = new String[NUM_BOOK][NUM_ITEM];
Scanner input = new Scanner(System.in);
System.out.println("당신의 이름을 입력하세요: ");
String userName = input.next();
System.out.println("연락처를 입력하세요: ");
int userMobile = input.nextInt();
String greeting = "Welcome to Shopping Mall";
String tagline = "Welcome to Book Market!";
boolean quit = false;
while (!quit) {
System.out.println("********************************************");
System.out.println("\t" + greeting);
System.out.println("\t" + tagline);
menuIntroduction();
System.out.println("메뉴 번호를 선택해주세요");
int n = input.nextInt();
System.out.println(n + "번을 선택했습니다.");
if (n < 1 || n > 8) {
System.out.println("1부터 8까지의 숫자를 입력하세요.");
} else {
switch (n) {
case 1:
System.out.println("1.현재 고객 정보: ");
System.out.println("이름 " + userName + " 연락처 " + userMobile);
menuGuestInfo(userName, userMobile);
break;
case 2:
System.out.println("2.장바구니 상품 목록 보기: ");
menuCartItemList();
break;
case 3:
System.out.println("3.장바구니 비우기");
menuCartClear();
break;
case 4:
menuCartAddItem(book);
break;
case 5:
System.out.println("5.장바구니의 항목 수량 줄이기");
menuCartRemoveItemCount();
break;
case 6:
System.out.println("6.장바구니의 항목 삭제하기");
menuCartRemoveItem();
break;
case 7:
System.out.println("7.영수증 표시하기");
menuCartBill();
break;
case 8:
System.out.println("8.종료");
menuExit();
quit = true;
break;
}
} // if else문 끝
} // while문 끝
} // main()
public static void menuIntroduction() {
System.out.println("********************************************");
System.out.println("1.고객 정보 확인하기 \t 4.바구니에 항목 추가하기");
System.out.println("2.장바구니 상품 목록 보기 \t 5.장바구니의 항목 수량 줄이기");
System.out.println("3.장바구니 비우기 \t 6.장바구니의 항목 삭제하기");
System.out.println("7.영수증 표시하기 \t 8.종료");
System.out.println("********************************************");
}
public static void menuGuestInfo(String name, int mobile) {
System.out.println("현재 고객 정보: ");
System.out.println("이름 " + name + " 연락처 " + mobile);
Person person = new Person(name, mobile);
System.out.println("이름" + person.getName() + " 연락처 " + person.getPhone());
}
public static void menuCartItemList() {
System.out.println("2.장바구니 상품 목록: ");
System.out.println("--------------------------------------------");
System.out.println("도서ID \t| 수량 \t| 합계");
for (int i = 0; i < mCartCount; i++) {
System.out.println(" " + mCartItem[i].getBookID() + " \t| ");
System.out.println(" " + mCartItem[i].getQuantity() + " \t| ");
System.out.println(" " + mCartItem[i].getTotalPrice() + " \t| ");
System.out.println(" ");
}
System.out.println("--------------------------------------------");
}
public static void menuCartClear() {
System.out.println("3.장바구니 비우기");
}
public static void menuCartAddItem(String[][] book) {
System.out.println("4.장바구니에 항목 추가하기");
boolean quit = false;
while (!quit) {
System.out.println("장바구니에 추가할 도서의 ID를 입력하세요: ");
Scanner input = new Scanner(System.in);
String str = input.nextLine();
boolean flag = false; // 입력된 도서의 ID와 저장되어 있는 도서 정보와 ID가 일치하는지 확인하여 일치하면 도서정보의 numId(인덱스번호)와 flag(일치
int numId = -1; // 여부)변수에 값을 변경하여 저장하고 반복문 종료
BookList(book);
for (int i = 0; i < NUM_BOOK; i++) {
if (str.equals(book[i][0])) {
numId = i;
flag = true;
break;
}
}
if (flag) { //flag가 참이면 반복문 종료, 거짓이면 다시 입력해 주세요
System.out.println("장바구니에 추가하시겠습니까? Y | N");
str = input.nextLine();
if (str.toUpperCase().equalsIgnoreCase("Y")) {
System.out.println(book[numId][0] + "도서가 장바구니에 추가되었습니다.");
if (!isCartInBook(book[numId][0]))
mCartItem[mCartCount++] = new CartItem(book[numId]);
}
quit = true;
} else
System.out.println("다시 입력해 주세요.");
}
}
public static boolean isCartInBook(String bookId) {
boolean flag = false;
for (int i = 0; i < mCartCount; i++) {
if (bookId == mCartItem[i].getBookID()) {
mCartItem[i].setQuantity(mCartItem[i].getQuantity()+1);
flag = true;
}
}
return flag;
}
public static void menuCartRemoveItemCount() {
System.out.println("5.장바구니의 항목 수량 줄이기");
}
public static void menuCartRemoveItem() {
System.out.println("6.장바구니의 항목 삭제하기");
}
public static void menuCartBill() {
System.out.println("7.영수증 표시하기");
}
public static void menuExit() {
System.out.println("8.종료");
}
public static void BookList(String[][] book) {
book[0][0] = "ISBN1234";
book[0][1] = "쉽게 배우는 JSP 웹 프로그래밍";
book[0][2] = "27000";
book[0][3] = "송미영";
book[0][4] = "단계별로 쇼핑몰을 구현하며 배우는 JSP 웹 프로그래밍";
book[0][5] = "IT전문서";
book[0][6] = "2018/10/18";
book[1][0] = "ISBN1235";
book[1][1] = "안드로이드 프로그래밍";
book[1][2] = "33000";
book[1][3] = "우재남";
book[1][4] = "실습 단계별 명쾌한 멘토링!";
book[1][5] = "IT전문서";
book[1][6] = "2022/01/22";
book[2][0] = "ISBN1236";
book[2][1] = "스크래치";
book[2][2] = "22000";
book[2][3] = "고광일";
book[2][4] = "컴퓨팅 사고력을 키우는 블록 코딩";
book[2][5] = "컴퓨터입문";
book[2][6] = "2019/06/10";
}
}
package market;
public class Person {
private String name;
private int phone;
private String address;
public Person(String name, int phone) {
this.name = name;
this.phone = phone;
}
public Person(String name, int phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
public String getName() {
return name;
}
public int getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(int phone) {
this.phone = phone;
}
public void setAddress(String address) {
this.address = address;
}
}
package market;
public class CartItem {
private String[] itemBook = new String[7];
private String bookID;
private int quantity;
private int totalPrice;
public CartItem() {
} // 기본
public CartItem(String[] book) {
this.itemBook = book;
this.bookID = book[0];
this.quantity = 1;
updateTotalPrice();
}
public String[] getItemBook() {
return itemBook;
}
public String getBookID() {
return bookID;
}
public int getQuantity() {
return quantity;
}
public int getTotalPrice() {
return totalPrice;
}
public void setItemBook(String[] itemBook) {
this.itemBook = itemBook;
}
public void setBookID(String bookID) {
this.bookID = bookID;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void setTotalPrice(int totalPrice) {
this.totalPrice = totalPrice;
}
public void updateTotalPrice() {
totalPrice = Integer.parseInt(this.itemBook[2]) * this.quantity;
}
}
package market;
public class Admin extends Person {
private String id = "Admin";
private String password = "Admin1234";
public Admin(String name, int phone) {
super(name, phone);
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
}
package market;
public class User extends Person{
public User(String name, int phone) {
super(name, phone);
}
public User(String username, int phone, String address) {
super(username, phone, address);
}
}