Advertisement
GeorgiLukanov87

Encapsulation - Lab

Oct 24th, 2022
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. # Encapsulation - Lab
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/1938#0
  4.  
  5. # 01. Person
  6. # 02. Mammal
  7. # 03. Profile
  8. # 04. Email Validator
  9. # 05. Account
  10. ====================================================================================================
  11.  
  12. # 01. Person
  13.  
  14.  
  15. class Person:
  16.     def __init__(self, name, age):
  17.         self.__name = name
  18.         self.__age = age
  19.  
  20.     def get_name(self):
  21.         return self.__name
  22.  
  23.     def get_age(self):
  24.         return self.__age
  25.    
  26. ====================================================================================================
  27.  
  28. # 02. Mammal
  29.  
  30.  
  31. class Mammal:
  32.     __kingdom = 'animals'
  33.  
  34.     def __init__(self, name, type, sound):
  35.         self.name = name
  36.         self.type = type
  37.         self.sound = sound
  38.  
  39.     def make_sound(self):
  40.         return f"{self.name} makes {self.sound}"
  41.  
  42.     def get_kingdom(self):
  43.         return Mammal.__kingdom
  44.  
  45.     def info(self):
  46.         return f"{self.name} is of type {self.type}"
  47.  
  48. ====================================================================================================
  49.  
  50. # 03. Profile
  51.  
  52.  
  53. class Profile:
  54.     def __init__(self, username, password):
  55.         self.username = username
  56.         self.password = password
  57.        
  58.     @property
  59.     def username(self):
  60.         return self.__username
  61.    
  62.     @username.setter
  63.     def username(self, value):
  64.         if not (5 <= len(value) <= 15):
  65.             raise ValueError("The username must be between 5 and 15 characters.")
  66.         self.__username = value
  67.  
  68.     @property
  69.     def password(self):
  70.         return self.__password
  71.  
  72.     @password.setter
  73.     def password(self, value):
  74.         is_length_valid = len(value) >= 8
  75.         is_letter_case_presented = [char for char in value if char.isupper()]
  76.         is_digit_presented = [char for char in value if char.isdigit()]
  77.  
  78.         if not is_length_valid or not is_digit_presented or not is_letter_case_presented:
  79.             raise ValueError("The password must be 8 or more characters with at least 1 digit and 1 uppercase letter.")
  80.  
  81.         self.__password = value
  82.  
  83.     def __str__(self):
  84.         return f'You have a profile with username: "{self.username}" and password: {"*" * len(self.password)}'
  85.  
  86. ====================================================================================================
  87.  
  88. # 04. Email Validator
  89.  
  90.  
  91. class EmailValidator:
  92.     def __init__(self, min_length, mails, domains):
  93.         self.min_length = min_length
  94.         self.mails = mails
  95.         self.domains = domains
  96.  
  97.     def __is_name_valid(self, name):
  98.         return len(name) >= self.min_length
  99.  
  100.     def __is_mail_valid(self, mail):
  101.         return mail in self.mails
  102.  
  103.     def __is_domain_valid(self, domain):
  104.         return domain in self.domains
  105.  
  106.     def validate(self, email):
  107.         username = email.split('@')[0]
  108.         mail, domain = email.split('@')[1].split('.')
  109.         is_valid = self.__is_name_valid(username) and self.__is_mail_valid(mail) and self.__is_domain_valid(domain)
  110.         return is_valid
  111.  
  112. ====================================================================================================
  113.  
  114. # 05. Account
  115.  
  116.  
  117. class Account:
  118.     def __init__(self, id, balance, pin):
  119.         self.__id = id
  120.         self.balance = balance
  121.         self.__pin = pin
  122.  
  123.     def get_id(self, pin):
  124.         if self.__pin == pin:
  125.             return self.__id
  126.         return 'Wrong pin'
  127.  
  128.     def change_pin(self, old_pin, new_pin):
  129.         if old_pin == self.__pin:
  130.             self.__pin = new_pin
  131.             return 'Pin changed'
  132.         return 'Wrong pin'
  133.  
  134. ====================================================================================================
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement