몰입하며 나아가는 개발이란

Language/Java

[2D Array]practice ([2차원 배열]연습), file print

류하을 2019. 12. 2. 09:41
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class mainClass {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		String student[][] = new String[10][5];

		for (int i = 0; i < student.length; i++) {
			for (int j = 0; j < student[i].length; j++) {
				student[i][j] = "";
			}
		}
		student[0][0] = 0 + "";
		student[0][1] = "lee";
		student[0][2] = "24";
		student[0][3] = "85";
		student[0][4] = "75";

		student[1][0] = 1 + "";
		student[1][1] = "laa";
		student[1][2] = "25";
		student[1][3] = "80";
		student[1][4] = "70";

		student[2][0] = 2 + "";
		student[2][1] = "lss";
		student[2][2] = "27";
		student[2][3] = "65";
		student[2][4] = "75";

		int memberNum = 3;
		while (true) {
			System.out.println("===== 비트 학생 성적 조회 프로그램 =====");
			System.out.println(
					"1.학생정보추가\n2.학생정보 삭제\n3.학생정보 검색\n4.학생정보 수정\n5.학생 성적조회\n6.학생 성적총점\n7.학생성적 평균\n8.학생성적순 출력\n9.메모장 출력\n0.종료");
			System.out.print("메뉴를 고르세요: ");
			int choice = sc.nextInt();
			if (choice == 1) {
				// 학생정보 추가
				studentAdd(student, sc, memberNum);
			} else if (choice == 2) {
				// 학생정보 삭제
				if (datacheck(student))
					studentDel(student, sc);
			} else if (choice == 3) {
				// 학생정보 검색
				if (datacheck(student))
					studentSreach(student, sc);
			} else if (choice == 4) {
				// 학생정보 수정
				if (datacheck(student))
					studentUpdate(student, sc);
			} else if (choice == 5) {
				// 학생 성적조회
				if (datacheck(student))
					studentScoreOne(student, sc);
			} else if (choice == 6) {
				// 학생 성적총점
				if (datacheck(student))
					studentScoreTotal(student);
			} else if (choice == 7) {
				// 학생 성적평균
				if (datacheck(student))
					studentScoreTotalAvg(student);
			} else if (choice == 8) {
				// 학생 성적순 정렬 출력
				if (datacheck(student))
					scorePrint(student);
			} else if (choice == 9) {
				// 파일로 저장하기
				if (datacheck(student)) {
					saveFile(student);
				}
			} else if (choice == 0) {
				// 종료
				System.out.println("프로그램을 종료합니다.");
				break;
			}
		}
		sc.close();
	}

	private static boolean datacheck(String[][] student) {
		for (int i = 0; i < student.length; i++) {
			if (!student[i][1].equals("")) {
				return true;
			}
		}
		System.out.println("학생정보가 없습니다.");
		return false;
	}

	private static void saveFile(String[][] student) throws IOException {
		File file = new File("d:/tmp/studentFile.txt");
		FileWriter fWriter = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fWriter);
		PrintWriter pw = new PrintWriter(bw);

		for (int i = 0; i < student.length; i++) {
			for (int j = 0; j < student[i].length; j++) {
				pw.print(student[i][j] + " ");
			}
			pw.print("\n");
		}
		pw.close();

	}

	private static void scorePrint(String[][] student) {
		int[][] sortStudent = new int[10][2];
		int sum = 0;
		for (int i = 0; i < student.length; i++) {
			if (!student[i][1].equals("")) {
				sum = Integer.parseInt(student[i][3]) + Integer.parseInt(student[i][4]);
				sortStudent[i][0] = i;
				sortStudent[i][1] = sum;
			}
		}
		int temp, temp2; // sorting
		for (int i = 0; i < sortStudent.length; i++) {
			for (int j = 0; j < sortStudent.length; j++) {
				if (sortStudent[i][1] > sortStudent[j][1]) {
					temp = sortStudent[i][0];
					temp2 = sortStudent[i][1];
					sortStudent[i][0] = sortStudent[j][0];
					sortStudent[i][1] = sortStudent[j][1];
					sortStudent[j][0] = temp;
					sortStudent[j][1] = temp2;
				}
			}
		}
		for (int i = 0; i < sortStudent.length; i++) { // 출력
			int temp3 = sortStudent[i][0];
			if (!student[i][1].equals("")) {
				System.out.println((i + 1) + "등 이름: " + student[temp3][1] + " 영어: " + student[temp3][2] + " 수학: "
						+ student[temp3][3] + " 총점: "
						+ (Integer.parseInt(student[temp3][3]) + Integer.parseInt(student[temp3][4])) + " 평균: "
						+ ((int) (Integer.parseInt(student[temp3][3]) + Integer.parseInt(student[temp3][4])) / 2.0));
			}
		}
	}

	private static void studentScoreTotalAvg(String[][] student) {
		double count = 0.0;
		int avg = 0;
		int sum = 0;
		for (int i = 0; i < student.length; i++) {
			if (!student[i][1].equals("")) {
				sum += (Integer.parseInt(student[i][3]) + Integer.parseInt(student[i][4]));
				count++;
			}
		}
		avg = (int) (sum / count);
		System.out.println("학생들의 총점 평균은 : " + avg + " 점 입니다.");
	}

	private static void studentScoreTotal(String[][] student) {
		int sum = 0;
		for (int i = 0; i < student.length; i++) {
			if (!student[i][1].equals("")) {
				sum += (Integer.parseInt(student[i][3]) + Integer.parseInt(student[i][4]));
			}
		}
		System.out.println("학생들의 총점은 : " + sum + " 점 입니다.");
	}

	private static void studentScoreOne(String[][] student, Scanner sc) {
		int studentOne = selectOne(student, sc);
		if (studentOne != -1) {
			System.out.println("학생이름: " + student[studentOne][1] + " 영어점수: " + student[studentOne][3] + " 수학점수: "
					+ student[studentOne][4] + " 총점: "
					+ (Integer.parseInt(student[studentOne][3]) + Integer.parseInt(student[studentOne][4])) + " 평균: "
					+ ((int) (Integer.parseInt(student[studentOne][3]) + Integer.parseInt(student[studentOne][4]))
							/ 2.0));
		} else {
			System.out.println("검색한정보가 없습니다.");
		}
	}

	private static void studentUpdate(String[][] student, Scanner sc) {
		int studentOne = selectOne(student, sc);
		if (studentOne != -1) {
			for (int i = 0; i < student.length; i++) {
				if (student[i][1].equals(student[studentOne][1])) {
					System.out.println("이름: ");
					student[i][1] = sc.next();
					System.out.println("나이: ");
					student[i][2] = sc.next();
					System.out.println("영어점수: ");
					student[i][3] = sc.next();
					System.out.println("수학점수: ");
					student[i][4] = sc.next();
				}
			}
			System.out.println("정보가 수정되었습니다.");
		} else {
			System.out.println("검색한정보가 없습니다.");
		}
	}

	private static void studentSreach(String[][] student, Scanner sc) {
		int studentOne = selectOne(student, sc);
		if (studentOne != -1) {
			System.out.println("학생이름: " + student[studentOne][1] + " 나이: " + student[studentOne][2]);
		} else {
			System.out.println("검색한정보가 없습니다.");
		}
	}

	private static void studentDel(String[][] student, Scanner sc) {
		int studentOne = selectOne(student, sc);
		if (studentOne != -1) {
			for (int i = 0; i < student.length; i++) {
				if (student[i][1].equals(student[studentOne][1])) {
					student[i][0] = "";
					student[i][1] = "";
					student[i][2] = "";
					student[i][3] = "";
					student[i][4] = "";
				}
			}
			System.out.println("정보가 삭제되었습니다.");
		} else {
			System.out.println("검색한정보가 없습니다.");
		}
	}

	private static int selectOne(String[][] student, Scanner sc) {
		System.out.print("검색할 학생의 이름을 입력하세요: ");
		String name = sc.next();
		for (int i = 0; i < student.length; i++) {
			if ((student[i][1].equals(name))) {
				return i;
			}
		}
		return -1;
	}

	private static void studentAdd(String[][] student, Scanner sc, int memberNum) {
		System.out.println("이름: ");
		String name = sc.next();
		System.out.println("나이: ");
		String age = sc.next();
		System.out.println("영어점수: ");
		String eng = sc.next();
		System.out.println("수학점수: ");
		String math = sc.next();
		out: for (int i = 0; i < student.length; i++) {
			for (int j = 0; j < student[i].length; j++) {
				if (student[i][1] == "") {
					student[i][0] = memberNum + "";
					student[i][1] = name;
					student[i][2] = age;
					student[i][3] = eng;
					student[i][4] = math;
					memberNum++;
					break out;
				}
			}
		}
	}
}