private.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. class BankAccount:
  2. def __init__(self, name, balance, passport) -> None:
  3. self._name = name
  4. self._balance = balance
  5. self._passport = passport
  6. self.__name = name
  7. self.__balance = balance
  8. self.__passport = passport
  9. def print_protected_data(self):
  10. print(self._name, self._balance, self._passport)
  11. # Инкапсуляция - сокрытие данных
  12. def print_private_data(self):
  13. print(self.__name, self.__balance, self.__passport)
  14. def __private_method(self):
  15. pass
  16. class Student:
  17. def __init__(self, name, age, branch) -> None:
  18. self.__name = name
  19. self.__age = age
  20. self.__branch = branch
  21. def __display_details(self):
  22. print(f'Имя: {self.__name}\n Возраст: {self.__age}\n Направление: {self.__branch}')
  23. def access_private_method(self):
  24. self.__display_details()
  25. class BankDeposit:
  26. def __init__(self, name, balance, rate):
  27. self.name = name
  28. self.balance = balance
  29. self.rate = rate
  30. def __calculate_profit(self):
  31. return self.balance/100*self.rate
  32. def get_balance_with_profit(self):
  33. return self.balance + self.__calculate_profit()
  34. class Library:
  35. def __init__(self, books: list) -> None:
  36. self.__books = books
  37. def __check_availability(self, book_name):
  38. return True if book_name in self.__books else False
  39. def search_book(self, book_name):
  40. return self.__check_availability(book_name)
  41. def return_book(self, book_name):
  42. self.__books.append(book_name)
  43. def _checkout_book(self, book_name):
  44. if self.__check_availability(book_name):
  45. self.__books.remove(book_name)
  46. return True
  47. else:
  48. return False
  49. class Employee:
  50. def __init__(self, name, position, hours_worked, hourly_rate):
  51. self.name = name
  52. self.__position = position
  53. self.__hours_worked = hours_worked
  54. self.__hourly_rate = hourly_rate
  55. def __calculate_salary(self):
  56. return self.__hours_worked*self.__hourly_rate
  57. def _set_position(self, position):
  58. self.__position = position
  59. def get_position(self):
  60. return self.__position
  61. def get_salary(self):
  62. return self.__calculate_salary()
  63. def get_employee_details(self):
  64. return f"Name: {self.name}, Position: {self.__position}, Salary: {self.__calculate_salary()}"
  65. def main():
  66. '''
  67. account1 = BankAccount('Bob', 1000, 2312312312)
  68. account1.print_protected_data()
  69. account1.print_private_data()
  70. print(dir(account1))
  71. # Доступ к приватным атрибутам и методам
  72. account1._BankAccount__name = 'sdfsasd'
  73. print(account1._BankAccount__name)
  74. '''
  75. '''
  76. account = BankDeposit("John Connor", 1000, 5)
  77. assert account.name == "John Connor"
  78. assert account.balance == 1000
  79. assert account.rate == 5
  80. assert account._BankDeposit__calculate_profit() == 50.0
  81. assert account.get_balance_with_profit() == 1050.0
  82. account_2 = BankDeposit("Sarah Connor", 200, 27)
  83. assert account_2.name == "Sarah Connor"
  84. assert account_2.balance == 200
  85. assert account_2.rate == 27
  86. assert account_2._BankDeposit__calculate_profit() == 54.0
  87. assert account_2.get_balance_with_profit() == 254.0
  88. print('Good')
  89. '''
  90. '''
  91. library = Library(["War and Peace", "Moby-Dick", "Pride and Prejudice"])
  92. assert library._Library__books == ["War and Peace", "Moby-Dick", "Pride and Prejudice"]
  93. assert library.search_book("Moby-Dick") == True
  94. assert library.search_book("Jane Air") == False
  95. assert library._Library__check_availability("War and Peace") == True
  96. assert library._checkout_book("Moby-Dick") == True
  97. assert library._Library__books == ["War and Peace", "Pride and Prejudice"]
  98. assert library.search_book("Moby-Dick") == False
  99. assert library.return_book("Moby-Dick") is None
  100. assert library._Library__books == ["War and Peace", "Pride and Prejudice", "Moby-Dick"]
  101. assert library.search_book("Moby-Dick") == True
  102. print('Good')
  103. '''
  104. employee = Employee("Джеки Чан", 'manager', 20, 40)
  105. assert employee.name == 'Джеки Чан'
  106. assert employee._Employee__hours_worked == 20
  107. assert employee._Employee__hourly_rate == 40
  108. assert employee._Employee__position == 'manager'
  109. assert employee.get_position() == 'manager'
  110. assert employee.get_salary() == 800
  111. assert employee._Employee__calculate_salary() == 800
  112. assert employee.get_employee_details() == 'Name: Джеки Чан, Position: manager, Salary: 800'
  113. employee._set_position('Director')
  114. assert employee.get_employee_details() == 'Name: Джеки Чан, Position: Director, Salary: 800'
  115. employee_2 = Employee("Пирс Броснан", 'actor', 35, 30)
  116. assert employee_2._Employee__calculate_salary() == 1050
  117. assert employee_2.get_employee_details() == 'Name: Пирс Броснан, Position: actor, Salary: 1050'
  118. print('Good')
  119. if __name__ == '__main__':
  120. main()