SOLID Principles · Coding problem

Payslips: Calculate, Format, Archive

A free coding problem from the Single Responsibility Principle skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

A payroll system used to have one Payslip class that computed tax, formatted the text and stored the result. Three different teams kept editing it - finance (tax rules), design (the layout) and records (storage) - and their changes kept colliding.

It has been split so each class has one reason to change. The coordinating Solution class is written for you; implement the three single-responsibility classes:

  • PayCalculator (finance): tax(gross) = gross * taxPercent / 100, rounded down; net(gross) = gross - tax(gross)
  • PayslipFormatter (design): format(name, gross, tax, net) returns <name> | gross <g> | tax <t> | net <n>
  • PayslipArchive (records): store(name, payslip) keeps every payslip per employee in order; count(name) returns how many they have (0 if none); latest(name) returns the most recent one

Notice how the TAX command - a finance change - replaces only the calculator. Nothing else is touched.

Commands: PAY <name> <gross>, TAX <percent> (the rate starts at 10%), HISTORY <name>.

Example

Input:
5
PAY asha 50000
PAY ravi 32150
TAX 20
PAY asha 50000
HISTORY asha

Output:
asha | gross 50000 | tax 5000 | net 45000
ravi | gross 32150 | tax 3215 | net 28935
tax rate 20%
asha | gross 50000 | tax 10000 | net 40000
asha has 2 payslip(s), latest: asha | gross 50000 | tax 10000 | net 40000

Constraints

  • At most 1000 commands; 0 <= gross <= 10^9; 0 <= percent <= 100; names are single words.

How it's graded

  • Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Single Responsibility Principle

Solve it Payslips: Calculate, Format, Archive free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime