JAVA / / 2019. 6. 13. 19:57

[Java] ArrayList를 이용한 성적처리V3

반응형

ArrayList를 이용한 다수의 성적처리 방법입니다.

자동으로 배열을 잡아주기에 코딩이 간단해졌습니다.

조회,생성,삭제,수정,전체조회,종료 메뉴로 구성 했습니다.

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
 
public class Student {
 
    private String name;        //필드 변수
    private int[] score;
    private float avg;
    private float sum;
    private int a;
 
    public Student() {            //생성자 초기화
        score = new int[3];
        name = null;
        avg = 0;
        sum = 0;
        a=0;
        
        
    }
 
    public void Sum() {            //sum함수
        sum=0;
        for(int i=0; i<score.length; i++){
        sum += score[i];        
        }
    }
    
    public void Avg() {            //avg함수
        avg = sum/3;
    }
    
    //setter&getter
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getScore(int i) {
        return score[i];
    }
 
    public void setScore(int score,int i) {
        this.score[i] = score;
    }
    
    public float getAvg() {
        return avg;
    }
    
    public float getSum() {
        return sum;
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 
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
122
123
124
125
126
127
import java.util.*;
 
public class StudentMan {
    private Student st;
    private List<Student> list; // 설명: List 저장되는 값이 Student, Student 클래스를 list라 칭한다
                                //
 
    public StudentMan() {
        list = new ArrayList<Student>(); // 설명: ArrayList에 student클래스를 넣은것을 list에 넣는다
    } // <> 어떤 데이터 타입(객체)을 저장할지
 
    public void Insert(String name, int score, int score1, int score2) {     //생성 기능
        st = new Student();                    //배열 생설
        st.setName(name);                    //이름 입력
        st.setScore(score, 0);                //점수 입력
        st.setScore(score1, 1);
        st.setScore(score2, 2);
        list.add(st);                        //list에 입력
    }
 
    public void Select(String name) {        //조회 기능
        boolean logical = false;            //잘못된값 조회를 대비한 논리값
        for (int i = 0; i < list.size(); i++) {        //size만큼 반복
            if (list.get(i).getName().equals(name)) { // list.get(i) == student 객체
                st = list.get(i);                //st에 list값 입력
                logical = true;                    // 논리타입 트루 if문으로 진행
                break;
            }
        }
        if (logical) {
            System.out.println("이름 : " + st.getName() + "\t");
            System.out.println("국어 : " + st.getScore(0+ "\t");
            System.out.println("영어 : " + st.getScore(1+ "\t");
            System.out.println("수학 : " + st.getScore(2+ "\t");
            System.out.println("총점 : " + st.getSum() + "\t");
            System.out.println("평균 : " + st.getAvg() + "\t");
        } else {                                //조회값이 없을시
            System.out.println("이름이 없습니다");
        }
    }
 
    public void SelectAll() {                //전체조회
        for (Student stN : list) {            //list 만큼의 반복
            System.out.println("이름 : " + stN.getName() + "\t");
            System.out.println("국어 : " + stN.getScore(0+ "\t");
            System.out.println("영어 : " + stN.getScore(1+ "\t");
            System.out.println("수학 : " + stN.getScore(2+ "\t");
            System.out.println("총점 : " + stN.getSum() + "\t");
            System.out.println("평균 : " + stN.getAvg() + "\t");
        }
        return;    //반복문 밖이라 return으로 메서드 탈출
    }
 
    public void Delete(String name) {                //삭제 메소드
        for (int i = 0; i < list.size(); i++) {        
            if (list.get(i).getName().equals(name)) { // list.get(i) == student 객체
                list.remove(i);            //삭제 키워드 remove
                break;
            }
        }
    }
 
    public void Update(String name1,String name, int score, int score1, int score2) {        //수정 메소드
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name1)) { // list.get(i) == student 객체
                list.get(i).setName(name);
                list.get(i).setScore(score, 0);
                list.get(i).setScore(score1, 1);
                list.get(i).setScore(score2, 2);
                list.get(i).Sum();
                list.get(i).Avg();
                st.Sum();
                st.Avg();
                break;
            }
        }
 
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);            // sc 생성
        StudentMan stu = new StudentMan();                // stu 생성
 
        while (true) {                //1을 넣어서 무한루프
            System.out.println("1.조회  2.생성  3.삭제  4.수정  5.전체 조회  6.종료");
            System.out.println("원하는 기능을 선택하시오  : ");
            int num = sc.nextInt();
            switch (num){                    //메뉴 선탤을 위한 스위치문
            case 1:
                System.out.println("조회를 원하는 이름을 입력하세요 : ");
                stu.Select(sc.next());
                break;
 
            case 2:
                System.out.println("이름,국어성적,영어성적,수학성적을 차례로 입력하세요 : ");
                break;
 
            case 3:
                System.out.println("삭제를 원하는 이름을 입력하세요 : ");
                stu.Delete(sc.next());
                break;
 
            case 4:
                System.out.println("수정를 원하는 이름을 입력하세요 : ");
                String name1 = sc.next();
                stu.Select(name1);
                System.out.println("수정할 이름,국어성적,영어성적,수학성적을 차례로 입력하세요 : ");
                break;
 
            case 5:
                stu.SelectAll();        
                break;
 
            case 6:
                System.exit(0);            //무한반복문이라 강제종료
 
            }
        }
 
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유