[JAVA 개인 프로젝트]_시험 평균&석차 구하기

정만·2023년 1월 27일
0

JAVA 개인 프로젝트

목록 보기
3/3
post-thumbnail

학생들의 시험 평균과 석차 구하기

이번 프로젝트에서의 중요한 목적이 있다.
기존과는 다르게 입력, 출력, 메인, 성적, 데이터를 나눠 객체지향적으로 프로젝트를 구현하고자 하였다.

시나리오는 다음과 같다.

  1. 중간고사 2. 기말고사 중에 선택한다.
  2. 학생수를 입력한다.
  3. 학번을 입력한다. (6자리)
  4. 국어 점수, 영어점수, 수학점수, 과학점수를 입력한다.
  5. 각 과목들의 점수와 평균, 석차가 계산되어 출력된다.
  6. 출력이 끝난 후 "종료하시겠습니까? (y/n)" 물음에
    y로 응답 시 프로그램이 처음부터 시작된다.
    n으로 응답 시 "종료되었습니다." 라는 문구가 출력되고
    다른 문자로 응답 시 "다시 입력해주세요." 라는 문구가 출력된다.

이제 구현한 소스코드를 아래에 기록해본다.

InputScore.java

package com.java.input;

import java.util.Scanner;

import com.java.score.FinalScore;
import com.java.score.MiddleScore;

public class InputScore {
	
	public static void inputData(Scanner sc, int select,MiddleScore[] scores) {

		System.out.println("1. 중간고사 2. 기말고사");
		System.out.print("선택하세요.");
		select = Integer.parseInt(sc.nextLine());
		if (select == 1) {
			System.out.print("중간고사 학생수 : ");
		} else if (select == 2) {
			System.out.print("기말고사 학생수 : ");
		}

		int studentNum = Integer.parseInt(sc.nextLine());
		// 몇명
		scores = new MiddleScore[studentNum];

		if (select == 1) {
			for (int i = 0; i < scores.length; i++) {
				scores[i] = new MiddleScore();
			}

		} else if (select == 2) {
			for (int i = 0; i < scores.length; i++) {
				scores[i] = new FinalScore();
			}
		}

		// Data 입력
		for (int i = 0; i < scores.length; i++) {
			System.out.print("학번을 입력하시오.(6자리)");
			scores[i].setHakbun(sc.nextLine());

			System.out.print("국어 점수 : ");
			scores[i].setKor(Integer.parseInt(sc.nextLine()));
			System.out.print("영어 점수 : ");
			scores[i].setEng(Integer.parseInt(sc.nextLine()));
			System.out.print("수학 점수 : ");
			scores[i].setMath(Integer.parseInt(sc.nextLine()));
			System.out.print("과학 점수 : ");
			scores[i].setScien(Integer.parseInt(sc.nextLine()));

			if (select == 2) {
				FinalScore ff = (FinalScore) scores[i];
				System.out.print("한국사 점수 : ");
				ff.setKoreaHistory(Integer.parseInt(sc.nextLine()));
				System.out.print("세계사 점수 : ");
				ff.setWorldHistory(Integer.parseInt(sc.nextLine()));
			}
			System.out.println();
			scores[i].avg();

		}

	}
}

OutputScore.java

package com.java.output;

import com.java.score.FinalScore;
import com.java.score.MiddleScore;

public class OutputScore {

	public static void outputData(int select,MiddleScore[] scores) {
		// 출력
		if (select == 1) {
			System.out.println(MiddleScore.LABEL);
		}
		if (select == 2) {
			System.out.println(FinalScore.LABEL);
		}

		for (int i = 0; i < scores.length; i++) {
			System.out.println(scores[i]);
		}
		System.out.println();
	}
}

Main.java

package com.java.main;

import java.util.Scanner;

import com.java.input.InputScore;
import com.java.output.OutputScore;
import com.java.process.ProcessScore;
import com.java.source.DataSource;

public class Main {

	public static void main(String[] args) {
		Main main = new Main();
		main.start();
	} // main()

	public void start() {
		// 학생수 만큼 MiddleScore 인스턴스를 생성
		DataSource source = new DataSource();

		boolean flag = true;
		boolean flag2 = true;
		while (flag) {

			InputScore.inputData(source.getSc(), source.getSelect(), source.getScores());
			ProcessScore.processData(source.getScores());
			OutputScore.outputData(source.getSelect(), source.getScores());

			Scanner sc = source.getSc();

			do { // 입력받는 와일문.
				System.out.println("종료하시겠습니까? (y/n)");
				char game = sc.nextLine().charAt(0);
				if (game == 'y' || game == 'Y') {
					flag = false;
					flag2 = false;
					System.out.println("종료되었습니다.");
				} else if (game == 'n' || game == 'N') {
					flag2 = false;
				} else
					System.out.println("다시입력해주세요.");
			} while (flag2);
			// flag - 전체 와일문 통제하기
			// flag2 - 입력받는 와일문을 통제하기

		}

	}

}

Main2.java

package com.java.main;

import com.java.score.FinalScore;
import com.java.score.MiddleScore;

public class Main2 {

