Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.91 KB | None | 0 0
  1. enum FileStatus {
  2.  
  3.     case hidden
  4.     case opened
  5.     case password
  6. }
  7.  
  8. struct File {
  9.    
  10.     private var content: String
  11.     private var access: FileStatus
  12.     private  var password: String?
  13.  
  14.     init(content: String, access: FileStatus, password: String?) {
  15.         self.content = content
  16.         self.access = access
  17.         self.password = self.access == .opened ? nil : password
  18.     }
  19.    
  20.     func showContent(password: String?) -> String {
  21.         let resultOfProcessing: String
  22.         switch self.access {
  23.         case .opened:
  24.             resultOfProcessing = self.content
  25.         case .hidden:
  26.             resultOfProcessing = "No access. File is hidden"
  27.         case .password:
  28.             if password == self.password {
  29.                 resultOfProcessing = self.content    
  30.             } else {
  31.                 resultOfProcessing = "Wrong password"
  32.             }    
  33.         }
  34.         return resultOfProcessing
  35.     }
  36.    
  37.     mutating func changePasswordByMethod(newPassword: String?) {
  38.         if newPassword != self.password && self.access != .opened {
  39.             self.password = newPassword
  40.             print("Success! We changed password!")
  41.         } else {
  42.             print("Failed. Your new password must be different from the old")
  43.         }
  44.     }
  45.    
  46.     var changePasswordByComputedProperty: String? {
  47.         get {
  48.             return self.password
  49.         }
  50.         set {
  51.             if newValue != self.password && self.access != .opened {
  52.                 self.password = newValue
  53.                 print("Success! We changed password!")
  54.             }    
  55.         }
  56.     }
  57.    
  58. }
  59.  
  60. var movie = File(content: "Columbia pictures presents....", access: .password, password: "543")
  61.  
  62. movie.changePasswordByMethod(newPassword: "332")
  63.  
  64. print(movie.showContent(password: "111"))
  65.  
  66. movie.changePasswordByComputedProperty = "111"
  67.  
  68. print(movie.showContent(password: "111"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement