반응형
package dayday19;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JOptionPane;
/*
* Student Class
* 필드 : private 이름, 점수, 등급
* 메소드 :
* 생성자
* setGrade() => 점수를 통해 등급을 계산 A~F
* toString() => Override
* Getter,Setter
* compareTo() => 점수 기준으로 내림차순 정렬(높은게 먼저)
* -> 점수가 같으면 이름순으로 정렬
*
* Quiz01 Class
* ArrayList생성
* 메뉴 :
* 1. 등록 -> 이름, 점수
* 2. 저장 -> Array리스트의 정보들이 "student.stu"에 저장
* 3. 종료
* Quiz02 Class
* student.stu의 모든 객체정보를 JOPtionPain으로 출력
*
*
*/
class Student implements Comparable<Student>, Serializable{
private String name;
private int point;
private char grade;
Student(String name, int point){
setName(name);
setPoint(point);
}
public void setGrade(){
if(point <= 60){
this.grade = 'F';
}else if(point <= 70){
this.grade = 'C';
}else if(point <= 80){
this.grade = 'B';
}else if(point <= 90){
this.grade = 'A';
}else this.grade = 'S';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point > 0 ? point : 0;
setGrade();
}
@Override
public String toString() {
return "이름 = " + name + "/ 점수 = " + point + "/ 등급 = " + grade + "입니다.";
}
@Override
public int compareTo(Student s) {
// TODO Auto-generated method stub
//나이순 정렬!
if(this.point > s.point){
return -1; //앞으로
}else if(this.point < s.point){
return 1; //뒤로
} else {//나이가 같으면 이름순
return this.name.compareTo(s.name);
}
}
}
public class Quiz01 {
public static void main(String[] args) {
int count = 0;
ArrayList<Student> a1 = new ArrayList<Student>();
/*
* 메뉴 :
* 1. 등록 -> 이름, 점수
* 2. 저장 -> Array리스트의 정보들이 "student.stu"에 저장
* 3. 종료
*/
String menu = "1. 등록\n2. 저장\n3. 종료";
while(true){
String sel = JOptionPane.showInputDialog(menu);
if(sel.equals("1")){
String userName = JOptionPane.showInputDialog("이름을 입력하세요.");
String suserPoint = JOptionPane.showInputDialog("점수를 입력하세요.");
int userPoint = Integer.parseInt(suserPoint);
Student stu = new Student(userName,userPoint);
a1.add(stu);
count++;
Collections.sort(a1);
}else if(sel.equals("2")){
try {
FileOutputStream fos = new FileOutputStream("student.stu");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i = 0 ; i < a1.size() ; i++){
oos.writeObject(a1.get(i));
}
JOptionPane.showMessageDialog(null, count+"개 객체를 저장하였습니다.");
oos.flush();
fos.flush();
oos.close();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if(sel.equals("3")){
JOptionPane.showMessageDialog(null, "종료합니다.");
return;
}else
JOptionPane.showMessageDialog(null, "잘못 입력하셨습니다. 다시 입력하세요");
}
}
}
package dayday19;
import java.io.*;
import javax.swing.JOptionPane;
/*
* Quiz02 Class
* student.stu의 모든 객체정보를 JOPtionPain으로 출력
*
*
*/
public class Quiz02 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("student.stu");
ObjectInputStream ois = new ObjectInputStream(fis);
String mess = "";
while(true){
try{
Student stu = (Student)ois.readObject();
mess += stu.toString() +"\n";
} catch(EOFException e){
break;
}
}
JOptionPane.showMessageDialog(null, mess);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
반응형
'IT > Programming' 카테고리의 다른 글
[JAVA, HTML] Servlet(web.xml수정) (0) | 2023.04.20 |
---|---|
[JAVA, HTML] - Servlet(Anotation으로 호출) (1) | 2023.04.19 |
[JAVA] Server&Client 통신 - Client (0) | 2023.04.19 |
[JAVA] Server&Client 통신 - Server (0) | 2023.04.19 |
[JAVA] Quiz Server&Client - 구구단 문제 - Client (0) | 2023.04.19 |