본문 바로가기

STUDY/Java

성적 관리 프로그램 예제1 - 2021.09.03

 

1. DTO (Score.java)

package com.score;

public class Score {
	String group; // 반
	String name;  // 이름
	int kor, eng, math;  // 국어, 영어 수학
 	int tot;  // 총점
	double avg;  // 평균
	String res;  // 학점
	public Score() {
		super();
	}
	public Score(String group, String name, int kor, int eng, int math) {
		super();
		this.group = group;
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.math = math;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	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 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 getTot() {
		return tot;
	}
	public void setTot(int tot) {
		this.tot = tot;
	}
	public double getAvg() {
		return avg;
	}
	public void setAvg(double avg) {
		this.avg = avg;
	}
	public String getRes() {
		return res;
	}
	public void setRes(String res) {
		this.res = res;
	}
}
 

 

2. Main (DataTest.java)

package com.score;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class DataTest {
		static ArrayList<Score> alist = new ArrayList<Score>();
	public static void main(String[] args) throws FileNotFoundException {
		Scanner sc = new Scanner(System.in);
		DataInput di = new DataInput();
		DataOutput dout = new DataOutput();
		DataSort ds = new DataSort();
		DataMap dmap = new DataMap();
		boolean stop = true;
		
		while(stop) {
			System.out.println("1.자료입력 2.자료출력 3.반오름차순 4.반오름차순총점내림차순 5.과목별평균 0.종료");
			System.out.print("원하는 번호를 입력하세요: ");
			int input = sc.nextInt();
			switch(input){
				case 1: di.File_Input(alist); break;
				case 2: dout.Arraylist_Output(alist); break;
				case 3: ds.group_asc(alist); break;
				case 4: ds.tot_des(alist); break;
				case 5: dmap.Subject_avg(alist); break;
				case 0: stop = false; break;
			}//swtich-end
		}//while-end
	}
}
 

 

03. DataInput.java

package com.score;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class DataInput {
	public void File_Input(ArrayList<Score> alist) throws FileNotFoundException {
		FileReader fr = new FileReader("c:/java_test/score.txt");
		BufferedReader br = new BufferedReader(fr);
		Score ss = new Score();
		DataProcess dp = new DataProcess();
		while(true) {
			try {
				String str = br.readLine();
				if(str==null) break;
				
				String group = str.substring(0, 1);
				String name = str.substring(1, 4);
				int kor = Integer.parseInt(str.substring(4, 7));
				int eng = Integer.parseInt(str.substring(7, 10));
				int math = Integer.parseInt(str.substring(10, 13));

				Score score = new Score(group, name, kor, eng, math);
				alist.add(score);
				score.setTot(dp.Total(score.getKor(), score.getEng(), score.getMath()));
				score.setAvg(dp.Average(score.getTot()));
				score.setRes(dp.Result(score.getAvg()));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}//while-end
	}//File_input()-end
}
 
 

c:/java_test/score.txt

 

 

04 DataOutput.java

package com.score;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class DataOutput {
	DecimalFormat df = new DecimalFormat("#,##0.0");
	public void Arraylist_Output(ArrayList<Score> alist){
		System.out.println("반\t이름\t국어\t영어\t수학\t총점\t평균\t학점");
		alist.forEach(k->System.out.println(k.getGroup() + "\t" + k.getName() + "\t" + k.getKor() + "\t" + k.getEng() + "\t" + k.getMath() + "\t" + k.getTot() + "\t" + df.format(k.getAvg()) + "\t" + k.getRes()));
	}
	
	public void List_Output(List<Score> list) {
		System.out.println("반\t이름\t국어\t영어\t수학\t총점\t평균\t학점");
		list.forEach(k->System.out.println(k.getGroup() + "\t" + k.getName() + "\t" + k.getKor() + "\t" + k.getEng() + "\t" + k.getMath() + "\t" + k.getTot() + "\t" + df.format(k.getAvg()) + "\t" + k.getRes()));
	}
}
 

 

 

05 DataProcess.java

package com.score;

public class DataProcess {
	Score ss = new Score();
	public int Total(int k, int e, int m) { // 총점
		return k + e + m;
	}
	
	public double Average(int t) { // 평균
		return (double) t / 3;
	}
	
	public String Result(double avg) { // 학점
		String res="";
		switch((int)avg / 10) {
				case 10: 
				case 9: res = "A"; break;
				case 8: res = "B"; break;
				case 7: res = "C"; break;
				case 6: res = "D"; break;
				default: res = "F";	
		}
		return res;
	}
}
 

 

06 DataSort.java

package com.score;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class DataSort {
	DataOutput dout = new DataOutput();
	
	public void group_asc(ArrayList<Score> alist) { // 반별 오름차순 정렬
		List<Score> g_asc = alist.stream().sorted(Comparator.comparing(Score::getGroup)).collect(Collectors.toList());
		dout.List_Output(g_asc);
	}
	
	public void tot_des(ArrayList<Score> alist) {  // 반별 오름차순 정렬 후 총점별 내림차순 정렬
		Comparator<Score> t_des = Comparator.comparing(Score::getTot).reversed();
		alist.sort(Comparator.comparing(Score::getGroup).thenComparing(t_des));
		dout.List_Output(alist);
	}
}
 

 

07 DataMap.java

package com.score;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class DataMap {
	DataSort ds = new DataSort();
	public void Subject_avg(ArrayList<Score> alist) {
		DecimalFormat df = new DecimalFormat("#,##0.0");
		// 반별 오름차순 정렬
		ds.group_asc(alist);
		// 반별 국어, 영어, 수학 평균
		Map<String, Integer> kor_map = new HashMap<String, Integer>();
		Map<String, Integer> eng_map = new HashMap<String, Integer>();
		Map<String, Integer> math_map = new HashMap<String, Integer>();
		
		for(int i=0; i<alist.size(); i++) {
			kor_map.put(alist.get(i).getGroup(), alist.get(i).getKor());
			eng_map.put(alist.get(i).getGroup(), alist.get(i).getEng());
			math_map.put(alist.get(i).getGroup(), alist.get(i).getMath());
		}
		Set<String> kkset = kor_map.keySet();
		
		System.out.println("반\t\t국어평균\t영어평균\t수학평균");
		
		Iterator<String> k_iter = kkset.iterator();
		while(k_iter.hasNext()) {
			String k_key = k_iter.next();
			double k_avg = alist.stream().filter(k->k.getGroup().equals(k_key)).mapToDouble(Score::getKor).average().getAsDouble();
			double e_avg = alist.stream().filter(k->k.getGroup().equals(k_key)).mapToDouble(Score::getEng).average().getAsDouble();
			double m_avg = alist.stream().filter(k->k.getGroup().equals(k_key)).mapToDouble(Score::getMath).average().getAsDouble();
			System.out.println(k_key + "\t\t" + k_avg + "\t" + e_avg + "\t" + m_avg);
		}
		// 전체 국어, 영어, 수학 평균
		double kor_avg = alist.stream().mapToDouble(Score::getKor).average().getAsDouble();
		double eng_avg = alist.stream().mapToDouble(Score::getEng).average().getAsDouble();
		double math_avg = alist.stream().mapToDouble(Score::getMath).average().getAsDouble();
		
		System.out.println("반\t\t국어평균\t영어평균\t수학평균");
		System.out.println("A,B\t\t" + df.format(kor_avg) + "\t" + df.format(eng_avg) + "\t" + df.format(math_avg));
	}
}
 

 

<결과>