자바프로그래밍

[JAVA] ArrayList 를 이용한 성적처리 프로그램

제주도소년 2020. 4. 21. 09:44
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayMain extends AbClass {
    
    ArrayList<Subject> stu;
    
    public ArrayMain() {
        stu = new ArrayList<Subject>();
    }
 
    @Override
    public void input() {
        Scanner sc = new Scanner(System.in);
        Subject st = new Subject();
 
        System.out.print("이름 : ");
        st.setName(sc.next());
        System.out.print("국어 : ");
        st.setKor(sc.next());
        System.out.print("영어 : ");
        st.setEng(sc.next());
        System.out.print("수학 : ");
        st.setMat(sc.next());
 
        st.cal();
 
        stu.add(st);
    }
 
    @Override
    public void modi() {
        String temp, temp2;
        int num;
        Scanner sc = new Scanner(System.in);
 
        System.out.print("수정할 학생의 이름은... :");
        temp = sc.next();
 
        for (Subject n : stu) {
            if (temp.equals(n.getName())) {
                System.out.println("1.국어\t 2.영어\t 3.수학");
                num = sc.nextInt();
                switch (num) {
                case 1:
                    System.out.println("현재 국어 점수 : " + n.getKor());
                    System.out.print("수정할 국어 점수 : ");
                    temp2 = sc.next();
                    n.setKor(temp2);
                    System.out.println("수정 완료!");
                    n.cal();
                    break;
                case 2:
                    System.out.println("현재 영어 점수 : " + n.getEng());
                    System.out.print("수정할 영어어 점수 : ");
                    temp2 = sc.next();
                    n.setEng(temp2);
                    System.out.println("수정 완료!");
                    n.cal();
                    break;
                case 3:
                    System.out.println("현재 수학 점수 : " + n.getMat());
                    System.out.print("수정할 수학 점수 : ");
                    temp2 = sc.next();
                    n.setMat(temp2);
                    System.out.println("수정 완료!");
                    n.cal();
                    break;
                }
            }
        }
    }
 
    @Override
    public void disp() {
        String temp;
        int num;
        Scanner sc = new Scanner(System.in);
 
        System.out.print("1.이름검색\t2.총점검색");
        num = sc.nextInt();
        // temp2 = Integer.parseInt(temp3);
        switch (num) {
        case 1:
            System.out.print("이름 :");
            temp = sc.next();
 
            System.out.println("이름\t국어\t영어\t수학\t총점\t평균");
            System.out.println("*******************************************");
 
            for (Subject n : stu) {
                if (temp.equals(n.getName())) {
                    System.out.print(n.getName() + "\t");
                    System.out.print(n.getKor() + "\t");
                    System.out.print(n.getEng() + "\t");
                    System.out.print(n.getMat() + "\t");
                    System.out.print(n.getSum() + "\t");
                    System.out.print(n.getAvg() + "\t");
                    System.out.println("");
                }
            }
            break;
        case 2:
            System.out.print("총점 :");
            num = sc.nextInt();
 
            System.out.println("이름\t국어\t영어\t수학\t총점\t평균");
            System.out.println("*******************************************");
 
            for (Subject n : stu) {
                if (n.subSum() >= num) {
                    System.out.print(n.getName() + "\t");
                    System.out.print(n.getKor() + "\t");
                    System.out.print(n.getEng() + "\t");
                    System.out.print(n.getMat() + "\t");
                    System.out.print(n.getSum() + "\t");
                    System.out.print(n.getAvg() + "\t");
                    System.out.println("");
                }
            }
            break;
        }
    }
 
    @Override
    public void delete() {
        Scanner sc = new Scanner(System.in);
        String sName;
        System.out.println("삭제하고 싶은 이름을 입력하세요 : ");
        sName = sc.next();
        for (int i = 0; i < stu.size(); i++) {
            if (sName.equals(stu.get(i).getName())) {
                stu.remove(i);
                System.out.println("complete");
            }
        }
    }
 
    public void save() {
        try {
            Object aa = stu;
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("arrayData.bin"));
            oos.writeObject(aa);
            oos.close();
        } catch (IOException e) {
            System.out.println("File save Error");
            e.printStackTrace();
        }
    }
 
    public void open() {
        Object obj = null;
 
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("arrayData.bin"));
 
            try {
                obj = ois.readObject();
 
                stu = (ArrayList<Subject>) obj;
                ois.close();
 
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        ArrayMain am = new ArrayMain();
 
        int num;
        boolean a = true;
        Scanner sc = new Scanner(System.in);
 
        do {
            System.out.println("1)학생정보입력\t2)학생정보수정\t3)학생정보검색\t4)학생정보삭제\t5)저장\t6)불러오기\t7)종료");
            num = sc.nextInt();
            switch (num) {
            case 1:
                am.input();
                break;
            case 2:
                am.modi();
                break;
            case 3:
                am.disp();
                break;
            case 4:
                am.delete();
                break;
            case 5:
                am.save();
                break;
            case 6:
                am.open();
                break;
            case 7:
                a = false;
                System.out.println("End");
                break;
            default:
                System.out.println("잘못 입력함 !!");
                break;
            }
        } while (a);
    }
}
public class Subject {
    private static final long serialVersionUID = -7523738235826498048L;
    private String name; // 이름
    private String kor; // 국
    private String eng; // 영
    private String mat; // 수
    private String sum; // 총
    private String avg; // 평
 
    public Subject() {
        name = "";
        kor = "";
        eng = "";
        mat = "";
        sum = "";
        avg = "";
    }
 
    public int subSum() {
        String str = getSum();
        int sum = Integer.parseInt(str);
 
        return sum;
    }
 
    public void cal() {
        String temp;
        int sum = Integer.parseInt(kor) + Integer.parseInt(eng) + Integer.parseInt(mat);
        int avg = sum / 3;
        temp = Integer.toString(sum);
        setSum(temp);
        temp = Integer.toString(avg);
        setAvg(temp);
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getKor() {
        return kor;
    }
 
    public void setKor(String kor) {
        this.kor = kor;
    }
 
    public String getEng() {
        return eng;
    }
 
    public void setEng(String eng) {
        this.eng = eng;
    }
 
    public String getMat() {
        return mat;
    }
 
    public void setMat(String mat) {
        this.mat = mat;
    }
 
    public String getSum() {
        return sum;
    }
 
    public void setSum(String sum) {
        this.sum = sum;
    }
 
    public String getAvg() {
        return avg;
    }
 
    public void setAvg(String avg) {
        this.avg = avg;
    }
 
}
public abstract class AbClass
{    
    public abstract void input();
    public abstract void modi();
    public abstract void delete();
    public abstract void disp();
}

'자바프로그래밍' 카테고리의 다른 글

컬렉션  (0) 2020.04.23
Wrapper class  (0) 2020.04.23
[ JDK 1.7 ] Interface (인터페이스)  (0) 2020.04.12
추상 클래스  (0) 2020.04.10
상속 ( Inheritance )  (0) 2020.04.07