From 3a610d8df1daef1a340eba03f547c3e20e5f54a8 Mon Sep 17 00:00:00 2001 From: Sanskar <> Date: Sat, 29 Jan 2022 13:45:12 +0530 Subject: [PATCH] assignment 2 added --- Assignment_2/.gitignore | 28 ++ .../Assignment_2.xcodeproj/project.pbxproj | 303 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + Assignment_2/Assignment_2/DisplayUsers.swift | 28 ++ Assignment_2/Assignment_2/User.swift | 26 ++ Assignment_2/Assignment_2/UserHandler.swift | 245 ++++++++++++++ Assignment_2/Assignment_2/Validator.swift | 72 +++++ Assignment_2/Assignment_2/main.swift | 60 ++++ 9 files changed, 777 insertions(+) create mode 100644 Assignment_2/.gitignore create mode 100644 Assignment_2/Assignment_2.xcodeproj/project.pbxproj create mode 100644 Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 Assignment_2/Assignment_2/DisplayUsers.swift create mode 100644 Assignment_2/Assignment_2/User.swift create mode 100644 Assignment_2/Assignment_2/UserHandler.swift create mode 100644 Assignment_2/Assignment_2/Validator.swift create mode 100644 Assignment_2/Assignment_2/main.swift diff --git a/Assignment_2/.gitignore b/Assignment_2/.gitignore new file mode 100644 index 0000000..cc0c13c --- /dev/null +++ b/Assignment_2/.gitignore @@ -0,0 +1,28 @@ + +# Xcode +# +build/ +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata +#*.xccheckout +*.moved-aside +DerivedData +*.hmap +*.ipa +*.xcuserstate +*.framework + +# CocoaPods +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control +# +Pods diff --git a/Assignment_2/Assignment_2.xcodeproj/project.pbxproj b/Assignment_2/Assignment_2.xcodeproj/project.pbxproj new file mode 100644 index 0000000..f04cc00 --- /dev/null +++ b/Assignment_2/Assignment_2.xcodeproj/project.pbxproj @@ -0,0 +1,303 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 55; + objects = { + +/* Begin PBXBuildFile section */ + CB5791C027A00A3600A0B1B9 /* UserHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB5791BF27A00A3600A0B1B9 /* UserHandler.swift */; }; + CB675FD92795814A004EE877 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB675FD82795814A004EE877 /* main.swift */; }; + CB6D90D7279FD82100EAA8D4 /* DisplayUsers.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB6D90D6279FD82100EAA8D4 /* DisplayUsers.swift */; }; + CBBFCD4B279B143A00DA0404 /* Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBBFCD4A279B143A00DA0404 /* Validator.swift */; }; + CBF2D28227995806003D1727 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBF2D28127995806003D1727 /* User.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + CB675FD32795814A004EE877 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + CB5791BF27A00A3600A0B1B9 /* UserHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserHandler.swift; sourceTree = ""; }; + CB675FD52795814A004EE877 /* Assignment_2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Assignment_2; sourceTree = BUILT_PRODUCTS_DIR; }; + CB675FD82795814A004EE877 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + CB6D90D6279FD82100EAA8D4 /* DisplayUsers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayUsers.swift; sourceTree = ""; }; + CBBFCD4A279B143A00DA0404 /* Validator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Validator.swift; sourceTree = ""; }; + CBF2D28127995806003D1727 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + CB675FD22795814A004EE877 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + CB675FCC2795814A004EE877 = { + isa = PBXGroup; + children = ( + CB675FD72795814A004EE877 /* Assignment_2 */, + CB675FD62795814A004EE877 /* Products */, + ); + sourceTree = ""; + }; + CB675FD62795814A004EE877 /* Products */ = { + isa = PBXGroup; + children = ( + CB675FD52795814A004EE877 /* Assignment_2 */, + ); + name = Products; + sourceTree = ""; + }; + CB675FD72795814A004EE877 /* Assignment_2 */ = { + isa = PBXGroup; + children = ( + CB675FD82795814A004EE877 /* main.swift */, + CBF2D28127995806003D1727 /* User.swift */, + CBBFCD4A279B143A00DA0404 /* Validator.swift */, + CB6D90D6279FD82100EAA8D4 /* DisplayUsers.swift */, + CB5791BF27A00A3600A0B1B9 /* UserHandler.swift */, + ); + path = Assignment_2; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + CB675FD42795814A004EE877 /* Assignment_2 */ = { + isa = PBXNativeTarget; + buildConfigurationList = CB675FDC2795814A004EE877 /* Build configuration list for PBXNativeTarget "Assignment_2" */; + buildPhases = ( + CB675FD12795814A004EE877 /* Sources */, + CB675FD22795814A004EE877 /* Frameworks */, + CB675FD32795814A004EE877 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Assignment_2; + productName = Assignment_2; + productReference = CB675FD52795814A004EE877 /* Assignment_2 */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + CB675FCD2795814A004EE877 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1320; + LastUpgradeCheck = 1320; + TargetAttributes = { + CB675FD42795814A004EE877 = { + CreatedOnToolsVersion = 13.2.1; + }; + }; + }; + buildConfigurationList = CB675FD02795814A004EE877 /* Build configuration list for PBXProject "Assignment_2" */; + compatibilityVersion = "Xcode 13.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = CB675FCC2795814A004EE877; + productRefGroup = CB675FD62795814A004EE877 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + CB675FD42795814A004EE877 /* Assignment_2 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + CB675FD12795814A004EE877 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + CBF2D28227995806003D1727 /* User.swift in Sources */, + CB6D90D7279FD82100EAA8D4 /* DisplayUsers.swift in Sources */, + CB5791C027A00A3600A0B1B9 /* UserHandler.swift in Sources */, + CBBFCD4B279B143A00DA0404 /* Validator.swift in Sources */, + CB675FD92795814A004EE877 /* main.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + CB675FDA2795814A004EE877 /* 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++17"; + CLANG_CXX_LIBRARY = "libc++"; + 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 = 11.5; + 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; + }; + CB675FDB2795814A004EE877 /* 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++17"; + CLANG_CXX_LIBRARY = "libc++"; + 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 = 11.5; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + CB675FDD2795814A004EE877 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + CB675FDE2795814A004EE877 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + CB675FD02795814A004EE877 /* Build configuration list for PBXProject "Assignment_2" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + CB675FDA2795814A004EE877 /* Debug */, + CB675FDB2795814A004EE877 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + CB675FDC2795814A004EE877 /* Build configuration list for PBXNativeTarget "Assignment_2" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + CB675FDD2795814A004EE877 /* Debug */, + CB675FDE2795814A004EE877 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = CB675FCD2795814A004EE877 /* Project object */; +} diff --git a/Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/Assignment_2/Assignment_2.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Assignment_2/Assignment_2/DisplayUsers.swift b/Assignment_2/Assignment_2/DisplayUsers.swift new file mode 100644 index 0000000..7f1114f --- /dev/null +++ b/Assignment_2/Assignment_2/DisplayUsers.swift @@ -0,0 +1,28 @@ +// +// DisplayUsers.swift +// Assignment_2 +// +// Created by Sanskar on 25/01/22. +// + +import Foundation + +class DisplayUsers +{ + static func display(_ dictionary : Array<(key: Int, value: User)>) + { + print() + print() + print("-----------------------------------------------------------------------------------------") + print() + print("Name Roll Number Age Address Course") + print() + print("-----------------------------------------------------------------------------------------") + for index in dictionary + { + print() + print("\(index.value.fullName) \(index.value.rollNumber) \(index.value.age) \(index.value.address) \(index.value.course)") + } + print() + } +} diff --git a/Assignment_2/Assignment_2/User.swift b/Assignment_2/Assignment_2/User.swift new file mode 100644 index 0000000..f49bdbd --- /dev/null +++ b/Assignment_2/Assignment_2/User.swift @@ -0,0 +1,26 @@ +// +// User.swift +// Assignment_2 +// +// Created by Sanskar on 20/01/22. +// + +import Foundation + +class User +{ + let fullName : String + let address : String + let age : Int + let rollNumber : Int + let course : [String] + + init(_ fullName: String , _ address: String , _ age: Int , _ rollNumber: Int , _ course: [String]) + { + self.fullName = fullName + self.address = address + self.age = age + self.rollNumber = rollNumber + self.course = course + } +} diff --git a/Assignment_2/Assignment_2/UserHandler.swift b/Assignment_2/Assignment_2/UserHandler.swift new file mode 100644 index 0000000..4bf463c --- /dev/null +++ b/Assignment_2/Assignment_2/UserHandler.swift @@ -0,0 +1,245 @@ +// +// UserHandler.swift +// Assignment_2 +// +// Created by Sanskar on 25/01/22. +// + +import Foundation + +class UserHandler +{ + //function to add user details + + static func add() + { + var fullName : String? + var address : String? + var age : Int? + var rollNumber : Int? + var courseArray : [String]? + var flag = true + + print() + flag = true + while(flag) //took first input rollnumber bcoz it should be unique + { + print("Please Enter the roll number") + if let tempRollNumber = readLine() + { + if let tempIntRollNumber = (Int)(tempRollNumber) + { + if(Validator.existingRollNumber(rollNumber: tempIntRollNumber)) + { + print("Roll Number already exists") + continue + } + rollNumber = tempIntRollNumber + flag = false + } + else + { + print(invalidChoice) + } + } + else + { + print(invalidChoice) + } + } + print() + + flag = true + while(flag) + { + print("Please Enter the name") + if let tempFullName = readLine() + { + if(tempFullName.isEmpty) + { + print(invalidChoice) + continue + } + fullName = tempFullName + flag = false + } + else + { + print(invalidChoice) + } + } + print() + + flag = true + while(flag) + { + print("Please Enter the age") + if let tempAgeString = readLine() + { + if let tempAge = (Int)(tempAgeString) + { + if(!Validator.validateage(age: tempAge)) + { + print(invalidChoice) + continue + } + + age = tempAge + flag = false + } + else + { + print(invalidChoice) + } + } + else + { + print(invalidChoice) + } + } + print() + + flag = true + while(flag) + { + print("Please Enter the address") + if let tempAddress = readLine() + { + address = tempAddress + flag = false + } + else + { + print(invalidChoice) + } + } + print() + + flag = true + while(flag) + { + print("Please enter the 4 courses you want to opt amoung space separated manner (A , B , C , D , E , F ") + if let course = readLine() + { + courseArray = course.components(separatedBy: " ") + if(!Validator.validateCourse(courseArray: courseArray!)) + { + print("Invalid course/number of course selected selected") + continue + } + else + { + flag = false + } + } + } + print() + + if let tempname = fullName , let tempaddress = address , let tempage = age , let temproll = rollNumber , let tempcourse = courseArray + { + let obj = User.init(tempname, tempaddress, tempage, temproll, tempcourse) + userDictionary[obj.rollNumber] = obj + } + } + + //----------------------------------------------------------------------------------------------------------------------// + + //User function to diplay details + + //----------------------------------------------------------------------------------------------------------------------// + + static func display() + { + var flag = true + while(flag) + { + if(userDictionary.count == 0) + { + print("No data to show") + break + } + + print(""" + CHOOSE THE OPTION IN WHICH YOU WANT TO SEE DATA: + + 1. Based on name (Ascending). + 2. Based on name (Descending). + 3. Based on rollNumber (Ascending). + 4. Based on rollNumber (Descending). + 5. Based on address (Ascending). + 6. Based on address (Descending). + 7. Based on age (Ascending). + 8. Based on age (Descending). + """) + + if let input = readLine() + { + if let inputInt = (Int)(input) + { + switch inputInt + { + case 1 , 2 : + let sortedByName = userDictionary.sorted { (($0.1.fullName < $1.1.fullName) && (inputInt%2==1)) } + DisplayUsers.display(sortedByName) + flag = false + + case 3 , 4: + let sortedByRollNumber = userDictionary.sorted { (($0.1.rollNumber < $1.1.rollNumber) && (inputInt%2==1)) } + DisplayUsers.display(sortedByRollNumber) + flag = false + + case 5 , 6: + let sortedByAddress = userDictionary.sorted { (($0.1.address < $1.1.address) && (inputInt%2==1)) } + DisplayUsers.display(sortedByAddress) + flag = false + + case 7 , 8: + let sortedByAge = userDictionary.sorted { (($0.1.fullName < $1.1.fullName) && (inputInt%2==1)) } + DisplayUsers.display(sortedByAge) + flag = false + + default: + print(invalidChoice) + + } + } + else + { + print(invalidChoice) + } + } + else + { + print(invalidChoice) + } + } + } + + //----------------------------------------------------------------------------------------------------------------------// + + //User function to delete user details + + //----------------------------------------------------------------------------------------------------------------------// + + + static func delete() + { + print("Enter the roll number of user you want to delete") + if let roll = readLine() + { + if let intRoll = (Int)(roll) + { + if(!Validator.existingRollNumber(rollNumber: intRoll)) + { + print("User with roll number \(intRoll) does not exist") + return + } + userDictionary[intRoll] = nil + print() + print("Successfully Deleted") + return + } + } + print(invalidChoice) + } +} diff --git a/Assignment_2/Assignment_2/Validator.swift b/Assignment_2/Assignment_2/Validator.swift new file mode 100644 index 0000000..8232cb4 --- /dev/null +++ b/Assignment_2/Assignment_2/Validator.swift @@ -0,0 +1,72 @@ +// +// Validator.swift +// Assignment_2 +// +// Created by Sanskar on 21/01/22. +// + +import Foundation + +class Validator +{ + static var countOfCourse = [Int](repeating : 1 , count : 6) + + static func validCourse(course: String) ->Bool + { + switch course + { + case "A" : + countOfCourse[0] -= 1 + return (countOfCourse[0] == 0) + case "B" : + countOfCourse[1] -= 1 + return (countOfCourse[1] == 0) + case "C" : + countOfCourse[2] -= 1 + return (countOfCourse[2] == 0) + case "D" : + countOfCourse[3] -= 1 + return (countOfCourse[3] == 0) + case "E" : + countOfCourse[4] -= 1 + return (countOfCourse[4] == 0) + case "F" : + countOfCourse[5] -= 1 + return (countOfCourse[5] == 0) + + default: + return false + } + } + + static func validateCourse(courseArray: [String]) -> Bool + { + var count = 0 //count for course opted + for index in courseArray + { + if(validCourse(course: index)) + { + count+=1 + } + } + if(count == 4) + { + return true + } + else + { + countOfCourse = [Int](repeating : 1 , count : 6) + return false + } + } + + static func existingRollNumber(rollNumber : Int) -> Bool + { + return userDictionary[rollNumber] != nil + } + + static func validateage(age: Int) -> Bool + { + return (age >= 0 && age < 100) + } +} diff --git a/Assignment_2/Assignment_2/main.swift b/Assignment_2/Assignment_2/main.swift new file mode 100644 index 0000000..a8f4e9e --- /dev/null +++ b/Assignment_2/Assignment_2/main.swift @@ -0,0 +1,60 @@ + +// +// main.swift +// assignment1 +// +// Created by Sanskar on 11/01/22. +// +import Foundation + +var userDictionary = [Int : User]() + +let invalidChoice = "Invalid Input Choosen , Please Enter Valid Input" + +func main() +{ + var temp = true // variable to know if user select exit as option + while(temp) + { + print(""" + CHOOSE THE OPTION FOR OPERATION : + + 1. Add User details. + 2. Display User details. + 3. Delete User details + 4. Exit + + Enter your Choice + """) + if let input = readLine() + { + if let choice = (Int)(input) + { + switch choice + { + case 1 : + UserHandler.add() + + case 2 : + UserHandler.display() + + case 3 : + UserHandler.delete() + + case 4 : + print("THANK YOU") + temp = false + default : + print(invalidChoice) + } + } + } + else + { + print(invalidChoice) + } + } +} + +main() +