diff --git a/StudentGradeManager/.gitignore b/StudentGradeManager/.gitignore new file mode 100644 index 0000000..8ff29cc --- /dev/null +++ b/StudentGradeManager/.gitignore @@ -0,0 +1,95 @@ +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## DS_Store +.DS_Store + +## User settings +xcuserdata/ +xcuserdata/* +*.xcuserstate + +## 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/ diff --git a/StudentGradeManager/StudentGradeManager.xcodeproj/project.pbxproj b/StudentGradeManager/StudentGradeManager.xcodeproj/project.pbxproj new file mode 100644 index 0000000..69fa8f5 --- /dev/null +++ b/StudentGradeManager/StudentGradeManager.xcodeproj/project.pbxproj @@ -0,0 +1,309 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + EBECF44E2951467F00F52364 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF44D2951467F00F52364 /* main.swift */; }; + EBECF455295146A400F52364 /* Student.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF454295146A400F52364 /* Student.swift */; }; + EBECF457295146E800F52364 /* Grade.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF456295146E800F52364 /* Grade.swift */; }; + EBECF459295146FD00F52364 /* GradeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF458295146FD00F52364 /* GradeManager.swift */; }; + EBECF45B2951471200F52364 /* GradeManagerSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF45A2951471200F52364 /* GradeManagerSystem.swift */; }; + EBECF45D2951473B00F52364 /* InputManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF45C2951473B00F52364 /* InputManager.swift */; }; + EBECF45F2951475100F52364 /* InputError.swift in Sources */ = {isa = PBXBuildFile; fileRef = EBECF45E2951475100F52364 /* InputError.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + EBECF4482951467F00F52364 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + EBECF44A2951467F00F52364 /* StudentGradeManager */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = StudentGradeManager; sourceTree = BUILT_PRODUCTS_DIR; }; + EBECF44D2951467F00F52364 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + EBECF454295146A400F52364 /* Student.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Student.swift; sourceTree = ""; }; + EBECF456295146E800F52364 /* Grade.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Grade.swift; sourceTree = ""; }; + EBECF458295146FD00F52364 /* GradeManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradeManager.swift; sourceTree = ""; }; + EBECF45A2951471200F52364 /* GradeManagerSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradeManagerSystem.swift; sourceTree = ""; }; + EBECF45C2951473B00F52364 /* InputManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputManager.swift; sourceTree = ""; }; + EBECF45E2951475100F52364 /* InputError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputError.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + EBECF4472951467F00F52364 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + EBECF4412951467F00F52364 = { + isa = PBXGroup; + children = ( + EBECF44C2951467F00F52364 /* StudentGradeManager */, + EBECF44B2951467F00F52364 /* Products */, + ); + sourceTree = ""; + }; + EBECF44B2951467F00F52364 /* Products */ = { + isa = PBXGroup; + children = ( + EBECF44A2951467F00F52364 /* StudentGradeManager */, + ); + name = Products; + sourceTree = ""; + }; + EBECF44C2951467F00F52364 /* StudentGradeManager */ = { + isa = PBXGroup; + children = ( + EBECF44D2951467F00F52364 /* main.swift */, + EBECF454295146A400F52364 /* Student.swift */, + EBECF456295146E800F52364 /* Grade.swift */, + EBECF458295146FD00F52364 /* GradeManager.swift */, + EBECF45A2951471200F52364 /* GradeManagerSystem.swift */, + EBECF45C2951473B00F52364 /* InputManager.swift */, + EBECF45E2951475100F52364 /* InputError.swift */, + ); + path = StudentGradeManager; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + EBECF4492951467F00F52364 /* StudentGradeManager */ = { + isa = PBXNativeTarget; + buildConfigurationList = EBECF4512951467F00F52364 /* Build configuration list for PBXNativeTarget "StudentGradeManager" */; + buildPhases = ( + EBECF4462951467F00F52364 /* Sources */, + EBECF4472951467F00F52364 /* Frameworks */, + EBECF4482951467F00F52364 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = StudentGradeManager; + productName = StudentGradeManager; + productReference = EBECF44A2951467F00F52364 /* StudentGradeManager */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + EBECF4422951467F00F52364 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1420; + LastUpgradeCheck = 1420; + TargetAttributes = { + EBECF4492951467F00F52364 = { + CreatedOnToolsVersion = 14.2; + }; + }; + }; + buildConfigurationList = EBECF4452951467F00F52364 /* Build configuration list for PBXProject "StudentGradeManager" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = EBECF4412951467F00F52364; + productRefGroup = EBECF44B2951467F00F52364 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + EBECF4492951467F00F52364 /* StudentGradeManager */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + EBECF4462951467F00F52364 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + EBECF45D2951473B00F52364 /* InputManager.swift in Sources */, + EBECF45F2951475100F52364 /* InputError.swift in Sources */, + EBECF44E2951467F00F52364 /* main.swift in Sources */, + EBECF455295146A400F52364 /* Student.swift in Sources */, + EBECF457295146E800F52364 /* Grade.swift in Sources */, + EBECF459295146FD00F52364 /* GradeManager.swift in Sources */, + EBECF45B2951471200F52364 /* GradeManagerSystem.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + EBECF44F2951467F00F52364 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 13.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + EBECF4502951467F00F52364 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 13.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + EBECF4522951467F00F52364 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + EBECF4532951467F00F52364 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + EBECF4452951467F00F52364 /* Build configuration list for PBXProject "StudentGradeManager" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + EBECF44F2951467F00F52364 /* Debug */, + EBECF4502951467F00F52364 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + EBECF4512951467F00F52364 /* Build configuration list for PBXNativeTarget "StudentGradeManager" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + EBECF4522951467F00F52364 /* Debug */, + EBECF4532951467F00F52364 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = EBECF4422951467F00F52364 /* Project object */; +} diff --git a/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/xcuserdata/jojo.xcuserdatad/UserInterfaceState.xcuserstate b/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/xcuserdata/jojo.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..3f8b9a5 Binary files /dev/null and b/StudentGradeManager/StudentGradeManager.xcodeproj/project.xcworkspace/xcuserdata/jojo.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/StudentGradeManager/StudentGradeManager.xcodeproj/xcuserdata/jojo.xcuserdatad/xcschemes/xcschememanagement.plist b/StudentGradeManager/StudentGradeManager.xcodeproj/xcuserdata/jojo.xcuserdatad/xcschemes/xcschememanagement.plist new file mode 100644 index 0000000..80662c2 --- /dev/null +++ b/StudentGradeManager/StudentGradeManager.xcodeproj/xcuserdata/jojo.xcuserdatad/xcschemes/xcschememanagement.plist @@ -0,0 +1,14 @@ + + + + + SchemeUserState + + StudentGradeManager.xcscheme_^#shared#^_ + + orderHint + 0 + + + + diff --git a/StudentGradeManager/StudentGradeManager/Grade.swift b/StudentGradeManager/StudentGradeManager/Grade.swift new file mode 100644 index 0000000..c957283 --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/Grade.swift @@ -0,0 +1,68 @@ +// +// Grade.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +enum Grade: Double { + case APlus = 4.5 + case AZero = 4.0 + case BPlus = 3.5 + case BZero = 3.0 + case CPlus = 2.5 + case CZero = 2.0 + case DPlus = 1.5 + case DZero = 1.0 + case F = 0.0 + + static func gradeForString(for gradeString: String) throws -> Grade { + switch gradeString { + case "A+": + return Grade.APlus + case "A0": + return Grade.AZero + case "B+": + return Grade.BPlus + case "B0": + return Grade.BZero + case "C+": + return Grade.CPlus + case "C0": + return Grade.CZero + case "D+": + return Grade.DPlus + case "D0": + return Grade.DZero + case "F": + return Grade.F + default: + throw InputError.invalidInput + } + } + + static func printGradeString(for grade: Grade) { + switch grade { + case .APlus: + print("A+") + case .AZero: + print("A0") + case .BPlus: + print("B+") + case .BZero: + print("B0") + case .CPlus: + print("C+") + case .CZero: + print("C0") + case .DPlus: + print("D+") + case .DZero: + print("D0") + case .F: + print("F") + } + } +} diff --git a/StudentGradeManager/StudentGradeManager/GradeManager.swift b/StudentGradeManager/StudentGradeManager/GradeManager.swift new file mode 100644 index 0000000..6ed744c --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/GradeManager.swift @@ -0,0 +1,24 @@ +// +// GradeManager.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +class GradeManager { + + private let gradeManageSystem = GradeManagerSystem() + + func run() { + while !gradeManageSystem.isDone { + do { + let menuInput = try gradeManageSystem.receiveMenuInput() + gradeManageSystem.performMenuAction(menuInput: menuInput) + } catch { + print(error) + } + } + } +} diff --git a/StudentGradeManager/StudentGradeManager/GradeManagerSystem.swift b/StudentGradeManager/StudentGradeManager/GradeManagerSystem.swift new file mode 100644 index 0000000..4fc9867 --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/GradeManagerSystem.swift @@ -0,0 +1,134 @@ +// +// GradeManagerSystem.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +class GradeManagerSystem { + + let inputManager = InputManager() + + var isDone = false + + enum Menu: String { + case addStudent = "1" + case removeStudent = "2" + case addOrModifyGrade = "3" + case removeGrade = "4" + case lookupGrade = "5" + case stopProgram = "X" + } + + let userDefaults = UserDefaults.standard + + var studentDictionary: [String: Student] = [:] + + func receiveMenuInput() throws -> String { + try inputManager.menuInput() + } + + func performMenuAction(menuInput: String) { + do { + switch Menu(rawValue: menuInput) { + case .addStudent: + let studentName = try inputManager.addStudentInput() + addStudent(studentName) + + case .removeStudent: + let studentName = try inputManager.removeStudentInput() + removeStudent(studentName) + + case .addOrModifyGrade: + let (studentName, subject, grade) = try inputManager.addOrModifyGradeInput() + addOrModifyGrade(for: studentName, withSubject: subject, as: grade) + + case .removeGrade: + let (studentName, subject) = try inputManager.removeGradeInput() + removeGrade(for: studentName, withSubject: subject) + + case .lookupGrade: + let studentName = try inputManager.findAverageInput() + lookupGrade(for: studentName) + + case .stopProgram: + stopProgram() + + case .none: + inputManager.printInvalidMenuInput() + } + } catch { + inputManager.printInvalidInput() + } + } + + func addStudent(_ studentName: String) { + guard studentDictionary[studentName] == nil else { + print("\(studentName)은 이미 존재하는 학생입니다. 추가하지 않습니다.") + return + } + + let newStudentEntity = Student(name: studentName) + studentDictionary[studentName] = newStudentEntity + print("\(studentName) 학생을 추가했습니다.") + } + + func removeStudent(_ studentName: String) { + guard studentDictionary[studentName] != nil else { + print("\(studentName) 학생을 찾지 못했습니다.") + return + } + + studentDictionary.removeValue(forKey: studentName) + print("\(studentName) 학생을 삭제하였습니다.") + } + + func addOrModifyGrade(for studentName: String, withSubject subject: String, as grade: String ) { + guard var student = studentDictionary[studentName] else { + print("\(studentName) 학생을 찾지 못했습니다.") + return + } + + student.updateGrade(for: subject, as: grade) + studentDictionary[studentName] = student + + print("\(studentName) 학생의 \(subject) 과목이 \(grade)로 추가(변경)되었습니다.") + } + + func removeGrade(for studentName: String, withSubject subject: String) { + guard var student = studentDictionary[studentName] else { + print("\(studentName) 학생을 찾지 못했습니다.") + return + } + + student.deleteGrade(for: subject) + studentDictionary[studentName] = student + + print("\(studentName) 학생의 \(subject) 과목의 성적이 삭제되었습니다.") + } + + func lookupGrade(for studentName: String) { + + guard let student = studentDictionary[studentName] else { + print("\(studentName) 학생을 찾지 못했습니다.") + return + } + + student.printAllGrades() + do { + let averageScore = try student.calculateAverageScore() + print("평점 : \(averageScore)") + } catch { + print("\(studentName) 학생은 입력된 성적이 없습니다.") + } + + } + + func stopProgram() { + isDone = true + inputManager.printCloseProgram() + } +} + diff --git a/StudentGradeManager/StudentGradeManager/InputError.swift b/StudentGradeManager/StudentGradeManager/InputError.swift new file mode 100644 index 0000000..24a7a4b --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/InputError.swift @@ -0,0 +1,14 @@ +// +// InputError.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +enum InputError: Error { + case invalidMenuInput + case invalidInput + case emptyGrades +} diff --git a/StudentGradeManager/StudentGradeManager/InputManager.swift b/StudentGradeManager/StudentGradeManager/InputManager.swift new file mode 100644 index 0000000..6c2f91c --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/InputManager.swift @@ -0,0 +1,121 @@ +// +// InputManager.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +struct InputManager { + + private let menuInputMessage = "원하는 기능을 입력해주세요\n1: 학생추가, 2: 학생삭제, 3: 성적추가(변경), 4: 성적삭제, 5: 평점보기, X: 종료" + private let invalidMenuInputMessage = "뭔가 입력이 잘못되었습니다. 1~5 사이의 숫자 혹은 X를 입력해주세요." + private let addStudentMessage = "추가할 학생의 이름을 입력해주세요" + private let removeStudentMessage = "삭제할 학생의 이름을 입력해주세요" + private let addGradeMessage = "성적을 추가할 학생의 이름, 과목 이름, 성적(A+, A0, F 등)을 띄어쓰기로 구분하여 차례로 작성해주세요.\n입력예) Mickey Swift A+\n만약에 학생의 성적 중 해당 과목이 존재하면 기존 점수가 갱신됩니다." + private let removeGradeMessage = "성적을 삭제할 학생의 이름, 과목 이름을 띄어쓰기로 구분하여 차례로 작성해주세요.\n입력예) Mickey Swift" + private let findAverageMessage = "평점을 알고싶은 학생의 이름을 입력해주세요." + private let invalidInputMessage = "입력이 잘못되었습니다. 다시 확인해주세요." + private let closeProgramMessage = "프로그램을 종료합니다..." + + func menuInput() throws -> String { + print(menuInputMessage) + + let receivedInput = readLine() + guard let receivedInput = receivedInput else { + throw InputError.invalidMenuInput + } + + return receivedInput + } + + func addStudentInput() throws -> String { + print(addStudentMessage) + let receivedInput = readLine() + + let regexExpression = "^[a-zA-Z0-9]*$" + let regexTest = NSPredicate(format:"SELF MATCHES %@", regexExpression) + + guard let receivedInput = receivedInput, receivedInput != "", regexTest.evaluate(with: receivedInput) else { + throw InputError.invalidInput + } + + return receivedInput + } + + func removeStudentInput() throws -> String { + print(removeStudentMessage) + + let receivedInput = readLine() + + let regexExpression = "^[a-zA-Z0-9]*$" + let regexTest = NSPredicate(format:"SELF MATCHES %@", regexExpression) + + guard let receivedInput = receivedInput, receivedInput != "", regexTest.evaluate(with: receivedInput) else { + throw InputError.invalidInput + } + + return receivedInput + } + + func addOrModifyGradeInput() throws -> (String, String, String) { + print(addGradeMessage) + + let receivedInput = readLine() + + let regexExpression = "[a-zA-Z0-9]+\\s[a-zA-Z0-9]+\\s([A, B, C, D]+[0, +]|[F])" + let regexTest = NSPredicate(format:"SELF MATCHES %@", regexExpression) + + guard let receivedInput = receivedInput, receivedInput != "", regexTest.evaluate(with: receivedInput) else { + throw InputError.invalidInput + } + + let inputComponents = receivedInput.split(separator: " ").map{ String($0) } + + return (inputComponents[0], inputComponents[1], inputComponents[2]) + } + + func removeGradeInput() throws -> (String, String) { + print(removeGradeMessage) + + let receivedInput = readLine() + + let regexExpression = "[a-zA-Z0-9]+\\s[a-zA-Z0-9]*$" + let regexTest = NSPredicate(format:"SELF MATCHES %@", regexExpression) + + guard let receivedInput = receivedInput, receivedInput != "", regexTest.evaluate(with: receivedInput) else { + throw InputError.invalidInput + } + + let inputComponents = receivedInput.split(separator: " ").map{ String($0) } + + return (inputComponents[0], inputComponents[1]) + } + + func findAverageInput() throws -> String { + print(findAverageMessage) + let receivedInput = readLine() + + let regexExpression = "^[a-zA-Z0-9]*$" + let regexTest = NSPredicate(format:"SELF MATCHES %@", regexExpression) + + guard let receivedInput = receivedInput, receivedInput != "", regexTest.evaluate(with: receivedInput) else { + throw InputError.invalidInput + } + + return receivedInput + } + + func printInvalidMenuInput() { + print(invalidMenuInputMessage) + } + + func printInvalidInput() { + print(invalidInputMessage) + } + + func printCloseProgram() { + print(closeProgramMessage) + } +} diff --git a/StudentGradeManager/StudentGradeManager/Student.swift b/StudentGradeManager/StudentGradeManager/Student.swift new file mode 100644 index 0000000..7942eee --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/Student.swift @@ -0,0 +1,51 @@ +// +// Student.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +struct Student { + + var name: String + var gradeForSubject: [String: Grade] + + init(name: String) { + self.name = "" + self.gradeForSubject = [:] + } + + mutating func updateGrade(for subjuct: String, as grade: String) { + + do { + gradeForSubject[subjuct] = try Grade.gradeForString(for: grade) + } catch { + print(InputError.invalidInput) + } + } + + mutating func deleteGrade(for subject: String) { + gradeForSubject[subject] = nil + } + + func printAllGrades() { + for grade in gradeForSubject { + print(grade.key, terminator: ": ") + Grade.printGradeString(for: grade.value) + } + } + + func calculateAverageScore() throws -> Double { + guard gradeForSubject.count != 0 else { + throw InputError.emptyGrades + } + + let averageGrade = gradeForSubject.values + .map{ $0.rawValue } + .reduce(0.0, +) / Double(gradeForSubject.count) + + return averageGrade + } +} diff --git a/StudentGradeManager/StudentGradeManager/main.swift b/StudentGradeManager/StudentGradeManager/main.swift new file mode 100644 index 0000000..497081b --- /dev/null +++ b/StudentGradeManager/StudentGradeManager/main.swift @@ -0,0 +1,13 @@ +// +// main.swift +// StudentGradeManager +// +// Created by 조용현 on 2022/12/20. +// + +import Foundation + +let gradeManager = GradeManager() +gradeManager.run() + +