뷰 클래스
package view;
import java.util.ArrayList;
import java.util.Scanner;
import model.StudentDTO;
public class StudentView {
private Scanner sc;
public StudentView() {
sc=new Scanner(System.in);
}
public void printMenu() {
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("0. 종료");
}
public int inputAction() {
System.out.print("메뉴입력 >> ");
int action=sc.nextInt();
return action;
}
public String inputName() {
System.out.print("이름입력 >> ");
String name=sc.next();
return name;
}
public int inputScore() {
System.out.print("점수입력 >> ");
int score=sc.nextInt();
return score;
}
public int inputNum() {
System.out.print("번호입력 >> ");
int num=sc.nextInt();
return num;
}
public void printTrue() {
System.out.println("기능 수행 완료 !");
}
public void printFalse() {
System.out.println("기능 수행 실패 ...");
}
public void printDatas(ArrayList<StudentDTO> datas) {
for(StudentDTO data:datas) {
System.out.println(data);
}
}
public void printData(StudentDTO data) {
System.out.println("현재 출력된 학생");
System.out.println(" ▼▼▼");
System.out.println(data);
}
public void printUpdateMenu() {
System.out.println("===변경중...===");
System.out.println("1. 성적변경");
System.out.println("2. 이름변경");
}
}
모델(DTO) 클래스
package model;
public class StudentDTO {
private int num; // PK
private String name;
private int score;
public StudentDTO(int num,String name,int score) {
this.num=num;
this.name=name;
this.score=score;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [num=" + num + ", name=" + name + ", score=" + score + "]";
}
}
모델(DAO) 클래스
package model;
import java.util.ArrayList;
public class StudentDAO {
private ArrayList<StudentDTO> datas; // DB의 역할
public StudentDAO() {
this.datas=new ArrayList<StudentDTO>();
this.datas.add(new StudentDTO(1001,"티모",50));
this.datas.add(new StudentDTO(1002,"아리",89));
}
// R : 데이터 반환이 목표
public ArrayList<StudentDTO> selectAll(StudentDTO studentDTO){
return datas;
}
public StudentDTO selectOne(StudentDTO studentDTO) {
boolean flag=false;
int i;
for(i=0;i<datas.size();i++) {
if(datas.get(i).getNum()==studentDTO.getNum()) {
flag=true;
break;
}
}
if(!flag) {
return null;
}
return datas.get(i);
}
public boolean insert(StudentDTO studentDTO) {
this.datas.add(new StudentDTO(studentDTO.getNum(),studentDTO.getName(),studentDTO.getScore()));
return true;
}
public boolean update(StudentDTO studentDTO) {
boolean flag=false;
int i;
for(i=0;i<datas.size();i++) {
if(datas.get(i).getNum()==studentDTO.getNum()) {
flag=true;
break;
}
}
if(!flag) {
return false;
}
if(studentDTO.getName() != null) { // name 인자에 값을 부여했다면, 변경을 하고싶었던것!
datas.get(i).setName(studentDTO.getName());
System.out.println("[로그] 이름변경");
}
else {
datas.get(i).setScore(studentDTO.getScore());
System.out.println("[로그] 점수변경");
}
return true;
}
public boolean delete(StudentDTO studentDTO) {
boolean flag=false;
int i;
for(i=0;i<datas.size();i++) {
if(datas.get(i).getNum()==studentDTO.getNum()) {
flag=true;
break;
}
}
if(!flag) {
return false;
}
datas.remove(i);
return true;
}
}
컨트롤러 클래스
package ctrl;
import java.util.ArrayList;
import model.StudentDTO;
import model.StudentDAO;
import view.StudentView;
public class StudentCtrl {
private StudentView view;
private StudentDAO model;
public StudentCtrl() {
this.view=new StudentView();
this.model=new StudentDAO();
}
public void startApp() {
int PK=1003;
while(true) {
view.printMenu();
int action=view.inputAction();
if(action==0) {
break;
}
else if(action==1) {
String name=view.inputName();
int score=view.inputScore();
StudentDTO studentDTO=new StudentDTO(PK++, name, score);
boolean flag=model.insert(studentDTO);
if(flag) {
view.printTrue();
}
else {
view.printFalse();
}
}
else if(action==2) {
StudentDTO studentDTO=new StudentDTO(0,null,0);
ArrayList<StudentDTO> datas=model.selectAll(studentDTO);
view.printDatas(datas);
}
else if(action==3) {
int num=view.inputNum();
StudentDTO studentDTO=new StudentDTO(num,null,0);
StudentDTO data=model.selectOne(studentDTO);
if(data==null) {
view.printFalse();
continue;
}
view.printData(data);
}
else if(action==4) {
view.printUpdateMenu();
action=view.inputAction();
boolean flag;
int num=view.inputNum();
if(action==1) {
int score=view.inputScore();
StudentDTO studentDTO=new StudentDTO(num, null, score);
flag=model.update(studentDTO);
}
else {
String name=view.inputName();
StudentDTO studentDTO=new StudentDTO(num, name, 0);
flag=model.update(studentDTO);
}
if(flag) {
view.printTrue();
}
else {
view.printFalse();
}
}
else if(action==5) {
int num=view.inputNum();
StudentDTO studentDTO=new StudentDTO(num, null, 0);
boolean flag=model.delete(studentDTO);
if(flag) {
view.printTrue();
}
else {
view.printFalse();
}
}
}
}
}
메인(실행) 클래스
package client;
import ctrl.StudentCtrl;
public class Test {
public static void main(String[] args) {
StudentCtrl app=new StudentCtrl(); // 설치
app.startApp(); // 실행
}
}
'개발일지 > 복습' 카테고리의 다른 글
MVC 패턴 활용한 자판기 프로그램 ver.2 (0) | 2023.12.13 |
---|---|
MVC 패턴 활용한 자판기 프로그램 ver.1 (0) | 2023.12.13 |
DAO란 (0) | 2023.12.12 |
DTO란 (0) | 2023.12.12 |
MVC패턴 활용한 학생부 프로그램 (0) | 2023.12.11 |
댓글