	public static void main(String[] args) {
		
		MiddleScore m = new MiddleScore();
		m.setHakbun("123456");
		m.setKor(89);
		m.setEng(90);
		m.setMath(76);
		m.setScien(90);
		m.avg();
		
		System.out.println(MiddleScore.LABEL);
		System.out.println(m);

		
		MiddleScore f = new FinalScore();
		f.setHakbun("123456");
		f.setKor(80);
		f.setEng(90);
		f.setMath(80);
		f.setScien(80);
		FinalScore ff = (FinalScore)f;
		ff.setKoreaHistory(80);
		ff.setWorldHistory(80);
		f.avg();
		
		System.out.println(FinalScore.LABEL);
		System.out.println(f);
		
		
	}

}

ProcessScore.java

package com.java.process;

import com.java.score.MiddleScore;

public class ProcessScore {
	

	public static void processData(MiddleScore[] scores) {
		// 처리
		MiddleScore temp;
		for (int i = 0; i < scores.length; i++) {
			for (int j = i + 1; j < scores.length; j++) {
				if (scores[i].getTotal() < scores[j].getTotal()) {
					temp = scores[i];
					scores[i] = scores[j];
					scores[j] = temp;

				}

			}
		}
		for (int i = 0; i < scores.length; i++) {
			scores[i].setRank(i + 1);
		}

	}

}

FinalScore.java

package com.java.score;

public class FinalScore extends MiddleScore {
	public final static String LABEL=
				"소속\t\t\t학번\t국어\t영어\t수학\t과학\t국사\t세계사\t총점\t평균\t석차";
	
	private int koreaHistory=0;
	private int worldHistory=0;	
		
	@Override
	public void total() {
		super.total(); // 4과목 합산
		super.setTotal(super.getTotal()+koreaHistory+worldHistory);
	}
	
	@Override
	public void avg() {
		this.total();
		super.setAvg((super.getTotal()*10/6)/10f);
	}
	
	
	public int getKoreaHistory() {
		return koreaHistory;
	}

	public void setKoreaHistory(int koreaHistory) {
		this.koreaHistory = koreaHistory;
	}

	public int getWorldHistory() {
		return worldHistory;
	}

	public void setWorldHistory(int worldHistory) {
		this.worldHistory = worldHistory;
	}

	@Override
	public String toString() {
		return sosok+"\t" 
				+ "\t"+this.getHakbun() 
				+ "\t"+this.getKor()
				+ "\t"+this.getEng()
				+ "\t"+this.getMath()
				+ "\t"+this.getScien()
				+ "\t"+this.koreaHistory
				+ "\t"+this.worldHistory
				+ "\t"+this.getTotal()
				+ "\t"+this.getAvg()
				+ "\t"+this.getRank();
				
	}
}

MiddleScore.java

package com.java.score;

public class MiddleScore {

	public final static String LABEL = "소속\t\t\t학번\t국어\t영어\t수학\t과학\t총점\t평균\t석차";

	public static String sosok = "SBS아카데미 I강의장";
	private static int sukcha = 0;

	private String hakbun = "";

	private int kor = 0;
	private int eng = 0;
	private int math = 0;
	private int scien = 0;

	private int total = 0;
	private float avg = 0f;
	private int rank = 1;

	public MiddleScore() {
	}

	public MiddleScore(String hakbun, int kor, int eng, int math, int scien) {
		super();
		this.hakbun = hakbun;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.scien = scien;

	}

	public int getRank() {
		return rank;
	}

	public void setRank(int rank) {
		this.rank = rank;
	}

	public void total() {
		this.total = kor + eng + math + scien;
	}

	public void avg() {
		total();
		this.avg = (total * 10 / 4) / 10f;
	}

	public String getHakbun() {
		return hakbun;
	}

	public void setHakbun(String hakbun) {
		this.hakbun = hakbun;
	}

	public int getKor() {
		return kor;
	}

	public void setKor(int kor) {
		this.kor = kor;
	}

	public int getEng() {
		return eng;
	}

	public void setEng(int eng) {
		this.eng = eng;
	}

	public int getMath() {
		return math;
	}

	public void setMath(int math) {
		this.math = math;
	}

	public int getScien() {
		return scien;
	}

	public void setScien(int scien) {
		this.scien = scien;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public float getAvg() {
		return avg;
	}

	public void setAvg(float avg) {
		this.avg = avg;
	}

	@Override
	public String toString() {
		return sosok + "\t" + "\t" + this.hakbun + "\t" + this.kor + "\t" + this.eng + "\t" + this.math + "\t"
				+ this.scien + "\t" + this.total + "\t" + this.avg + "\t" + this.rank;
	}

}

DataSource.java

package com.java.source;

import java.util.Scanner;

import com.java.score.MiddleScore;

public class DataSource {

	private MiddleScore[] scores;
	private int select;
	private Scanner sc = new Scanner(System.in);

	public MiddleScore[] getScores() {
		return scores;
	}

	public void setScores(MiddleScore[] scores) {
		this.scores = scores;
	}

	public int getSelect() {
		return select;
	}

	public void setSelect(int select) {
		this.select = select;
	}

	public Scanner getSc() {
		return sc;
	}

	public void setSc(Scanner sc) {
		this.sc = sc;
	}

}
profile
어른이 되고싶은 정만이의 개발log

0개의 댓글