etc..

스터디#4 _ 학생부 프로그램(MVC 패턴)

Joyfullyever 2025. 1. 9. 20:31

#1 작성한 코드


package student;

import java.util.Random;
import java.util.Scanner;

public class SchoolTeamProjectFinal {
	public static final int LEN = 5; // 학생부에 저장될 수 있는 최대 학생 수
	public static int[] stuList = new int[LEN]; // 학생부

	// Model
	public static int[] selectAll() {
		return stuList;
	}

	public static int selectOne(int num) {
		return stuList[num - 1];
	}

	public static int printFirst(int[] stuList, int cnt) {
		// 학생부 배열에서 점수가 가장 큰 학생을 찾아서
		// --->> 최대값 찾기 알고리즘

		int max = stuList[0];
		int maxIndex = 0;
		for (int i = 1; i < cnt; i++) { // 중요
			if (max < stuList[i]) {
				max = stuList[i];
				maxIndex = i;
			}
		}
		return maxIndex;
	}

	public static void changeInfo(int num, int[] stuList) {
		Random r = new Random();

		// 어떤 점수로 변경할지 결정
		// 랜덤으로 변경
		int score; // scope 이슈 해결
		while (true) {
			score = r.nextInt(101); // 0~100
			if (score != stuList[num - 1]) { // 점수가 이전과 다르면
				break;
			}
		}
		stuList[num - 1] = score;
	}

	public static int getStuLength() {
		return LEN;
	}

	// VIEW
	public static int selectMenu() { // 메뉴를 프린트하고 그중에 선택을 입력받는 함수
		Scanner sc = new Scanner(System.in);
		final int MENU_SIZE = 4;
		int selection;
		System.out.println("=====학생부 프로그램=====");
		System.out.println("1. 전체 출력");
		System.out.println("2. 1등 출력");
		System.out.println("3. 정보 추가");
		System.out.println("4. 정보 변경");
		System.out.println("0. 프로그램 종료");
		System.out.println("=========================");
		System.out.print(">> ");
		while (true) {
			selection = sc.nextInt();
			if (0 <= selection && selection <= MENU_SIZE) {
				break;
			}
			System.out.println("잘못된 입력입니다! 다시 입력해주세요!");
		}
		return selection;
	}

	public static boolean isNoData(int cnt) { // 배열이 비었다면 오류 출력
		boolean isOut = false;
		if (cnt <= 0) {
			System.out.println("데이터가 없습니다!");
			isOut = true;
		}
		return isOut;
	}

	public static boolean isDataFull(int cnt, int size) { // 배열이 꽉찼다면 오류 출력
		boolean isOut = false;
		if (cnt >= LEN) {
			System.out.println("학생부에 저장공간이 부족합니다!");
			System.out.println("관리자에게 문의바랍니다.");
			isOut = true;
		}
		return isOut;
	}

	public static int inputStuScore() { // 학생의 점수를 입력받아서 리턴
		Scanner sc = new Scanner(System.in);
		// 사용자로부터 학생의 점수를 입력
		int score; // scope 이슈 해결
		while (true) {
			System.out.print("추가할 학생의 점수 입력 >> ");
			score = sc.nextInt();
			if (0 <= score && score <= 100) { // 종료조건
				break;
			}
			System.out.println("0~100점 사이만 입력가능합니다!");
		}
		return score;
	}

	public static int inputStuNum(int cnt) { // 학생 번호를 입력받아 리턴
		Scanner sc = new Scanner(System.in);
		// 정보변경할 학생의 번호를 입력받음
		int num; // scope 이슈 해결
		while (true) { // 사용자로부터 "입력"을 받으면? 유효성 검사!
			System.out.print("정보변경할 학생의 번호 입력 >> ");
			num = sc.nextInt();
			if (1 <= num && num <= cnt) { // 종료조건
				break;
			}
			System.out.println("해당 번호의 학생은 존재하지않습니다!");
		}
		return num;
	}

	public static void printAll(int[] stuList, int cnt) {
		// 현재 저장된 학생들의 점수정보를 출력
		for (int i = 0; i < cnt; i++) { // stuList.length => 학생부 자체의 크기
			System.out.println((i + 1) + "번 학생의 점수 : " + stuList[i] + "점");
		}
	}

	public static void printlnMsg(String msg) { // 메시지와 개행문자 출력
		System.out.println(msg);
	}

