IT/Programming / / 2023. 4. 20. 13:18

[JAVA] Map을 사용한 학생관리프로그램 예제

반응형
package dayday16;

/*
 * Map을 사용하여 다음 실습 진행 
 * 
 * < 학생 관리 프로그램 >
 * 1. Student 클래스 
 * 	필드 : 이름, 점수, 등급
 *  생성자 : 이름, 점수 (등급은 자동 계산)
 *  메소드 : setPoint() => 인자값을 점수가 들어오면, 
 *   멤버에 점수 재저장 + 등급 재저장
 *  toString() 오버라이드 
 *  
 */

import java.util.*;

import javax.swing.JOptionPane;

class Student {
	String name;
	int point;
	char grade;
	
	Student(String name, int point){
		this.name = name;
		setPoint(point);
	}
	void setPoint(int point){
		this.point = point;
		if(point >= 90) grade = 'A';
		else if(point >= 80) grade = 'B';
		else if(point >= 70) grade = 'C';
		else if(point >= 60) grade = 'D';
		else grade = 'F';
	}
	@Override
	public String toString() {
		return "이름 : " +name + "\n"
				+"점수 : " + point + "점\n"
				+"등급 : " + grade + "학점\n";
	}
}
 /* 2. Quiz01
 *  Map 생성 ( Hash or Tree )
 *  메뉴 : 
 *  	1. 학생 등록 : 이름, 점수
 *  	2. 학생 수정
 *  		수정할 학생 이름 입력 + 검색 후
 *  		1. 이름 수정
 *  		2. 점수 수정
 *  	3. 학생 삭제 
 *  		삭제할 학생 이름 입력 -> 해당 학생 삭제
 *  	4. 종료
 */
public class Quiz01 {

	public static void main(String[] args) {
		HashMap<String,Student> m = new HashMap<String,Student>();
		String message = "1. 학생 등록 \n"
						+"2. 학생 수정 \n"
						+"3. 학생 삭제 \n"
						+"4. 종료\n"
						+ "(5. 모두 보기)";
		String select;
		while(true){
			select = JOptionPane.showInputDialog(message);
			if(select.equals("1")){
				String name = JOptionPane.showInputDialog("이름");
				String sPoint = JOptionPane.showInputDialog("점수");
				int point = Integer.parseInt(sPoint);
				m.put(name, new Student(name, point));
				JOptionPane.showMessageDialog(null, "등록 완료!");
			}else if(select.equals("2")){
				String key = JOptionPane.showInputDialog("수정할 학생 이름");
				if(m.containsKey(key)){
					Student target = m.get(key);
					String sel =JOptionPane.showInputDialog("1. 이름수정\n2. 점수수정");
					if(sel.equals("1")){
						target.name = JOptionPane.showInputDialog("이름 수정");
						m.remove(key); // 일단 삭제..
						m.put(target.name, target); // 새로 추가
						
					} else if(sel.equals("2")){
						String sPoint = JOptionPane.showInputDialog("점수 수정");
						int point = Integer.parseInt(sPoint);
						target.setPoint(point);
					} else {
						JOptionPane.showMessageDialog(null, "잘못 입력. 메뉴로 돌아갑니다..");        
					}
				} else {
					JOptionPane.showMessageDialog(null, "수정할 학생을 찾지 못했습니다");
				}
			}else if(select.equals("3")){
				String key = JOptionPane.showInputDialog("삭제할 학생 이름");
				if(m.containsKey(key)){
					m.remove(key);
					JOptionPane.showMessageDialog(null, "삭제 완료!");
				} else {
					JOptionPane.showMessageDialog(null, "삭제할 학생을 찾지 못했습니다");
				}
			}else if(select.equals("4")){
				JOptionPane.showMessageDialog(null, "프로그램 종료");
				return;
			} else if(select.equals("5")){
				String msg = "";
				
				Set<String> keys = m.keySet();
				for(String ss : keys){
					msg += m.get(ss) + "=============\n";
				}
				JOptionPane.showMessageDialog(null, msg);
			} else {
				JOptionPane.showMessageDialog(null, "잘못 입력");
			}
		}
	}
}
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유