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

[13기] 홍주한 과제 제출합니다. #32

Open
wants to merge 3 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
49 changes: 49 additions & 0 deletions functionsREADME.md
Original file line number Diff line number Diff line change
@@ -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 제외한 모든 학점 합산해서 출력
- 과목평점 * 과목학점 / 과목학점의 총합 계산한 값 평점평균으로 출력
- 전공 평점평균만 출력

#### *전공과 교양을 나누어서 같은 과정을 수행
170 changes: 168 additions & 2 deletions src/main/java/gpacalc/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,173 @@
package gpacalc;

import java.util.ArrayList;
import camp.nextstep.edu.missionutils.Console;

public class Application {
ArrayList<Subject> majorList = new ArrayList<>();
ArrayList<Subject> liberalArtsList = new ArrayList<>();
double totalMajorAverageRating = 0;
double totalLiberalArtsAverageRating = 0;
int totalMajorCredit = 0;
int totalLiberalArtsCredit = 0;
int passCredit = 0;
int failCredit = 0;

public static void main(String[] args) {
//TODO: 구현
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);

System.out.println("교양 과목명과 이수학점, 평점을 입력해주세요(예시: 선형대수학-3-C0,인간관계와자기성장-3-P):");
app.input(app, app.liberalArtsList, false);

System.out.println("\n<과목 목록>");
app.printSubjects("[전공] ", app.majorList);
app.printSubjects("[교양] ", app.liberalArtsList);

app.printCalcResult(app);
}

public void input(Application app, ArrayList<Subject> subjectList, boolean majorSubjectStatus) {
String[] beforeSplitInput = Console.readLine().split(",");

app.creatingObject(app, beforeSplitInput, subjectList, majorSubjectStatus);
}

// input 나누는 것이 메소드를 최소 단위로 잘 나눈 것인지 의문
public void creatingObject(Application app, String[] beforeSplitInput, ArrayList<Subject> subjectList, boolean majorSubjectStatus) {
String[] subjectComponent;

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);
}
}

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) {
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);
}
}

public void creditCheck(int digit, Subject subject) {
try{
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);
}
}

public void gradeCheck(Application app, Subject subject, boolean majorSubject) {
try {
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 += subject.getCredit();
}
} else {
double grade = app.calcGradeStringToDouble(subject.getGrade());
if (grade == -1) {
System.out.println(subject.getGrade() + "grade == " + grade);
throw new IllegalArgumentException("과목성적을 잘못 입력하셨습니다.");
}
if (grade == 0){
app.failCredit += subject.getCredit();
}
else if (majorSubject){
app.totalMajorCredit += subject.getCredit();
app.totalMajorAverageRating += subject.getCredit() * grade;
}else{
app.totalLiberalArtsCredit += subject.getCredit();
app.totalLiberalArtsAverageRating += subject.getCredit() * grade;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
}

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 calcPAndFGradeStringToDouble(String grade) {
switch (grade) {
case "P":
return "Pass";
case "NP":
return "Not Passed";
default:
return "wrong value";
}
}

public void printSubjects(String subjectType, ArrayList<Subject> 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.totalMajorCredit + app.totalLiberalArtsCredit + app.passCredit + "학점");

System.out.println("\n<평점평균>");
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.totalMajorCredit)*100)/100.0 + " / 4.5");
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/gpacalc/Subject.java
Original file line number Diff line number Diff line change
@@ -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;
}
}