From 78bb4147a1226acf9d345e00fc0bafb91e8e36d5 Mon Sep 17 00:00:00 2001 From: JuanHubb Date: Fri, 27 Dec 2024 01:03:03 +0900 Subject: [PATCH 1/3] First added --- src/main/java/gpacalc/Application.java | 164 ++++++++++++++++++++++++- src/main/java/gpacalc/Subject.java | 39 ++++++ 2 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 src/main/java/gpacalc/Subject.java diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 57e79e1..1e9ea54 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -1,7 +1,167 @@ package gpacalc; +import java.util.ArrayList; + +import camp.nextstep.edu.missionutils.Console; + public class Application { + ArrayList majorList = new ArrayList<>(); + ArrayList liberalArtsList = new ArrayList<>(); + double totalMajorAverageRating = 0; + double totalLiberalArtsAverageRating = 0; + int total_major_Credit = 0; + int total_liberalArts_Credit = 0; + int passCredit = 0; + int failCredit = 0; + public static void main(String[] args) { - //TODO: 구현 + Application app = new Application(); + + System.out.println("전공 과목명과 이수학점, 평점을 입력해주세요(예시: 프로그래밍언어론-3-A+,소프트웨어공학-3-B+):"); + app.input(app, app.majorList, true); + + System.out.println("교양 과목명과 이수학점, 평점을 입력해주세요(예시: 선형대수학-3-C0,인간관계와자기성장-3-P):"); + app.input(app, app.liberalArtsList, false); + + System.out.println("\n<과목 목록>"); + for(Subject s : app.majorList){ + System.out.println("[전공] " + s.getTitle() + "," + s.getCredit() + "," + s.getGrade()); + } + for(Subject s : app.liberalArtsList){ + System.out.println("[교양] " + s.getTitle() + "," + s.getCredit() + "," + s.getGrade()); + } + + app.printAll(app); + } + + + public void input(Application app, ArrayList subjectList, boolean majorSubject) { + String[] temp; + String[] component; + temp = Console.readLine().split(","); + for (String eachSubject : temp) { + component = eachSubject.trim().split("-"); + Subject subject = new Subject(); + + // component[0]: title, component[1]: credit, component[2]: grade + app.titleCheck(component[0], subject); + app.creditCheck(component[1],subject); + app.gradeCheck(app, component,subject, majorSubject); + + subjectList.add(subject); + } + } + + // 과목명(component[0]) 검사 + public void titleCheck(String component, Subject subject) { + try{ + if (component.length() > 10) { + throw new IllegalArgumentException("과목명은 공백 포함 10자 이내로 입력해야 합니다."); + }else if (component.isBlank()) { + throw new IllegalArgumentException("과목명은 공백만으로 구성될 수 없습니다."); + }else{ + subject.setTitle(component); + } + } catch (Exception e) { + System.out.println(e.getMessage()); + System.exit(0); + } + } + + // 과목학점(component[1]) 검사 + public void creditCheck(String component, Subject subject) { + try{ + int digit = Integer.parseInt(component); + if (digit == 1 || digit == 2 || digit == 3 || digit == 4){ + subject.setCredit(digit); + }else{ + throw new IllegalArgumentException("과목학점을 잘못 입력하셨습니다."); + } + } catch (Exception e) { + System.out.println(e.getMessage()); + System.exit(0); + } + } + + // 과목성적(component[2]) 검사 + public void gradeCheck(Application app, String[] component, Subject subject, boolean majorSubject) { + try { + if (component[2].equals("P") || component[2].equals("NP")) { + String grade = app.calc_PF_GradeStringToDouble(component[2]); + if (grade.equals("wrong value")) { + throw new IllegalArgumentException("과목성적을 잘못 입력하셨습니다."); + } + if (grade.equals("Pass")){ + app.passCredit += Integer.parseInt(component[1]); + } + } else { + double grade = app.calcGradeStringToDouble(component[2]); + if (grade == -1) { + System.out.println(component[2] + "grade == " + grade); + throw new IllegalArgumentException("과목성적을 잘못 입력하셨습니다."); + } + if (grade == 0){ + app.failCredit += Integer.parseInt(component[1]); + } + else if (majorSubject){ + app.total_major_Credit += Integer.parseInt(component[1]); + app.totalMajorAverageRating += subject.getCredit() * grade; + }else{ + app.total_liberalArts_Credit += Integer.parseInt(component[1]); + app.totalLiberalArtsAverageRating += subject.getCredit() * grade; + } + } + } catch (Exception e) { + System.out.println(e.getMessage()); + System.exit(0); + } + subject.setGrade(component[2]); + } + + public double calcGradeStringToDouble(String grade) { + switch (grade){ + case "A+": + return 4.5; + case "A0": + return 4.0; + case "B+": + return 3.5; + case "B0": + return 3.0; + case "C+": + return 2.5; + case "C0": + return 2.0; + case "D+": + return 1.5; + case "D0": + return 1.0; + case "F": + return 0; + default: + return -1; + } + } + + public String calc_PF_GradeStringToDouble(String grade) { + switch (grade) { + case "P": + return "Pass"; + case "NP": + return "Not Passed"; + default: + return "wrong value"; + } + } + + public void printAll(Application app) { + System.out.println("\n<취득학점>"); + System.out.println(app.total_major_Credit + app.total_liberalArts_Credit + app.passCredit + "학점"); + + System.out.println("\n<평점평균>"); + System.out.println(Math.round((app.totalMajorAverageRating + app.totalLiberalArtsAverageRating) / (app.total_major_Credit + app.total_liberalArts_Credit + app.failCredit)*100)/100.0 + " / 4.5"); + + System.out.println("\n<전공 평점평균>"); + System.out.println(Math.round((app.totalMajorAverageRating / app.total_major_Credit)*100)/100.0 + " / 4.5"); } -} +} \ No newline at end of file diff --git a/src/main/java/gpacalc/Subject.java b/src/main/java/gpacalc/Subject.java new file mode 100644 index 0000000..ff9f4fc --- /dev/null +++ b/src/main/java/gpacalc/Subject.java @@ -0,0 +1,39 @@ +package gpacalc; + +public class Subject { + private String title; + private int credit; + private String grade; + + public Subject(){ + } + public Subject(String title, int credit, String grade) { + this.title = title; + this.credit = credit; + this.grade = grade; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getCredit() { + return credit; + } + + public void setCredit(int credit) { + this.credit = credit; + } + + public String getGrade() { + return grade; + } + + public void setGrade(String grade) { + this.grade = grade; + } +} From 1fb4888ace3b9324b23fc06b58848c9ccec62d94 Mon Sep 17 00:00:00 2001 From: JuanHubb Date: Fri, 27 Dec 2024 02:31:58 +0900 Subject: [PATCH 2/3] feat: Dividing the functions into small methods --- src/main/java/gpacalc/Application.java | 90 ++++++++++++++------------ 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 1e9ea54..9d5ee74 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -1,7 +1,6 @@ package gpacalc; import java.util.ArrayList; - import camp.nextstep.edu.missionutils.Console; public class Application { @@ -9,14 +8,17 @@ public class Application { ArrayList liberalArtsList = new ArrayList<>(); double totalMajorAverageRating = 0; double totalLiberalArtsAverageRating = 0; - int total_major_Credit = 0; - int total_liberalArts_Credit = 0; + int totalMajorCredit = 0; + int totalLiberalArtsCredit = 0; int passCredit = 0; int failCredit = 0; public static void main(String[] args) { Application app = new Application(); + app. run(app); + } + public void run(Application app) { System.out.println("전공 과목명과 이수학점, 평점을 입력해주세요(예시: 프로그래밍언어론-3-A+,소프트웨어공학-3-B+):"); app.input(app, app.majorList, true); @@ -24,35 +26,37 @@ public static void main(String[] args) { app.input(app, app.liberalArtsList, false); System.out.println("\n<과목 목록>"); - for(Subject s : app.majorList){ - System.out.println("[전공] " + s.getTitle() + "," + s.getCredit() + "," + s.getGrade()); - } - for(Subject s : app.liberalArtsList){ - System.out.println("[교양] " + s.getTitle() + "," + s.getCredit() + "," + s.getGrade()); - } + app.printSubjects("[전공] ", app.majorList); + app.printSubjects("[교양] ", app.liberalArtsList); - app.printAll(app); + app.printCalcResult(app); } + public void input(Application app, ArrayList subjectList, boolean majorSubjectStatus) { + String[] beforeSplitInput = Console.readLine().split(","); + + app.creatingObject(app, beforeSplitInput, subjectList, majorSubjectStatus); + } - public void input(Application app, ArrayList subjectList, boolean majorSubject) { - String[] temp; - String[] component; - temp = Console.readLine().split(","); - for (String eachSubject : temp) { - component = eachSubject.trim().split("-"); - Subject subject = new Subject(); + // input 나누는 것이 메소드를 최소 단위로 잘 나눈 것인지 의문 + public void creatingObject(Application app, String[] beforeSplitInput, ArrayList subjectList, boolean majorSubjectStatus) { + String[] subjectComponent; - // component[0]: title, component[1]: credit, component[2]: grade - app.titleCheck(component[0], subject); - app.creditCheck(component[1],subject); - app.gradeCheck(app, component,subject, majorSubject); + for (String eachSubject : beforeSplitInput) { + subjectComponent = eachSubject.trim().split("-"); + Subject subject = new Subject(subjectComponent[0],Integer.parseInt(subjectComponent[1]),subjectComponent[2]); + subjectComponentChecker(app, subject, majorSubjectStatus); subjectList.add(subject); } } - // 과목명(component[0]) 검사 + public void subjectComponentChecker(Application app, Subject subject, boolean majorSubjectStatus){ + app.titleCheck(subject.getTitle(), subject); + app.creditCheck(subject.getCredit(),subject); + app.gradeCheck(app,subject, majorSubjectStatus); + } + public void titleCheck(String component, Subject subject) { try{ if (component.length() > 10) { @@ -68,10 +72,8 @@ public void titleCheck(String component, Subject subject) { } } - // 과목학점(component[1]) 검사 - public void creditCheck(String component, Subject subject) { + public void creditCheck(int digit, Subject subject) { try{ - int digit = Integer.parseInt(component); if (digit == 1 || digit == 2 || digit == 3 || digit == 4){ subject.setCredit(digit); }else{ @@ -82,32 +84,31 @@ public void creditCheck(String component, Subject subject) { System.exit(0); } } - - // 과목성적(component[2]) 검사 - public void gradeCheck(Application app, String[] component, Subject subject, boolean majorSubject) { + + public void gradeCheck(Application app, Subject subject, boolean majorSubject) { try { - if (component[2].equals("P") || component[2].equals("NP")) { - String grade = app.calc_PF_GradeStringToDouble(component[2]); + if (subject.getGrade().equals("P") || subject.getGrade().equals("NP")) { + String grade = app.calcPAndFGradeStringToDouble(subject.getGrade()); if (grade.equals("wrong value")) { throw new IllegalArgumentException("과목성적을 잘못 입력하셨습니다."); } if (grade.equals("Pass")){ - app.passCredit += Integer.parseInt(component[1]); + app.passCredit += subject.getCredit(); } } else { - double grade = app.calcGradeStringToDouble(component[2]); + double grade = app.calcGradeStringToDouble(subject.getGrade()); if (grade == -1) { - System.out.println(component[2] + "grade == " + grade); + System.out.println(subject.getGrade() + "grade == " + grade); throw new IllegalArgumentException("과목성적을 잘못 입력하셨습니다."); } if (grade == 0){ - app.failCredit += Integer.parseInt(component[1]); + app.failCredit += subject.getCredit(); } else if (majorSubject){ - app.total_major_Credit += Integer.parseInt(component[1]); + app.totalMajorCredit += subject.getCredit(); app.totalMajorAverageRating += subject.getCredit() * grade; }else{ - app.total_liberalArts_Credit += Integer.parseInt(component[1]); + app.totalLiberalArtsCredit += subject.getCredit(); app.totalLiberalArtsAverageRating += subject.getCredit() * grade; } } @@ -115,7 +116,6 @@ else if (majorSubject){ System.out.println(e.getMessage()); System.exit(0); } - subject.setGrade(component[2]); } public double calcGradeStringToDouble(String grade) { @@ -143,7 +143,7 @@ public double calcGradeStringToDouble(String grade) { } } - public String calc_PF_GradeStringToDouble(String grade) { + public String calcPAndFGradeStringToDouble(String grade) { switch (grade) { case "P": return "Pass"; @@ -154,14 +154,20 @@ public String calc_PF_GradeStringToDouble(String grade) { } } - public void printAll(Application app) { + public void printSubjects(String subjectType, ArrayList subjectList) { + for(Subject eachSubject : subjectList){ + System.out.println(subjectType + " " + eachSubject.getTitle() + "," + eachSubject.getCredit() + "," + eachSubject.getGrade()); + } + } + + public void printCalcResult(Application app) { System.out.println("\n<취득학점>"); - System.out.println(app.total_major_Credit + app.total_liberalArts_Credit + app.passCredit + "학점"); + System.out.println(app.totalMajorCredit + app.totalLiberalArtsCredit + app.passCredit + "학점"); System.out.println("\n<평점평균>"); - System.out.println(Math.round((app.totalMajorAverageRating + app.totalLiberalArtsAverageRating) / (app.total_major_Credit + app.total_liberalArts_Credit + app.failCredit)*100)/100.0 + " / 4.5"); + System.out.println(Math.round((app.totalMajorAverageRating + app.totalLiberalArtsAverageRating) / (app.totalMajorCredit + app.totalLiberalArtsCredit + app.failCredit)*100)/100.0 + " / 4.5"); System.out.println("\n<전공 평점평균>"); - System.out.println(Math.round((app.totalMajorAverageRating / app.total_major_Credit)*100)/100.0 + " / 4.5"); + System.out.println(Math.round((app.totalMajorAverageRating / app.totalMajorCredit)*100)/100.0 + " / 4.5"); } } \ No newline at end of file From 6574231ee21cd150aa62e8065d8756b705650140 Mon Sep 17 00:00:00 2001 From: JuanHubb Date: Fri, 27 Dec 2024 02:42:40 +0900 Subject: [PATCH 3/3] feat: Adding functionsREADME.md --- functionsREADME.md | 49 ++++++++++++++++++++++++++ src/main/java/gpacalc/Application.java | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 functionsREADME.md diff --git a/functionsREADME.md b/functionsREADME.md new file mode 100644 index 0000000..1746fd8 --- /dev/null +++ b/functionsREADME.md @@ -0,0 +1,49 @@ +### #멋사 13기 홍주한 과제 + +--- + +# 백엔드 트랙 과제 - 학점 계산기 + +## 구현할 기능 목록 + +### 1. 과목 클래스 +- 과목명 +- 과목학점 +- 과목성적 + +### 2. Application 클래스 +- 전공 과목들 리스트 +- 교양 과목들 리스트 +- 알파벳 to 점수 + +### 3. 결과 +- 취득학점 출력 +- 평점평균 출력 +- 전공 평점평균 출력 + +--- + +## 구현 +### 1. 과목 클래스 +- 필드에 과목명, 과목학점, 과목성적 선언 +- getter와 setter 선언 + +### 2. Application 클래스 +#### 1) main +- 문자열로 입력받은 값을 ","를 구분자로 잘라서 과목 구분하기 +- 각 과목에 대한 이름, 학점, 성적을 "-"를 구분자로 잘라서 과목 클래스에 넣기 +- 과목명이 10자 이내, 문자 포함돼 있는지 검사 +- 과목학점이 1,2,3,4 값 중 하나인지 검사 +- 과목성적이 A+,A0,B+,B0,C+,C0,D+,D0,F,P,NP 값 중 하나인지 검사 +- 과목명, 과목학점, 과목성적 중 하나라도 조건에 맞지 않는 값이 있을 시 예외처리하고 종료 + +#### 2) 성적을 점수로 변환 +- 알파벳으로 입력된 과목성적을 각 성적이 매칭된 숫자로 변환하기 + +### 3. 출력 +- 전공, 교양 정보 일괄 출력 +- F, NP 제외한 모든 학점 합산해서 출력 +- 과목평점 * 과목학점 / 과목학점의 총합 계산한 값 평점평균으로 출력 +- 전공 평점평균만 출력 + +#### *전공과 교양을 나누어서 같은 과정을 수행 \ No newline at end of file diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 9d5ee74..202b01a 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -84,7 +84,7 @@ public void creditCheck(int digit, Subject subject) { System.exit(0); } } - + public void gradeCheck(Application app, Subject subject, boolean majorSubject) { try { if (subject.getGrade().equals("P") || subject.getGrade().equals("NP")) {