[Project] JAVA - 학생관리 πŸ› 

김상엽·2024λ…„ 1μ›” 12일
0

Project

λͺ©λ‘ 보기
2/9
post-thumbnail

⛺학생 관리 ν”„λ‘œμ νŠΈ

ν•„μˆ˜ μš”κ΅¬μ‚¬ν•­

ν•„μˆ˜ κ³Όλͺ©κ³Ό 선택 κ³Όλͺ©μ΄ μ‘΄μž¬ν•©λ‹ˆλ‹€.

ν•„μˆ˜ κ³Όλͺ©

  • Java
  • 객체지ν–₯
  • Spring
  • JPA
  • MySQL

선택 κ³Όλͺ©

λ””μžμΈ νŒ¨ν„΄

  • Spring Security
  • Redis
  • MongoDB

μΊ ν”„ μˆ˜κ°•μƒλ“€μ€ μ΅œμ†Œ 3개 μ΄μƒμ˜ ν•„μˆ˜ κ³Όλͺ©κ³Ό 2개 μ΄μƒμ˜ 선택 κ³Όλͺ©μ„ μ„ νƒν•©λ‹ˆλ‹€.

μΊ ν”„ μˆ˜κ°•μƒλ“€μ€ μΊ ν”„ κΈ°κ°„λ™μ•ˆ μ„ νƒν•œ κ³Όλͺ©λ³„λ‘œ 총 10회의 μ‹œν—˜μ„ λ΄…λ‹ˆλ‹€.

μ μˆ˜μ— 따라 등급이 λ§€κ²¨μ§‘λ‹ˆλ‹€.

e.g. Java

1회차2회차3회차
DDB

λ“±κΈ‰ μ‚°μ • κΈ°μ€€

  • ν•„μˆ˜κ³Όλͺ©
ABCDFN
95 ~ 10090 ~ 9480 ~ 8970 ~ 7960 ~ 6960점 미만
  • 선택과λͺ©
ABCDFN
90 ~ 10080 ~ 8970 ~ 7960 ~ 6950 ~ 5950점 미만
  • μΊ ν”„ λ§€λ‹ˆμ €λŠ” μˆ˜κ°•μƒμ„ 등둝 및 관리할 수 μžˆμŠ΅λ‹ˆλ‹€.
    • μˆ˜κ°•μƒ 정보 μ˜ˆμ‹œ
      • 고유 번호
      • 이름
      • κ³Όλͺ© λͺ©λ‘
      • 점수 λͺ©λ‘

  • μΊ ν”„ λ§€λ‹ˆμ €λŠ” μˆ˜κ°•μƒλ“€μ˜ κ³Όλͺ©κ³Ό μ‹œν—˜ 점수λ₯Ό 등둝 및 관리할 수 μžˆμŠ΅λ‹ˆλ‹€.
    • κ³Όλͺ© 정보 μ˜ˆμ‹œ
      • 고유 번호
      • κ³Όλͺ©λͺ…
      • κ³Όλͺ©νƒ€μž…(ν•„μˆ˜, 선택)

    • 점수 정보 μ˜ˆμ‹œ
      • κ³Όλͺ© 고유 번호
      • μˆ˜κ°•μƒ 고유 번호
      • 회차
      • 점수
      • λ“±κΈ‰

  • κΈ°λŠ₯ λͺ…μ„Έμ„œ
    • ν•„μˆ˜ μš”κ΅¬ 사항
      • μˆ˜κ°•μƒ 관리
        1. μˆ˜κ°•μƒ 정보λ₯Ό 등둝할 수 μžˆμŠ΅λ‹ˆλ‹€.
          • 등둝 ν•„μˆ˜ 정보
            • 고유 번호
            • 이름
            • κ³Όλͺ© λͺ©λ‘

          • 주의!
            • μˆ˜κ°•μƒμ˜ 고유 λ²ˆν˜ΈλŠ” 쀑볡될 수 μ—†μŠ΅λ‹ˆλ‹€!
            • μ„ νƒν•œ ν•„μˆ˜ κ³Όλͺ©μ΄ 3개 미만일 수 μ—†μŠ΅λ‹ˆλ‹€!
            • μ„ νƒν•œ 선택 κ³Όλͺ©μ΄ 2개 미만일 수 μ—†μŠ΅λ‹ˆλ‹€!
        2. μˆ˜κ°•μƒ λͺ©λ‘μ„ μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
          • 쑰회 ν˜•μ‹μ€ μžμœ μž…λ‹ˆλ‹€.
            • 쑰회 ν•„μˆ˜ 정보
              • 고유 번호
              • 이름

      • 점수 관리
        1. μˆ˜κ°•μƒμ˜ κ³Όλͺ©λ³„ μ‹œν—˜ 회차 및 점수λ₯Ό 등둝할 수 μžˆμŠ΅λ‹ˆλ‹€.
          • 점수λ₯Ό λ“±λ‘ν•˜λ©΄ μžλ™μœΌλ‘œ 등급이 μΆ”κ°€ μ €μž₯λ©λ‹ˆλ‹€.
          • 주의!
            • λ“±λ‘ν•˜λ €λŠ” κ³Όλͺ©μ˜ 회차 μ μˆ˜κ°€ 이미 λ“±λ‘λ˜μ–΄ μžˆλ‹€λ©΄ 등둝할 수 μ—†μŠ΅λ‹ˆλ‹€. (κ³Όλͺ©μ˜ 회차 μ μˆ˜κ°€ μ€‘λ³΅λ˜μ–΄ 등둝될 수 μ—†μŠ΅λ‹ˆλ‹€!)
            • νšŒμ°¨μ— 10 초과 및 1 미만의 μˆ˜κ°€ μ €μž₯될 수 μ—†μŠ΅λ‹ˆλ‹€. (νšŒμ°¨λŠ” 1 ~ 10)
            • μ μˆ˜μ— 100 초과 및 μŒμˆ˜κ°€ μ €μž₯될 수 μ—†μŠ΅λ‹ˆλ‹€. (μ μˆ˜λŠ” 0 ~ 100)

        2. μˆ˜κ°•μƒμ˜ κ³Όλͺ©λ³„ 회차 점수λ₯Ό μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
        3. μˆ˜κ°•μƒμ˜ νŠΉμ • κ³Όλͺ© νšŒμ°¨λ³„ 등급을 μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
          • 쑰회 ν˜•μ‹μ€ μžμœ μž…λ‹ˆλ‹€.
            • 쑰회 ν•„μˆ˜ 정보
              • 회차
              • λ“±κΈ‰

