본문 바로가기

Learning/JAVA

이름, 학과, 학번, 학점 정보를 가진 Student클래스, ArrayList<Student> 컬렉션을 만들고, 사용자로부터 입력받아 저장하고, 학생 이름으로 검색하는 StudentManager 클래스 작성

  • 이름, 학과, 학번, 학점 정보를 가진 Student클래스를 작성하라. ArrayList<Student> 컬렉션을 만들고, 사용자로부터 입력받아 저장하고, 학생 이름으로 검색하는 StudentManager 클래스를 작성하라.

    • com.exam01 패키지를 생성하고 Student, StudentManager 클래스 만들기. 

    • Student클래스에 멤버변수 선언

    • StudentManager에는 메뉴 입력 메소드, 학생정보 입력 메소드, 전체 학생 보기 메소드, 학생 찾는 메소드, 파일 저장 메소드 5가지를 생성.

    • 메뉴선택 메소드는 static을 사용

    • 학생입력 메소드는 한 라인으로 학생정보를 입력받아 ,로 구분하여 처리한다. (StringTokenizer 사용)

 

  • Student 클래스 (Student)
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 com.exam01;
import java.io.Serializable;
public class Student implements Serializable{
    String name;
    String dept;
    int studentID;
    double avg;
    
    public Student(String name, String dept, int studentID, double avg) {
        this.name=name;
        this.dept=dept;
        this.studentID=studentID;
        this.avg=avg;
    }
    
    public String getName() {
        return name;
    }
    public String getDept() {
        return dept;
    }
    public int getStudentID() {
        return studentID;
    }
    public double getAvg() {
        return avg;
    }
}
 

 


 

  • StudentManager 클래스 (StudentManager)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.exam01;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.StringTokenizer;
//메뉴선택, 학생정보 입력, 입력한 학생 전체 목록 보기, 학생 이름으로 찾기, 파일로 저장하기 메소드
public class StudentManager {
    static Scanner sc=new Scanner(System.in);
    static ArrayList<Student>arr=new ArrayList<Student>();
    File dir,file;
    
    public StudentManager() throws IOException, ClassNotFoundException {
        dir=new File("src\\com\\exam01");
        file=new File(dir, "Student.txt");
        if(file.exists()) {
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
            arr=(ArrayList<Student>)ois.readObject();
        }else {
            file.createNewFile();
        }
    }
    
    public static void showMenu() {
        System.out.println("선택하세요");
        System.out.println("1.데이터입력");
        System.out.println("2.전체보기");
        System.out.println("3.학생찾기");
        System.out.println("4.종료");
        System.out.println("선택>>");
    }
    
    public void inputData() {
        while(true) {
            System.out.println("학생 이름,학과,학번,학점평균 입력하세요.(입력은 , 로 구분하고 종료는 x)");
            System.out.println(">>");
        try {
            String text=StudentManager.sc.nextLine();
            if(text.toLowerCase().equals("x")) {
                System.out.println("입력 종료");
                break;
            }
            StringTokenizer stk=new StringTokenizer(text,",");
            String name=stk.nextToken();
            String dept=stk.nextToken();
            int studentID=Integer.parseInt(stk.nextToken());
            double avg=Double.parseDouble(stk.nextToken());
            arr.add(new Student(name, dept, studentID, avg));
        }catch (NoSuchElementException n) {
            System.out.println("정확하게 입력하세요.");
            return;
            }
        }
    }
    
    public void viewData() {
        System.out.println("전체보기");
        for(Student student:arr) {
            System.out.println("이름: "+student.getName());
            System.out.println("학과: "+student.getDept());
            System.out.println("학번: "+student.getStudentID());
            System.out.println("학점평균: "+student.getAvg());
            System.out.println();
        }
    }
    
    public void searchData() {
        System.out.println("학생 찾기....");
        System.out.println("찾을 학생 이름>>");
        String searchName=StudentManager.sc.next();
        Student s=search(searchName);
        if(s==null) {
            System.out.println("찾는 학생 없음");
            return;
        }
        System.out.println("이름: "+s.getName());
        System.out.println("학과: "+s.getDept());
        System.out.println("학번: "+s.getStudentID());
        System.out.println("학점평균: "+s.getAvg());
    }
    
    private Student search(String searchName) {
        for(int i=0;i<arr.size();i++) {
            if(searchName.equals(arr.get(i).getName())) { //arraylist는 get으로 접근, 크기는 size
                return arr.get(i);
            }
        }
        return null;
    }
    
    public void saveData() throws FileNotFoundException, ClassNotFoundException, EOFException, IOException  {
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(arr);
    }
    
    public static void main(String[] args) throws InputMismatchException, FileNotFoundException, ClassNotFoundException, IOException{
        StudentManager sm=new StudentManager();
        while(true) {
            StudentManager.showMenu();
            int num=sc.nextInt();
            sc.nextLine();
            switch(num) {
            case 1 : sm.inputData(); break;
            case 2 : sm.viewData(); break;
            case 3 : sm.searchData(); break;
            case 4 : sm.saveData();
                     System.out.println("프로그램 종료");
                     System.exit(0);
            default : System.out.println("입력오류");
            }
        }
    }
}