본문 바로가기

JAVA/국비 공부

3월 19일 수업(예제1)

package day0316;
// 캡슐화가 적용된 Student 클래스를 사용하여

// 학생 5명을 관리학 만약 똑같은 이름과 번호를 가진 학생은 또다시 입력할 수 없는
// 성적관리 프로그램을 작성하시오.

import java.util.Scanner;
import util.ScannerUtil;
import util.ArrayUtil;

public class GrandeBook02 {
    private static final int SIZE_STUDENT = 5;
    private static final int SCORE_MIN = 0;
    private static final int SCORE_MAX = 100;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        Student[] array = new Student[0];

        while (true) {
            String message = new String("1. 입력 2. 출력 3. 종료");
            int userChoice = ScannerUtil.nextInt(scanner, message, 1, 3);
            if (userChoice == 1) {
                if (ArrayUtil.size(array) < SIZE_STUDENT) {

                  // Student 객체를 생성해서, 그 주소를 참조변수 s에 저장된다.
                    Student s = new Student();
                    
                    
                    //번호 입력
                   
                    message = new String("번호를 입력해주세요");
                    s.setId(ScannerUtil.nextInt(scanner, message));
                                       
                    //이름 입력
                    message = new String("이름을 입력해주세요");
                    s.setName(ScannerUtil.nextLine(scanner, message));
                                       
                    //중복일 시  다시입력
                    while (ArrayUtil.contains(array, s)) {
                        // 학생번호
                        System.out.println("이미 존재하는 학생입니다.");
                        System.out.println("번호와 이름이 다른 학생을 일력해주세요");
                       
                        message = new String("번호를 입력해주세요");
                        s.setId(ScannerUtil.nextInt(scanner, message));
                        
                        message = new String("이름을 입력해주세요");
                        s.setName(ScannerUtil.nextLine(scanner, message));
                    }
                    //국어점수 입력
                    message = new String("국어점수를 입력해주세요");
                    s.setKor(ScannerUtil.nextInt(scanner, message, SCORE_MIN, SCORE_MAX));
                    
                    //영어점수 입력
                    message = new String("영어점수를 입력해주세요");
                    s.setEng(ScannerUtil.nextInt(scanner, message, SCORE_MIN, SCORE_MAX));
                    
                    //수학점수 입력
                    message = new String("수학점수를 입력해주세요");
                    s.setMath(ScannerUtil.nextInt(scanner, message, SCORE_MIN, SCORE_MAX));

                    array = ArrayUtil.add(array, s);

                } else {
                    System.out.println("더이상 입력하실 수 없습니다.");
                }

            } else if (userChoice == 2) {

                if (ArrayUtil.size(array) == 0) {
                    System.out.println(" 아직 입력된 정보가 존재하지 않습니다.");

                } else {
                    System.out.println();
                    for (int i = 0; i < ArrayUtil.size(array); i++) {
                        System.out.println("===================");
                        array[i].showInfo();
                        System.out.println("===================");
                    }

                    System.out.println();
                }
            } else if (userChoice == 3) {
                System.out.println("사용해주셔서 감사합니다.");
                break;
            }
        }

        scanner.close();
    }

}

'JAVA > 국비 공부' 카테고리의 다른 글

3월 16일 수업  (0) 2021.03.16
3월16일수업(캡슐화)  (0) 2021.03.16
3월16일수업(예제)  (0) 2021.03.16
3월15일 수업(예제4)  (0) 2021.03.15
3월15일 수업(예제3)  (0) 2021.03.15