선택 μš”κ΅¬μ‚¬ν•­

  • μˆ˜κ°•μƒ 관리
    1. μˆ˜κ°•μƒ μƒνƒœλ₯Ό 관리할 수 μžˆμŠ΅λ‹ˆλ‹€.
    - μƒνƒœ μ’…λ₯˜
    - Green
    - Red
    - Yellow


    2. μˆ˜κ°•μƒ 정보λ₯Ό μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    - 쑰회 ν˜•μ‹μ€ μžμœ μž…λ‹ˆλ‹€.
    - 쑰회 ν•„μˆ˜ 정보
    - 고유 번호
    - 이름
    - μƒνƒœ
    - μ„ νƒν•œ κ³Όλͺ©λͺ…


    3. μˆ˜κ°•μƒ 정보λ₯Ό μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    - μˆ˜μ • κ°€λŠ₯ν•œ 정보
    - 이름
    - μƒνƒœ
    - 이름 λ˜λŠ” μƒνƒœλ₯Ό μž…λ ₯λ°›μ•„ μˆ˜μ •ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€.


    4. μƒνƒœλ³„ μˆ˜κ°•μƒ λͺ©λ‘μ„ μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    - 쑰회 ν˜•μ‹μ€ μžμœ μž…λ‹ˆλ‹€.
    - 쑰회 ν•„μˆ˜ 정보
    - 고유 번호
    - 이름


    5. μˆ˜κ°•μƒμ„ μ‚­μ œν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    - ν•΄λ‹Ή μˆ˜κ°•μƒμ˜ 점수 기둝도 ν•¨κ»˜ μ‚­μ œλ©λ‹ˆλ‹€.

  • 점수 관리
    1. μˆ˜κ°•μƒμ˜ κ³Όλͺ©λ³„ 평균 등급을 μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
    - 쑰회 ν˜•μ‹μ€ μžμœ μž…λ‹ˆλ‹€.
    - 쑰회 ν•„μˆ˜ 정보
    - κ³Όλͺ©λͺ…
    - 평균 λ“±κΈ‰


    2. νŠΉμ • μƒνƒœ μˆ˜κ°•μƒλ“€μ˜ ν•„μˆ˜ κ³Όλͺ© 평균 등급을 μ‘°νšŒν•©λ‹ˆλ‹€.
    - 쑰회 ν˜•μ‹μ€ μžμœ μž…λ‹ˆλ‹€.
    - 쑰회 ν•„μˆ˜ 정보
    - μˆ˜κ°•μƒ 이름
    - ν•„μˆ˜ κ³Όλͺ© 평균 λ“±κΈ‰

클래슀 λ‹€μ΄μ–΄κ·Έλž¨

