SRP in Payroll System
This program applies SRP to payroll system.
Problem Statement
Write a Java program where SalaryCalculator handles salary calculation and SalaryPrinter handles printing.
Source Code
| 1 | class Employee { |
| 2 | String name; double basic; |
| 3 | Employee(String n,double b){ name=n; basic=b; } |
| 4 | } |
| 5 | class SalaryCalculator { |
| 6 | double calculate(Employee e){ return e.basic+0.2*e.basic; } |
| 7 | } |
| 8 | class SalaryPrinter { |
| 9 | void print(Employee e,double salary){ |
| 10 | System.out.println(e.name+"'s salary: "+salary); |
| 11 | } |
| 12 | } |
| 13 | public class SRPPayroll { |
| 14 | public static void main(String[] args) { |
| 15 | System.out.println("Eduinq SRP Payroll"); |
| 16 | Employee e=new Employee("Arun",10000); |
| 17 | SalaryCalculator sc=new SalaryCalculator(); |
| 18 | SalaryPrinter sp=new SalaryPrinter(); |
| 19 | double sal=sc.calculate(e); |
| 20 | sp.print(e,sal); |
| 21 | } |
| 22 | } |
Program Output
Eduinq SRP Payroll Arun's salary: 12000.0
Explanation
SRP separates responsibilities: calculation vs printing. - Each class has one reason to change. - This improves maintainability and clarity in larger systems. Conclusion: Shows SRP applied to payroll system.