Advertisement
eranseg

Error Handling

Mar 29th, 2020
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.82 KB | None | 0 0
  1. import Cocoa
  2.  
  3. class LoginError {
  4.    
  5.     enum errorList : Error {
  6.         case invalidNameLength(requiredLength: Int)
  7.         case invalidPassLength(requiredLength: Int)
  8.         case badPassword
  9.         case identicalPasswords
  10.     }
  11.    
  12.     func nameTooShort(_ name : String) throws {
  13.         if name.count < 3 {
  14.             throw errorList.invalidNameLength(requiredLength : 3)
  15.         }
  16.     }
  17.    
  18.     func passTooShort(_ pass : String) throws {
  19.         if pass.count < 3 {
  20.             throw errorList.invalidPassLength(requiredLength : 3)
  21.         }
  22.     }
  23.    
  24.     func identicalPasswords(_ pass1 : String, _ pass2 : String) throws {
  25.         if pass1 == pass2 {
  26.             throw errorList.identicalPasswords
  27.         }
  28.     }
  29.    
  30.     func invalidPassword(_ orgPass : String, _ enteredPass : String) throws {
  31.         if orgPass != enteredPass {
  32.             throw errorList.badPassword
  33.         }
  34.     }
  35. }
  36.  
  37. class User {
  38.     private var _name : String = ""
  39.     private var _pass : String = ""
  40.    
  41.     let loginHandler = LoginError()
  42.    
  43.     init(name : String, pass : String) {
  44.         createUser(name: name, pass: pass)
  45.     }
  46.    
  47.     func checkLogin(name : String, pass : String) {
  48.         do {
  49.             try loginHandler.invalidPassword(self._pass, pass)
  50.             print("Logged in successfully")
  51.         }
  52.         catch LoginError.errorList.badPassword {
  53.             print("You entered invalid password")
  54.         }
  55.         catch {
  56.             print("Something went wrong")
  57.         }
  58.     }
  59.    
  60.     func createUser(name : String, pass : String) {
  61.         do {
  62.             try loginHandler.nameTooShort(name)
  63.             try loginHandler.passTooShort(pass)
  64.             self._name = name
  65.             self._pass = pass
  66.             print("User created successfully")
  67.         }
  68.         catch LoginError.errorList.invalidNameLength(requiredLength: 3) {
  69.             print("Name length too short")
  70.         }
  71.         catch LoginError.errorList.invalidPassLength(requiredLength: 3) {
  72.             print("Password length too short")
  73.         }
  74.         catch {
  75.             print("Something went wrong")
  76.         }
  77.     }
  78.    
  79.     func changePassword(pass : String) {
  80.         do {
  81.             try loginHandler.passTooShort(pass)
  82.             try loginHandler.identicalPasswords(self._pass, pass)
  83.             self._pass = pass
  84.             print("Password changed successfully")
  85.         }
  86.         catch LoginError.errorList.invalidPassLength(requiredLength: 3) {
  87.             print("Password length too short")
  88.         }
  89.         catch LoginError.errorList.identicalPasswords {
  90.             print("Password is identical to your old password")
  91.         }
  92.         catch {
  93.             print("Something went wrong")
  94.         }
  95.     }
  96. }
  97.  
  98. var user = User(name : "Eran", pass: "12345")
  99. user.changePassword(pass: "12")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement