From 8bcc593b6cd6a827fe701118e78f34d821c0a7cf Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Tue, 9 Jan 2024 13:29:39 +0900 Subject: [PATCH 1/9] Mission Completed --- src/main/java/gpacalc/Application.java | 48 +++++++ src/main/java/gpacalc/Calculator.java | 191 +++++++++++++++++++++++++ src/main/java/gpacalc/Input.java | 25 ++++ src/main/java/gpacalc/ParseString.java | 22 +++ src/test/java/ApplicationTest.java | 108 +++++++------- 5 files changed, 340 insertions(+), 54 deletions(-) create mode 100644 src/main/java/gpacalc/Calculator.java create mode 100644 src/main/java/gpacalc/Input.java create mode 100644 src/main/java/gpacalc/ParseString.java diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 57e79e1..8ef7abf 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -1,7 +1,55 @@ package gpacalc; +import java.util.List; + + +// ... + + public class Application { public static void main(String[] args) { //TODO: 구현 + + // 1. 과목 - 학점 - 점수 묶음으로 입력 + + // 2. 형태에 맞춰 필요한 정보 담기 ( 제대로 담기만 하면 됨 ) + + // 3. parsing 후에 계산 + + // 4. 이쁘게 출력 + + +// 1. + + + Input input = new Input(); + String major_score = input.getMajorInput(); + String elec_score = input.getElectiveInput(); + + +// 2. + + ParseString parseString = new ParseString(); + List major_result = parseString.getSeparated(major_score); + List elec_result = parseString.getSeparated(elec_score); + + +// 3. + System.out.println("<과목 목록>"); + Calculator calculator = new Calculator(); + calculator.printMajorInput(major_result); + calculator.printElecInput(elec_result); + +// 4. + calculator.printSum(); + calculator.printAll(); + +//데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+ +//미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F + + + System.out.println("=== [System] : 계산이 끝났습니다. ==="); + System.exit(0); + } } diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java new file mode 100644 index 0000000..29f16f2 --- /dev/null +++ b/src/main/java/gpacalc/Calculator.java @@ -0,0 +1,191 @@ +package gpacalc; + +import java.util.Arrays; +import java.util.List; + +public class Calculator { + + double total_score = 0; // 총 취득 학점 + double major_total_score = 0; + double major_sum = 0; + double elec_sum = 0; + + double grade =0; + double tmp; + double rate; + + public void printMajorInput(List info_maj) { + + for (int i = 0; i < info_maj.size(); i++) { + if (i % 3 == 0) + if (info_maj.get(i) == null || info_maj.get(i).length() > 10) { + throw new IllegalArgumentException(); + } + System.out.print("[전공] "); + + System.out.print(info_maj.get(i)); + + + if (i % 3 == 1) { + tmp = Integer.parseInt(info_maj.get(i)); + if (tmp < 1 || tmp > 4) { + throw new IllegalArgumentException(); + } + + if (!info_maj.get(i + 1).equals("F") && !info_maj.get(i + 1).equals("NP")) { + total_score += tmp; + major_total_score += tmp; + + } + + if (!info_maj.get(i + 1).equals("P") && !info_maj.get(i + 1).equals("NP")) + grade += tmp; + + + System.out.print(","); + + } else if (i % 3 == 2) { + System.out.println(); + if (!Arrays.asList("A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F","P","NP").contains(info_maj.get(i))) { + throw new IllegalArgumentException(); + } + if (!info_maj.get(i).equals("P") && !info_maj.get(i).equals("NP")) { + switch (info_maj.get(i)) { + case "A+": + rate = 4.5; + break; + case "A0": + rate = 4.0; + break; + case "B+": + rate = 3.5; + break; + case "B0": + rate = 3.0; + break; + case "C+": + rate = 2.5; + break; + case "C0": + rate = 2.0; + break; + case "D+": + rate = 1.5; + break; + case "D0": + rate = 1.0; + break; + case "F": + rate = 0; + break; + } + + addMajorScore(tmp, rate); + } + + } else { + System.out.print(","); + } + } + + + } + + public void printElecInput(List info_elec) { + + for (int i = 0; i < info_elec.size(); i++) { + if (i % 3 == 0) + if (info_elec.get(i) == null || info_elec.get(i).length() > 10) { + throw new IllegalArgumentException(); + } + System.out.print("[교양] "); + + System.out.print(info_elec.get(i)); + + + if (i % 3 == 1) { + tmp = Integer.parseInt(info_elec.get(i)); + if (tmp < 1 || tmp > 4) { + throw new IllegalArgumentException(); + } + if (!info_elec.get(i + 1).equals("F") && !info_elec.get(i + 1).equals("NP")) + total_score += tmp; + + if (!info_elec.get(i + 1).equals("P") && !info_elec.get(i + 1).equals("NP")) + grade += tmp; + + System.out.print(","); + + } else if (i % 3 == 2) { + System.out.println(); + if (!Arrays.asList("A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F","P","NP").contains(info_elec.get(i))) { + throw new IllegalArgumentException(); + } + if (!info_elec.get(i).equals("P") && !info_elec.get(i).equals("NP")) { + switch (info_elec.get(i)) { + case "A+": + rate = 4.5; + break; + case "A0": + rate = 4.0; + break; + case "B+": + rate = 3.5; + break; + case "B0": + rate = 3.0; + break; + case "C+": + rate = 2.5; + break; + case "C0": + rate = 2.0; + break; + case "D+": + rate = 1.5; + break; + case "D0": + rate = 1.0; + break; + case "F": + rate = 0; + break; + } + + addElecScore(tmp, rate); + } + } else { + System.out.print(","); + } + } + + } + + public void printSum() { + System.out.println("\n <취득 학점>"); + System.out.println(total_score + "학점"); + } + + private void addMajorScore(double score, double r) { + major_sum += score * r; + + } + + private void addElecScore(double score, double r) { + elec_sum += score * r; + + } + + public void printAll() { + double avg = (major_sum + elec_sum) / grade; + double major_avg = major_sum / major_total_score; + + System.out.println("<평점 평균>"); + System.out.println(String.format("%.2f / 4.5", avg)); + + + System.out.println("<전공 평점 평균>"); + System.out.println(String.format("%.2f / 4.5", major_avg)); + } + +} diff --git a/src/main/java/gpacalc/Input.java b/src/main/java/gpacalc/Input.java new file mode 100644 index 0000000..8b84f89 --- /dev/null +++ b/src/main/java/gpacalc/Input.java @@ -0,0 +1,25 @@ +package gpacalc; + +import static camp.nextstep.edu.missionutils.Console.readLine; + +import camp.nextstep.edu.missionutils.Console; + + +public class Input { + + public String getMajorInput(){ + + System.out.println(" === 전공 과목명과 이수학점, 평점을 입력해주세요 === "); + + String major_score = Console.readLine(); + return major_score; + } + + public String getElectiveInput(){ + + System.out.println(" === 교양 과목명과 이수학점, 평점을 입력해주세요 === "); + + String elec_score = Console.readLine(); + return elec_score; + } +} diff --git a/src/main/java/gpacalc/ParseString.java b/src/main/java/gpacalc/ParseString.java new file mode 100644 index 0000000..48ec04b --- /dev/null +++ b/src/main/java/gpacalc/ParseString.java @@ -0,0 +1,22 @@ +package gpacalc; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class ParseString { + + public List getSeparated(String a) { + + String[] spl_one = a.split(","); + List resultList = new ArrayList<>(); + + + for (String tmp : spl_one) { + String[] pro_hyp = tmp.split("-"); + resultList.addAll(Arrays.asList(pro_hyp)); + } + + return resultList; + } +} diff --git a/src/test/java/ApplicationTest.java b/src/test/java/ApplicationTest.java index 38086f8..f27c73b 100644 --- a/src/test/java/ApplicationTest.java +++ b/src/test/java/ApplicationTest.java @@ -1,54 +1,54 @@ -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; - -import camp.nextstep.edu.missionutils.test.NsTest; -import gpacalc.Application; -import org.junit.jupiter.api.Test; - -public class ApplicationTest extends NsTest { - - @Test - void 평점평균_계산() { - run("데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+", - "미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F"); - assertThat(output()).contains( - "<과목 목록>", - "[전공] 데이타구조,3,A0", - "[전공] 자바프로그래밍언어,3,B+", - "[전공] 컴퓨터구조,3,C0", - "[전공] 컴퓨터네트워크,3,D+", - "[교양] 미술의이해,3,P", - "[교양] 교양특론3,1,NP", - "[교양] 기독교의이해,2,F", - "<취득학점>", - "15학점", - "<평점평균>", - "2.36 / 4.5", - "<전공 평점평균>", - "2.75 / 4.5" - ); - } - - @Test - void 예외처리_잘못된_성적_입력() { - assertThatIllegalArgumentException() - .isThrownBy(() -> { - run("자바프로그래밍언어-3-E0", "한동인성교육-1-P"); - run("자바프로그래밍언어-3-A+", "한동인성교육-1-PD"); - }); - } - - @Test - void 예외처리_잘못된_학점_입력() { - assertThatIllegalArgumentException() - .isThrownBy(() -> { - run("프로그래밍언어론-5-A0", "한동인성교육-1-P"); - run("프로그래밍언어론-3-A0", "한동인성교육-0-P"); - }); - } - - @Override - protected void runMain() { - Application.main(new String[]{}); - } -} +//import static org.assertj.core.api.Assertions.assertThat; +//import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +// +//import camp.nextstep.edu.missionutils.test.NsTest; +//import gpacalc.Application; +//import org.junit.jupiter.api.Test; +// +//public class ApplicationTest extends NsTest { +// +// @Test +// void 평점평균_계산() { +// run("데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+", +// "미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F"); +// assertThat(output()).contains( +// "<과목 목록>", +// "[전공] 데이타구조,3,A0", +// "[전공] 자바프로그래밍언어,3,B+", +// "[전공] 컴퓨터구조,3,C0", +// "[전공] 컴퓨터네트워크,3,D+", +// "[교양] 미술의이해,3,P", +// "[교양] 교양특론3,1,NP", +// "[교양] 기독교의이해,2,F", +// "<취득학점>", +// "15학점", +// "<평점평균>", +// "2.36 / 4.5", +// "<전공 평점평균>", +// "2.75 / 4.5" +// ); +// } +// +// @Test +// void 예외처리_잘못된_성적_입력() { +// assertThatIllegalArgumentException() +// .isThrownBy(() -> { +// run("자바프로그래밍언어-3-E0", "한동인성교육-1-P"); +// run("자바프로그래밍언어-3-A+", "한동인성교육-1-PD"); +// }); +// } +// +// @Test +// void 예외처리_잘못된_학점_입력() { +// assertThatIllegalArgumentException() +// .isThrownBy(() -> { +// run("프로그래밍언어론-5-A0", "한동인성교육-1-P"); +// run("프로그래밍언어론-3-A0", "한동인성교육-0-P"); +// }); +// } +// +// @Override +// protected void runMain() { +// Application.main(new String[]{}); +// } +//} From f2965dca85f05b377ffddc660e55c2ee2c9ff20a Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Tue, 16 Jan 2024 23:06:14 +0900 Subject: [PATCH 2/9] =?UTF-8?q?edit:=20=EB=B3=80=EC=88=98=EB=AA=85=20camel?= =?UTF-8?q?case=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gpacalc/Application.java | 4 +- src/main/java/gpacalc/Calculator.java | 186 ++++++++++++++++--------- src/main/java/gpacalc/Input.java | 8 +- src/main/java/gpacalc/ParseString.java | 10 +- src/test/java/ApplicationTest.java | 108 +++++++------- 5 files changed, 184 insertions(+), 132 deletions(-) diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 8ef7abf..b841cfd 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -26,7 +26,6 @@ public static void main(String[] args) { String major_score = input.getMajorInput(); String elec_score = input.getElectiveInput(); - // 2. ParseString parseString = new ParseString(); @@ -42,14 +41,13 @@ public static void main(String[] args) { // 4. calculator.printSum(); - calculator.printAll(); + calculator.printAverage(); //데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+ //미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F System.out.println("=== [System] : 계산이 끝났습니다. ==="); - System.exit(0); } } diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java index 29f16f2..1b512a1 100644 --- a/src/main/java/gpacalc/Calculator.java +++ b/src/main/java/gpacalc/Calculator.java @@ -5,52 +5,38 @@ public class Calculator { - double total_score = 0; // 총 취득 학점 - double major_total_score = 0; - double major_sum = 0; - double elec_sum = 0; + double totalScore = 0; + double majorTotalScore = 0; + double majorSum = 0; + double elecSum = 0; - double grade =0; - double tmp; + double grade = 0; + double credit; double rate; - public void printMajorInput(List info_maj) { - for (int i = 0; i < info_maj.size(); i++) { - if (i % 3 == 0) - if (info_maj.get(i) == null || info_maj.get(i).length() > 10) { - throw new IllegalArgumentException(); - } - System.out.print("[전공] "); - - System.out.print(info_maj.get(i)); + public void printMajorInput(List infoMaj) { + for (int i = 0; i < infoMaj.size(); i++) { + if (i % 3 == 0) { + System.out.print("[전공] "); + checkNamingException(infoMaj.get(i)); + } + System.out.print(infoMaj.get(i)); if (i % 3 == 1) { - tmp = Integer.parseInt(info_maj.get(i)); - if (tmp < 1 || tmp > 4) { - throw new IllegalArgumentException(); - } - - if (!info_maj.get(i + 1).equals("F") && !info_maj.get(i + 1).equals("NP")) { - total_score += tmp; - major_total_score += tmp; - - } - - if (!info_maj.get(i + 1).equals("P") && !info_maj.get(i + 1).equals("NP")) - grade += tmp; - + credit = Integer.parseInt(infoMaj.get(i)); + checkScoreException(credit); + calculateTotalScore(infoMaj.get(i + 1)); System.out.print(","); - } else if (i % 3 == 2) { + } + else if (i % 3 == 2) { System.out.println(); - if (!Arrays.asList("A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F","P","NP").contains(info_maj.get(i))) { - throw new IllegalArgumentException(); - } - if (!info_maj.get(i).equals("P") && !info_maj.get(i).equals("NP")) { - switch (info_maj.get(i)) { + checkRateException(infoMaj.get(i)); + if (!infoMaj.get(i).equals("P") && !infoMaj.get(i).equals("NP")) { + switch (infoMaj.get(i)) { case "A+": rate = 4.5; break; @@ -80,49 +66,41 @@ public void printMajorInput(List info_maj) { break; } - addMajorScore(tmp, rate); + addMajorScore(credit, rate); } } else { System.out.print(","); } } + } - } - public void printElecInput(List info_elec) { - for (int i = 0; i < info_elec.size(); i++) { - if (i % 3 == 0) - if (info_elec.get(i) == null || info_elec.get(i).length() > 10) { - throw new IllegalArgumentException(); - } - System.out.print("[교양] "); + public void printElecInput(List infoElec) { - System.out.print(info_elec.get(i)); + for (int i = 0; i < infoElec.size(); i++) { + if (i % 3 == 0) { + System.out.print("[교양] "); + checkNamingException(infoElec.get(i)); + } + System.out.print(infoElec.get(i)); if (i % 3 == 1) { - tmp = Integer.parseInt(info_elec.get(i)); - if (tmp < 1 || tmp > 4) { - throw new IllegalArgumentException(); - } - if (!info_elec.get(i + 1).equals("F") && !info_elec.get(i + 1).equals("NP")) - total_score += tmp; + credit = Integer.parseInt(infoElec.get(i)); + checkScoreException(credit); - if (!info_elec.get(i + 1).equals("P") && !info_elec.get(i + 1).equals("NP")) - grade += tmp; + calculateTotalScore(infoElec.get(i + 1)); - System.out.print(","); + System.out.print(","); } else if (i % 3 == 2) { System.out.println(); - if (!Arrays.asList("A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F","P","NP").contains(info_elec.get(i))) { - throw new IllegalArgumentException(); - } - if (!info_elec.get(i).equals("P") && !info_elec.get(i).equals("NP")) { - switch (info_elec.get(i)) { + checkRateException(infoElec.get(i)); + if (!infoElec.get(i).equals("P") && !infoElec.get(i).equals("NP")) { + switch (infoElec.get(i)) { case "A+": rate = 4.5; break; @@ -152,7 +130,7 @@ public void printElecInput(List info_elec) { break; } - addElecScore(tmp, rate); + addElecScore(credit, rate); } } else { System.out.print(","); @@ -161,24 +139,100 @@ public void printElecInput(List info_elec) { } + public boolean isRightScore(double userScore) { + boolean rightScore = true; + + if (userScore < 1 || userScore > 4) { + rightScore = false; + } + + return rightScore; + } + + + public boolean isWrongInput(String lectureName) { + boolean moreThanTen = true; + + if (lectureName.isBlank() || lectureName.length() > 10) { + moreThanTen = false; + } + + return moreThanTen; + } + + + public void calculateTotalScore(String lectureType) { + if (!lectureType.equals("F") && !lectureType.equals("NP")) + totalScore += credit; + + if (!lectureType.equals("P") && !lectureType.equals("NP")) + grade += credit; + } + + + public void checkNamingException(String lectureName) { + try { + if (!isWrongInput(lectureName)) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println("\n === 올바르지 않은 과목명입니다. ==="); + System.exit(0); + } + } + + public void checkScoreException(double lectureScore) { + try { + if (!isRightScore(lectureScore)) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println("\n === 학점이 올바르지 않습니다. ==="); + System.exit(0); + } + } + + public void checkRateException(String userGrade) { + try { + if (hasRightScore(userGrade)) { + throw new IllegalArgumentException(); + } + } catch (IllegalArgumentException e) { + System.out.println("\n === 성적이 올바르지 않습니다. ==="); + System.exit(0); + + } + } + + + public boolean hasRightScore(String score) { + boolean isRightScore = true; + + if (Arrays.asList("A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F", "P", "NP").contains(score)) { + isRightScore = false; + } + + return isRightScore; + } + public void printSum() { System.out.println("\n <취득 학점>"); - System.out.println(total_score + "학점"); + System.out.println(totalScore + "학점"); } private void addMajorScore(double score, double r) { - major_sum += score * r; + majorSum += score * r; } private void addElecScore(double score, double r) { - elec_sum += score * r; + elecSum += score * r; } - public void printAll() { - double avg = (major_sum + elec_sum) / grade; - double major_avg = major_sum / major_total_score; + public void printAverage() { + double avg = (majorSum + elecSum) / grade; + double major_avg = majorSum / majorTotalScore; System.out.println("<평점 평균>"); System.out.println(String.format("%.2f / 4.5", avg)); diff --git a/src/main/java/gpacalc/Input.java b/src/main/java/gpacalc/Input.java index 8b84f89..614423b 100644 --- a/src/main/java/gpacalc/Input.java +++ b/src/main/java/gpacalc/Input.java @@ -11,15 +11,15 @@ public String getMajorInput(){ System.out.println(" === 전공 과목명과 이수학점, 평점을 입력해주세요 === "); - String major_score = Console.readLine(); - return major_score; + String majorScore = Console.readLine(); + return majorScore; } public String getElectiveInput(){ System.out.println(" === 교양 과목명과 이수학점, 평점을 입력해주세요 === "); - String elec_score = Console.readLine(); - return elec_score; + String elecScore = Console.readLine(); + return elecScore; } } diff --git a/src/main/java/gpacalc/ParseString.java b/src/main/java/gpacalc/ParseString.java index 48ec04b..eb2a715 100644 --- a/src/main/java/gpacalc/ParseString.java +++ b/src/main/java/gpacalc/ParseString.java @@ -6,15 +6,15 @@ public class ParseString { - public List getSeparated(String a) { + public List getSeparated(String wholeInput) { - String[] spl_one = a.split(","); + String[] splFirst = wholeInput.split(","); List resultList = new ArrayList<>(); - for (String tmp : spl_one) { - String[] pro_hyp = tmp.split("-"); - resultList.addAll(Arrays.asList(pro_hyp)); + for (String resultSplit : splFirst) { + String[] result = resultSplit.split("-"); + resultList.addAll(Arrays.asList(result)); } return resultList; diff --git a/src/test/java/ApplicationTest.java b/src/test/java/ApplicationTest.java index f27c73b..38086f8 100644 --- a/src/test/java/ApplicationTest.java +++ b/src/test/java/ApplicationTest.java @@ -1,54 +1,54 @@ -//import static org.assertj.core.api.Assertions.assertThat; -//import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; -// -//import camp.nextstep.edu.missionutils.test.NsTest; -//import gpacalc.Application; -//import org.junit.jupiter.api.Test; -// -//public class ApplicationTest extends NsTest { -// -// @Test -// void 평점평균_계산() { -// run("데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+", -// "미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F"); -// assertThat(output()).contains( -// "<과목 목록>", -// "[전공] 데이타구조,3,A0", -// "[전공] 자바프로그래밍언어,3,B+", -// "[전공] 컴퓨터구조,3,C0", -// "[전공] 컴퓨터네트워크,3,D+", -// "[교양] 미술의이해,3,P", -// "[교양] 교양특론3,1,NP", -// "[교양] 기독교의이해,2,F", -// "<취득학점>", -// "15학점", -// "<평점평균>", -// "2.36 / 4.5", -// "<전공 평점평균>", -// "2.75 / 4.5" -// ); -// } -// -// @Test -// void 예외처리_잘못된_성적_입력() { -// assertThatIllegalArgumentException() -// .isThrownBy(() -> { -// run("자바프로그래밍언어-3-E0", "한동인성교육-1-P"); -// run("자바프로그래밍언어-3-A+", "한동인성교육-1-PD"); -// }); -// } -// -// @Test -// void 예외처리_잘못된_학점_입력() { -// assertThatIllegalArgumentException() -// .isThrownBy(() -> { -// run("프로그래밍언어론-5-A0", "한동인성교육-1-P"); -// run("프로그래밍언어론-3-A0", "한동인성교육-0-P"); -// }); -// } -// -// @Override -// protected void runMain() { -// Application.main(new String[]{}); -// } -//} +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; + +import camp.nextstep.edu.missionutils.test.NsTest; +import gpacalc.Application; +import org.junit.jupiter.api.Test; + +public class ApplicationTest extends NsTest { + + @Test + void 평점평균_계산() { + run("데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+", + "미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F"); + assertThat(output()).contains( + "<과목 목록>", + "[전공] 데이타구조,3,A0", + "[전공] 자바프로그래밍언어,3,B+", + "[전공] 컴퓨터구조,3,C0", + "[전공] 컴퓨터네트워크,3,D+", + "[교양] 미술의이해,3,P", + "[교양] 교양특론3,1,NP", + "[교양] 기독교의이해,2,F", + "<취득학점>", + "15학점", + "<평점평균>", + "2.36 / 4.5", + "<전공 평점평균>", + "2.75 / 4.5" + ); + } + + @Test + void 예외처리_잘못된_성적_입력() { + assertThatIllegalArgumentException() + .isThrownBy(() -> { + run("자바프로그래밍언어-3-E0", "한동인성교육-1-P"); + run("자바프로그래밍언어-3-A+", "한동인성교육-1-PD"); + }); + } + + @Test + void 예외처리_잘못된_학점_입력() { + assertThatIllegalArgumentException() + .isThrownBy(() -> { + run("프로그래밍언어론-5-A0", "한동인성교육-1-P"); + run("프로그래밍언어론-3-A0", "한동인성교육-0-P"); + }); + } + + @Override + protected void runMain() { + Application.main(new String[]{}); + } +} From 2ef59a6dd1e8c6c41247c795aa99e218d15f2dd7 Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Tue, 16 Jan 2024 23:07:13 +0900 Subject: [PATCH 3/9] =?UTF-8?q?refacor:=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gpacalc/Calculator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java index 1b512a1..52f2a01 100644 --- a/src/main/java/gpacalc/Calculator.java +++ b/src/main/java/gpacalc/Calculator.java @@ -71,6 +71,7 @@ else if (i % 3 == 2) { } else { System.out.print(","); + } } } From 04a2e55c2db7f4c4e64d268e8391e68b0b56ce8b Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Tue, 16 Jan 2024 23:08:00 +0900 Subject: [PATCH 4/9] =?UTF-8?q?refacor:=20=EA=B8=B4=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC(=EA=B0=9C=EC=84=A0=20=ED=95=84=EC=9A=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gpacalc/Calculator.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java index 52f2a01..1b512a1 100644 --- a/src/main/java/gpacalc/Calculator.java +++ b/src/main/java/gpacalc/Calculator.java @@ -71,7 +71,6 @@ else if (i % 3 == 2) { } else { System.out.print(","); - } } } From 74f1200c9dc917e49f22a1c6cccb75701a16b5b4 Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Tue, 16 Jan 2024 23:12:59 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refacor:=20=EA=B3=84=EC=82=B0,=EC=98=88?= =?UTF-8?q?=EC=99=B8=EB=A1=9C=EC=A7=81=EC=9D=84=20=EB=8B=A8=EC=9D=BC=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gpacalc/Application.java | 15 ++++++++++----- src/main/java/gpacalc/Calculator.java | 8 +++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index b841cfd..02676b4 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -19,29 +19,34 @@ public static void main(String[] args) { // 4. 이쁘게 출력 -// 1. +// 1. (완료) Input input = new Input(); String major_score = input.getMajorInput(); String elec_score = input.getElectiveInput(); -// 2. + +// 2. (완료) ParseString parseString = new ParseString(); List major_result = parseString.getSeparated(major_score); List elec_result = parseString.getSeparated(elec_score); -// 3. +// 3. (완료) + System.out.println("<과목 목록>"); Calculator calculator = new Calculator(); calculator.printMajorInput(major_result); calculator.printElecInput(elec_result); -// 4. +// 4. (완료) + calculator.printSum(); - calculator.printAverage(); + calculator.printAverageOfAll(); + calculator.printAverageOfMajor(); + //데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+ //미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java index 1b512a1..cc4d9b3 100644 --- a/src/main/java/gpacalc/Calculator.java +++ b/src/main/java/gpacalc/Calculator.java @@ -230,14 +230,16 @@ private void addElecScore(double score, double r) { } - public void printAverage() { + public void printAverageOfAll() { double avg = (majorSum + elecSum) / grade; - double major_avg = majorSum / majorTotalScore; - System.out.println("<평점 평균>"); System.out.println(String.format("%.2f / 4.5", avg)); + } + + public void printAverageOfMajor() { + double major_avg = majorSum / majorTotalScore; System.out.println("<전공 평점 평균>"); System.out.println(String.format("%.2f / 4.5", major_avg)); } From cba8de09806db3c538bd58f9d70f3115c5ad8731 Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Tue, 16 Jan 2024 23:45:49 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refacor:=20=ED=8F=89=EC=A0=90=ED=8F=89?= =?UTF-8?q?=EA=B7=A0=5F=EA=B3=84=EC=82=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=ED=86=B5=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gpacalc/Calculator.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java index cc4d9b3..e495b9e 100644 --- a/src/main/java/gpacalc/Calculator.java +++ b/src/main/java/gpacalc/Calculator.java @@ -29,6 +29,7 @@ public void printMajorInput(List infoMaj) { checkScoreException(credit); calculateTotalScore(infoMaj.get(i + 1)); + calculateTotalMajorScore(infoMaj.get(i + 1)); System.out.print(","); } @@ -160,6 +161,11 @@ public boolean isWrongInput(String lectureName) { return moreThanTen; } + public void calculateTotalMajorScore(String lectureType) { + if (!lectureType.equals("F") && !lectureType.equals("NP")) + majorTotalScore += credit; + + } public void calculateTotalScore(String lectureType) { if (!lectureType.equals("F") && !lectureType.equals("NP")) @@ -216,8 +222,8 @@ public boolean hasRightScore(String score) { } public void printSum() { - System.out.println("\n <취득 학점>"); - System.out.println(totalScore + "학점"); + System.out.println("\n <취득학점>"); + System.out.println((int)totalScore + "학점"); } private void addMajorScore(double score, double r) { @@ -232,15 +238,15 @@ private void addElecScore(double score, double r) { public void printAverageOfAll() { double avg = (majorSum + elecSum) / grade; - System.out.println("<평점 평균>"); + System.out.println("<평점평균>"); System.out.println(String.format("%.2f / 4.5", avg)); } public void printAverageOfMajor() { - double major_avg = majorSum / majorTotalScore; - System.out.println("<전공 평점 평균>"); + double major_avg = majorSum / majorTotalScore; //majorTotalScore + System.out.println("<전공 평점평균>"); System.out.println(String.format("%.2f / 4.5", major_avg)); } From 9a5dbf578fe7920c826625fde1506e8675b4f6dc Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Wed, 17 Jan 2024 01:35:03 +0900 Subject: [PATCH 7/9] =?UTF-8?q?feat:=ED=85=8C=EC=8A=A4=ED=8A=B8=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC-=20=EA=B3=BC=EB=AA=A9=EB=AA=85=20=EA=B3=B5=EB=B0=B1?= =?UTF-8?q?=20=EA=B2=80=EC=82=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/gpacalc/CalculatorTest.java | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/test/java/gpacalc/CalculatorTest.java diff --git a/src/test/java/gpacalc/CalculatorTest.java b/src/test/java/gpacalc/CalculatorTest.java new file mode 100644 index 0000000..e0c0870 --- /dev/null +++ b/src/test/java/gpacalc/CalculatorTest.java @@ -0,0 +1,52 @@ +package gpacalc; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CalculatorTest { + + //given + Calculator calculator; + + @BeforeEach + void setUp() { + calculator = new Calculator(); + } + + @AfterEach + void tearDown() { + calculator = null; + } + + @DisplayName("과목명 공백 검사") + @Test + void checkNamingException() { + String input=" "; + + assertThrows(IllegalArgumentException.class, () -> { + calculator.checkNamingException(input); + }); + } + + @DisplayName("학점 범위 검사") + @Test + void checkScoreException() { + + } + + @DisplayName("성적 범위 검사") + @Test + void checkRateException() { + + } + + @DisplayName("평점평균 계산") + @Test + void printAverageOfAll() { + + } +} \ No newline at end of file From b87d26aad8f69062d6e3e7a00b46fe247a7b6895 Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Wed, 17 Jan 2024 01:39:37 +0900 Subject: [PATCH 8/9] =?UTF-8?q?feat:=ED=85=8C=EC=8A=A4=ED=8A=B8=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC-=20=ED=95=99=EC=A0=90=20=20=EB=B2=94=EC=9C=84=20?= =?UTF-8?q?=EA=B2=80=EC=82=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/gpacalc/CalculatorTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/gpacalc/CalculatorTest.java b/src/test/java/gpacalc/CalculatorTest.java index e0c0870..fb130c4 100644 --- a/src/test/java/gpacalc/CalculatorTest.java +++ b/src/test/java/gpacalc/CalculatorTest.java @@ -25,8 +25,10 @@ void tearDown() { @DisplayName("과목명 공백 검사") @Test void checkNamingException() { + //when String input=" "; + //then assertThrows(IllegalArgumentException.class, () -> { calculator.checkNamingException(input); }); @@ -36,6 +38,14 @@ void checkNamingException() { @Test void checkScoreException() { + //when + int score = 7; + + //then + assertThrows(IllegalArgumentException.class, () -> { + calculator.checkScoreException(score); + }); + } @DisplayName("성적 범위 검사") From dc15fc103dcf719d355951f25a48dfdd7ce4e577 Mon Sep 17 00:00:00 2001 From: K-Minhyeok Date: Thu, 18 Jan 2024 10:18:53 +0900 Subject: [PATCH 9/9] =?UTF-8?q?feat:=EA=B3=84=EC=82=B0=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=91=90=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gpacalc/Application.java | 17 ++- src/main/java/gpacalc/Calculator.java | 172 ++++++++++++++++++---- src/test/java/gpacalc/CalculatorTest.java | 36 ++++- 3 files changed, 182 insertions(+), 43 deletions(-) diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 02676b4..b4ad342 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -23,29 +23,32 @@ public static void main(String[] args) { Input input = new Input(); - String major_score = input.getMajorInput(); - String elec_score = input.getElectiveInput(); + String majorScore = input.getMajorInput(); + String elecScore = input.getElectiveInput(); // 2. (완료) ParseString parseString = new ParseString(); - List major_result = parseString.getSeparated(major_score); - List elec_result = parseString.getSeparated(elec_score); + List majorResult = parseString.getSeparated(majorScore); + List elecResult = parseString.getSeparated(elecScore); // 3. (완료) System.out.println("<과목 목록>"); Calculator calculator = new Calculator(); - calculator.printMajorInput(major_result); - calculator.printElecInput(elec_result); + calculator.initializeNumbers(); + calculator.calculateMajorInputIntoGPA(majorResult); + calculator.printMajorInput(majorResult); + calculator.calculateElecInputIntoGPA(elecResult); + calculator.printElecInput(elecResult); // 4. (완료) calculator.printSum(); calculator.printAverageOfAll(); - calculator.printAverageOfMajor(); + calculator.printAverageOfMajor(calculator.calculateMajorInputIntoGPA(majorResult)); //데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+ diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java index e495b9e..0e5dda7 100644 --- a/src/main/java/gpacalc/Calculator.java +++ b/src/main/java/gpacalc/Calculator.java @@ -5,14 +5,20 @@ public class Calculator { - double totalScore = 0; - double majorTotalScore = 0; - double majorSum = 0; - double elecSum = 0; - double grade = 0; - double credit; - double rate; + private double totalScore; + private double majorTotalScore; + private double majorSum; + private double elecSum; + + private double grade; + private double credit; + private double rate; + + private final int minScore = 1; + private final int maxScore = 4; + + private final int maxLength = 10; public void printMajorInput(List infoMaj) { @@ -22,19 +28,39 @@ public void printMajorInput(List infoMaj) { System.out.print("[전공] "); checkNamingException(infoMaj.get(i)); } - System.out.print(infoMaj.get(i)); + System.out.print(infoMaj.get(i) + ","); if (i % 3 == 1) { credit = Integer.parseInt(infoMaj.get(i)); checkScoreException(credit); + + } else if (i % 3 == 2) { + System.out.println(); + checkRateException(infoMaj.get(i)); + + + } + } + + } + + public double calculateMajorInputIntoGPA(List infoMaj) { + + + for (int i = 0; i < infoMaj.size(); i++) { + if (i % 3 == 0) { + checkNamingException(infoMaj.get(i)); + } + + + if (i % 3 == 1) { + credit = Integer.parseInt(infoMaj.get(i)); + checkScoreException(credit); calculateTotalScore(infoMaj.get(i + 1)); calculateTotalMajorScore(infoMaj.get(i + 1)); - System.out.print(","); + } else if (i % 3 == 2) { - } - else if (i % 3 == 2) { - System.out.println(); checkRateException(infoMaj.get(i)); if (!infoMaj.get(i).equals("P") && !infoMaj.get(i).equals("NP")) { switch (infoMaj.get(i)) { @@ -66,19 +92,14 @@ else if (i % 3 == 2) { rate = 0; break; } - addMajorScore(credit, rate); } - - } else { - System.out.print(","); } } + return majorSum / majorTotalScore; } - - public void printElecInput(List infoElec) { for (int i = 0; i < infoElec.size(); i++) { @@ -86,20 +107,42 @@ public void printElecInput(List infoElec) { System.out.print("[교양] "); checkNamingException(infoElec.get(i)); } - System.out.print(infoElec.get(i)); + System.out.print(infoElec.get(i) + ","); + if (i % 3 == 1) { credit = Integer.parseInt(infoElec.get(i)); checkScoreException(credit); - calculateTotalScore(infoElec.get(i + 1)); - System.out.print(","); + } else if (i % 3 == 2) { System.out.println(); checkRateException(infoElec.get(i)); + + } + } + + } + + public double calculateElecInputIntoGPA(List infoElec) { + + for (int i = 0; i < infoElec.size(); i++) { + if (i % 3 == 0) { + checkNamingException(infoElec.get(i)); + } + + if (i % 3 == 1) { + credit = Integer.parseInt(infoElec.get(i)); + checkScoreException(credit); + + calculateTotalScore(infoElec.get(i + 1)); + + } else if (i % 3 == 2) { + + checkRateException(infoElec.get(i)); if (!infoElec.get(i).equals("P") && !infoElec.get(i).equals("NP")) { switch (infoElec.get(i)) { case "A+": @@ -133,17 +176,24 @@ public void printElecInput(List infoElec) { addElecScore(credit, rate); } - } else { - System.out.print(","); } } + return (majorSum + elecSum) / grade; + } + + public void initializeNumbers() { + setTotalScore(0); + setMajorTotalScore(0); + setMajorSum(0); + setElecSum(0); + setGrade(0); } public boolean isRightScore(double userScore) { boolean rightScore = true; - if (userScore < 1 || userScore > 4) { + if (userScore < minScore || userScore > maxScore) { rightScore = false; } @@ -154,7 +204,7 @@ public boolean isRightScore(double userScore) { public boolean isWrongInput(String lectureName) { boolean moreThanTen = true; - if (lectureName.isBlank() || lectureName.length() > 10) { + if (lectureName.isBlank() || lectureName.length() > maxLength) { moreThanTen = false; } @@ -223,7 +273,7 @@ public boolean hasRightScore(String score) { public void printSum() { System.out.println("\n <취득학점>"); - System.out.println((int)totalScore + "학점"); + System.out.println((int) totalScore + "학점"); } private void addMajorScore(double score, double r) { @@ -244,10 +294,74 @@ public void printAverageOfAll() { } - public void printAverageOfMajor() { - double major_avg = majorSum / majorTotalScore; //majorTotalScore + public void printAverageOfMajor(double majorCalculateResult) { System.out.println("<전공 평점평균>"); - System.out.println(String.format("%.2f / 4.5", major_avg)); + System.out.println(String.format("%.2f / 4.5", majorCalculateResult)); + } + + + + + + + + + + + public double getTotalScore() { + return totalScore; + } + + public void setTotalScore(double totalScore) { + this.totalScore = totalScore; + } + + public double getMajorTotalScore() { + return majorTotalScore; + } + + public void setMajorTotalScore(double majorTotalScore) { + this.majorTotalScore = majorTotalScore; + } + + public double getMajorSum() { + return majorSum; + } + + public void setMajorSum(double majorSum) { + this.majorSum = majorSum; + } + + public double getElecSum() { + return elecSum; + } + + public void setElecSum(double elecSum) { + this.elecSum = elecSum; + } + + public double getGrade() { + return grade; + } + + public void setGrade(double grade) { + this.grade = grade; + } + + public double getCredit() { + return credit; + } + + public void setCredit(double credit) { + this.credit = credit; + } + + public double getRate() { + return rate; + } + + public void setRate(double rate) { + this.rate = rate; } } diff --git a/src/test/java/gpacalc/CalculatorTest.java b/src/test/java/gpacalc/CalculatorTest.java index fb130c4..1118af1 100644 --- a/src/test/java/gpacalc/CalculatorTest.java +++ b/src/test/java/gpacalc/CalculatorTest.java @@ -5,6 +5,11 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; class CalculatorTest { @@ -17,20 +22,16 @@ void setUp() { calculator = new Calculator(); } - @AfterEach - void tearDown() { - calculator = null; - } @DisplayName("과목명 공백 검사") @Test void checkNamingException() { //when - String input=" "; + String lectureName = " "; //then assertThrows(IllegalArgumentException.class, () -> { - calculator.checkNamingException(input); + calculator.checkNamingException(lectureName); }); } @@ -39,24 +40,45 @@ void checkNamingException() { void checkScoreException() { //when - int score = 7; + int score = 6; //then assertThrows(IllegalArgumentException.class, () -> { calculator.checkScoreException(score); }); + } @DisplayName("성적 범위 검사") @Test void checkRateException() { + //when + String grade = "E+"; + + //then + assertThrows(IllegalArgumentException.class, () -> { + calculator.checkRateException(grade); + }); } @DisplayName("평점평균 계산") @Test void printAverageOfAll() { + + List lectureDump = Arrays.asList("데이타구조", "3", "A0", "자바프로그래밍언어", "3", "B+", "컴퓨터구조", "3", "C0", + "네트워크", "3", "D+", "미술의이해", "3", "P", "교양특론", "1", "NP", "기독교의 이해", "2", "F"); + + assertEquals(2.357142857142857, calculator.calculateElecInputIntoGPA(lectureDump)); + } + + + @AfterEach + void tearDown() { + calculator = null; } + + } \ No newline at end of file