	public static void main(String[] args) {

		Random rand = new Random();

		int cnt = 0; // 현재 학생부에 저장된 학생 수

		while (true) {
			int action = selectMenu();

			if (action == 0) { // 종료조건
				printlnMsg("프로그램을 종료합니다...");
				break;
			} else if (action == 1) { // 전체 출력
				if (isNoData(cnt)) { // UI/UX
					continue;
				}
				int[] stuList = selectAll();
				printAll(stuList, cnt);
			} else if (action == 2) { // 1등 출력
				if (isNoData(cnt)) { // UI/UX
					continue;
				}

				int[] stuList = selectAll();
				int maxIndex = printFirst(stuList, cnt);
				int max = selectOne(maxIndex + 1);
				// 출력
				printlnMsg("1등은 " + (maxIndex + 1) + "번 학생, " + max + "점 입니다.");
			} else if (action == 3) { // 정보 추가
				int size = getStuLength();
				if (isDataFull(cnt, size)) {
					continue;
				}

				int score = inputStuScore();
				// 입력받은 점수 정보를 배열에 저장
				stuList[cnt] = score;
				cnt++;

				// 저장완료! 안내
				printlnMsg("학생 정보 추가 완료!");
			} else if (action == 4) { // 정보 변경
				if (isNoData(cnt)) { // 학생부에 학생이 한명도없다면
					continue;
				}

				int num = inputStuNum(cnt);
				int[] stuList = selectAll();
				changeInfo(num, stuList);

				// 정보 변경 완료 안내
				printlnMsg(num + "번 학생 정보 변경 완료!");
			}
		}
	}
}

#2 담당한 파트

main 함수 action 2,4 파트

 

#2-1 Model

public static int[] selectAll() {

return stuList;

}

 

public static int selectOne(int num) {

return stuList[num - 1];

}

1. selectAll 과 selectOne 함수를 선언

2. selectAll

: 배열 전체를 return 값으로 입력

3. selectOne

: 사용자로부터 입력받은 값을 받기 위해 int num을 매개변수로 두고, 배열 속 값들 사이에서 입력값-1의 값을 return값으로 입력

 

#2-2 함수화

public static int printFirst(int[] stuList, int cnt) { // 최댓값을 찾는 알고리즘 함수화

 

int max = stuList[0];

int maxIndex = 0;

for (int i = 1; i < cnt; i++) {

if (max < stuList[i]) {

max = stuList[i];

maxIndex = i;

}

}

return maxIndex;

}

 

public static void changeInfo(int num, int[] stuList) { // 저장된 값에 새로운 랜덤값을 넣어주는 코드 함수화

Random r = new Random();

 

// 어떤 랜덤한 점수로 변경할지 결정

int score; // scope 이슈 해결

while (true) {

score = r.nextInt(101);

if (score != stuList[num - 1]) { // 점수가 이전과 다르면

break;

}

}

stuList[num - 1] = score;

}

1. 최댓값의 인덱스 자리를 출력하는 코드 함수화

2. 이미 저장된 값의 자리에 새로운 랜덤한 값을 저장하기 위한 코드 함수화

↪︎ 점수가 이전과 다르다면 while문을 종료, 같다면 while문 재실행

 

#2-3 Main 함수에서 실행

 

Action 2.

if (isNoData(cnt)) { // UI/UX

continue;

}

 

int[] stuList = selectAll(); // stuList의 배열을 불러옴

int maxIndex = printFirst(stuList, cnt); // printFirst함수를 실행하면서, 함수에 배열 stuList와, 학생부에 저장된 학생수를 인자값으로 입력

int max = selectOne(maxIndex + 1); // 최댓값은 위에서 찾은 index값에 1을 더해줘 사용자 입력값을 넣어주고, 해당 인덱스에 있는 최댓값을 저장

// 출력

printlnMsg("1등은 " + (maxIndex + 1) + "번 학생, " + max + "점 입니다.");

 

Action 4. 

if (isNoData(cnt)) { // 학생부에 학생이 한명도없다면

continue;

}

 

int num = inputStuNum(cnt); // 학생번호를 찾는 함수

int[] stuList = selectAll(); // stuList의 배열을 불러옴

changeInfo(num, stuList); // 배열에 저장된 성적을 바꾸는 함수를 실행하기 위해, 학생번호 값과 배열 stuList를 인자값으로 입력

 

// 정보 변경 완료 안내

printlnMsg(num + "번 학생 정보 변경 완료!");