코드 자체 검토

25116 단어
개발 과정에서 코드의 평가는 원본 코드에 대해 체계적인 검사를 하는 과정으로 자기 평가, 그룹 평가, 팀 평가를 할 수 있다.
여기 제가 사용하기 전에 저희 실습에서 제가 작성한 데이터베이스의 조작 클래스를 자평합니다.
package imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bean.Admin;
import bean.Book;
import bean.Lend;
import bean.User;

import tools.ConnectionManager;
import dao.AdminDao;

public class AdminDaoImp implements AdminDao {

    public Connection con = null;
    PreparedStatement ps = null;
    public ResultSet rs = null;

    @Override
    public Admin login(String a_name, String a_password) {
        Admin admin = new Admin();
        con = ConnectionManager.getConnection();
        String sql = "select * from admin where a_username=? and a_password=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, a_name);
            ps.setString(2, a_password);
            rs = ps.executeQuery();
            if (rs.next() == false) {
                admin.setA_username("00000000");
            } else {
                admin.setA_username("a_name");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }

    @Override
    public int addbooks(Book book) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "insert into bookinfo(b_name,b_price,b_writer,b_press,b_type,b_location,b_barcoad) values(?,?,?,?,?,?,?)";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, book.getB_name());
            ps.setString(2, book.getB_price());
            ps.setString(3, book.getB_writer());
            ps.setString(4, book.getB_press());
            ps.setString(5, book.getB_type());
            ps.setString(6, book.getB_location());
            ps.setString(7, book.getB_barcoad());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List getAllUser() {
        List list = new ArrayList();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("select * from userinfo");
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setR_id(rs.getString("r_ID"));
                user.setR_name(rs.getString("r_name"));
                user.setR_sex(rs.getString("r_sex"));
                user.setR_occ(rs.getString("r_occ"));
                user.setR_IDnum(rs.getString("r_IDnum"));
                user.setTel(rs.getString("r_tel"));
                user.setR_mail(rs.getString("r_mail"));
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List getAllBook() {
        List list = new ArrayList();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("select * from bookinfo");
            rs = ps.executeQuery();
            while (rs.next()) {
                Book book = new Book();
                book.setB_ID(rs.getString("b_ID"));
                book.setB_name(rs.getString("b_name"));
                book.setB_price(rs.getString("b_price"));
                book.setB_writer(rs.getString("b_writer"));
                book.setB_press(rs.getString("b_press"));
                book.setB_type(rs.getString("b_type"));
                book.setB_location(rs.getString("b_location"));
                book.setB_barcoad(rs.getString("b_barcoad"));
                list.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deletBook(String bid) {
        int result = 0;
        Connection connection = ConnectionManager.getConnection();
        String sql = "DELETE FROM bookinfo WHERE b_ID=?";

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, bid);
            result = preparedStatement.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Book getbook(String bid) {
        Book book = new Book();
        con = ConnectionManager.getConnection();
        String sql = "select * from bookinfo where b_ID=?";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, bid);
            rs = ps.executeQuery();
            while (rs.next()) {
                book.setB_ID(rs.getString("b_ID"));
                book.setB_name(rs.getString("b_name"));
                book.setB_price(rs.getString("b_price"));
                book.setB_writer(rs.getString("b_writer"));
                book.setB_press(rs.getString("b_press"));
                book.setB_type(rs.getString("b_type"));
                book.setB_location(rs.getString("b_location"));
                book.setB_barcoad(rs.getString("b_barcoad"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book;
    }

    @Override
    public int updatebook(String bid, Book book) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "UPDATE bookinfo SET b_name=?, b_price=?, b_writer=?, b_press=?, b_type=?, b_location=?, b_barcoad=? WHERE b_ID=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, book.getB_name());
            ps.setString(2, book.getB_price());
            ps.setString(3, book.getB_writer());
            ps.setString(4, book.getB_press());
            ps.setString(5, book.getB_type());
            ps.setString(6, book.getB_location());
            ps.setString(7, book.getB_barcoad());
            ps.setString(8, bid);
            result = ps.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int borrowBook(Lend lend) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "insert into lending(r_tel,b_barcoad,l_borrowtime,l_backtime,l_return,l_punishment) values(?,?,?,?,?,?)";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, lend.getR_tel());
            ps.setString(2, lend.getB_barcoad());
            ps.setString(3, lend.getL_borrowtime());
            ps.setString(4, lend.getL_backtime());
            ps.setString(5, lend.getL_return());
            ps.setString(6, lend.getL_punishment());
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int returnbook(Lend lend) {
        int result = 0;
        con = ConnectionManager.getConnection();
        String sql = "UPDATE lending SET l_return=? WHERE r_tel=? AND b_barcoad=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, 1);
            ps.setString(2, lend.getR_tel());
            ps.setString(3, lend.getB_barcoad());
            result = ps.executeUpdate();
            if (result != 0) {
                result = 1;
            } else {
                result = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void overdueBook() {
        List time = getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < time.size(); i++) {
            Lend lend = new Lend();
            lend = (Lend) time.get(i);
            try {
                Date date = new Date();
                String times = df.format(date);
                Date d1 = df.parse(lend.getL_backtime());
                Date d2 = df.parse(times);
                long diff = d1.getTime() - d2.getTime();
                long punishment = diff / (1000 * 60 * 60 * 24);
                if (punishment < 0) {
                    punishment(lend.getID());
                }
            } catch (Exception e) {
            }
        }
    }


    public void punishment(String ID) {
        con = ConnectionManager.getConnection();
        String sql = "UPDATE lending SET l_punishment=1 WHERE ID=?;";

        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, ID);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List getTime() {
        List list = new ArrayList();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("SELECT * FROM lending WHERE l_punishment = 0;");
            rs = ps.executeQuery();
            while (rs.next()) {
                Lend lend = new Lend();
                lend.setL_backtime(rs.getString("l_backtime"));
                lend.setID(rs.getString("ID"));
                list.add(lend);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List getoverdue() {
        overdueBook();
        List list = new ArrayList();
        try {
            con = ConnectionManager.getConnection();
            ps = con.prepareStatement("select * from lending WHERE l_punishment = 1;");
            rs = ps.executeQuery();
            while (rs.next()) {
                Lend lend = new Lend();
                lend.setR_tel(rs.getString("r_tel"));
                lend.setB_barcoad(rs.getString("b_barcoad"));
                lend.setL_backtime(rs.getString("l_backtime"));
                list.add(lend);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}

심사에서 코드의 작성자에게 문제를 제기하는 것이지 코드에서 직접 수정하는 것이 아니다.
다음은 저의 이런 종류의 코드 리뷰입니다.
4
  • import에서 어댑터를 사용하지 않는 것이 가장 좋다

  • 4
  • connection,preparedStatement의 구성원 변수를 정의했고deletBook() 방법에서 다시 정의하여 코드의 복잡성을 증가시켰다

  • 4
  • 오버듀북 방법에서Date의 이름 d1, d2는 너무 자유롭다

  • 4
  • 클래스에 주석이 없고 방법의 기능이 명확하지 않으며 파라미터 값의 의미가 명확하지 않다
  • 좋은 웹페이지 즐겨찾기