티스토리 뷰

LANGUAGE/JAVA

[JAVA] LINKEDLIST를 이용한 성적 처리

진심스테이크 2018. 3. 27. 15:31

 

package Grade;
 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
 
class Student {
    private String name;
    private int kor;
    private int eng;
    private int math;
    private int total;
    private float avg;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getKor() {
        return kor;
    }
 
    public void setKor(int kor) {
        this.kor = kor;
    }
 
    public int getEng() {
        return eng;
    }
 
    public void setEng(int eng) {
        this.eng = eng;
    }
 
    public int getMath() {
        return math;
    }
 
    public void setMath(int math) {
        this.math = math;
    }
 
    public int getTotal() {
        total = kor + math + eng;
        return total;
    }
 
    public void setTotal(int total) {
        this.total = total;
    }
 
    public float getAvg() {
        avg = total / 3.f;
        return avg;
    }
 
    public void setAvg(float avg) {
        this.avg = avg;
    }
 
    @Override
    public String toString() {
        return "이름 : " + this.getName() + " 국어 : " + this.getKor() + " 영어 : " + this.getEng() + " 수학 : "
                + this.getMath() + " 총점 : " + this.getTotal() + " 평균 : " + this.getAvg() + "\n";
    }
}
 
public class Grade {
    LinkedList<Student> list = new LinkedList<Student>();
    Iterator<Student> it = list.iterator();
    Student stu;
 
    public void input() {
        Scanner sc = new Scanner(System.in);
        stu = new Student();
        System.out.print("이름 : ");
        stu.setName(sc.next());
        System.out.print("국어 : ");
        stu.setKor(sc.nextInt());
        System.out.print("영어 : ");
        stu.setEng(sc.nextInt());
        System.out.print("수학 : ");
        stu.setMath(sc.nextInt());
        list.add(stu);
    }
 
    public void output() {
        for (Student stu : list) {
            System.out.println(stu); //toString()함수 호출
        }
    }
 
    public void search() {
        Scanner sc = new Scanner(System.in);
        System.out.print("이름 검색 : ");
        String n = sc.next();
        it = list.iterator(); //위에 선언한 iterator을 불러옴
        while (it.hasNext()) { //찾을 떄까지 넘김
            Student stu = it.next();
            if (stu.getName().equals(n)) { //입력한 n이 같으면
                System.out.println(stu); //n이 들어있는 list 호출
            }
        }
    }
 
    public void fix() {
        Scanner sc = new Scanner(System.in);
        System.out.print("수정 할 이름 입력 : ");
        String n = sc.next();
        it = list.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            if (stu.getName().equals(n)) { 
                System.out.print("수정 - 1.국어  2.영어  3.수학 : ");
                int f = sc.nextInt();
                switch (f) {
                case 1: // 국어 
                    System.out.print("국어 : ");
                    stu.setKor(sc.nextInt());
                    break;
                case 2: // 영어
                    System.out.print("영어 : ");
                    stu.setEng(sc.nextInt());
                    break;
                case 3: // 수학 
                    System.out.print("수학 : "); 
                    stu.setMath(sc.nextInt());
                    break;
                }
            }
        }
    }
 
    public void delete() {
        Scanner sc = new Scanner(System.in);
        if (stu == null) {
            System.out.println("삭제할 내역이 없습니다");
        } else {
            System.out.print("삭제 할 이름 검색 : ");
            String n = sc.next();
            it = list.iterator();
            while (it.hasNext()) {
                Student stu = it.next();
                if (stu.getName().equals(n)) {
                    list.remove(stu);
                }
            }
        }
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Grade g = new Grade();
        while (true) {
            System.out.println("1.입력  2.출력  3.검색  4.수정  5.삭제  6.끝내기");
            System.out.print("선택 : ");
            int choose;
            choose = sc.nextInt();
            while (choose != 6) {
                switch (choose) {
                case 1: // 입력
                    g.input();
                    break;
 
                case 2: // 출력
                    g.output();
                    break;
 
                case 3: // 검색
                    g.search();
                    break;
 
                case 4: // 수정
                    g.fix();
                    break;
 
                case 5: // 삭제
                    g.delete();
                    break;
 
                case 6: // 끝내기
                    break;
                }
                break;
            }
        }
    }
}

'LANGUAGE > JAVA' 카테고리의 다른 글

[JAVA] WRAPPER CLASS  (0) 2018.04.15
[JAVA] GUI를 이용한 성적 처리  (0) 2018.04.14
[JAVA] SYNCHRONIZED - 동기화  (0) 2018.03.27
[JAVA] GUI  (0) 2018.03.26
[JAVA] INNER CLASS - 내부 클래스  (0) 2018.03.22
댓글