티스토리 뷰
ArrayList와 Iterator를 이용한 성적관리 프로그램
Z7_sungjuk.java
import java.util.Comparator;
public class Z7_sungjuk implements Comparable<Z7_sungjuk>, Comparator<Z7_sungjuk>{
String name;
int kor;
int eng;
int math;
int total;
double avg;
int highScore;
int lowScore;
public Z7_sungjuk() {}
public Z7_sungjuk(String name, int kor, int eng, int math) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public String lastToString() {
return name+"\t"+kor+"\t"+math+"\t"+eng+"\t"+total+"\t"+avg+"\t"+highScore+"\t"+lowScore;
}
@Override
public int compare(Z7_sungjuk stu1, Z7_sungjuk stu2) {
// TODO Auto-generated method stub
return stu1.total - stu2.total;
}
@Override
public int compareTo(Z7_sungjuk stu) {
// TODO Auto-generated method stub
return name.compareTo(stu.name);
}
public void calc() {
this.total = kor+eng+math;
this.avg = (double)total/3;
highScore = lowScore = kor;
if(highScore < eng)
highScore = eng;
if(highScore < math)
highScore = math;
if(lowScore > eng)
lowScore = eng;
if(lowScore > math)
lowScore = math;
}
}
Z7_sungjukMenu.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
//입력, 삭제, 편집, 조회, 종료
public class Z7_sungjukMenu {
static ArrayList<Z7_sungjuk> student = new ArrayList<Z7_sungjuk>();
static Scanner sc = new Scanner(System.in);
String name;
int kor;
int math;
int eng;
int total;
double avg;
public void write() {
Z7_sungjuk st = new Z7_sungjuk();
System.out.println("이름을 입력하세요.");
name = sc.next();
System.out.println("국어점수를 입력하세요.");
kor = sc.nextInt();
System.out.println("수학점수를 입력하세요.");
math = sc.nextInt();
System.out.println("영어점수를 입력하세요.");
eng = sc.nextInt();
sc.nextLine();
st.name = name;
st.kor = kor;
st.math = math;
st.eng = eng;
student.add(st);
}
public void print() {
Iterator<Z7_sungjuk> itr = student.iterator();
System.out.println("이름\t국어\t수학\t영어\t총점\t평균\t최고점수\t최소점수");
while (itr.hasNext()) {
Z7_sungjuk str = (Z7_sungjuk) itr.next();
str.calc();
System.out.println(str.lastToString());
}
}
public void edit() {
System.out.println("이름을 입력해주세요.");
String tmpName = sc.next();
Iterator<Z7_sungjuk> itr = student.iterator();
while (itr.hasNext()) {
Z7_sungjuk str = (Z7_sungjuk) itr.next();
if (str.name.equals(tmpName)) {
System.out.println("편집하고 싶은 것을 선택하세요.");
System.out.println("1.국어 2.영어 3.수학");
int num1 = sc.nextInt();
System.out.print("점수를 입력해주세요.");
int sco = sc.nextInt();
if (num1 == 1)
str.kor = sco;
else if (num1 == 2)
str.eng = sco;
else if (num1 == 3)
str.math = sco;
else
System.out.println("잘못 입력하셨습니다.");
}
}
}
public void remove() {
System.out.println("이름을 입력해주세요.");
String tmpName = sc.next();
Iterator<Z7_sungjuk> itr = student.iterator();
while (itr.hasNext()) {
Z7_sungjuk str = (Z7_sungjuk) itr.next();
if (str.name.equals(tmpName)) {
itr.remove();
}
}
}
}
Z7_sungjukMain.java
import java.util.Scanner;
//입력, 삭제, 편집, 조회, 정렬, 종료
public class Z7_sungjukMain {
public static void main(String[] args) {
Z7_sungjukMenu menu = new Z7_sungjukMenu();
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("숫자를 입력하세요: 1.입력 2.조회 3.편집 4.삭제 0.종료");
int num = sc.nextInt();
if (num == 1)
menu.write();
else if (num == 2)
menu.print();
else if (num == 3)
menu.edit();
else if (num == 4)
menu.remove();
else if (num == 0) {
System.out.println("=================프로그램종료=====================");
break;
}
else {
System.out.println("잘못 입력하셨습니다.");
}
}
}
}
'JAVA' 카테고리의 다른 글
예외처리 (0) | 2019.09.25 |
---|---|
에러(Error)와 예외(Exception) (0) | 2019.09.24 |
인터페이스의 타입변환 및 상속 (0) | 2019.09.23 |
익명구현객체(Anonymous class) (0) | 2019.09.23 |
Mail Client (0) | 2019.09.23 |