2026년 1월 13일 화요일 - 4주차
수업을 진행하면서, 이전에 올렸던 StudentManagerV2를 조금 더 수정해보았다.
이번 수정에서는 구조를 크게 바꾸기보다는, 메서드 단위로 책임을 분리하며 SRP를 보완했고, 동시에 코드의 안전성을 조금 더 높이는 방향으로 리팩토링했다.
추가로, 이번 내용은 수업에서 작성한 코드가 아니고 수업을 들으면서 이해한 내용을 바탕으로 이전 포스트에 있는 코드를 가지고 스스로 리팩토링 한 결과물이다.
[StudentManagerController] - Updated
package com.joongang.stm.controller;
import com.joongang.stm.service.StudentManagerService;
import com.joongang.stm.util.IoUtil;
// 컴포넌트: 로직을 다루는 객체 <-> Dto
// 컨트롤러 역할:
// 입출력 담당(최전방)
// ㄴ 큰 흐름을 제어하며 비즈니스(코어)로직이 필요할땐 서비스(컴포넌트)를 선택해서 활용한다
public class StudentMangerController {
private StudentManagerService service = new StudentManagerService();
public void run() {
// 실질적 시작 시점
hello();
while(true) {
displayMenu();
String command = inputCommand();
// inputCommand 메서드에서 return받아온 값을 command에 넣는다.
if(isExitProgram(command)) {
break;
}
handleCommandBranch(command);
pause();
}
bye();
}
public void hello() {
IoUtil.print("***************************");
IoUtil.print("* 학생 관리 프로그램 *");
IoUtil.print("* version 2.0 *");
IoUtil.print("***************************");
}
public void displayMenu() {
IoUtil.print("========== 메뉴 ===========");
IoUtil.print("1. 등록");
IoUtil.print("2. 목록");
IoUtil.print("3. 검색");
IoUtil.print("4. 삭제");
IoUtil.print("5. 수정");
IoUtil.print("6. 통계");
IoUtil.print("0. 프로그램 종료");
}
public String inputCommand() {
return IoUtil.input("명령 입력 > "); // 입력받은 command를 return한다.
}
public boolean isExitProgram(String command) {
return "0".equals(command);
// return command.equals("0");은 command를 null 값으로
// 가지고 올 수 있는 가능성이 있기 때문에
}
public void handleCommandBranch(String command) {
if(command.equals("1")) {
service.register();
} else if(command.equals("2")) {
service.list();
} else if(command.equals("3")) {
} else if(command.equals("4")) {
} else if(command.equals("5")) {
} else if(command.equals("6")) {
} else {
IoUtil.print("잘못된 명령을 입력하셨습니다. 다시 입력해주세요");
}
}
public void pause() {
IoUtil.pause();
}
public void bye() {
IoUtil.print("프로그램이 종료됩니다.");
IoUtil.print("이용해주셔서 감사합니다.");
}
}