비트교육_단기과정
List를 사용한 성적관리
- -
학생정보.class
package project_0707; public class StudentInfo {// 학생 학과 이름 성적들(리스트) 학번 객체 private String std_id;// 학번 private String std_department;// 학과 private String name;// 이름 private Score kor; private Score eng; private Score mat; private float avg; private int total; public StudentInfo() { super(); } public StudentInfo(String std_id, String std_department, String name, int kor, int eng, int mat) { super(); this.std_id = std_id; this.std_department = std_department; this.name = name; this.kor = new Score(kor); this.eng = new Score(eng); this.mat = new Score(mat); this.total = kor + eng + mat; this.avg = this.total / 3.1f; } public String getStd_id() { return std_id; } public void setStd_id(String std_id) { this.std_id = std_id; } public String getStd_department() { return std_department; } public void setStd_department(String std_department) { this.std_department = std_department; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Score getKor() { return kor; } public void setKor(int kor) { this.kor = new Score(kor); } public Score getEng() { return eng; } public void setEng(int eng) { this.eng = new Score(eng); } public Score getMat() { return mat; } public void setMat(int mat) { this.mat = new Score(mat); } public float getAvg() { return avg; } public void setAvg(float avg) { this.avg = this.total / 3.1f; } public int getTotal() { return total; } public void setTotal(int total) { this.total = this.mat.getSubject() + this.eng.getSubject() + this.kor.getSubject(); } } | cs |
교과점수.class
package project_0707; public class Score { private int subject; public Score(int subject) { this.subject = subject; } public int getSubject() { return subject; } public void setSubject(int subject) { this.subject = subject; } } | cs |
Gui.class
package project_0707; import java.util.*; public class Gui { public void exe() { Scanner sc = new Scanner(System.in); List<StudentInfo> arr = new ArrayList<>(); InformationManagement im = new InformationManagement(); out: while (true) { System.out.print("1.입력 2.출력 3.검색 4.삭제 5.수정 6.종료 ==> "); switch (sc.nextInt()) { case 1: try { im.insertInfo(arr, sc); } catch (Exception e) { System.out.println("해당 학번은 이미 사용중입니다 다시 입력해주십시오"); } break; case 2: System.out.print("정렬기준 : 1.등록 2.총점 3.이름 4.학과 ==>"); switch (sc.nextInt()) { case 1: im.dispALLInfo(arr);// 등록순으로 출력 break; case 2: im.dispALLInfo(im.listSort_total(arr));// 라스트출력(return 총점순으로 정렬된 리스트) break; case 3: im.dispALLInfo(im.listSort_name(arr));// 라스트출력(return 이름으로 정렬된 리스트) break; case 4: im.dispALLInfo(im.listSort_std_department(arr));// 라스트출력(return 학과이름순으로 정렬된 리스트) break; default: System.out.println("형식에 맞지 않는 입력값입니다"); break; } break; case 3: System.out.print("감색기준 : 1.학번 2.이름 3.학과 ==>"); int opt_3 = sc.nextInt(); System.out.print("검색정보를 입력하시오 : "); String value = sc.next(); switch (opt_3) { case 1: im.dispInfo(im.searchInfo_stdId(arr, value)); break; case 2: im.dispALLInfo(im.searchInfo_stdName(arr, value)); break; case 3: im.dispALLInfo(im.searchInfo_stdDepartment(arr, value)); break; default: System.out.println("형식에 맞지 않는 입력값입니다"); break; } break; case 4: im.dispALLInfo(arr);// 삭제 전 학생 정보 출력 im.StudentInfo_delete(arr, sc); im.dispALLInfo(arr);// 삭제 후 학생 정보 출력 break; case 5: System.out.print("수정할 학생의 사번을 입력하시오 : "); String std_id = sc.next(); System.out.print("수정항목 : 1.이름 2.학과 3.국어 4.영어 5.수학 ==>"); int opt5 = sc.nextInt(); System.out.print("수정정보를 입력하시오 : "); String value_opt5 = sc.next(); im.StudentInfo_modify(arr, opt5, value_opt5, std_id); break; case 6: break out; default: System.out.println("형식에 맞지 않는 입력값입니다"); break; } } } } | cs |
학생정보관리.class
package project_0707; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class InformationManagement { public void overlapCheck(List<StudentInfo> arr, String std_id) throws Exception {// 학번 중복체크 for (StudentInfo info : arr) { if (std_id.equals(info.getStd_id())) {// 리스트에 해당 학번이 존재할경우 throw new Exception(); } } } public void insertInfo(List<StudentInfo> arr, Scanner sc) throws Exception {// 학생정보 입력 System.out.println("======학생정보입력======"); System.out.print("학번을 입력하시오 : "); String std_id = sc.next(); overlapCheck(arr, std_id);// 사번 중복입력 메서드 호출 , 중복값이 존재하지않으면 true System.out.print("학과를 입력하시오 : "); String std_department = sc.next(); System.out.print("이름을 입력하시오 : "); String name = sc.next(); System.out.print("국어 점수를 입력하시오 : "); int kor = sc.nextInt(); System.out.print("영어 점수를 입력하시오 : "); int eng = sc.nextInt(); System.out.print("수학 점수를 입력하시오 : "); int mat = sc.nextInt(); StudentInfo studentInfo = new StudentInfo(std_id, std_department, name, kor, eng, mat); arr.add(studentInfo); } public void dispALLInfo(List<StudentInfo> arr) {// 리스트 전체출력 if (!arr.isEmpty()) { System.out.println("======학생 정보 출력======"); for (StudentInfo info : arr) { System.out.println("학번 : " + info.getStd_id() + " 학과 : " + info.getStd_department() + " 이름 : " + info.getName() + " 국어 : " + info.getKor().getSubject() + " 영어 : " + info.getEng().getSubject() + " 수학 : " + info.getMat().getSubject() + " 총점 : " + info.getTotal() + " 평균 : " + info.getAvg()); } } else System.out.println("출력할 데이터가 존재하지않습니다"); } public void dispInfo(StudentInfo info) { if (!(info == null)) { System.out.println("학번 : " + info.getStd_id() + " 학과 : " + info.getStd_department() + " 이름 : " + info.getName() + " 국어 : " + info.getKor().getSubject() + " 영어 : " + info.getEng().getSubject() + " 수학 : " + info.getMat().getSubject() + " 총점 : " + info.getTotal() + " 평균 : " + info.getAvg()); } else System.out.println("출력할 데이터가 존재하지않습니다"); } public List<StudentInfo> listSort_name(List<StudentInfo> arr) {// 이름기준으로 오름차순 정렬 List<StudentInfo> temp = new ArrayList<>(); temp.addAll(arr); Collections.sort(temp, new Comparator<StudentInfo>() { @Override public int compare(StudentInfo std1, StudentInfo std2) { return std1.getName().compareTo(std2.getName()); } }); return temp; } public List<StudentInfo> listSort_total(List<StudentInfo> arr) {// 총점기준으로 내림차순 정렬 List<StudentInfo> temp = new ArrayList<>(); temp.addAll(arr); Collections.sort(temp, new Comparator<StudentInfo>() {// 총점순으로 temp 정렬 @Override public int compare(StudentInfo std1, StudentInfo std2) { return ((Integer) std1.getTotal()).compareTo((Integer) std2.getTotal()); } }); Collections.reverse(temp);// 정렬된 temp를 reverse메서드를 사용하여 내림차순으로 변경 return temp; } public List<StudentInfo> listSort_std_department(List<StudentInfo> arr) {// 이름기준으로 오름차순 정렬 List<StudentInfo> temp = new ArrayList<>(); temp.addAll(arr); Collections.sort(temp, new Comparator<StudentInfo>() { @Override public int compare(StudentInfo std1, StudentInfo std2) { return std1.getStd_department().compareTo(std2.getStd_department()); } }); return temp; } public List<StudentInfo> searchInfo_stdName(List<StudentInfo> arr, String value) {// 이름으로 리스트 검색 List<StudentInfo> temp = new ArrayList<>(); for (StudentInfo info : arr) { if (value.equals(info.getName())) { temp.add(info); } } return temp; } public List<StudentInfo> searchInfo_stdDepartment(List<StudentInfo> arr, String value) {// 학과로 리스트 검색 List<StudentInfo> temp = new ArrayList<>(); for (StudentInfo info : arr) { if (value.equals(info.getStd_department())) { temp.add(info); } } return temp; } public StudentInfo searchInfo_stdId(List<StudentInfo> arr, String value) {// 사번으로 리스트 검색 StudentInfo search_info = null; for (StudentInfo info : arr) { if (value.equals(info.getStd_id())) { search_info = info; } } return search_info; } public void StudentInfo_delete(List<StudentInfo> arr, Scanner sc) { System.out.print("삭제할 학생의 사번을 입력하시오 : "); String std_id = sc.next(); // for (StudentInfo info : arr) { // if (info.getStd_id().equals(std_id)) { // arr.remove(info); // } // } for (Iterator<StudentInfo> iterator = arr.iterator(); iterator.hasNext();) { StudentInfo info = iterator.next(); if (info.getStd_id().equals(std_id)) { iterator.remove(); break; } } } public void StudentInfo_modify(List<StudentInfo> arr, int opt5, String value_opt5, String std_id) { StudentInfo info = null; int cnt = 0; for (int i = 0; i < arr.size(); i++) { if (arr.get(i).getStd_id().equals(std_id)) cnt = i; info = arr.get(i); } switch (opt5) { case 1: info.setName(value_opt5); // arr[i]. = new StudentInfo(info.getStd_id(), std_id, std_id, opt5, opt5, // opt5); break; case 2: info.setStd_department(value_opt5); break; case 3: arr.set(cnt, new StudentInfo(info.getStd_id(), info.getStd_department(), info.getName(), Integer.valueOf(value_opt5), info.getEng().getSubject(), info.getMat().getSubject())); break; case 4: arr.set(cnt, new StudentInfo(info.getStd_id(), info.getStd_department(), info.getName(), info.getKor().getSubject(), Integer.valueOf(value_opt5), info.getMat().getSubject())); break; case 5: arr.set(cnt, new StudentInfo(info.getStd_id(), info.getStd_department(), info.getName(), info.getKor().getSubject(), info.getEng().getSubject(), Integer.valueOf(value_opt5))); break; default: break; } } } | cs |
실행.class
package project_0707; public class Start { public static void main(String[] args) { Gui go = new Gui(); go.exe(); } } | cs |
'비트교육_단기과정' 카테고리의 다른 글
Map<E> 성적관리 (0) | 2022.07.08 |
---|---|
Set<E> 성적관리 (0) | 2022.07.08 |
Generic Memory (0) | 2022.07.06 |
스레드 동기화(ATM 번갈아서) : 마기창 (0) | 2022.07.05 |
12장_추상 클래스와 인터페이스 연습문제 풀이 (0) | 2022.07.04 |
Contents
소중한 공감 감사합니다