Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum FileStatus {
- case hidden
- case opened
- case password
- }
- struct File {
- private var content: String
- private var access: FileStatus
- private var password: String?
- init(content: String, access: FileStatus, password: String?) {
- self.content = content
- self.access = access
- self.password = self.access == .opened ? nil : password
- }
- func showContent(password: String?) -> String {
- let resultOfProcessing: String
- switch self.access {
- case .opened:
- resultOfProcessing = self.content
- case .hidden:
- resultOfProcessing = "No access. File is hidden"
- case .password:
- if password == self.password {
- resultOfProcessing = self.content
- } else {
- resultOfProcessing = "Wrong password"
- }
- }
- return resultOfProcessing
- }
- mutating func changePasswordByMethod(newPassword: String?) {
- if newPassword != self.password && self.access != .opened {
- self.password = newPassword
- print("Success! We changed password!")
- } else {
- print("Failed. Your new password must be different from the old")
- }
- }
- var changePasswordByComputedProperty: String? {
- get {
- return self.password
- }
- set {
- if newValue != self.password && self.access != .opened {
- self.password = newValue
- print("Success! We changed password!")
- }
- }
- }
- }
- var movie = File(content: "Columbia pictures presents....", access: .password, password: "543")
- movie.changePasswordByMethod(newPassword: "332")
- print(movie.showContent(password: "111"))
- movie.changePasswordByComputedProperty = "111"
- print(movie.showContent(password: "111"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement