Student ( 필드, 총점, 평균, 제너레이터, Get, Set )
package score0602;
public class Student {
// 필드
private String name;
private int kor;
private int math;
private int eng;
private int tot;
private int avg;
//총점 메소드
public int total() {
return kor + math + eng;
}
//평균
public double avg() {
return total() / 3.0;
}
@Override // 제너레이터 to string
public String toString() {
return "Student [name=" + name + ", kor=" + kor + ", math=" + math + ", eng=" + eng + ", tot=" + tot + ", avg="+ avg + "]";
}
//생성자
public Student() {}
public Student(String name, int kor, int math, int eng) {
this.name = name;
this.kor = kor;
this.math = math;
this.eng = eng;
}
//Get,Set
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 getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getTot() {
return tot;
}
public void setTot(int tot, int kor, int math, int eng) {
this.tot = ( kor + math + eng);
}
public int getAvg() {
return getAvg();
}
public void setAvg(int avg) {
this.avg = avg;
}
}
싱글톤 ( 최고 점수,이름, 향상된 for문 )
package score0602;
public class Singleton {
//최고 점수/이름 필드 생성
public String korKingName;
public String mathKingName;
public String engKingName;
public int korKingScore;
public int mathKingsScore;
public int engKingScore;
//이름으로 성적 검색
public Student search(String name) {
for (Student student : students) {
if(name.equals(student.getName())) {
return student;
}
}
return null;
}
//최고 점수/이름 메서드
public void makeKing() {
for (Student student : students) {
if (korKingScore < student.getKor()) {
korKingScore = student.getKor();
korKingName = student.getName();
}
}
for (Student student : students) {
if (mathKingsScore < student.getMath()) {
mathKingsScore = student.getMath();
mathKingName = student.getName();
}
}
for (Student student : students) {
if (engKingScore < student.getEng()) {
engKingScore = student.getEng();
engKingName = student.getName();
}
}
}
// 클래스 배열
Student[] students = new Student[3];
//생성자
private static Singleton st = new Singleton();
private Singleton() {
// 학생 클래스 배열의 객체를 생성
for(int i=0; i< students.length; i++) {
students[i] = new Student();
}
}
//메소드
public static Singleton getInstance() {
return st;
}
}
실행
package score0602;
import java.util.Scanner;
public class ScoreEx {
public static void main(String[] args) {
boolean run = true;
Scanner sc = new Scanner(System.in);
Singleton st = Singleton.getInstance();
// for (int i=0; i < st.students.length; i++) {
// //임시 자료 더미코드
// st.students[i].setName("김"+i);
// int score =(int) (Math.random()*100 + 1);
// st.students[i].setKor(score);
// score =(int) (Math.random()*100 + 1);
// st.students[i].setMath(score);
// score =(int) (Math.random()*100 + 1);
// st.students[i].setEng(score);
//
// }
for(int i=0; i <st.students.length; i++) {
System.out.print(i+1+"번 학생 이름>");
String name = sc.nextLine();
st.students[i].setName(name);
// 국어 점수 입력
System.out.print(i+1+"번 학생 국어 점수>");
int kor = Integer.parseInt(sc.nextLine());
st.students[i].setKor(kor);
// 수학 점수 입력
System.out.print(i+1+"번 학생 수학 점수>");
int math = Integer.parseInt(sc.nextLine());
st.students[i].setMath(math);
// 영어 점수 입력
System.out.print(i+1+"번 학생 영어 점수>");
int eng = Integer.parseInt(sc.nextLine());
st.students[i].setEng(eng);
}
for(Student student : st.students) {
System.out.println(student);
}
while(run) { // 무한루프
System.out.println("-----------------------------------------------");
System.out.println("1.학생별 총점/평균 2.과목별 최고 점수/이름 3.이름으로 성적 검색 4.종료");
System.out.println("-----------------------------------------------");
int num = Integer.parseInt(sc.nextLine());
switch(num) {
case 1: // 1. 학생별 총점/평균
System.out.println("이름\t국어\t수학\t영어\t총점\t평균");
System.out.println("---------------------------------------------");
// 배열에 저장된 학생 객체를 하나씩 가져와서 반복문의 코드 블록을 실행하는 역할
for(Student student : st.students) {
System.out.printf("%s\t%d\t%d\t%d\t%d\t%f\n",
student.getName(), student.getKor(),
student.getMath(), student.getEng(),
student.total(), student.avg());
}
System.out.println("---------------------------------------------");
break;
case 2: // 2.과목별 최고 점수/이름
st.makeKing();// 싱글톤에서 작성한 for문 메서드 호출
System.out.printf("국어 최고 점수: %d, 이름: %s\n",
st.korKingScore, st.korKingName);
System.out.printf("수학 최고 점수: %d, 이름: %s\n",
st.mathKingsScore, st.mathKingName);
System.out.printf("영어 최고 점수: %d, 이름: %s\n",
st.engKingScore, st.engKingName);
break;
case 3://3.이름으로 성적 검색
System.out.print("이름입력>");
String name = sc.nextLine();
Student student = st.search(name);
if(student != null) {
System.out.println("이름\t국어\t수학\t영어\t총점\t평균");
System.out.println("---------------------------------------------");
System.out.printf("%s\t%d\t%d\t%d\t%d\t%f\n",
student.getName(), student.getKor(),
student.getMath(), student.getEng(),
student.total(), student.avg());
}System.out.println("정보가 없습니다.");
break;
case 4:
run = false; // 루프 종료
}
}
System.out.println("프로그램 종료");
}
}
결과 ( 점수 입력 후 추가 메뉴 선택 )
결과 ( 1. 학생별 총점/ 평균 출력 )
결과 ( 2. 과목별 최고 점수/이름 )
결과 ( 3. 이름으로 성적 검색 )
'프로젝트 기반 자바(JAVA) 응용 SW개발자 취업과정 > 추가 복습' 카테고리의 다른 글
DB_JDBC (0) | 2023.07.02 |
---|---|
필드, 생성자, 메소드 정리 (2) | 2023.06.06 |
싱글톤 2 ( JSP 환경 ) 이름, 국영수 점수 입력=>총점, 평균, 최고점수 (0) | 2023.06.01 |
싱글톤 활용 ( JSP 환경 ) (0) | 2023.05.31 |
생성자, 메서드1 (0) | 2023.05.29 |