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기] 장세혁 과제 제출합니다. #27

Open
wants to merge 1 commit 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
188 changes: 187 additions & 1 deletion src/main/java/gpacalc/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,193 @@
package gpacalc;

import java.util.ArrayList;
import java.util.Arrays;
import camp.nextstep.edu.missionutils.Console;
import static camp.nextstep.edu.missionutils.Console.readLine;

public class Application {
public static void main(String[] args) {
//TODO: 구현
//TODO:
String[] mainSubjects;
String[] subSubjects;

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

mainSubjects = input.split(",");

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

subSubjects = input.split(",");

ArrayList<Subject> mainContent = new ArrayList<>();
ArrayList<Subject> subContent = new ArrayList<>();

Subject main = null;
Subject sub = null;
String[] temp;

for(String subject: mainSubjects){
temp = subject.split("-");
main = new Subject(temp[0], temp[1], temp[2]);
mainContent.add(main);
}

for(String subject: subSubjects){
temp = subject.split("-");
sub = new Subject(temp[0], temp[1], temp[2]);
subContent.add(sub);
}

for(Subject subject: mainContent){ // 전공과목의 argument validation
try{
if((!(subject.getGrade().equalsIgnoreCase("A+") || subject.getGrade().equalsIgnoreCase("A0")
|| subject.getGrade().equalsIgnoreCase("B+") || subject.getGrade().equalsIgnoreCase("B0")
|| subject.getGrade().equalsIgnoreCase("C+") || subject.getGrade().equalsIgnoreCase("C0")
|| subject.getGrade().equalsIgnoreCase("D+") || subject.getGrade().equalsIgnoreCase("D0")
|| subject.getGrade().equalsIgnoreCase("F") || subject.getGrade().equalsIgnoreCase("P")
|| subject.getGrade().equalsIgnoreCase("NP"))
) || (!(subject.getCredit().equalsIgnoreCase("1") || subject.getCredit().equalsIgnoreCase("2")
|| subject.getCredit().equalsIgnoreCase("3") || subject.getCredit().equalsIgnoreCase("4"))
) || (subject.getName().length() > 10)){
IllegalArgumentException IllegalArgumentException = new IllegalArgumentException();
throw IllegalArgumentException;
}
}catch (IllegalArgumentException IllegalArgumentException){
throw new IllegalArgumentException();
// System.exit(0);
}
}

for(Subject subject: subContent){ // 교양과목의 argument validation
try{
if((!(subject.getGrade().equalsIgnoreCase("A+") || subject.getGrade().equalsIgnoreCase("A0")
|| subject.getGrade().equalsIgnoreCase("B+") || subject.getGrade().equalsIgnoreCase("B0")
|| subject.getGrade().equalsIgnoreCase("C+") || subject.getGrade().equalsIgnoreCase("C0")
|| subject.getGrade().equalsIgnoreCase("D+") || subject.getGrade().equalsIgnoreCase("D0")
|| subject.getGrade().equalsIgnoreCase("F") || subject.getGrade().equalsIgnoreCase("P")
|| subject.getGrade().equalsIgnoreCase("NP"))
) || (!(subject.getCredit().equalsIgnoreCase("1") || subject.getCredit().equalsIgnoreCase("2")
|| subject.getCredit().equalsIgnoreCase("3") || subject.getCredit().equalsIgnoreCase("4"))
) || (subject.getName().length() > 10)){
Exception IllegalArgumentException = new Exception();
throw IllegalArgumentException;
}
}catch (Exception IllegalArgumentException){
System.exit(0);
}
}

int totalCredit = 0;
int mainCredits = 0;
int subCredits = 0;
double totalGrade = 0;
double averageGradeForAll = 0;

for(Subject subject: mainContent){
if(!(subject.getGrade().equalsIgnoreCase("NP") || subject.getGrade().equalsIgnoreCase("F"))){
totalCredit += Integer.parseInt(subject.getCredit());
}
}

for(Subject subject: subContent){
if(!(subject.getGrade().equalsIgnoreCase("NP") || subject.getGrade().equalsIgnoreCase("F"))){
totalCredit += Integer.parseInt(subject.getCredit());
}
}

for(Subject subject: mainContent){ //전공과목 성적가중치 계산
if(!(subject.getGrade().equalsIgnoreCase("P") || subject.getGrade().equalsIgnoreCase("NP"))){
mainCredits += Integer.parseInt(subject.getCredit());
switch (subject.getGrade()){
case "A+":
totalGrade += 4.5*(Integer.parseInt(subject.getCredit()));
break;
case "A0":
totalGrade += 4.0*(Integer.parseInt(subject.getCredit()));
break;
case "B+":
totalGrade += 3.5*(Integer.parseInt(subject.getCredit()));
break;
case "B0":
totalGrade += 3.0*(Integer.parseInt(subject.getCredit()));
break;
case "C+":
totalGrade += 2.5*(Integer.parseInt(subject.getCredit()));
break;
case "C0":
totalGrade += 2.0*(Integer.parseInt(subject.getCredit()));
break;
case "D+":
totalGrade += 1.5*(Integer.parseInt(subject.getCredit()));
break;
case "D0":
totalGrade += 1.0*(Integer.parseInt(subject.getCredit()));
break;
default:
totalGrade += 0;
break;
}
}
}

double averageGradeForMain = totalGrade/mainCredits; //전공과목 평점평균 계산

for(Subject subject: subContent){ //교양과목 성적가중치 계산
if(!(subject.getGrade().equalsIgnoreCase("P") || subject.getGrade().equalsIgnoreCase("NP"))){
subCredits += Integer.parseInt(subject.getCredit());
switch (subject.getGrade()){
case "A+":
totalGrade += 4.5*(Integer.parseInt(subject.getCredit()));
break;
case "A0":
totalGrade += 4.0*(Integer.parseInt(subject.getCredit()));
break;
case "B+":
totalGrade += 3.5*(Integer.parseInt(subject.getCredit()));
break;
case "B0":
totalGrade += 3.0*(Integer.parseInt(subject.getCredit()));
break;
case "C+":
totalGrade += 2.5*(Integer.parseInt(subject.getCredit()));
break;
case "C0":
totalGrade += 2.0*(Integer.parseInt(subject.getCredit()));
break;
case "D+":
totalGrade += 1.5*(Integer.parseInt(subject.getCredit()));
break;
case "D0":
totalGrade += 1.0*(Integer.parseInt(subject.getCredit()));
break;
default:
totalGrade += 0;
break;
}
}
}

averageGradeForAll = totalGrade/(mainCredits+subCredits); //전체과목 평균평점 계산

System.out.println("\n<과목 목록>");
for(Subject subject: mainContent){
System.out.println("[전공] " + subject.getName() + "," + subject.getCredit() + "," + subject.getGrade());
}
for(Subject subject: subContent){
System.out.println("[교양] " + subject.getName() + "," + subject.getCredit() + "," + subject.getGrade());
}

System.out.println("\n<취득학점>");
System.out.println(totalCredit + "학점");

System.out.println("\n<평점평균>");
System.out.println((double)Math.round(averageGradeForAll*100)/100 + " / 4.5");

System.out.println("\n<전공 평점평균>");
System.out.println((double)Math.round(averageGradeForMain*100)/100 + " / 4.5");

}

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

public class Subject {
private String name;
private String credit;
private String grade;

public Subject(String name, String credit, String grade){
this.name = name;
this.credit = credit;
this.grade = grade;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCredit() {
return credit;
}

public void setCredit(String credit) {
this.credit = credit;
}

public String getGrade() {
return grade;
}

public void setGrade(String grade) {
this.grade = grade;
}
}
1 change: 1 addition & 0 deletions src/test/java/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ protected void runMain() {
Application.main(new String[]{});
}
}