public class Manager {
//ArrayList를 만들자
ArrayList<Student> list = new ArrayList<Student>();
Scanner sc = new Scanner(System.in);
int choice;
Manager(){
}
void disp(){
System.out.println("-------성적 관리 프로그램--------");
System.out.println("1.입력 2.저장 3.불러오기 4.정렬 5.끝!");
}
void choice()throws Exception{
while(true){
disp();
try{choice = sc.nextInt();
sc.nextLine();
}catch(Exception e){
System.out.println("숫자만 입력하시오!");
return;
}
switch(choice){
case 1:
Input();
break;
case 2:
save();
break;
case 3:
load();
break;
case 4:
sort();
break;
case 5:
sort();
break;
case 6:
return;
}
}
}
void Input()throws Exception{
System.out.print("이름 : ");
String name = sc.nextLine();
System.out.print("국어 : ");
int kor = Integer.parseInt(sc.nextLine());
System.out.print("영어 : ");
int eng = Integer.parseInt(sc.nextLine());
System.out.print("수학 : ");
int math = Integer.parseInt(sc.nextLine());
list.add(new Student(name, kor, eng, math));
}
void save()throws Exception{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("kyd.ser"));
oos.writeObject(list);//파일에 출력스트림을 연결한 뒤 오브젝트 출력스트림을 연결
oos.close();
//list = null;
System.out.println("파일에 저장되었습니다.");
}
void load()throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("kyd.ser"));
list = (ArrayList<Student>)ois.readObject();//파일에 입력스트림을 연결한 뒤 오브젝트 입력스트림으로 연결
ois.close();
for(int i=0; i<list.size(); i++){
System.out.println(((Student)list.get(i)));
}
System.out.println("파일을 불러왔습니다.");
}
void sort()throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("kyd.ser"));
list = (ArrayList<Student>)ois.readObject();
ois.close();
Comparator<Student> cp = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int total1 = o1.getEng() + o1.getKor() + o1.getMath();
int total2 = o2.getEng() + o2.getKor() + o2.getMath();
if(total1 < total2){
return 1;
}else if(total1 > total2){
return -1;
}
return 0;
}
};
Collections.sort(list, cp);
for(int i=0; i<list.size(); i++){
System.out.println(i+1 + "등 " + ((Student)list.get(i)));
}
}
}
댓글 없음:
댓글 쓰기