Main : Management 클래슀λ₯Ό μ‹€ν–‰μ‹œν‚€λŠ” λ©”μΈν΄λž˜μŠ€
Management : μ‚¬μš©μžμ˜ μž…λ ₯에 따라 Database클래슀의 ν•¨μˆ˜λ₯Ό ν™œμš©ν•˜μ—¬ 좜λ ₯ν•˜λŠ” μ•± 클래슀
DataBase : 학생관리에 ν•„μš”ν•œ κΈ°λŠ₯을 μ²˜λ¦¬ν•˜λŠ” 클래슀
Student : ν•™μƒμ˜ 이름, 고유번호, μƒνƒœλ₯Ό λ‹΄κ³ μžˆλŠ” 클래슀
Subject : κ³Όλͺ©μ˜ 이름, ν•„μˆ˜μ—¬λΆ€λ₯Ό λ‹΄κ³ μžˆλŠ” 클래슀
SubjectScore : κ³Όλͺ©μ˜ 회차, 점수, 등급을 λ‹΄κ³ μžˆλŠ” 클래슀
ScreenData : ν™”λ©΄ 좜λ ₯μ—μ„œ λ°˜λ³΅λ˜λŠ” 뢀뢄을 λ‹΄κ³ μžˆλŠ” 클래슀 좜λ ₯문을 κ°„μ†Œν™” ν•΄μ€Œ
ScreenStage : ν™”λ©΄ 좜λ ₯λΆ€μ˜ 이름을 μ •μ˜ν•˜λŠ” enumeration 클래슀

1. Main.java

  • managementλ₯Ό μ„ μ–Έν•˜κ³ , μ•± 클래슀λ₯Ό 호좜
import Service.Management;

public class Main {
    public static void main(String[] args) {
        Management management = new Management();
        management.run();

    }
}

2. Management.java

  • dataBase, screenDataλ₯Ό μ„ μ–Έν•˜κ³ , λ©”λ‰΄ν•¨μˆ˜λ“€μ„ 톡해 μ‚¬μš©μžμ˜ μž…λ ₯에 따라 μž‘λ™
  • screenDataλ₯Ό μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬μ€„ λ°˜λ³΅ν•˜μ—¬ μ‚¬μš©λ˜λŠ” printf문을 ν•œμ€„μ˜ μ½”λ“œλ‘œ λ³€κ²½
