(협업) 팀프로젝트 캠프 관리 프로그램 제작

앞고기랑 소금·2024년 8월 2일

스파르타 TIL

목록 보기
14/43

14일차

맡은 역할 : 수강생 목록조회, 수강생 건강상태 등록, 수강생 정보수정

  • 수강생 목록조회

public class Camp
private static List<Student> studentStore;

/*수강생 등록 메서드에 기입
studentStore.add(student.getStudent());
*/

private static void inquireStudent() {
	System.out.println("\n수강생 목록을 조회합니다...");
    // 기능 구현
    for (Student student : studentStore) {
    	System.out.println(student.getStudentId() + " " + student.getStudentName());
    }
        System.out.println("\n수강생 목록 조회 성공!");
}
public class Student

public String getStudentId() {
	return studentId;
}
public String getStudentName() {
	return studentName;
}

public Student getStudent() {
	return this;
}

Camp 클래스에서 for-each문이 아닌 studentStore.toString()으로 조회하려 했으나 참조값이 나와서 for-each문으로 변경.

  • 수강생 건강상태 등록

public class Camp
// 수강생 상태 등록 추가
System.out.print("수강생 상태 입력(Green, Yellow, Red): ");
String studentCondition = sc.next();

// 수강생 상태 기입란 변경
Student student = new Student(sequence(INDEX_TYPE_STUDENT), studentName, studentCondition);
public class Student
// 수강생 상태 추가
private String studentCondition;

// 수강생 기입 수정
public Student(String seq, String studentName, String studentCondition) {
	this.studentId = seq;
    this.studentName = studentName;
    this.studentCondition = studentCondition;
}
    
// 수강생 상태 Getter 메서드 추가
public String getCondition() {
	return studentCondition;
}

"상태를 String으로 받아서 아무거나 다 받는다" 라는 피드백을 받아 리펙토링함.

public class Camp
// 수강생 상태 등록 리펙토링
String studentCondition = displayStudentCondition();

private static String displayStudentCondition() {
	boolean flag = true;
    while (flag) {
    	System.out.println("수강생 상태 입력 :");
        System.out.println("1. 양호");
        System.out.println("2. 주의");
        System.out.println("3. 위험");
        System.out.print("관리 항목을 선택하세요...");
        int input = sc.nextInt();

        switch (input) {
        	case 1 :
            	return  "Green";   // 양호
            case 2 :
                return  "Yellow";  // 주의
            case 3 :
                return  "Red";     // 위험
            default :
            	System.out.println("잘못된 입력입니다.\n다시 입력해주세요.\n");
        	}
    	}
	return null;
}

원하는 입력이 아니면 다시 입력되게 리펙토링함.

  • 수강생 정보수정

public class Camp
private static void updateStudent() {
	if (!studentStore.isEmpty()) {
    	boolean flag = true;
        while (flag) {
        	System.out.println("\n수정할 수강생의 이름을 입력하세요 :");
            String name = sc.next();
            int nameIndex = studentStore.indexOf(name);
            for (Student student : studentStore) {
            	student.getStudentName().equals(name);
            }
            if (nameIndex == -1) {
            	System.out.println("잘못된 입력입니다.\n다시 입력해주세요.\n");
            }
            System.out.println("이름과 상태를 확인해 주세요.");
            System.out.println("이름 : " + studentStore.get(nameIndex).getStudentName());
            System.out.println("상태 : " + studentStore.get(nameIndex).getCondition());
            System.out.println("\n수정하시겠습니까?");
            System.out.println("1. 예");
            System.out.println("2. 아니요");
            int input = sc.nextInt();
            if (input == 2) {
            	flag = false;
            }
            if (input == 1) {
            	System.out.println("수강생 이름 : " + studentStore.get(nameIndex).getStudentName());
                System.out.print("\n수강생 이름 수정 :");
                String studentName = sc.next();
                studentStore.get(nameIndex).setStudentName(studentName);
                System.out.println("수강생 상태 : " + studentStore.get(nameIndex).getCondition());
                System.out.print("\n수강생 상태 수정");
                studentStore.get(nameIndex).setStudentName(displayStudentCondition());
                System.out.println("수정완료.");
                System.out.println("수정된 이름 : " + studentStore.get(nameIndex).getStudentName());
                System.out.println("수정된 상태 : " + studentStore.get(nameIndex).getCondition());
                flag = false;
            }
                System.out.println("잘못된 입력입니다.\n다시 입력해주세요.\n");
		}
	}
    else {
    	System.out.println("수정할 수강생의 정보가 없습니다.");
    }
}

studentStore.indexOf()으로 이름 입력하면 이름과 맞는 index값을 가져와서 수정 하려했으나.. 똑같은 실수! 참조값이 불러와짐
팀원분들께 도움요청...

public class Camp

// List -> Map 으로 변경
private static Map<String, Student> studentStore;

// 변경
studentStore = new HashMap<>();

// 수강생 등록 메서드 기입한거 변경
studentStore.put(student.getStudentId(), student.getStudent());

// 수강생 목록 조회 for-each문도 변경
for (Student student : studentStore.values()) {
	System.out.println(student.getStudentId() + " " + student.getStudentName());
}

// 수강생의 ID를 입력받고 수정할수있게 팀원분들이 으쌰으쌰해주심.
private static void updateStudent() {
	if (!studentStore.isEmpty()) {
    	boolean flag = true;
        while (flag) {
        	System.out.println("\n수정할 수강생의 ID를 입력하세요 :");
        	String studentId = sc.next();
        	Student student = studentStore.get(studentId);

        	System.out.println("이름과 상태를 확인해 주세요.");
        	System.out.println("이름 : " + student.getStudentName());
        	System.out.println("상태 : " + student.getCondition());
        	System.out.println("\n수정하시겠습니까?");
        	System.out.println("1. 예");
        	System.out.println("2. 아니요");
        	int input = sc.nextInt();
			if (input == 2) {
            	flag = false;
        		break;
        	}
			if (input == 1) {
            	System.out.println("수강생 이름 : " + student.getStudentName());
            	System.out.print("\n수강생 이름 수정 :");
            	String studentName = sc.next();
            	student.setStudentName(studentName);
            	System.out.println("수강생 상태 : " + student.getCondition());
            	System.out.print("\n수강생 상태 수정");
            	student.setStudentCondition(displayStudentCondition());
            	System.out.println("수정완료.");
            	System.out.println("수정된 이름 : " + student.getStudentName());
            	System.out.println("수정된 상태 : " + student.getCondition());
            	flag = false;
        		break;
        	}
        	System.out.println("잘못된 입력입니다.\n다시 입력해주세요.\n");
    	}
    } else {
    	System.out.println("수정할 수강생의 정보가 없습니다.");
	}
}
public class Student

// Setter 추가
public void setStudentName(String studentName) {
	this.studentName = studentName;
}

public void setStudentCondition(String studentCondition) {
	this.studentCondition = studentCondition;
}

기능 마구 때려 박고 Camp 클래스에 마구 기능 때려박은거 캡슐화 하려고 팀 다 모여서 화면공유 하면서 어찌할지 의논하면고 클래스만들어서 기능들 찢어 넣고 캡슐화함.

0개의 댓글