Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Cocoa
- class LoginError {
- enum errorList : Error {
- case invalidNameLength(requiredLength: Int)
- case invalidPassLength(requiredLength: Int)
- case badPassword
- case identicalPasswords
- }
- func nameTooShort(_ name : String) throws {
- if name.count < 3 {
- throw errorList.invalidNameLength(requiredLength : 3)
- }
- }
- func passTooShort(_ pass : String) throws {
- if pass.count < 3 {
- throw errorList.invalidPassLength(requiredLength : 3)
- }
- }
- func identicalPasswords(_ pass1 : String, _ pass2 : String) throws {
- if pass1 == pass2 {
- throw errorList.identicalPasswords
- }
- }
- func invalidPassword(_ orgPass : String, _ enteredPass : String) throws {
- if orgPass != enteredPass {
- throw errorList.badPassword
- }
- }
- }
- class User {
- private var _name : String = ""
- private var _pass : String = ""
- let loginHandler = LoginError()
- init(name : String, pass : String) {
- createUser(name: name, pass: pass)
- }
- func checkLogin(name : String, pass : String) {
- do {
- try loginHandler.invalidPassword(self._pass, pass)
- print("Logged in successfully")
- }
- catch LoginError.errorList.badPassword {
- print("You entered invalid password")
- }
- catch {
- print("Something went wrong")
- }
- }
- func createUser(name : String, pass : String) {
- do {
- try loginHandler.nameTooShort(name)
- try loginHandler.passTooShort(pass)
- self._name = name
- self._pass = pass
- print("User created successfully")
- }
- catch LoginError.errorList.invalidNameLength(requiredLength: 3) {
- print("Name length too short")
- }
- catch LoginError.errorList.invalidPassLength(requiredLength: 3) {
- print("Password length too short")
- }
- catch {
- print("Something went wrong")
- }
- }
- func changePassword(pass : String) {
- do {
- try loginHandler.passTooShort(pass)
- try loginHandler.identicalPasswords(self._pass, pass)
- self._pass = pass
- print("Password changed successfully")
- }
- catch LoginError.errorList.invalidPassLength(requiredLength: 3) {
- print("Password length too short")
- }
- catch LoginError.errorList.identicalPasswords {
- print("Password is identical to your old password")
- }
- catch {
- print("Something went wrong")
- }
- }
- }
- var user = User(name : "Eran", pass: "12345")
- user.changePassword(pass: "12")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement