열심히 끝까지
[코딩 과제] - 7/4 강사님 문제 본문
---내가 짠 코드
package class04;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Scanner;
class ProductVO{
private String name;
private int price;
private int cnt;
private String category;
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;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@Override
public String toString() {
return name+ "[" + price + "|" + cnt + "|"+ category +"]";
}
}
class ProductView{
public int action;
Scanner sc = new Scanner(System.in);
public void mainMenu() {
while(true) {
System.out.println("===메인메뉴===");
System.out.println("1) 제품 추가");
System.out.println("2) 제품 검색");
System.out.println("3) 제품 전체 목록");
System.out.println("4) 제품 삭제");
System.out.println("5) 제품 변경(재고)");
System.out.println("6) 종료");
System.out.print("번호 입력 : ");
action = sc.nextInt();
if(action > 0 && action < 7) {
break;
}
System.out.println("잘못된 입력입니다.");
System.out.println();
}
}
public void productAdd() { // 추가 로직
System.out.println("===제품추가===");
}
public void productName() { // 문고 이름 입력 로직
System.out.print("제품 입력");
}
public void productPrice() { // 문고 가격 입력 로직
System.out.print("가격 입력");
}
public void productCnt() { // 문고 개수 입력 로직
System.out.print("개수 입력");
}
public void productCategory() { // 문고 카테고리 입력 로직
System.out.print("카테고리 입력");
}
public void productCateList() {
while(true) {
System.out.println("===카테고리 리스트===");
System.out.println("1. 전자제품");
System.out.println("2. 도서");
System.out.println("3. 빵");
System.out.println("4. 음료");
System.out.println("5. 신발");
System.out.print("카테고리 입력 : ");
action = sc.nextInt();
if(action > 0 && action < 6) {
break;
}
}
}
public void productOne() {
System.out.println("===제품검색===");
}
public void productAll() {
System.out.println("===제품목록===");
}
public void productDelete() {
System.out.println("===제품삭제===");
}
public void productUpdate() {
System.out.println("===제품변경===");
}
public void productEnd() {
System.out.print("프로그램을 종료합니다.");
for(int i = 0; i < 3; i++) {
System.out.print(".");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int inputInt() {
System.out.print(" : ");
int num = sc.nextInt();
return num;
}
public String inputString() {
System.out.print(" : ");
String str = sc.next();
return str;
}
}
public class Test02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final String driverName = "oracle.jdbc.driver.OracleDriver";
final String url = "jdbc:oracle:thin:@localhost:1521:xe";
final String user = "ryo";
final String passwd = "1234";
int num = 1;
final String sql_selectOne = "SELECT NAME FROM STUDENT WHERE NUM="+num;
final String sql_selectAll = "SELECT NAME FROM STUDENT";
Connection conn = null;
Statement stmt = null;
ProductView view = new ProductView();
ArrayList<String> category = new ArrayList<String>();
category.add("전자제품");
category.add("도서");
category.add("빵");
category.add("음료");
category.add("신발");
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, passwd);
stmt = conn.createStatement();
while(true) {
view.mainMenu();
if(view.action == 1) {
ProductVO vo = new ProductVO();
view.productAdd();
view.productName();
vo.setName(view.inputString());
view.productPrice();
vo.setPrice(view.inputInt());
view.productCnt();
vo.setCnt(view.inputInt());
view.productCateList();
vo.setCategory(category.get(view.action-1));
}
else if(view.action == 2) {
view.productOne();
ResultSet rs = stmt.executeQuery(sql_selectOne);
view.productName();
num = sc.nextInt();
if(rs.next()) {
System.out.println("이름 : " + rs.getString("NAME"));
}
}
else if(view.action == 3) {
view.productAll();
ResultSet rs = stmt.executeQuery(sql_selectAll);
while(rs.next()) {
ProductVO vo = new ProductVO();
vo.setName(rs.getString("NAME"));
vo.setPrice(rs.getInt("NUM"));
vo.setCnt(rs.getInt("CNT"));
vo.setCategory(rs.getString("CATEGORY"));
System.out.println(vo);
}
}
else if(view.action == 4) {
view.productDelete();
String sql = "DELETE FROM STUDENT WHERE NUM = " + num;
num = sc.nextInt();
stmt.execute(sql);
}
else if(view.action == 5) {
view.productUpdate();
String sql = "";
num = sc.nextInt();
stmt.execute(sql);
}
else {
view.productEnd();
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
conn.close();
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
--- 강사님 코드
'디바이스 융합 자바(Java)기반 풀스택 개발자 양성과정(과제)' 카테고리의 다른 글
[10분 테코톡] 제리의 MVC 패턴 (0) | 2022.07.07 |
---|---|
[10분 테코톡] 루피의 우아한 테크코스 도서관리시스템 (0) | 2022.07.06 |
[코딩 과제] - 6/29 MVC 자판기 프로그램 + 관리자 프로그램 (0) | 2022.06.29 |
[코딩 예시] - 6/28 MVC 자판기 프로그램 + 관리자 추가 과제 (0) | 2022.06.28 |
[코딩 문제] - 6/27 강사님 문제 (0) | 2022.06.27 |