Skip to content
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
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ dependencies {

test {
useJUnitPlatform()
}
}

tasks.withType(JavaCompile){
options.encoding = "UTF-8"
}
13 changes: 13 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## 프로그램 진행 순서 ##
1. 전공 과목 정보 입력
2. 교양 과목 정보 입력
3. 과목 목록 출력
4. 취득 학점 출력
5. 평점 평균 줄력
6. 전공 평점 평균 출력

## 구현 기능 ##

1. 입력 받은 내용을 과목명, 학점, 성적으로 구분하여 배열에 저장
2. 입력 받은 내용을 기반으로 취득 학점 계산
3. 전공, 교양에 맞춰 평점 평균 구하는 기능 구현
27 changes: 26 additions & 1 deletion src/main/java/gpacalc/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
package gpacalc;

import camp.nextstep.edu.missionutils.Console;

public class Application {
public static void main(String[] args) {
//TODO: 구현
Method method = new Method();

System.out.println("전공 과목명과 이수학점, 평점을 입력해주세요(예시: 프로그래밍언어론-3-A+,소프트웨어공학-3-B+):");
String major = Console.readLine();
System.out.println("교양 과목명과 이수학점, 평점을 입력해주세요(예시: 선형대수학-3-C0,인간관계와자기성장-3-P):");
String liberal = Console.readLine();

System.out.println("<과목 목록>");
method.categorize(major, "전공");
method.categorize(liberal, "교양");
System.out.println();

int credit = method.getCredit(major) + method.getCredit(liberal);
System.out.println("<취득학점>");
System.out.println(credit + "학점\n");

System.out.println("<평점평균>");
System.out.println(method.calculate(major + "," + liberal ,credit) + " / 4.5\n");

System.out.println("<전공 평점평균>");
System.out.println(method.calculate(major, method.getCredit(major)) + " / 4.5\n");

return;
}
}

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

public class CheckException {
public void checkSubjectLength(String subject) {
if (checkBlank(subject) || checkMaxName(subject)){
throw new IllegalArgumentException();
}
}

public boolean checkMaxName(String subject){
int maxNameLength = 10;
return maxNameLength < subject.length();
}

public boolean checkBlank(String subject){
return subject.isBlank();
}

public void checkGradeRange(String Grade){
if(Grade.equals("A+") || Grade.equals("A0") || Grade.equals("B+") || Grade.equals("B0") || Grade.equals("C+") || Grade.equals("C0") || Grade.equals("D+") || Grade.equals("D0") || Grade.equals("F") || Grade.equals("P") || Grade.equals("NP") ) return;
throw new IllegalArgumentException();
}

public void checkCreditRange(int subject_credit){
int minCredit = 1,maxCredit = 4;
if (subject_credit < minCredit|| maxCredit < subject_credit)
throw new IllegalArgumentException("학점을 확인해주세요.");
}

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

public class Method {
CheckException checking = new CheckException();

public void categorize(String subjects, String major_liberal) {
String[] first_split = subjects.split(",");
String[][] second_split = new String[first_split.length][3];
for (int i = 0; i < first_split.length; i++) second_split[i] = first_split[i].split("-");

for (int i = 0; i < first_split.length; i++) {
checking.checkSubjectLength(second_split[i][0]);
checking.checkGradeRange(second_split[i][2]);

System.out.print("[" + major_liberal + "] ");
for (int j = 0; j < 3; j++) {
System.out.print(second_split[i][j]);
if (j != 2) System.out.print(",");
}
System.out.println();
}
}

public int getCredit(String input) {
int credit = 0;
String[] first_split = input.split(",");
String[][] second_split = new String[first_split.length][3];
for (int i = 0; i < first_split.length; i++) second_split[i] = first_split[i].split("-");

for (String[] strings : second_split) {
int subject_credit = Integer.parseInt(strings[1]);
checking.checkCreditRange(subject_credit);

if (strings[2].equals("F") || strings[2].equals("NP")) continue;
credit += Integer.parseInt(strings[1]);
}
return credit;
}

public static double calculate(String subjects, int credit){
String[] first_split = subjects.split(",");
String[][] second_split = new String[first_split.length][3];
for(int i = 0 ; i < first_split.length ; i++) second_split[i] = first_split[i].split("-");

double sum = 0;
for(int i = 0 ; i < first_split.length ; i++) {
if(second_split[i][2].equals("P")) credit -= Integer.parseInt(second_split[i][1]);
if(second_split[i][2].equals("F")) credit += Integer.parseInt(second_split[i][1]);
sum += (Double.parseDouble(second_split[i][1]) * Matching(second_split[i][2]));
}

double result = sum / credit;
result = Math.round(result*100) / (double)100 ;

return result;
}

public static double Matching(String grade){
double score = 0;
switch(grade){
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;
}

}
30 changes: 15 additions & 15 deletions src/test/java/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ public class ApplicationTest extends NsTest {
@Test
void 평점평균_계산() {
run("데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+",
"미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F");
"미술의이해-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"
"<과목 목록>",
"[전공] 데이타구조,3,A0",
"[전공] 자바프로그래밍언어,3,B+",
"[전공] 컴퓨터구조,3,C0",
"[전공] 컴퓨터네트워크,3,D+",
"[교양] 미술의이해,3,P",
"[교양] 교양특론3,1,NP",
"[교양] 기독교의이해,2,F",
"<취득학점>",
"15학점",
"<평점평균>",
"2.36 / 4.5",
"<전공 평점평균>",
"2.75 / 4.5"
);
}

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/gpacalc/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gpacalc;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

class ApplicationTest {
Method method = new Method();
CheckException checkException = new CheckException();

@Test
void checkSubjectLength() {
Exception exception = assertThrows(IllegalArgumentException.class, () ->
checkException.checkSubjectLength(" ")
);
}

@Test
void checkGradeRange() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
checkException.checkGradeRange("E0");
}
);
}

@Test
void checkCreditRange() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
checkException.checkCreditRange(0);
}
);

Exception exception1 = assertThrows(IllegalArgumentException.class, () -> {
checkException.checkCreditRange(5);
}
);
}

@Test
void calculate() {
assertThat(method.calculate("데이타구조-3-A0,자바프로그래밍언어-3-B+,컴퓨터구조-3-C0,컴퓨터네트워크-3-D+,미술의이해-3-P,교양특론3-1-NP,기독교의이해-2-F", 15)).as("계산에 오류가 있습니다.").isEqualTo(2.36);
}
}