본문 바로가기

JAVA

[JAVA] MVC 학생부 프로그램 실습

반응형

MVC 패턴 실습으로 학생부 프로그램을 만들어보도록 하겠습니다. 

 

VO 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package model;
 
public class StudentVO {
    private int num; // PK
    private String name; //이름 
    private int 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 "StudentVO [num=" + num + ", name=" + name + ", score=" + score + "]";
    }
}
cs

DAO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package model;
 
import java.util.ArrayList;
 
public class StudentDAO {
    ArrayList<StudentVO> datas; // DB
    private int pk; // 시스템(프로그램)에서 부여
    public StudentDAO() {
        datas=new ArrayList<StudentVO>();
        pk=1001// pk값 설정 
 
        // 샘플(초기) 데이터
        StudentVO vo=new StudentVO(); 
        vo.setName("홍길동");
        vo.setNum(pk++);
        vo.setScore(50);
        datas.add(vo);   
    }
    public boolean insert(StudentVO vo) { 
        try {
            vo.setNum(pk++);
            datas.add(vo);
            return true;
 
        }catch(Exception e) {
            e.printStackTrace();
            System.out.println("로그 : insert에서 확인되지 않은 예외");
            return false;
        }
    }
    public boolean update(StudentVO vo) {
        //vo에 들어온 num값이 datas에 존재하는지 확인하는 작업 
        for(int i =0; i<datas.size();i++) {
        }
        for(StudentVO data:datas) {
            if (data.getNum()==vo.getNum()) {
                data.setScore(vo.getScore()); 
                return true;
            }
        }
        return false;
    }
    public boolean delete(StudentVO vo) {
        //c에서 유효한 vo인지 확인하는 작업으르 해준다면? 
//        datas.remove(pk);
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getNum()==vo.getNum()) {
                datas.remove(i);
                return true;
            }
        }
        return false;
    }
    public StudentVO selectOne(StudentVO vo) {
        //num을 받아서 num와 동일한값을 가진 vo를 반환 
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).getNum()==vo.getNum()) {
                return datas.get(i);
            }
        }
        return null;
    }
 
        public ArrayList<StudentVO> selectAll(StudentVO vo) {
            if(vo.getName()!=null) {
                //검색 [서비스]
                ArrayList<StudentVO> datas12=new ArrayList<StudentVO>();
                for(int i=0; i<this.datas.size();i++) {
                    if (this.datas.get(i).getName().equals(vo.getName())) {
                        datas12.add(this.datas.get(i));
                    }
                }
                return datas12;
            }
            return datas;
        }
    }
cs

VIEW

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package view;
 
import java.util.ArrayList;
import java.util.Scanner;
 
import model.StudentVO;
 
public class StudentView {
    public int action;
    Scanner sc=new Scanner(System.in);
    public void startView() {
        while(true) {
            System.out.println();
            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.print("메뉴입력) ");
            try {
                action=sc.nextInt();
                if(1<=action&&action<=6) {
                    break;
                }
                System.out.println("범위외입력!");
            } catch(Exception e) {
                sc.nextLine();
                System.out.println("정수만 입력하세요!");
            }
        }
    }
 
    public String inputString() { // 이름입력
        System.out.print("문자열입력) ");
        String str=sc.next();
        return str;
    }
    public int inputInt() { // 성적입력 
        System.out.print("정수입력) ");
        int i=sc.nextInt();
        return i;
    }
    public void printDatas(ArrayList<StudentVO> datas) { //학생목록
        System.out.println();
        if (datas.size()==0) {
            System.out.println("저장된 정보가 없습니다");
            return;
        }
        System.out.println("=== 학생목록 ===");
        for(StudentVO vo:datas) {
            System.out.println(vo.getNum()+"번 "+vo.getName()+"학생 "+vo.getScore()+"점");
        }
        System.out.println("===============");
    }
 
