diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..26bccfd --- /dev/null +++ b/.gitignore @@ -0,0 +1,101 @@ +# Created by https://www.toptal.com/developers/gitignore/api/swift,xcode +# Edit at https://www.toptal.com/developers/gitignore?templates=swift,xcode + +### Swift ### +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## User settings +xcuserdata/ + +## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) +*.xcscmblueprint +*.xccheckout + +## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) +build/ +DerivedData/ +*.moved-aside +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 + +## Obj-C/Swift specific +*.hmap + +## App packaging +*.ipa +*.dSYM.zip +*.dSYM + +## Playgrounds +timeline.xctimeline +playground.xcworkspace + +# Swift Package Manager +# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. +# Packages/ +# Package.pins +# Package.resolved +# *.xcodeproj +# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata +# hence it is not needed unless you have added a package configuration file to your project +# .swiftpm + +.build/ + +# CocoaPods +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# Pods/ +# Add this line if you want to avoid checking in source code from the Xcode workspace +# *.xcworkspace + +# Carthage +# Add this line if you want to avoid checking in source code from Carthage dependencies. +# Carthage/Checkouts + +Carthage/Build/ + +# Accio dependency management +Dependencies/ +.accio/ + +# fastlane +# It is recommended to not store the screenshots in the git repo. +# Instead, use fastlane to re-generate the screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://docs.fastlane.tools/best-practices/source-control/#source-control + +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots/**/*.png +fastlane/test_output + +# Code Injection +# After new code Injection tools there's a generated folder /iOSInjectionProject +# https://github.com/johnno1962/injectionforxcode + +iOSInjectionProject/ + +### Xcode ### + +## Xcode 8 and earlier + +### Xcode Patch ### +*.xcodeproj/* +!*.xcodeproj/project.pbxproj +!*.xcodeproj/xcshareddata/ +!*.xcodeproj/project.xcworkspace/ +!*.xcworkspace/contents.xcworkspacedata +/*.gcno +**/xcshareddata/WorkspaceSettings.xcsettings + +# End of https://www.toptal.com/developers/gitignore/api/swift,xcode \ No newline at end of file diff --git a/Git_ Exercise.xcodeproj/project.pbxproj b/Git_ Exercise.xcodeproj/project.pbxproj index 424a5e0..e5e450d 100644 --- a/Git_ Exercise.xcodeproj/project.pbxproj +++ b/Git_ Exercise.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 2768CA08294F453B00990B98 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2768CA07294F453B00990B98 /* main.swift */; }; + F076A28E29514BD700F97617 /* ConsoleIO.swift in Sources */ = {isa = PBXBuildFile; fileRef = F076A28D29514BD700F97617 /* ConsoleIO.swift */; }; + F076A29029514C0400F97617 /* CreditManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F076A28F29514C0400F97617 /* CreditManager.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -25,6 +27,8 @@ /* Begin PBXFileReference section */ 2768CA04294F453B00990B98 /* Git_ Exercise */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Git_ Exercise"; sourceTree = BUILT_PRODUCTS_DIR; }; 2768CA07294F453B00990B98 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + F076A28D29514BD700F97617 /* ConsoleIO.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConsoleIO.swift; sourceTree = ""; }; + F076A28F29514C0400F97617 /* CreditManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CreditManager.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -58,6 +62,8 @@ isa = PBXGroup; children = ( 2768CA07294F453B00990B98 /* main.swift */, + F076A28F29514C0400F97617 /* CreditManager.swift */, + F076A28D29514BD700F97617 /* ConsoleIO.swift */, ); path = "Git_ Exercise"; sourceTree = ""; @@ -121,6 +127,8 @@ buildActionMask = 2147483647; files = ( 2768CA08294F453B00990B98 /* main.swift in Sources */, + F076A29029514C0400F97617 /* CreditManager.swift in Sources */, + F076A28E29514BD700F97617 /* ConsoleIO.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Git_ Exercise/ConsoleIO.swift b/Git_ Exercise/ConsoleIO.swift new file mode 100644 index 0000000..1841dfd --- /dev/null +++ b/Git_ Exercise/ConsoleIO.swift @@ -0,0 +1,125 @@ +// +// ConsoleIO.swift +// MyCreditManager_targ2t +// +// Created by 김보미 on 2022/12/06. +// + +import Foundation + + +/// 콘솔의 모든 입력 및 출력 요소를 관리하는 클래스이다. +class ConsoleIO { + + // MARK: - 메뉴 관련 메세지 출력 함수 목록 + /// 기능을 입력받기 위한 메뉴 안내를 콘솔에 출력하기 위한 함수이다. + func printAllMenu() { + print("원하는 기능을 입력해 주세요") + print("1: 학생추가, 2: 학생삭제, 3: 성적추가(변경), 4: 성적삭제, 5: 평점보기, X: 종료") + } + + /// 메뉴 선택 시 잘못된 입력을 하는 경우 에러 문구를 출력하는 함수이다. + func printMenuInputError() { + print("뭔가 입력이 잘못되었습니다. 1~5 사이의 숫자 혹은 X를 입력해주세요.") + } + + // MARK: - 기능 내부에서 반복적으로 사용되는 에러 메세지 출력 함수 목록 + /// 기능 내부에서 잘못된 입력을 하는 경우 동일 내용의 메세지 출력이 중복되므로 함수로 별도 구현하여 사용한다. + func printFuncInputError() { + print("입력이 잘못되었습니다. 다시 확인해주세요.") + } + + /// 없는 학생에 대한 에러 문구를 출력하는 함수이다. + func printStudentMissingError(name: String) { + print("\(name) 학생을 찾지 못했습니다.") + } + + // MARK: - 학생 추가 메뉴 관련 메세지 출력 함수 목록 + /// 학생 추가 메뉴 안내 문구를 출력하는 함수이다. + func printAddStudentGuid() { + print("추가할 학생의 이름을 입력해주세요") + } + + /// 학생 추가 메뉴 중 중복 에러 문구를 출력하는 함수이다. + func printAddStudentDuplicateError(name: String) { + print("\(name)은 이미 존재하는 학생입니다. 추가하지 않습니다.") + } + + /// 학생 추가가 정상적으로 실행된 후 안내 문구를 출력하는 함수이다. + func printAddStudentSuccess(name: String) { + print("\(name) 학생을 추가했습니다") + } + + // MARK: - 학생 삭제 메뉴 관련 메세지 출력 함수 목록 + /// 학생 삭제 메뉴 안내 문구를 출력하는 함수이다. + func printDeleteStudentGuid() { + print("삭제할 학생의 이름을 입력해주세요") + } + + /// 학생 삭제 메뉴에서 정상적으로 학생을 삭제한 후 안내 문구를 출력하는 함수이다. + func printDeleteStudentSuccess(name: String) { + print("\(name) 학생을 삭제하였습니다.") + } + + // MARK: - 성적 추가 메뉴 관련 메세지 출력 함수 목록 + /// 성적 추가 메뉴 안내 문구를 출력하는 함수이다. + func printAddGradeGuid() { + print("성적을 추가할 학생의 이름, 과목 이름, 성적(A+, A0, F 등)을 띄어쓰기로 구분하여 차례로 작성해 주세요.") + print("입력예) Mickey Swift A+") + print("만약에 학생의 성적 중 해당 과목이 존재한다면 기존 점수가 갱신됩니다.") + } + + /// 성적 추가 메뉴에서 정상적으로 성적을 추가(변경)한 후 안내 문구를 출력하는 함수 + func printAddGradeSuccess(name: String, subject: String, grade: String) { + print("\(name) 학생의 \(subject) 과목이 \(grade)로 추가(변경)되었습니다.") + } + + // MARK: - 성적 삭제 메뉴 관련 메세지 출력 함수 목록 + /// 성적 삭제 메뉴 안내 문구를 출력하는 함수이다. + func printDeleteGradeGuid() { + print("성적을 삭제할 학생의 이름, 과목 이름을 띄어쓰기로 구분하여 차례로 작성해 주세요.") + print("입력예) Mickey Swift") + } + + /// 성적 삭제 메뉴에서 정상적으로 성적을 추가(변경)한 후 안내 문구를 출력하는 함수이다. + func printDeleteGradeSuccess(name: String, subject: String) { + print("\(name) 학생의 \(subject) 과목의 성적이 삭제되었습니다.") + } + + // MARK: - 평점 메뉴 관련 메세지 출력 함수 목록 + /// 평점 메뉴 안내 문구를 출력하는 함수이다. + func printAverageGuid() { + print("평점을 알고싶은 학생의 이름을 입력해주세요") + } + + /// 평점 메뉴에서 과목들을 출력하는 함수이다. + func printAverageSubjectsSuccess(subject: String, grade: String) { + print("\(subject): \(grade)") + } + + /// 평점 메뉴에서 평점을 출력하는 함수이다. + func printAverageSuccess(average: Double) { + /// 소수점을 2자리까지만 보여주게 하는 String format을 사용한다. + print("평점: \(String(format: "%.2f", average))") + } + + // MARK: - 프로그램 종료 메세지 출력 함수 + /// 프로그램 종료 문구를 출력하는 함수이다. + func printQuitGuide() { + print("프로그램을 종료합니다...") + } + + // MARK: - 데이터 입력 관련 함수 + /// 사용자에게 데이터를 입력받는 함수이다. + func getInput() -> String { + /// FileHandle은 파일 디스크립터를 객체 지향 API로 감싼 얇은 래퍼로, 기본입력을 keyboard 변수에 할당한다. + /// availableData는 입력받은 데이터를 읽어오는 역할을 수행한다. + let inputData = FileHandle.standardInput.availableData + /// inputData의 데이터를 utf8형식의 String으로 Encoding하는 과정이다. + /// 데이터가 없을 수도 있으므로 없는 경우 공백을 반환하도록 언래핑한다. + let strData = String(data: inputData, encoding: String.Encoding.utf8) ?? "" + /// newline(엔터를 누르면 입력되는 새 줄)을 제거하고 남은 문자열을 return한다. + return strData.trimmingCharacters(in: CharacterSet.newlines) + } + +} diff --git a/Git_ Exercise/CreditManager.swift b/Git_ Exercise/CreditManager.swift new file mode 100644 index 0000000..405c0b5 --- /dev/null +++ b/Git_ Exercise/CreditManager.swift @@ -0,0 +1,398 @@ +// +// CreditManager.swift +// MyCreditManager_targ2t +// +// Created by 김보미 on 2022/12/06. +// + +import Foundation + +/// 메뉴의 타입을 정해주는 열거형 enum +enum OptionType: String { + case addStudent = "1" + case deleteStudent = "2" + case addGrade = "3" + case deleteGrade = "4" + case average = "5" + case quit = "X" + case unknown + + /// enum의 value에 따라서 실행해야 할 기능등을 매칭해주는 초기화함수 init + init(value: String) { + switch value { + case "1": self = .addStudent + case "2": self = .deleteStudent + case "3": self = .addGrade + case "4": self = .deleteGrade + case "5": self = .average + case "X": self = .quit + default: self = .unknown + } + } +} + +// MARK: - 전체적으로 사용될 배열과 상수들의 집합을 모아두었다. +/// 성적, 점수를 각각 Array로 구현 후 Dictionary로 Key-Value를 매칭시킨다. `평점에 활용 예정` +/// CaseIterable 프로토콜, CustomStringConvertible을 사용하는 경우 case로 처리 가능함! - Jason +let grade: [String] = ["A+", "A0", "B+", "B0", "C+", "C0", "D+", "D0", "F"] +let credit: [Double] = [4.5, 4, 3.5, 3, 2.5, 2, 1.5, 1, 0] +let creditByGrade = Dictionary(uniqueKeysWithValues: zip(grade, credit)) + +/// 학생 성적 목록의 저장형식으로 선언된 구조체이다. +struct StudentCreditList { + var name: String + var subject: [String] + var credit: [String] +} + +/// StudentCreditList 구조체를 따르는 배열을 선언한다. +var studentCreditList: [StudentCreditList] = [] + +// MARK: - CreditManager Class +class CreditManager { + + /// 콘솔의 모든 입력 및 출력 요소를 관리하는 클래스(ConsoleIO)를 상수로 선언하여 사용한다. + let consoleIO = ConsoleIO() + + /// 입력받은 옵션 타입을 value로 전환하는 과정을 함수로 선언하여 사용하였다. + func getOption(_ option: String) -> OptionType { + return (OptionType(value: option)) + } + + /// 실제 프로그램이 실행되는 동안 수행되는 함수이다. + func creditManager() { + + /// 콘솔 프로그램 종료 여부를 판단하기 위해 선언된 변수이다. + var isRun: Bool = true + + /// shouldQuit이 false일 동안 수행될 while 반복문이다. + /// while true가 되는 무한반복문 사용은 지양하는 것이 좋다 - Jason + while isRun { + /// while 반복문 시작과 동시에 사용할 수 있는 메뉴를 모두 print해준다. + consoleIO.printAllMenu() + + /// 메뉴에 대한 입력을 받는 상수를 선언하고 입력받은 내용을 할당한다. + let inputMenu = getOption(consoleIO.getInput()) + + /// 입력받은 메뉴를 기점으로 switch문으로 메뉴를 분기처리한다. + switch inputMenu { + + // MARK: - 학생 추가 기능 + case .addStudent: + /// 학생 추가 안내 문구를 출력한다. + consoleIO.printAddStudentGuid() + + /// 사용자에게 추가할 학생의 이름을 입력받는 상수를 선언하고 입력받은 내용을 할당한다. + let addStudentName: String = consoleIO.getInput() + + /// 학생 이름의 중복여부를 체크할 변수를 선언하고 기본값으로 false를 할당한다. + var checkDuplicateName: Bool = false + + /// studentCreditList의 요소 중 이름을 하나씩 꺼내서 입력된 이름과 같은지 확인하고 중복여부를 true로 바꾼다. + for studentCell in studentCreditList { + if addStudentName == studentCell.name { + checkDuplicateName = true + break + } + } + + /// 이름이 알파벳으로만 이루어졌는지 확인하기 위한 변수를 선언하고 기본값으로 true를 할당한다. + var isOnlyAlphabet: Bool = true + + /// 이름이 알파벳으로만 이루어져있는지 확인하는 예외처리 함수를 선언한다. + func checkNameIsOnlyAlphabet(_ name: String) { + + /// 정규식으로 이름이 알파벳으로만 이루어졌는지 확인하기 위한 조건 pattern을 선언하고 정규식을 할당한다. + let pattern = "^[a-zA-Z]*$" + + /// range를 통하여 pattern으로만 이루어져 있으면 해당 문자를 반환하고 + /// 알파벳이 아닌 숫자, 특수문자가 들어간 경우 nil을 반환한다. + let range = name.range(of: pattern, options: .regularExpression) + + /// 숫자, 특수문자가 들어간 경우에는 isOnlyAlphabet의 값을 false로 변환한다. + if range == nil { + isOnlyAlphabet = false + } + } + + /// 이름이 알파벳으로만 이루어져있는지 확인하는 예외처리 함수를 실행한다. + checkNameIsOnlyAlphabet(addStudentName) + + /// 예외처리 및 출력을 실행하는 구간이다. + if addStudentName == "" || !isOnlyAlphabet { + /// 1. 잘못된 입력(공백, 숫자)인 경우 에러 메세지를 출력한다. + consoleIO.printFuncInputError() + } else if checkDuplicateName == true { + /// 2. 이미 존재하는 학생을 추가하는 경우 에러 메세지를 출력한다. + consoleIO.printAddStudentDuplicateError(name: addStudentName) + } else { + /// 정상적으로 추가된 경우 배열에 추가하고 추가 안내 텍스트 출력한다. + let addNewStudent = StudentCreditList(name: "\(addStudentName)", subject: [], credit: []) + studentCreditList.append(addNewStudent) + consoleIO.printAddStudentSuccess(name: addStudentName) + } + /// 학생이 잘 추가되었는지 확인하기 위해 print문을 사용한다. +// print(studentCreditList) + + // MARK: - 학생 삭제 기능 + case .deleteStudent: + /// 학생 삭제 안내 문구를 출력한다. + consoleIO.printDeleteStudentGuid() + + /// 사용자에게 삭제할 학생의 이름을 입력받는 상수를 선언하고 입력받은 내용을 할당한다. + let deleteStudentName: String = consoleIO.getInput() + + /// 삭제할 곳의 Index 값을 가지고 있는 변수를 선언한다. + var deleteIndex: Int = 0 + + /// 예외처리 및 출력을 실행하는 if문을 선언한다. + if deleteStudentName == "" { + /// 1. 잘못된 입력(공백, 저장된 학생 이름이 없는 경우)인 경우 오류 메세지를 출력한다. + consoleIO.printFuncInputError() + } else if studentCreditList.count == 0 { + /// 배열이 비어있는 경우 학생이 없으므로 오류 메세지를 출력한다. + consoleIO.printStudentMissingError(name: deleteStudentName) + } else { + /// 정상적으로 삭제된 경우 배열에서 삭제하고 삭제 안내 텍스트 출력한다. + for studentCell in studentCreditList { + if studentCell.name == deleteStudentName { + studentCreditList.remove(at: deleteIndex) + consoleIO.printDeleteStudentSuccess(name: deleteStudentName) + + break + } else { + deleteIndex += 1 + if deleteIndex == studentCreditList.count { + /// 2. 없는 학생을 삭제하는 경우 오류 메세지를 출력한다. + consoleIO.printStudentMissingError(name: deleteStudentName) + } + } + } + + } + /// 학생이 잘 삭제되었는지 확인하기 위해 print문을 사용한다. +// print(studentCreditList) + + // MARK: - 성적 추가 기능 + case .addGrade: + /// 성적 추가 안내 문구를 출력한다. + consoleIO.printAddGradeGuid() + + /// 사용자에게 추가할 학생의 이름, 과목 이름, 성적을 입력받는 상수를 선언하고 입력받은 내용을 할당한다. + let addGradeInput: String = consoleIO.getInput() + + /// 띄어쓰기로 구분하여 입력받은 addGradeInput을 separator(공백문자) 기준으로 구분하여 배열로 만들어주는 split를 사용한 후 상수에 할당한다. + let splitAddGradeInput = addGradeInput.split(separator: " ") + + /// 학생이 이미 존재하는지를 판단하는 변수를 선언하고 기본값을 true로 할당한다. + var isStudentExist: Bool = true + + /// 학생이 존재하는 위치의 index를 가지게 될 변수를 선언한다. + var existIndex: Int = 0 + + /// 학생이 존재하는지를 판단할 함수를 선언한다. + func checkStudentExist() { + for studentCell in studentCreditList { + /// 입력을 학생 이름, 과목 이름, 성적 순서대로 받으므로 학생 이름을 받아오기 위해서는 index가 0인 것을 불러와야 한다. + if studentCell.name == splitAddGradeInput[0] { + break + } else { + existIndex += 1 + } + } + /// count한 index 값이 studentCreditList의 배열 길이와 같으면 학생이 존재하지 않으므로 존재하는지 판단하는 변수를 false로 전환한다. + if existIndex == studentCreditList.count { + isStudentExist = false + } + } + + /// 과목이 존재하는지를 판단하는 변수를 선언 후 false를 기본값으로 할당한다. + var isSubjectExist: Bool = false + + /// 과목의 위치 index를 가지는 변수를 선언한다. + var subjectIndex: Int = 0 + + /// 과목이 존재하는지를 판단할 함수를 선언한다. + func subjectExistCheck() { + for subject in studentCreditList[existIndex].subject { + if subject == splitAddGradeInput[1] { + isSubjectExist = true + break + } else { + subjectIndex += 1 + } + } + } + + /// 입력이 없는데 존재여부를 판단하게 되는 경우 에러가 발생하므로, 입력값이 있을 때만 확인하도록 if 조건문을 사용한다. + if addGradeInput != "" { + /// 학생이 존재하는지를 판단하는 함수 실행한다. + checkStudentExist() + + /// 과목이 존재하는지를 판단할 함수를 실행한다. + subjectExistCheck() + } + + /// 예외처리 및 출력을 실행하기 위해 if문을 선언한다. + if addGradeInput == "" || splitAddGradeInput.count != 3 || !isStudentExist { + /// 1. 잘못된 입력(공백, 학생이름/과목이름/성적 중 하나가 입력되지 않거나 초과 입력한 경우)인 경우 에러 메세지를 출력한다. + consoleIO.printFuncInputError() + } else if isSubjectExist { + /// 과목이 이미 존재하는 경우 성적만 바꿔준다. + studentCreditList[existIndex].credit[subjectIndex] = String(splitAddGradeInput[2]) + consoleIO.printAddGradeSuccess(name: String(splitAddGradeInput[0]), subject: String(splitAddGradeInput[1]), grade: String(splitAddGradeInput[2])) + } else { + /// 과목이 없지만 잘못된 입력이 아닌 경우 과목을 추가하고 성적도 추가해준다. + studentCreditList[existIndex].subject.append(String(splitAddGradeInput[1])) + studentCreditList[existIndex].credit.append(String(splitAddGradeInput[2])) + consoleIO.printAddGradeSuccess(name: String(splitAddGradeInput[0]), subject: String(splitAddGradeInput[1]), grade: String(splitAddGradeInput[2])) + } + + /// 성적이 잘 추가(변경)되었는지 확인하기 위해 print문을 사용한다. +// print(studentCreditList) + + // MARK: - 성적 삭제 기능 + case .deleteGrade: + /// 성적 삭제 안내 문구를 출력한다. + consoleIO.printDeleteGradeGuid() + + /// 사용자에게 삭제할 학생의 이름, 과목 이름을 입력받는 상수를 선언하고 입력받은 내용을 할당한다. + let deleteGradeInput: String = consoleIO.getInput() + + /// 띄어쓰기로 구분하여 입력받은 deleteGradeInput을 separator(공백문자) 기준으로 구분하여 배열로 만들어주는 split를 사용한 후 상수에 할당한다. + let splitDeleteGradeInput = deleteGradeInput.split(separator: " ") + + /// 학생이 이미 존재하는지를 판단하는 변수를 선언 후 기본값을 true로 선언한다. + var isStudentExist: Bool = true + + /// 학생이 존재하는 위치의 index를 가지게 될 변수를 선언한다. + var existIndex: Int = 0 + + /// 학생이 존재하는지를 판단할 함수를 선언한다. + func studentExistCheck() { + for studentCell in studentCreditList { + /// 입력받은 이름이 studentCreditList에 있는지를 확인한다. + if studentCell.name == splitDeleteGradeInput[0] { + break + } else { + existIndex += 1 + } + } + if existIndex == studentCreditList.count { + isStudentExist = false + } + } + + /// 과목이 존재하는지를 판단하는 변수를 선언하고 true를 할당한다. + var isSubjectExist: Bool = true + + /// 과목의 위치 index를 가지는 변수를 선언한다. + var subjectIndex: Int = 0 + + /// 과목이 존재하는지, 몇 번째 인덱스에 있는지 찾아내는 함수를 선언한다. + func subjectExistCheck() { + for subject in studentCreditList[existIndex].subject { + if subject == splitDeleteGradeInput[1] { + break + } else { + subjectIndex += 1 + } + } + if subjectIndex == studentCreditList[existIndex].subject.count { + isSubjectExist = false + } + } + + /// 입력이 없는데 존재여부를 판단하게 되는 경우 Index out of range 에러가 발생하므로, 입력값이 있을 때만 확인하도록 if 조건문을 사용한다. + if deleteGradeInput != "" { + /// 학생이 존재하는지를 판단할 함수를 호출한다. + studentExistCheck() + if isStudentExist == true { + /// 학생이 존재한다면 과목이 존재하는지, 몇 번째 인덱스에 있는지 찾아내는 함수를 호출한다. + subjectExistCheck() + } + } + + // 예외처리 및 출력을 실행하는 구간 + if deleteGradeInput == "" || splitDeleteGradeInput.count != 2 || !isSubjectExist { + consoleIO.printFuncInputError() + } else if !isStudentExist { + consoleIO.printStudentMissingError(name: String(splitDeleteGradeInput[0])) + } else { + studentCreditList[existIndex].subject.remove(at: subjectIndex) + studentCreditList[existIndex].credit.remove(at: subjectIndex) + consoleIO.printDeleteGradeSuccess(name: String(splitDeleteGradeInput[0]), subject: String(splitDeleteGradeInput[1])) + } + + /// 성적이 잘 삭제되었는지 확인하기 위해 print문을 사용한다. +// print(studentCreditList) + + // MARK: - 평점 기능 + case .average: + /// 평점 기능 안내 문구를 출력한다. + consoleIO.printAverageGuid() + + /// 사용자가 평점을 조회하고 싶은 학생의 이름을 입력받는 상수를 선언하고 입력받은 내용을 할당한다. + let nameForAverage: String = consoleIO.getInput() + + /// 학생이 이미 존재하는지를 판단하는 변수를 선언하고 true를 할당한다. + var isStudentExist: Bool = true + + /// 학생이 존재하는 위치의 index를 가지게 될 변수를 선언한다. + var existIndex: Int = 0 + + /// 학생이 존재하는지를 판단할 함수를 선언한다. + func studentExistCheck() { + for studentCell in studentCreditList { + if studentCell.name == nameForAverage { + break + } else { + existIndex += 1 + } + } + if existIndex == studentCreditList.count { + isStudentExist = false + } + } + + /// 학생이 존재하는지를 판단할 함수를 호출한다. + studentExistCheck() + + /// 점수의 위치 index값을 가지게 될 변수를 선언한다. + var creditIndex: Int = 0 + + /// 점수의 합을 할당받을 변수를 선언한다. + var sum: Double = 0.0 + + /// 점수의 합을 과목 수로 나눈 평균을 할당받을 변수를 선언한다. + var average: Double = 0.0 + + /// 예외처리 및 출력을 실행하는 구간을 위해 if문을 선언한다. + if nameForAverage == "" { + consoleIO.printFuncInputError() + } else if !isStudentExist { + consoleIO.printStudentMissingError(name: nameForAverage) + } else { + for subject in studentCreditList[existIndex].subject { + consoleIO.printAverageSubjectsSuccess(subject: subject, grade: String(studentCreditList[existIndex].credit[creditIndex])) + sum += creditByGrade[studentCreditList[existIndex].credit[creditIndex]]! + creditIndex += 1 + } + + /// 출력형식에 소수점까지 보여주어야 하므로 Int인 creditIndex를 Double로 형변환하여 나누는 연산을 수행한다. + average = sum / Double(creditIndex) + consoleIO.printAverageSuccess(average: average) + } + + // MARK: - 종료 기능 + case .quit: + consoleIO.printQuitGuide() + isRun = false + + /// case가 아닌 경우(메뉴가 아닌 다른 것을 입력한 경우) default를 수행한다. + default: + consoleIO.printMenuInputError() + } + } + } +}