SimeonTs

SUPyF Basics OOP Principles - 03. Mankind

Aug 12th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.92 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1556#2
  4.  
  5. SUPyF Basics OOP Principles - 03. Mankind
  6.  
  7. Problem:
  8. Your task is to model an application. It is very simple.
  9. The mandatory models of our application are 3:  Human, Worker and Student.
  10. The parent class – Human should have first name and last name. Every student has a faculty number.
  11. Every worker has a week salary and work hours per day.
  12. It should be able to calculate the money he earns by an hour. You can see the constraints below.
  13. Input
  14. On the first input line, you will be given info about a single student - a name and faculty number.
  15. On the second input line, you will be given info about a single worker - first name, last name, salary and working hours.
  16. Output
  17. You should print the info about the student first,
  18. followed by a single blank line and after that the info about the worker in the given formats:
  19. - Print the student info in the following format:
  20.    First Name: {student's first name}
  21.    Last Name: {student's last name}
  22.    Faculty number: {student's faculty number}
  23.  
  24. - Print the worker info in the following format:
  25.    First Name: {worker's first name}
  26.    Last Name: {worker's second name}
  27.    Week Salary: {worker's salary}
  28.    Hours per day: {worker's working hours}
  29.    Salary per hour: {worker's salary per hour}
  30. Print exactly two digits after every double value's decimal separator (e.g. 10.00).
  31. Consider the workweek from Monday to Friday. A faculty number should be consisted only of digits and letters.
  32.  
  33. Constraints:
  34. -----------------------------------------------------------------------------------------------------------------------
  35. Parameter:        |Constraint                         |Exception Message
  36. -----------------------------------------------------------------------------------------------------------------------
  37. Human first name  |Should start with a capital letter |"Expected upper case letter! Argument: firstName"
  38. Human first name  |Should be more than 3 symbols      |"Expected length at least 4 symbols! Argument: firstName"
  39. Human last name   |Should start with a capital letter |"Expected upper case letter! Argument: lastName"
  40. Human last name   |Should be more than 2 symbols      |"Expected length at least 3 symbols! Argument: lastName "
  41. Human last name   |Should be in range [5..10] symbols |"Invalid faculty number!"
  42. Week salary       |Should be more than 10             |"Expected value mismatch! Argument: weekSalary"
  43. Working hours     |Should be in the range [1..12]     |"Expected value mismatch! Argument: workHoursPerDay"
  44. -----------------------------------------------------------------------------------------------------------------------
  45.  
  46. Examples:
  47.    Input:
  48.        Ivan Ivanov 08
  49.        Pesho Kirov 1590 10
  50.        Stefo Mk321 0812111
  51.        Ivcho Ivancov 1590 10
  52.    Output:
  53.        Invalid faculty number!
  54.        First Name: Stefo
  55.        Last Name: Mk321
  56.        Faculty number: 0812111
  57.        
  58.        First Name: Ivcho
  59.        Last Name: Ivancov
  60.        Week Salary: 1590.00
  61.        Hours per day: 10.00
  62.        Salary per hour: 31.80
  63. """
  64.  
  65.  
  66. class Human:
  67.     def __init__(self, first_name, last_name):
  68.         self.first_name = first_name
  69.         self.last_name = last_name
  70.  
  71.     @property
  72.     def first_name(self):
  73.         return self._first_name
  74.  
  75.     @property
  76.     def last_name(self):
  77.         return self._last_name
  78.  
  79.     @first_name.setter
  80.     def first_name(self, firstname):
  81.         if firstname[0].isupper():
  82.             self._first_name = firstname
  83.         else:
  84.             raise Exception("Expected upper case letter! Argument: firstName")
  85.         if len(firstname) > 3:
  86.             self._first_name = firstname
  87.         else:
  88.             raise Exception("Expected length at least 4 symbols! Argument: firstName")
  89.  
  90.     @last_name.setter
  91.     def last_name(self, lastname):
  92.         if lastname[0].isupper():
  93.             self._last_name = lastname
  94.         else:
  95.             raise Exception("Expected upper case letter! Argument: lastName")
  96.         if len(lastname) > 2:
  97.             self._last_name = lastname
  98.         else:
  99.             raise Exception("Expected length at least 3 symbols! Argument: lastName")
  100.  
  101.  
  102. class Student(Human):
  103.     def __init__(self, first_name, last_name, faculty_number):
  104.         Human.__init__(self, first_name, last_name)
  105.         self.faculty_number = faculty_number
  106.  
  107.     @property
  108.     def faculty_number(self):
  109.         return self._faculty_number
  110.  
  111.     @faculty_number.setter
  112.     def faculty_number(self, faculty_number):
  113.         if 5 <= len(faculty_number) <= 10 and faculty_number.isalnum():
  114.             self._faculty_number = faculty_number
  115.         else:
  116.             raise Exception("Invalid faculty number!")
  117.  
  118.     def __str__(self):
  119.         return f"First Name: {self.first_name}" + "\n" + f"Last Name: {self.last_name}" + "\n" + \
  120.                f"Faculty number: {self.faculty_number}"
  121.  
  122.  
  123. class Worker(Human):
  124.     def __init__(self, first_name, last_name, week_salary, work_hours_per_day):
  125.         Human.__init__(self, first_name, last_name)
  126.         self.week_salary = week_salary
  127.         self.work_hours_per_day = work_hours_per_day
  128.         self.salary_per_hour = week_salary / 5 / work_hours_per_day
  129.  
  130.     @property
  131.     def week_salary(self):
  132.         return self._week_salary
  133.  
  134.     @property
  135.     def work_hours_per_day(self):
  136.         return self._work_hours_per_day
  137.  
  138.     @week_salary.setter
  139.     def week_salary(self, week_salary):
  140.         if week_salary > 10:
  141.             self._week_salary = week_salary
  142.         else:
  143.             raise Exception("Expected value mismatch! Argument: weekSalary")
  144.  
  145.     @work_hours_per_day.setter
  146.     def work_hours_per_day(self, work_hours_per_day):
  147.         if 1 <= int(work_hours_per_day) <= 12:
  148.             self._work_hours_per_day = work_hours_per_day
  149.         else:
  150.             raise Exception("Expected value mismatch! Argument: workHoursPerDay")
  151.  
  152.     def __str__(self):
  153.         return \
  154.             f"First Name: {self.first_name}" + "\n" + \
  155.             f"Last Name: {self.last_name}" + "\n" + \
  156.             f"Week Salary: {self.week_salary:.2f}" + "\n" + \
  157.             f"Hours per day: {self.work_hours_per_day:.2f}" + "\n" + \
  158.             f"Salary per hour: {self.salary_per_hour:.2f}"
  159.  
  160.  
  161. s_d = input().split()
  162. student_first_name = s_d[0]
  163. student_last_name = s_d[1]
  164. student_number = s_d[2]
  165.  
  166. w_d = input().split()
  167. worker_f_name = w_d[0]
  168. worker_l_name = w_d[1]
  169. worker_week_sel = float(w_d[2])
  170. worker_w_h_p_d = float(w_d[3])
  171.  
  172. its_ok = False
  173.  
  174. try:
  175.     st = Student(first_name=student_first_name, last_name=student_last_name, faculty_number=student_number)
  176.     its_ok = True
  177.     wo = Worker(first_name=worker_f_name, last_name=worker_l_name, week_salary=worker_week_sel,
  178.                 work_hours_per_day=worker_w_h_p_d)
  179.     if its_ok:
  180.         print(st)
  181.         print()
  182.         print(wo)
  183. except Exception as e:
  184.     print(str(e))
  185.     its_ok = False
Add Comment
Please, Sign In to add comment