Work out the pay for different kinds of staff.
What to build
A parent class and two children that change how pay is worked out.
Requirements
Employee(name, base)haspay(), which returns the base.Manager(name, base, bonus)is paid the base plus the bonus. Usesuper()in its__init__.Intern(name, base)is paid half the base, rounded down.- Print each person as
NAME PAY, and thenTotal T.
Input
The first line has n. Each of the next n lines is employee NAME BASE, manager NAME BASE BONUS or intern NAME BASE.
Output
One line per person, then the total.
Constraints
n is from 1 to 100, and amounts are from 0 to 1000000.
Example
Input:
3
employee Rahim 100
manager Nadia 200 50
intern Karim 101
Output:
Rahim 100
Nadia 250
Karim 50
Total 400
Why it works
Each class knows only its own pay rule, and the total works for any mix of staff, because every object answers pay().
Examples
Example 1 · Input
3 employee Rahim 100 manager Nadia 200 50 intern Karim 101
Expected output
Rahim 100 Nadia 250 Karim 50 Total 400
Example 2 · Input
1 intern A 0
Expected output
A 0 Total 0
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Employee stores name and base, and has pay() returning base.
Hint 2
Manager's __init__ calls super().__init__(name, base) and stores the bonus.
Hint 3
Intern's pay is base // 2.
Solution
Show one correct solution (try it yourself first)
class Employee:
def __init__(self, name, base):
self.name = name
self.base = base
def pay(self):
return self.base
class Manager(Employee):
def __init__(self, name, base, bonus):
super().__init__(name, base)
self.bonus = bonus
def pay(self):
return self.base + self.bonus
class Intern(Employee):
def pay(self):
return self.base // 2
staff = []
n = int(input())
for _ in range(n):
parts = input().split()
if parts[0] == "manager":
staff.append(Manager(parts[1], int(parts[2]), int(parts[3])))
elif parts[0] == "intern":
staff.append(Intern(parts[1], int(parts[2])))
else:
staff.append(Employee(parts[1], int(parts[2])))
for person in staff:
print(person.name, person.pay())
print("Total", sum(p.pay() for p in staff))