abs.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from abc import ABC, abstractmethod
  2. class Employee(ABC):
  3. def __init__(self, name):
  4. pass
  5. @abstractmethod
  6. def calculate_salary(self):
  7. pass
  8. class HourlyEmployee(Employee):
  9. def __init__(self, hours_worked, hourly_rate):
  10. self.hours_worked = hours_worked
  11. self.hourly_rate = hourly_rate
  12. def calculate_salary(self, ):
  13. return self.hours_worked * self.hourly_rate
  14. class SalariedEmployee(Employee):
  15. def __init__(self, monthly_salary):
  16. self.monthly_salary = monthly_salary
  17. def calculate_salary(self):
  18. return self.monthly_salary
  19. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. class Datebase(ABC):
  21. def __init__(self):
  22. pass
  23. @abstractmethod
  24. def connect(self):
  25. pass
  26. @abstractmethod
  27. def disconnect(self):
  28. pass
  29. @abstractmethod
  30. def execute(self):
  31. pass
  32. class MySQLDatabase(Datebase):
  33. def connect(self):
  34. print("Connecting to MySQL database...")
  35. def disconnect(self):
  36. print("Disconnecting from MySQL database...")
  37. def execute(self, query):
  38. print(f"Executing query '{query}' in MySQL database...")
  39. class PostgreSQLDatabase(Datebase):
  40. def connect(self):
  41. print("Connecting to PostgreSQL database...")
  42. def disconnect(self):
  43. print("Disconnecting from PostgreSQL database...")
  44. def execute(self, query):
  45. print(f"Executing query '{query}' in PostgreSQL database...")
  46. def main():
  47. mysql_db = MySQLDatabase()
  48. postgresql_db = PostgreSQLDatabase()
  49. mysql_db.connect()
  50. mysql_db.execute(
  51. "SELECT * FROM customers;")
  52. mysql_db.disconnect()
  53. postgresql_db.connect()
  54. postgresql_db.execute(
  55. "SELECT * FROM customers;")
  56. postgresql_db.disconnect()
  57. '''
  58. hourly_employee = HourlyEmployee(100, 25)
  59. assert hourly_employee.hours_worked == 100
  60. assert hourly_employee.hourly_rate == 25
  61. assert hourly_employee.calculate_salary() == 2500
  62. salaried_employee = SalariedEmployee(4000)
  63. assert salaried_employee.monthly_salary == 4000
  64. assert salaried_employee.calculate_salary() == 4000
  65. print('Good')
  66. '''
  67. if __name__ == '__main__':
  68. main()