본문 바로가기
공부기록/실습

5월 1일 과자 제품 관리 프로그램 프로젝트(MVC 패턴 - 출력부분)

by project100 2023. 5. 2.

Dto 

package com.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SnackDto {
    private String snname;
    private String snpri;
    private int snnum;
    private String sndate;

    @Override
    public String toString(){
        return "이름 : " + snname + "\n"
                + "가격 : " + snpri + "\n"
                + "수량 : " + snnum + "\n"
                + "유통기한 : " + sndate;
    }
}

Dao

package com.dao;

import com.dto.SnackDto;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SnackDao {
    private String drv = "com.mysql.cj.jdbc.Drive";
    private String url = "jdbc:mysql://127.0.0.1:3306/jdbc_db";
    private String dbuser = "dbuser";
    private String dbpass = "12341234";

    private Connection conn;
    private PreparedStatement pstmt;
    private ResultSet rs;

    public SnackDao(){
        try{
            Class.forName(drv);
        } catch (ClassNotFoundException e){
        }
    }

    public void close(){
        try{
            if(rs != null) rs.close();
            if(pstmt != null) pstmt.close();
            if(conn != null) conn.close();
        } catch (SQLException e){}
    }

    public List<SnackDto> getList() throws SQLException {
        List<SnackDto> sList = null;

        String query = "SELECT * FROM snacktb";

        conn = DriverManager.getConnection(url, dbuser, dbpass);
        pstmt = conn.prepareStatement(query);
        rs = pstmt.executeQuery();

        while (rs.next()){
            if(sList == null){
                sList = new ArrayList<>();
            }

            SnackDto sd = new SnackDto();
            sd.setSnname(rs.getString(1));
            sd.setSnpri(rs.getString(2));
            sd.setSnnum(rs.getInt(3));
            sd.setSndate(rs.getString(4));

            sList.add(sd);
        }
        return sList;
    }
}

Controller

package com.controller;

import com.dto.SnackDto;
import com.service.SnackService;
import com.view.SnackView;

import java.util.List;

public class SnackController {
    private SnackView sView = new SnackView();
    private SnackService sSev = new SnackService();

    public void run() {
        int s = -1;
        sView.viewTitle("과자 제품 관리 프로그램");

        while (true){
            s = sView.showMenu(1);

            if(s == 0){
                sView.prMsg("프로그램 종료");
                return;
            }

            switch (s){
                case 1:
                    break;
                case 2:
                    outputSnack();
                    break;
                case 3:
                    break;
                default:
                    sView.prMsg("0~3번을 입력하세요");
            }
        }
    }

    private void outputSnack() {
        sView.viewTitle("과자 정보 출력");
        List<SnackDto> sList = sSev.getList();
        if(sList != null){
            sView.outputList(sList);
        } else {
            sView.prMsg("저장된 재고가 없습니다.");
        }
    }
}

Service

import com.dao.SnackDao;
import com.dto.SnackDto;

import java.sql.SQLException;
import java.util.List;

public class SnackService {
    private SnackDao sDao = new SnackDao();

    public List<SnackDto> getList() {
        List<SnackDto> sList =  null;

        try {
            sList = sDao.getList();
        } catch (SQLException e){
        } finally {
            sDao.close();
        }
        return sList;
    }
}