123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- class BankAccount:
- def __init__(self, name, balance, passport) -> None:
- self._name = name
- self._balance = balance
- self._passport = passport
- self.__name = name
- self.__balance = balance
- self.__passport = passport
- def print_protected_data(self):
- print(self._name, self._balance, self._passport)
- # Инкапсуляция - сокрытие данных
- def print_private_data(self):
- print(self.__name, self.__balance, self.__passport)
- def __private_method(self):
- pass
- class Student:
- def __init__(self, name, age, branch) -> None:
- self.__name = name
- self.__age = age
- self.__branch = branch
- def __display_details(self):
- print(f'Имя: {self.__name}\n Возраст: {self.__age}\n Направление: {self.__branch}')
- def access_private_method(self):
- self.__display_details()
- class BankDeposit:
- def __init__(self, name, balance, rate):
- self.name = name
- self.balance = balance
- self.rate = rate
- def __calculate_profit(self):
- return self.balance/100*self.rate
-
- def get_balance_with_profit(self):
- return self.balance + self.__calculate_profit()
-
- class Library:
- def __init__(self, books: list) -> None:
- self.__books = books
- def __check_availability(self, book_name):
- return True if book_name in self.__books else False
- def search_book(self, book_name):
- return self.__check_availability(book_name)
-
- def return_book(self, book_name):
- self.__books.append(book_name)
- def _checkout_book(self, book_name):
- if self.__check_availability(book_name):
- self.__books.remove(book_name)
- return True
- else:
- return False
-
- class Employee:
- def __init__(self, name, position, hours_worked, hourly_rate):
- self.name = name
- self.__position = position
- self.__hours_worked = hours_worked
- self.__hourly_rate = hourly_rate
- def __calculate_salary(self):
- return self.__hours_worked*self.__hourly_rate
-
- def _set_position(self, position):
- self.__position = position
- def get_position(self):
- return self.__position
-
- def get_salary(self):
- return self.__calculate_salary()
-
- def get_employee_details(self):
- return f"Name: {self.name}, Position: {self.__position}, Salary: {self.__calculate_salary()}"
- def main():
- '''
- account1 = BankAccount('Bob', 1000, 2312312312)
- account1.print_protected_data()
- account1.print_private_data()
- print(dir(account1))
- # Доступ к приватным атрибутам и методам
- account1._BankAccount__name = 'sdfsasd'
- print(account1._BankAccount__name)
- '''
- '''
- account = BankDeposit("John Connor", 1000, 5)
- assert account.name == "John Connor"
- assert account.balance == 1000
- assert account.rate == 5
- assert account._BankDeposit__calculate_profit() == 50.0
- assert account.get_balance_with_profit() == 1050.0
- account_2 = BankDeposit("Sarah Connor", 200, 27)
- assert account_2.name == "Sarah Connor"
- assert account_2.balance == 200
- assert account_2.rate == 27
- assert account_2._BankDeposit__calculate_profit() == 54.0
- assert account_2.get_balance_with_profit() == 254.0
- print('Good')
- '''
- '''
- library = Library(["War and Peace", "Moby-Dick", "Pride and Prejudice"])
- assert library._Library__books == ["War and Peace", "Moby-Dick", "Pride and Prejudice"]
- assert library.search_book("Moby-Dick") == True
- assert library.search_book("Jane Air") == False
- assert library._Library__check_availability("War and Peace") == True
- assert library._checkout_book("Moby-Dick") == True
- assert library._Library__books == ["War and Peace", "Pride and Prejudice"]
- assert library.search_book("Moby-Dick") == False
- assert library.return_book("Moby-Dick") is None
- assert library._Library__books == ["War and Peace", "Pride and Prejudice", "Moby-Dick"]
- assert library.search_book("Moby-Dick") == True
- print('Good')
- '''
- employee = Employee("Джеки Чан", 'manager', 20, 40)
- assert employee.name == 'Джеки Чан'
- assert employee._Employee__hours_worked == 20
- assert employee._Employee__hourly_rate == 40
- assert employee._Employee__position == 'manager'
- assert employee.get_position() == 'manager'
- assert employee.get_salary() == 800
- assert employee._Employee__calculate_salary() == 800
- assert employee.get_employee_details() == 'Name: Джеки Чан, Position: manager, Salary: 800'
- employee._set_position('Director')
- assert employee.get_employee_details() == 'Name: Джеки Чан, Position: Director, Salary: 800'
- employee_2 = Employee("Пирс Броснан", 'actor', 35, 30)
- assert employee_2._Employee__calculate_salary() == 1050
- assert employee_2.get_employee_details() == 'Name: Пирс Броснан, Position: actor, Salary: 1050'
- print('Good')
- if __name__ == '__main__':
- main()
|