헬린코린이
MVC 패턴 사용해서 로직 구성 본문
요 몇일 MVC를 이용해서 코드를 작성하는 법을 배우고 있는데
M -> V-> C로 코드를 작성하는 것이 좋은 것같다.
배우고 있는 중이라 코드가 지저분하고 미숙하다.
감안하고 보면 정신에 도움이 될 것같다.
더 배워서 좀 더 좋은 방향으로 코드를 작성할 것이다.
model
- VO
package model;
public class StudentVO {
private int snum;
private String name;
private int score;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSnum() {
return snum;
}
public void setSnum(int snum) {
this.snum = snum;
}
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 "StudentVO [snum=" + snum + ", name=" + name + ", score=" + score + ", age=" + age + "]";
}
public StudentVO() {
//웹에서 주로 사용하게될 "기본 생성자"
// + setter x N
}
public StudentVO(int num, String name, int score, int age) {
this.snum=num;
this.name=name;
this.score=score;
this.age=age;
// 샘플 데이터 생성때문에 사요중임(나중에 안쓸예정)
}
}
DAO
package model;
import java.util.ArrayList;
public class StudentDAO { //DB와 연결하는 공간
private int PK;
ArrayList<StudentVO> datas;
public StudentDAO() {
PK=100;
datas=new ArrayList<StudentVO>();
datas.add(new StudentVO(PK++, "홍길동", 91,20));
datas.add(new StudentVO(PK++, "임꺾정", 86,80));
//샘플데이터
}
// 학생추가 C
public boolean insert(StudentVO vo) {
//public boolean insert(String name, int score, int age) {
//PK,이름,성적
try {
datas.add(new StudentVO(PK++,vo.getName(),vo.getScore(),vo.getAge()));
//datas.add(new StudentVO(PK++, name, score,age));
System.out.println(" 로그: 학생추가 성공");
}catch(Exception e) {
return false;
}
return true;
}
// 이름변경 U
public boolean update(StudentVO vo) { //오버로딩할 수 있지만 헷갈릴까봐 따로 정의함.
// 4 -> Name 있고
// 5 -> Name xxx
for(int i=0 ;i<datas.size(); i++) {
if(datas.get(i).getSnum()==vo.getSnum()) {
if(vo.getName() !=null) {//변경할 이름정보가 있니?
datas.get(i).setName(vo.getName());
}else {
datas.get(i).setScore(vo.getScore());
}
System.out.println("학생정보변경 성공.");//굿
return true;
}
}
System.out.println("학생정보 변경 실패.");//굿
return false;
}
// 성적변경 U
// public void updateScore(StudentVO vo) {
// for(int i=0; i<datas.size(); i++) {
// if(datas.get(i).getSnum()==vo.getSnum()) {
// datas.get(i).setScore(vo.getScore());
// System.out.println(" 로그: 성적변경 성공");
// return;
// }
// }
// System.out.println(" 로그: 성적변경 실패");
// }
// 학생삭제 D
public boolean delete(StudentVO vo) {
for(int i=0; i<datas.size(); i++) {
if(datas.get(i).getSnum()==vo.getSnum()) {
datas.remove(i);
System.out.println(" 로그: 학생삭제 성공");
return true;
}
}
System.out.println(" 로그: 학생삭제 실패");
return false;
}
// C로부터 데이터를 받아와서, M에게 넘겨주는 작업
// 목록보기 R
// public ArrayList<StudentVO> selectAll(StudentVO vo){
// return datas;
// }
// 학생보기 R
// public StudentVO selectOne(StudentVO vo){
// for(int i=0; i<datas.size(); i++) {
// if(datas.get(i).getSnum()==vo.getSnum()) {
// return datas.get(i);
// }
// }
// return null;
// }
// public ArrayList<StudentVO> search(String searchContent){
// ArrayList<StudentVO> vos = new ArrayList<StudentVO>();
// for(int i=0; i<datas.size(); i++) {
// if(datas.get(i).getName().indexOf(searchContent) != -1) //세미콜론 찍어놈 병신?
// vos.add(datas.get(i));
// }
// return vos;
// }
public ArrayList<StudentVO> search(StudentVO searchContent){
if(searchContent==null) { //아예 아무것도 값이 없다면 그대로 모든 목록을 출력
return datas;
}
ArrayList<StudentVO> vos = new ArrayList<StudentVO>();
for(int i=0; i<datas.size(); i++) { //getName도 안 쓰고 뭐랑 비교할려고함 그러니까 당연히 에러가나지 하...
//주어가 null일 때 메서드를 수행 xxx 연산자로 확인해야함.
if(searchContent.getName() != null) {
if(datas.get(i).getName().contains(searchContent.getName())) { //세미콜론 찍어놈 병신?
vos.add(datas.get(i));
}
}else {
if(datas.get(i).getScore() > searchContent.getScore()) {
vos.add(datas.get(i));
}
}
}
return vos;
}
// M이 C에게 데이터를 전달하는 작업
}
view
package view;
import java.util.ArrayList;
import java.util.Scanner;
import model.StudentVO;
public class StudentView {
//사용자에게 뭘 보여주면 input이 있고
// 사용자가 뭘 입력을 해야하면 output이 있어야함.
private int action; //일반변수 /private /public
private Scanner sc;
public StudentView() {
sc=new Scanner(System.in);
}
public int 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("6. 학생삭제");
System.out.println("7. 프로그램종료");
System.out.println("8. 검색");
System.out.print(">>>");
Scanner sc = new Scanner(System.in);
int action=sc.nextInt();
return action;
}
public void printData(ArrayList<StudentVO> datas) {
System.out.println("== 결과 화면 ==");
for(StudentVO v: datas) {
System.out.println(v);
}
}
public String getString() {
Scanner sc = new Scanner(System.in);
System.out.println("문자열 입력>>> ");
String str= sc.next();
return str;
}
public int getScore() {
Scanner sc = new Scanner(System.in);
System.out.println(" 성적 입력>>> ");
int i= sc.nextInt();
return i;
}
public int getAge() {
Scanner sc = new Scanner(System.in);
System.out.println(" 나이 입력>>> ");
int i= sc.nextInt();
return i;
}
public int getNum() {
Scanner sc = new Scanner(System.in);
System.out.println(" 번호 입력>>> ");
int i= sc.nextInt();
return i;
}
public void success() {
System.out.println("성공입니다.");
}
public void failed() {
System.out.println("실패입니다.");
}
public int submenu() {
Scanner sc = new Scanner(System.in);
System.out.println("1. 이름");
System.out.println("2. 성적");
int i= sc.nextInt();
return i;
}
public void not() {
System.out.println("80점 이상은 없습니다.");
}
}
ctrl
package ctrl;
import model.StudentDAO;
import model.StudentVO;
import view.StudentView;
public class StudentCtrl {
private StudentDAO model;
private StudentView view;
public StudentCtrl() {
model=new StudentDAO();
view = new StudentView();
}
public void startApp() {
while(true) {
int action =view.printMenu();
if(action ==1) {
// String name= view.getString();
// int score=view.getScore();
// int age=view.getAge();
StudentVO vo = new StudentVO();
vo.setName(view.getString());
vo.setScore(view.getScore());
vo.setAge(view.getAge());
if(model.insert(vo)) {
view.success();
continue;
}
view.failed();
}else if(action==2) {
StudentVO vo = new StudentVO();
view.printData(model.search(null));
}else if(action==3) {
StudentVO vo=new StudentVO();
vo.setName(view.getString());
view.printData(model.search(vo));
}else if(action==4) {
StudentVO vo = new StudentVO();
vo.setSnum(view.getNum());
vo.setName(view.getString());
if(model.update(vo)) {
view.success();
continue;
}
view.failed();
}else if(action==5) {
StudentVO vo = new StudentVO();
vo.setSnum(view.getNum());
vo.setScore(view.getScore());
if(model.update(vo)) {
view.success();
continue;
}
view.failed();
}else if(action==6) {
StudentVO vo = new StudentVO();
vo.setSnum(view.getNum());
if(model.delete(vo)) {
view.success();
continue;
}
view.failed();
}else if(action==8){
StudentVO vo = new StudentVO();
action = view.submenu();
if(action==1) {
vo.setName(view.getString());
//view.printData(model.search(vo));
}else if(action==2) {
vo.setScore(view.getScore());
}
view.printData(model.search(vo)); //호.. 하나로도 처리 가능...
}else {
break;
}
}
}
}
Comments