Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12기] 이지광 과제 제출합니다. #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Sat Dec 30 05:45:58 KST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
7 changes: 0 additions & 7 deletions src/main/java/gpacalc/Application.java

This file was deleted.

76 changes: 76 additions & 0 deletions src/main/java/gpacalc/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package gpacalc;

public class Calculator {
int point = 0; //취득학점
int gradePoint = 0; //과목학점 총합
double allGrade = 0.0; //과목성적가중치 총합
int majorPoint = 0; //전공과목학점
double majorGrade = 0.0; //전공성적가중치

public int getPoint(String[][] major, String[][] general){
for (String[] score : major) {
if (!(score[2].equals("F") || score[2].equals("NP"))) {
point += Integer.parseInt(score[1]);
}
}
for (String[] score : general) {
if (!(score[2].equals("F") || score[2].equals("NP"))) {
point += Integer.parseInt(score[1]);
}
}
return point;
}

public double getAllAverage(String[][] major, String[][] general){
for (String[] point : major) {
if(!(point[2].equals("P")||point[2].equals("NP"))) {
double score = getScore(point[2]);
allGrade += calculateGPA(score, point[1]);
gradePoint += Integer.parseInt(point[1]);
}
}
for (String[] point : general) {
if(!(point[2].equals("P")||point[2].equals("NP"))) {
double score = getScore(point[2]);
allGrade += calculateGPA(score, point[1]);
gradePoint += Integer.parseInt(point[1]);
}
}
return allGrade/(double) gradePoint;
}

public double getMajorAverage(String[][] major){
for (String[] point : major) {
if(!(point[2].equals("P")||point[2].equals("NP"))) {
double score = getScore(point[2]);
majorGrade += calculateGPA(score, point[1]);
majorPoint += Integer.parseInt(point[1]);
}
}
return majorGrade/(double) majorPoint;
}

public double calculateGPA(double score, String point){
return score * (double) Integer.parseInt(point);
}

static double getScore(String scr) {
double score;

switch(scr){
case "A+" -> score = 4.5;
case "A0" -> score = 4.0;
case "B+" -> score = 3.5;
case "B0" -> score = 3.0;
case "C+" -> score = 2.5;
case "C0" -> score = 2.0;
case "D+" -> score = 1.5;
case "D0" -> score = 1.0;
case "F" -> score = 0;
case "P" -> score = 0;
case "NP" -> score = 0;
default -> throw new IllegalArgumentException("잘못된 입력입니다.");
}
return score;
}
}
36 changes: 36 additions & 0 deletions src/main/java/gpacalc/CheckingError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gpacalc;
public class CheckingError {

public void checkError(String[][] mem){
for (String[] num : mem) {
if(num[0].isBlank()){
throw new IllegalArgumentException("공백으로만 채워지면 안됩니다.");
}
if(num[0].length() > 10) {
throw new IllegalArgumentException("글자수가 10개가 넘어가면 안됩니다.");
}
if(Integer.parseInt(num[1]) > 4 || Integer.parseInt(num[1]) < 1) {
throw new IllegalArgumentException("전공 학점입력이 잘못되었습니다.");
}
checkScore(num[2]);
}
}

public void checkScore(String score){
boolean check;
switch(score){
case "A+" -> check = true;
case "A0" -> check = true;
case "B+" -> check = true;
case "B0" -> check = true;
case "C+" -> check = true;
case "C0" -> check = true;
case "D+" -> check = true;
case "D0" -> check = true;
case "F" -> check = true;
case "P" -> check = true;
case "NP" -> check = true;
default -> throw new IllegalArgumentException("잘못된 성적 입력입니다.");
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/gpacalc/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gpacalc;
import camp.nextstep.edu.missionutils.Console;

public class Input {

public String getMajorInput(){
System.out.println("전공 과목명과 이수학점, 평점을 입력해주세요(예시: 프로그래밍언어론-3-A+,소프트웨어공학-3-B+):");
return Console.readLine();
}

public String getElectiveInput(){
System.out.println("교양 과목명과 이수학점, 평점을 입력해주세요(예시: 선형대수학-3-C0,인간관계와자기성장-3-P):");
return Console.readLine();
}

public String[][] separateStr (String str){
String[] firstSplit = str.split(",");
String[][] lastSplit = new String[firstSplit.length][]; //전공과목 저장을 위한 배열(문자열 형태)
for (int i = 0; i < firstSplit.length; i++) {
lastSplit[i] = firstSplit[i].split("-");
}
return lastSplit;
}
}
31 changes: 31 additions & 0 deletions src/main/java/gpacalc/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gpacalc;

public class Main {
public static void main(String[] args) {

//받기
gpacalc.Input newStr = new Input();
String major = newStr.getMajorInput();
String general = newStr.getElectiveInput();

//나눠서 저장
String[][] majorInfo = newStr.separateStr(major);
String[][] generalInfo = newStr.separateStr(general);

//에러 체크
gpacalc.CheckingError errorCheck = new CheckingError();
errorCheck.checkError(majorInfo);
errorCheck.checkError(generalInfo);

//계산
gpacalc.Calculator calculator = new Calculator();
int point = calculator.getPoint(majorInfo,generalInfo);
double allAverage = calculator.getAllAverage(majorInfo,generalInfo);
double majorAverage = calculator.getMajorAverage(majorInfo);

//출력
gpacalc.Output printOutput = new Output();
printOutput.printInfo(majorInfo,generalInfo);
printOutput.printScore(point,allAverage,majorAverage);
}
}
25 changes: 25 additions & 0 deletions src/main/java/gpacalc/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gpacalc;

public class Output {

public void printInfo(String[][] major, String[][] general){
System.out.println("\n<과목 목록>");
for (String[] info : major) {
System.out.println("[전공] " + info[0] + "," + info[1] + "," + info[2]);
}
for (String[] info : general) {
System.out.println("[교양] " + info[0] + "," + info[1] + "," + info[2]);
}
}

public void printScore(int point, double allAve, double majorAve){
System.out.println("\n<취득학점>");
System.out.println(point + "학점");

System.out.println("\n<평점평균>");
System.out.println(String.format("%.2f", allAve) + " / 4.5");

System.out.println("\n<전공 평점평균>");
System.out.println(String.format("%.2f", majorAve) + " / 4.5");
}
}
41 changes: 41 additions & 0 deletions src/test/java/gpacalc/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gpacalc;

import static org.junit.jupiter.api.Assertions.*;

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 org.mockito.internal.matchers.Null;

import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {

Calculator calculator;

@BeforeEach
public void setUp() {
calculator = new Calculator();
}

@DisplayName("전체 학점 평균 테스트")
@Test
void allAverageTest() {
//when
String[][] input = {{"데이타구조","3","A0"},{"자바프로그래밍언어","3","B+"},{"컴퓨터구조","3","C0"},{"컴퓨터네트워크","3","D+"}};
double result = calculator.getAllAverage(input,input);
//then
assertEquals(2.75, result);
}

@DisplayName("전공 학점 평균 테스트")
@Test
void majorAverageTest() {
//when
String[][] input = {{"데이타구조","3","A0"},{"자바프로그래밍언어","3","B+"},{"컴퓨터구조","3","C0"},{"컴퓨터네트워크","3","D+"}};
double result = calculator.getMajorAverage(input);
//then
assertEquals(2.75, result);
}

}
63 changes: 63 additions & 0 deletions src/test/java/gpacalc/CheckingErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gpacalc;

import static org.junit.jupiter.api.Assertions.*;

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 org.mockito.internal.matchers.Null;

import static org.junit.jupiter.api.Assertions.*;

class CheckingErrorTest {

CheckingError checkingError;

@BeforeEach
public void setUp() {
checkingError = new CheckingError();
}

@DisplayName("공백 메서드 테스트")
@Test
void checkError() {
//when
String[][] num = {{" ", " "}, {" ", " "}};
//then
assertThrows(IllegalArgumentException.class, () -> {
checkingError.checkError(num);
});
}

@DisplayName("학점 범위 테스트")
@Test
void checkPointException() {

//when
String[][] mem = {{"데이타구조","5","A0"},{"자바프로그래밍언어","3","B+"},{"컴퓨터구조","3","C0"},{"컴퓨터네트워크","3","D+"}};

//then
assertThrows(IllegalArgumentException.class, () -> {
checkingError.checkError(mem);
});

}

@DisplayName("성적 범위 테스트")
@Test
void checkScoreException() {

//when
String[][] mem = {{"데이타구조","2","A0"},{"자바프로그래밍언어","3","P+"},{"컴퓨터구조","3","C0"},{"컴퓨터네트워크","3","D+"}};

//then
assertThrows(IllegalArgumentException.class, () -> {
checkingError.checkError(mem);
});

}
}



Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package gpacalc;

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 gpacalc.Main;
import org.junit.jupiter.api.Test;

public class ApplicationTest extends NsTest {
public class MainTest extends NsTest {

@Test
void 평점평균_계산() {
Expand Down Expand Up @@ -49,6 +51,6 @@ public class ApplicationTest extends NsTest {

@Override
protected void runMain() {
Application.main(new String[]{});
Main.main(new String[]{});
}
}
}