    public void func1() {
        System.out.println("학생추가 메뉴입니다.");
    }
    public void func2() {
        System.out.println("학생목록 메뉴입니다.");
    }
    public void func3() {
        System.out.println("성적변경 메뉴입니다.");
    }
    public void func10() {
        System.out.println("학생삭제 메뉴입니다.");
    }
    public void func4() {
        System.out.println("이름을 입력합니다..");
    }
    public void func5() {
        System.out.println("성적을 입력합니다.");
    }
    public void func6() {
        System.out.println("번호를 입력합니다..");
    }
    public void func7() {
        System.out.println("프로그램을 종료합니다.....");
    }
    public void func8() {
        System.out.println("수행성공!");
    }
    public void func9() {
        System.out.println("수행실패...");
    }
    public String func11() {
        String ans;
        while(true) {
        System.out.println("성적을 추가입력하세겠습니가? (Y/N)");
        ans=sc.next();
        ans=ans.toUpperCase();
        if (ans.equals("YES"|| ans.equals("NO")) {
            break;
        }
        System.out.println("YES or NO 만 입력가능합니다");
        }
        return ans;
    }   
    public void func12() {
        System.out.println("학생검색 메뉴입니다.");
    }
 
}
cs

CONTROLLER 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package controller;
 
import java.util.ArrayList;
 
import model.StudentDAO;
import model.StudentVO;
import view.StudentView;
 
public class StudentController {
    private StudentDAO model;
    private StudentView view;
    public StudentController() {
        model=new StudentDAO();
        view=new StudentView();
 
    }
    public void startApp() { 
        while(true) {
            view.startView();
            if (view.action==1) { //학생추가 
                view.func1(); // 학생추가문구 출력 
                view.func4(); // 이름입력문구 출력 
                String name=view.inputString(); //이름입력
                String ans=view.func11(); 
                int score=0;
                if (ans.equals("YES")) {
                    view.func5();
                    score=view.inputInt();
                }
                StudentVO vo = new StudentVO();
                vo.setName(name); //v에서 입력받은 값을
                vo.setScore(score);//vo객체에 setter로 설정해서 인자로 넘겨줌 
                boolean flag=model.insert(vo);
                if (flag) {
                    view.func8();
                }else {
                    view.func9();
                }
            }
            else if (view.action==2) { //★★★★★ 학생목록 
                view.func2();
                StudentVO vo=new StudentVO();
                ArrayList<StudentVO> datas=model.selectAll(vo);
                view.printDatas(datas);
            }else if (view.action==3) { // 성적변경
                view.func3();
                view.func6();
                int num=view.inputInt();
                view.func5();
                int score=view.inputInt();
 
                StudentVO vo = new StudentVO();
                vo.setNum(num);
                vo.setScore(score);
                boolean flag=model.update(vo);
                if (flag) {
                    view.func8();
                }else {
                    view.func9();
                }
 
            }else if (view.action==4) { //학생삭제
                view.func10();
                view.func6();
                int num=view.inputInt();
 
                //사용자가 입력한 num에 대한 데이터가 존재합니까? 
                //selectOne()
                StudentVO vo = new StudentVO();
                vo.setNum(num);    
                vo=model.selectOne(vo);
                if (vo==null) {
                    System.out.println("로그 : 해당"+num+"번의 학생은 존재하지않습니다!");
                    view.func9();
                    continue;
                }
 
                boolean flag=model.delete(vo);
                if (flag) {
                    view.func8();
                }else {
                    view.func9();
                }
            }else if (view.action==5) { // 프로그램 종료 
                view.func7();
                break;
            }
            else if (view.action==6) {
                view.func12();
                view.func4();
                String name=view.inputString();
                // 로직이 필요한 순간 
                // DAO에서 제작해야함 
                // R = selectAll selectOne 
                // output 2개 이상 vs 1개(or 0개)
                // -> pk로 검색하는 경우가 아니라면 모두 All()
                StudentVO vo = new StudentVO();
                vo.setName(name);
                //v에서 사용자가 입력한 이름정보를 
                //m DAO의 핵심로직(비즈니스메서드) 인자로 전달하는 코드 
                ArrayList<StudentVO> datas =model.selectAll(vo);
                view.printDatas(datas);
            }
        }
    }
 
}
 
cs

CLIENT 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package client;
 
import controller.StudentController;
 
 
public class Client {
    public static void main(String[] args) {
        StudentController app =new StudentController();
        
        app.startApp();
    }
 
}
 
cs

 

 

 

반응형