diff --git a/src/main/java/gpacalc/Application.java b/src/main/java/gpacalc/Application.java index 57e79e1..b4ad342 100644 --- a/src/main/java/gpacalc/Application.java +++ b/src/main/java/gpacalc/Application.java @@ -1,7 +1,61 @@ 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 majorScore = input.getMajorInput(); + String elecScore = input.getElectiveInput(); + + +// 2. (완료) + + ParseString parseString = new ParseString(); + List majorResult = parseString.getSeparated(majorScore); + List elecResult = parseString.getSeparated(elecScore); + + +// 3. (완료) + + System.out.println("<과목 목록>"); + Calculator calculator = new Calculator(); + calculator.initializeNumbers(); + calculator.calculateMajorInputIntoGPA(majorResult); + calculator.printMajorInput(majorResult); + calculator.calculateElecInputIntoGPA(elecResult); + calculator.printElecInput(elecResult); + +// 4. (완료) + + calculator.printSum(); + calculator.printAverageOfAll(); + calculator.printAverageOfMajor(calculator.calculateMajorInputIntoGPA(majorResult)); + + +//데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+ +//미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F + + + System.out.println("=== [System] : 계산이 끝났습니다. ==="); + } } diff --git a/src/main/java/gpacalc/Calculator.java b/src/main/java/gpacalc/Calculator.java new file mode 100644 index 0000000..0e5dda7 --- /dev/null +++ b/src/main/java/gpacalc/Calculator.java @@ -0,0 +1,367 @@ +package gpacalc; + +import java.util.Arrays; +import java.util.List; + +public class Calculator { + + + 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) { + + 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) { + 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)); + } else if (i % 3 == 2) { + + 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; + 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(credit, rate); + } + } + } + return majorSum / majorTotalScore; + } + + + public void printElecInput(List infoElec) { + + 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) { + credit = Integer.parseInt(infoElec.get(i)); + checkScoreException(credit); + + + + + } 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+": + 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(credit, rate); + } + } + } + + 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 < minScore || userScore > maxScore) { + rightScore = false; + } + + return rightScore; + } + + + public boolean isWrongInput(String lectureName) { + boolean moreThanTen = true; + + if (lectureName.isBlank() || lectureName.length() > maxLength) { + moreThanTen = false; + } + + 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")) + 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((int) totalScore + "학점"); + } + + private void addMajorScore(double score, double r) { + majorSum += score * r; + + } + + private void addElecScore(double score, double r) { + elecSum += score * r; + + } + + public void printAverageOfAll() { + double avg = (majorSum + elecSum) / grade; + System.out.println("<평점평균>"); + System.out.println(String.format("%.2f / 4.5", avg)); + + + } + + public void printAverageOfMajor(double majorCalculateResult) { + System.out.println("<전공 평점평균>"); + 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/main/java/gpacalc/Input.java b/src/main/java/gpacalc/Input.java new file mode 100644 index 0000000..614423b --- /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 majorScore = Console.readLine(); + return majorScore; + } + + public String getElectiveInput(){ + + System.out.println(" === 교양 과목명과 이수학점, 평점을 입력해주세요 === "); + + String elecScore = Console.readLine(); + return elecScore; + } +} diff --git a/src/main/java/gpacalc/ParseString.java b/src/main/java/gpacalc/ParseString.java new file mode 100644 index 0000000..eb2a715 --- /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 wholeInput) { + + String[] splFirst = wholeInput.split(","); + List resultList = new ArrayList<>(); + + + for (String resultSplit : splFirst) { + String[] result = resultSplit.split("-"); + resultList.addAll(Arrays.asList(result)); + } + + return resultList; + } +} diff --git a/src/test/java/gpacalc/CalculatorTest.java b/src/test/java/gpacalc/CalculatorTest.java new file mode 100644 index 0000000..1118af1 --- /dev/null +++ b/src/test/java/gpacalc/CalculatorTest.java @@ -0,0 +1,84 @@ +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 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 { + + //given + Calculator calculator; + + @BeforeEach + void setUp() { + calculator = new Calculator(); + } + + + @DisplayName("과목명 공백 검사") + @Test + void checkNamingException() { + //when + String lectureName = " "; + + //then + assertThrows(IllegalArgumentException.class, () -> { + calculator.checkNamingException(lectureName); + }); + } + + @DisplayName("학점 범위 검사") + @Test + void checkScoreException() { + + //when + 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