package Service;
import Data.DataBase;
import Screen.ScreenStage;
import Screen.ScreenData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Management{
    private Scanner sc = new Scanner(System.in);
    private DataBase dataBase = new DataBase();
    private ScreenData screenData = new ScreenData();
    
    public void run() {
        mainMenu();
    }
    
    private void mainMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println(screenData.getStringData(ScreenStage.MAIN_MENU));
            String input = sc.nextLine();
            switch (inputSelect(input)) {
                case 1 -> studentMenu(); // μˆ˜κ°•μƒ 관리
                case 2 -> scoreMenu(); // 점수 관리
                case 3 -> flag = false; // ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
                default -> {
                    System.out.println("잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€.\nλ˜λŒμ•„κ°‘λ‹ˆλ‹€!");
                }
            }
        }
    }
    
    private void studentMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println(screenData.getStringData(ScreenStage.STUDENT_MAIN_MENU));
            String input = sc.nextLine();
            switch (inputSelect(input)) {
                case 1 -> registerStudentMenu(); // μˆ˜κ°•μƒ 등둝
                case 2 -> inquireStudentMenu(); // μˆ˜κ°•μƒ 쑰회
                case 3 -> changeStudentMenu(); // μˆ˜κ°•μƒ μˆ˜μ •
                case 4 -> deleteStudentMenu(); // μˆ˜κ°•μƒ μ‚­μ œ
                case 5 -> flag = false; // ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
                default -> {
                    System.out.println("잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€.\nλ˜λŒμ•„κ°‘λ‹ˆλ‹€!");
                }
            }
        }
    }
    
 private void scoreMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println(screenData.getStringData(ScreenStage.SCORE_MAIN_MENU));
            String input = sc.nextLine();
            switch (inputSelect(input)) {
                case 1 -> registerScoreMenu(); // 점수 등둝
                case 2 -> inquireScoreMenu(); // 점수 쑰회
                case 3 -> changeScoreMenu(); // 점수 μ •
                case 4 -> flag = false;
                default -> {
                    System.out.println("잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€.\nλ˜λŒμ•„κ°‘λ‹ˆλ‹€!");
                }
            }
        }
    }

    private void registerStudentMenu() {
        String studentSubject;
        List<String> studentsubjectList = new ArrayList<>();
        System.out.println(screenData.getStringData(ScreenStage.STUDENT_REGISTER_MENU));
        System.out.println("μˆ˜κ°•μƒμ˜ 이름을 μž…λ ₯ν•΄μ£Όμ„Έμš”.");
        String studentName = sc.nextLine();
        System.out.println("μˆ˜κ°•μƒμ˜ μƒνƒœλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.");
        String studentStatus = sc.nextLine();
        do {
            System.out.println("μˆ˜κ°•μƒμ˜ ν•„μˆ˜κ³Όλͺ© λͺ©λ‘μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”. [Java 객체지ν–₯ Spring JPA MySQL]");
            studentSubject = sc.nextLine();
        }while(studentSubject.split(" ").length<2);
        studentsubjectList.addAll(Arrays.asList(studentSubject.split(" ")));
        do {
            System.out.println("μˆ˜κ°•μƒμ˜ 선택과λͺ© λͺ©λ‘μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”. [λ””μžμΈ_νŒ¨ν„΄ Spring_Security Redis MongoDB]");
            studentSubject = sc.nextLine();
        }while(studentSubject.split(" ").length<1);
        studentsubjectList.addAll(Arrays.asList(studentSubject.split(" ")));
        System.out.printf("이름 : %-5s | κ³Όλͺ© : %-40s | μƒνƒœ : %-5s\n", studentName, String.join(", ",studentsubjectList),studentStatus);
        System.out.println("μˆ˜κ°•μƒμ„ 등둝 ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
        System.out.println("1. λ„€");
        System.out.println("2. μ•„λ‹ˆμ˜€");
        String input = sc.nextLine();
        if(inputSelect(input)==1)
            dataBase.createStudent(studentName, studentStatus, studentsubjectList);
    }
    
    private void inquireStudentMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println(screenData.getStringData(ScreenStage.STUDENT_INQUIRE_MENU));
            System.out.println("1. 고유번호둜 μ‘°νšŒν•˜κΈ°");
            System.out.println("2. μƒνƒœλ‘œ μ‘°νšŒν•˜κΈ°");
            String input = sc.nextLine();
            switch (inputSelect(input)) {
                case 1 -> {
                    System.out.println("μ‘°νšŒν•  μˆ˜κ°•μƒμ˜ 고유번호λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
                    input = sc.nextLine();
                    dataBase.printStudentById(input);
                }
                case 2 -> {
                    System.out.println("μ‘°νšŒν•  μˆ˜κ°•μƒμ˜ μƒνƒœλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
                    input = sc.nextLine();
                    dataBase.printStudentByStatus(input);
                }
                case 3 -> flag = false; // ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
                default -> {
                    System.out.println("잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€.\nλ˜λŒμ•„κ°‘λ‹ˆλ‹€!");
                }
            }
        }
    }
    
    private void changeStudentMenu() {
        System.out.println(screenData.getStringData(ScreenStage.STUDENT_DELETE_MENU));
        System.out.println("μˆ˜μ •ν•  μˆ˜κ°•μƒμ˜ 고유번호λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String id = sc.nextLine();
        dataBase.printStudentById(id);
        System.out.println("μˆ˜κ°•μƒμ˜ μƒˆλ‘œμš΄ 이름을 μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String name = sc.nextLine();
        System.out.println("μˆ˜κ°•μƒμ˜ μƒˆλ‘œμš΄ μƒνƒœλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String status = sc.nextLine();
        System.out.println("μˆ˜κ°•μƒ 정보λ₯Ό λ³€κ²½ ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
        System.out.println("1. λ„€");
        System.out.println("2. μ•„λ‹ˆμ˜€");
        String input = sc.nextLine();
        if(inputSelect(input)==1)
            dataBase.changeStudent(id,name,status);
    }

    private void deleteStudentMenu() {
        System.out.println(screenData.getStringData(ScreenStage.STUDENT_DELETE_MENU));
        System.out.println("μ‚­μ œν•  μˆ˜κ°•μƒμ˜ 고유번호λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String id = sc.nextLine();
        dataBase.printStudentById(id);
        System.out.println("μˆ˜κ°•μƒμ„ μ‚­μ œ ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
        System.out.println("1. λ„€");
        System.out.println("2. μ•„λ‹ˆμ˜€");
        String input = sc.nextLine();
        if(inputSelect(input)==1)
            dataBase.deleteStudent(id);
    }

    private void registerScoreMenu() {
        System.out.println(screenData.getStringData(ScreenStage.SCORE_REGISTER_MENU));
        System.out.println("점수λ₯Ό μž…λ ₯ν•  ν•™μƒμ˜ 고유번호λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String id = sc.nextLine();
        System.out.println("점수λ₯Ό μž…λ ₯ν•  κ³Όλͺ©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String subject = sc.nextLine();
        String key = id + subject;
        System.out.printf("%d 회차 점수λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”.", dataBase.getRound(key));
        String score = sc.nextLine();
        System.out.printf("%d 회차 점수 : %s점\n", dataBase.getRound(key), score);
        System.out.println("점수λ₯Ό 등둝 ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
        System.out.println("1. λ„€");
        System.out.println("2. μ•„λ‹ˆμ˜€");
        String input = sc.nextLine();
        if(inputSelect(input)==1)
        dataBase.registerScore(id, subject, inputSelect(score));
    }
    
    private void inquireScoreMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println(screenData.getStringData(ScreenStage.SCORE_INQUIRE_MENU));
            System.out.println("1. 고유번호둜 μ‘°νšŒν•˜κΈ°");
            System.out.println("2. μƒνƒœλ‘œ μ‘°νšŒν•˜κΈ°");
            String input = sc.nextLine();
            switch (inputSelect(input)) {
                case 1 -> {
                    System.out.println("μ‘°νšŒν•  μˆ˜κ°•μƒμ˜ 고유번호λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
                    input = sc.nextLine();
                    dataBase.printScoreById(input);
                }
                case 2 -> {
                    System.out.println("μ‘°νšŒν•  μˆ˜κ°•μƒμ˜ μƒνƒœλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
                    input = sc.nextLine();
                    dataBase.printScoreByStatus(input);
                }
                case 3 -> flag = false; // ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
                default -> {
                    System.out.println("잘λͺ»λœ μž…λ ₯μž…λ‹ˆλ‹€.\nλ˜λŒμ•„κ°‘λ‹ˆλ‹€!");
                }
            }
        }
    }
    
    private void changeScoreMenu() {
        System.out.println(screenData.getStringData(ScreenStage.SCORE_CHANGE_MENU));
        System.out.println("점수λ₯Ό μˆ˜μ •ν•  μˆ˜κ°•μƒμ˜ 고유번호λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String id = sc.nextLine();
        dataBase.printStudentById(id);
        System.out.println("점수λ₯Ό μˆ˜μ •ν•  κ³Όλͺ©μ„ μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String subject = sc.nextLine();
        System.out.println("점수λ₯Ό μˆ˜μ •ν•  회차λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”");
        String round = sc.nextLine();
        System.out.printf("%d 회차의 μƒˆλ‘œμš΄ 점수λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”\n",inputSelect(round));
        String score = sc.nextLine();
        System.out.println("점수λ₯Ό λ³€κ²½ ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?");
        System.out.println("1. λ„€");
        System.out.println("2. μ•„λ‹ˆμ˜€");
        String input = sc.nextLine();
        if(inputSelect(input)==1)
            dataBase.changeScore(id,subject, inputSelect(round), inputSelect(score));
    }
    
    private boolean isDigit(String number){
        char[] chars = number.toCharArray();
        for (char ch : chars) {
            if (ch < '0' || ch > '9') return false;
        }
        return true;
    }

    private int inputSelect(String number){
        if(isDigit(number))
            return Integer.parseInt(number);
        else
            return 0;
    }   
}

3. DataBase.java

  • Management ν΄λž˜μŠ€μ—μ„œ μ‚¬μš©μžκ°€ μž…λ ₯받은 값을 처리
  • 학생과 κ³Όλͺ©μ— λŒ€ν•œ 정보λ₯Ό keyκ°’μœΌλ‘œ μ‘°νšŒν•  수 μžˆλ„λ‘ 정보λ₯Ό Map 으둜 κ΅¬ν˜„
package Data;
import Resource.Student;
import Resource.Subject;
import Resource.SubjectScore;
import java.util.*;
public class DataBase {
    private final List<Student> studentList = new ArrayList<>();
    private final Set<Subject> subjectList = new HashSet<>();
    
    // key : studentId + subjectName    value : κ³Όλͺ©μ΄λ¦„
    private final Set<String> subjectSet = new HashSet<>();


    // key : studentId + subjectName   value : ν•΄λ‹Ήid의 ν•™μƒμ˜ ν•΄λ‹Ή κ³Όλͺ©μ˜ νšŒμ°¨λ³„ 점수 λͺ©λ‘
    private final Map<String, List<SubjectScore>> subjectScoreMap = new HashMap<>();

    // key : μƒνƒœ                      value : ν•™μƒλ¦¬μŠ€νŠΈ
    private final Map<String, List<Student>> studentStatusMap = new HashMap<>();

    public DataBase() {
        databaseInit();
    }
    
     private void databaseInit() {
        subjectList.add(new Subject("Java", true));
        subjectList.add(new Subject("객체지ν–₯", true));
        subjectList.add(new Subject("Spring", true));
        subjectList.add(new Subject("JPA", true));
        subjectList.add(new Subject("MySQL", true));
        subjectList.add(new Subject("λ””μžμΈ_νŒ¨ν„΄", false));
        subjectList.add(new Subject("Spring_Security", false));
        subjectList.add(new Subject("Redis", false));
        subjectList.add(new Subject("MongoDB", false));
        studentStatusMap.put("Green", new ArrayList<Student>());
        studentStatusMap.put("Yellow", new ArrayList<Student>());
        studentStatusMap.put("Red", new ArrayList<Student>());
    }
    
    public void createStudent(String name, String status, List<String> subjectNameList) {
        Student student = new Student(name, status);
        studentStatusMap.get(status).add(student);
        for (String subjectName : subjectNameList) {
            student.getSubjectList().add(subjectName);
            String key = student.getStudentId() + subjectName;
            subjectSet.add(key);
            subjectScoreMap.put(key, new ArrayList<SubjectScore>());
        }
        System.out.println("μˆ˜κ°•μƒμ΄ λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
        printStudent(student);
    }
    
    public void changeStudent(String id, String name, String status){
        for (Student student : studentList)
            if(student.getStudentId().equals(id)){
                studentStatusMap.get(student.getStatus()).remove(student);
                student.setStudentName(name);
                student.setStatus(status);
                studentStatusMap.get(status).add(student);
                printStudent(student);
                System.out.println("μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
                break;
            }
    }
    
    public void deleteStudent (String id) {
        for (Student student : studentList)
            if(student.getStudentId().equals(id)){
            studentList.remove(student);
                studentStatusMap.get(student.getStatus()).remove(student);
            for (String subject : student.getSubjectList()) {
                String key = student.getStudentId() + subject;
                subjectSet.remove(key);
                subjectScoreMap.remove(key);
            }
            System.out.println("μˆ˜κ°•μƒμ΄ μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
            break;
        }
    }

    
    private  void printStudent(Student student){
        System.out.printf("ID : %-10s | 이름 : %-5s | κ³Όλͺ© : %-30s | μƒνƒœ : %-5s\n",student.getStudentId(), student.getStudentName(), String.join(", ",student.getSubjectList()),student.getStatus());
    }
    
    public void printStudentById(String id){
        for(Student student : studentList)
            if(student.getStudentId().equals(id))
                printStudent(student);
    }
    
    public void printStudentByStatus(String status){
        for(Student student : studentList)
            if(student.getStatus().equals(status))
                printStudent(student);
    }

    public void registerScore(String id, String subject, int score){
        String key = id + subject;
        subjectScoreMap.get(key).add(new SubjectScore(subjectScoreMap.get(key).size()+1,score, calcGrade(score,subject)));
        System.out.println("λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
    }
    
    private char calcGrade(int score, String subject) {
        Boolean mandatory = true;
        for(Subject subjectName : subjectList)
            if(subjectName.getSubjectName().equals(subject))
                 mandatory = subjectName.isMandatory();
        if (mandatory) {
            if (score >= 95) return 'A';
            else if (score >= 90) return 'B';
            else if (score >= 80) return 'C';
            else if (score >= 70) return 'D';
            else if (score >= 60) return 'F';
            else return 'N';
        }
        else
        if (score >= 90) return 'A';
        else if (score >= 80) return 'B';
        else if (score >= 70) return 'C';
        else if (score >= 60) return 'D';
        else if (score >= 50) return 'F';
        else return 'N';
    }

    public int getRound(String key){
        return subjectScoreMap.get(key).size()+1;
    }
    
    private int getAverage(String key){
        int sum = 0;
        for(SubjectScore subjectScore : subjectScoreMap.get(key))
            sum += subjectScore.getScore();
        return sum/(subjectScoreMap.get(key).size()+1);
    }
    
    public void printScore(String id, String subject){
        for(SubjectScore subjectScore : subjectScoreMap.get(id+subject)) {
            System.out.printf("%-5s κ³Όλͺ© %d회차 | %d점 | %cλ“±κΈ‰\n",subject, subjectScore.getRound(), subjectScore.getScore(), subjectScore.getGrade());
        }
        System.out.printf("%-5s κ³Όλͺ© 평균 : %d점 평균등급 : %cλ“±κΈ‰",subject,getAverage(id+subject),calcGrade(getAverage(id+subject), subject));
    }

    public void printScoreById(String id){
        for(Student student : studentList)
            if(student.getStudentId().equals(id))
                for(String subject : student.getSubjectList())
                    printScore(id, subject);

    }
    
    public void printScoreByStatus(String status){
        String id;
        for(Student student : studentList)
            if(student.getStatus().equals(status)){
                id = student.getStudentId();
                for(String subject : student.getSubjectList())
                    printScore(id, subject);
            }
    }

    public void changeScore(String id, String subject, int round, int score){
        for(SubjectScore subjectScore : subjectScoreMap.get(id+subject))
            if(subjectScore.getRound() == round) {
                subjectScore.setScore(score);
                subjectScore.setGrade(calcGrade(score,subject));
                System.out.println("λ³€κ²½λ˜μ—ˆμŠ΅λ‹ˆλ‹€.");
            }
    }
}

4. Student.java

  • ν•™μƒμ˜ 이름, 고유번호, μƒνƒœ, μˆ˜κ°•μ€‘μΈ κ³Όλͺ©λͺ©λ‘μ„ λ‹΄κ³ μžˆλŠ” 클래슀
  • ν•™μƒμ˜ 번호λ₯Ό μžλ™μœΌλ‘œ λΆ€μ—¬ν•˜μ—¬ κ³ μœ ν•œ 번호둜 κ΅¬ν˜„
package Resource;
import java.util.*;


public class Student {
    private static int NO = 1;
    private String studentId;
    private String studentName;
    private String status;
    private final List<String> subjectList = new ArrayList<>();
        public Student(String studentName, String status) {
        studentId = "STU" + NO;
        this.studentName = studentName;
        this.status = status;
        ++NO;
    }
    
    public String getStudentId() {
        return studentId;
    }
    
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public String getStudentName() {
        return studentName;
    }
    
    public String getStatus() {
        return status;
    }
    
    public List<String> getSubjectList() {
        return subjectList;
    }
}

5. Subject.java

  • κ³Όλͺ©μ˜ 이름과 ν•„μˆ˜/선택 μ—¬λΆ€λ₯Ό λ‹΄κ³ μžˆλŠ” 클래슀
package Resource;

public class Subject {
    private String subjectName;
    private boolean isMandatory;

    public Subject(String subjectName, boolean isMandatory) {
        this.subjectName = subjectName;
        this.isMandatory = isMandatory;
    }
    
    public String getSubjectName() {
        return subjectName;
    }
    
    public boolean isMandatory() {
        return isMandatory;
    }
}

6. SubjectScore.java

  • κ³Όλͺ©μ˜ 회차, 점수, 등급을 λ‹΄κ³ μžˆλŠ” 클래슀
package Resource;

public class SubjectScore {
    private int round;
    private int score;
    private char grade;

    public SubjectScore(int round, int score, char grade) {
        this.round = round;
        this.score = score;
        this.grade = grade;
    }

    public int getRound() {
        return round;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public char getGrade() {
        return grade;
    }

    public void setGrade(char grade) {
        this.grade = grade;
    }
}

7. ScreenData.java

  • λ°˜λ³΅ν•˜μ—¬ 좜λ ₯λ˜λŠ” ν™”λ©΄ UIλ₯Ό λ‹΄κ³ μžˆλŠ” 클래슀
package Screen;

import java.util.HashMap;
import java.util.Map;

public class ScreenData {

    private final Map<ScreenStage, String> screenMap = new HashMap<>();

    public ScreenData() {
        initScreenMap();
    }

    public String getStringData(ScreenStage screenStage) {
        return screenMap.get(screenStage);
    }

    private void initScreenMap() {
        screenMap.put(ScreenStage.MAIN_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        
                        1. μˆ˜κ°•μƒ 관리
                        2. 점수 관리
                        3. ν”„λ‘œκ·Έλž¨ μ’…λ£Œ
                        ==================================
                        """
        );
        
        screenMap.put(ScreenStage.STUDENT_MAIN_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        μˆ˜κ°•μƒ 관리 νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
                        
                        1. μˆ˜κ°•μƒ 등둝
                        2. μˆ˜κ°•μƒ 쑰회
                        3. μˆ˜κ°•μƒ μˆ˜μ •
                        4. μˆ˜κ°•μƒ μ‚­μ œ
                        5. λ’€λ‘œκ°€κΈ°
                        ==================================
                        """);
                        
        screenMap.put(ScreenStage.STUDENT_REGISTER_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        μˆ˜κ°•μƒ 등둝 νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
                       
                        """);
                        
        screenMap.put(ScreenStage.STUDENT_INQUIRE_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        μˆ˜κ°•μƒ 쑰회 νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
                        
                        """);
                        
        screenMap.put(ScreenStage.STUDENT_CHANGE_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        μˆ˜κ°•μƒ μˆ˜μ • νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
                        
                        """);
                        
        screenMap.put(ScreenStage.STUDENT_DELETE_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        μˆ˜κ°•μƒ μ‚­μ œ νŽ˜μ΄μ§€μž…λ‹ˆλ‹€.
                        
                        """);

        screenMap.put(ScreenStage.SCORE_MAIN_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        점수 관리 νŽ˜μ΄μ§€μž…λ‹ˆλ‹€
                        
                        1. 점수 μž…λ ₯
                        2. 점수 쑰회
                        3. 점수 μˆ˜μ •
                        4. λ’€λ‘œκ°€κΈ°
                        ==================================""");
                        
        screenMap.put(ScreenStage.SCORE_REGISTER_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        점수 μž…λ ₯ νŽ˜μ΄μ§€μž…λ‹ˆλ‹€
                        
                        """);
                        
        screenMap.put(ScreenStage.SCORE_INQUIRE_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        점수 쑰회 νŽ˜μ΄μ§€μž…λ‹ˆλ‹€
                        
                        """);
                        
        screenMap.put(ScreenStage.SCORE_CHANGE_MENU,
                """
                        ==================================
                        내일배움캠프 μŠ€ν”„λ§ μˆ˜κ°•μƒ 관리 ν”„λ‘œκ·Έλž¨
                        ==================================
                        점수 μˆ˜μ • νŽ˜μ΄μ§€μž…λ‹ˆλ‹€
                        
                        """);
    }
}

8. ScreenStage.java

  • ScreentData ν΄λž˜μŠ€κ°€ λ‹΄κ³ μžˆλŠ” 화면을 κ΅¬λ³„ν•˜λŠ” μ‹λ³„μžλ₯Ό λ‹΄κ³ μžˆλŠ” enumeration클래슀
package Screen;

public enum ScreenStage {
    MAIN_MENU,
    STUDENT_MAIN_MENU,
    STUDENT_REGISTER_MENU,
    STUDENT_INQUIRE_MENU,
    STUDENT_CHANGE_MENU,
    STUDENT_DELETE_MENU,
    SCORE_MAIN_MENU,
    SCORE_REGISTER_MENU,
    SCORE_INQUIRE_MENU,
    SCORE_CHANGE_MENU,
}

μ‹€ν–‰ν™”λ©΄

  1. 메인 메뉴
  1. μˆ˜κ°•μƒ 관리 메뉴
  1. μˆ˜κ°•μƒ 등둝 메뉴
  1. μˆ˜κ°•μƒ 쑰회 메뉴
  1. μˆ˜κ°•μƒ μ‚­μ œ 메뉴
  1. 점수 관리 메뉴
  1. 점수 등둝 메뉴
  1. 점수 쑰회 메뉴
  1. 점수 μˆ˜μ • 메뉴

였늘의 회고

사싀상 이전에 μ§„ν–‰ν•œ Java둜 ν‚€μ˜€μŠ€ν¬λ₯Ό κ΅¬ν˜„ν•˜λŠ” ν”„λ‘œμ νŠΈμ™€ 거의 μœ μ‚¬ν•˜λ‹€.
ν•˜μ§€λ§Œ νŒ€ν”„λ‘œμ νŠΈμΈ 만큼 Githubλ₯Ό μ‚¬μš©ν•˜λŠ” κ²½ν—˜μ„ μ£Όλ €κ³  λ§Œλ“  ν”„λ‘œμ νŠΈμ˜€λ‹€.
이 ν”„λ‘œμ νŠΈλŠ” 비둝 우리 νŒ€μ˜ μ½”λ“œκ°€ μ•„λ‹Œ, μš°λ¦¬νŒ€μ˜ μ½”λ“œλ₯Ό κ°œμ„ ν•˜μ—¬ 혼자 λ‹€μ‹œ λ§Œλ“ κ±°μ§€λ§Œ,
개인적으둜 Map도 배우고, Java에 λŒ€ν•œ 지식과 기술이 더 λ°œμ „ν•œκ±°κ°™μ•„ 맀우 λ§Œμ‘±μŠ€λŸ¬μ› λ‹€.
λ‹€μŒμ£ΌλΆ€ν„°λŠ” λ“œλ””μ–΄ λ°±μ—”λ“œμ— λŒ€ν•΄ 배우게 λœλ‹€.
μ§€κΈˆκΉŒμ§€ μ€€λΉ„μš΄λ™ λ‹¨κ³„μ˜€λ‹€κ³  μƒκ°ν•˜κ³ , 처음 λ°°μ›Œλ³΄λŠ” Spring μ—΄μ‹¬νžˆ 곡뢀해야겠닀 :)

profile
κ°œλ°œν•˜λŠ” 기둝자

0개의 λŒ“κΈ€