Provide a good approach for the practical Oops concept.
There should be an employer add facility. Employer can have two types t1 and t2. There should be an option to calculate the tax.
t1 and t2 will have different logics. What approach is good for php Oops?
-----------------
Here's an object-oriented approach using Strategy Pattern for tax calculation:
// Abstract base class for employers
abstract class Employer {
protected $name;
protected $type;
protected $taxCalculator;
public function __construct($name, TaxCalculatorStrategy $taxCalculator) {
$this->name = $name;
$this->taxCalculator = $taxCalculator;
}
// Strategy for tax calculation
public function calculateTax($income) {
return $this->taxCalculator->calculate($income);
}
}
// Tax calculation strategy interface
interface TaxCalculatorStrategy {
public function calculate($income);
}
// Type 1 Employer Tax Calculation
class TypeOneEmployerTax implements TaxCalculatorStrategy {
public function calculate($income) {
// Specific tax logic for Type 1
return $income * 0.1; // 10% tax rate
}
}
// Type 2 Employer Tax Calculation
class TypeTwoEmployerTax implements TaxCalculatorStrategy {
public function calculate($income) {
// Specific tax logic for Type 2
return $income * 0.15; // 15% tax rate
}
}
// Concrete Employer Classes
class TypeOneEmployer extends Employer {
public function __construct($name) {
parent::__construct($name, new TypeOneEmployerTax());
}
}
class TypeTwoEmployer extends Employer {
public function __construct($name) {
parent::__construct($name, new TypeTwoEmployerTax());
}
}
// Usage
$employer1 = new TypeOneEmployer("Tech Corp");
$employer2 = new TypeTwoEmployer("Finance Inc");
echo $employer1->calculateTax(50000); // Calculates with Type 1 tax logic
echo $employer2->calculateTax(50000); // Calculates with Type 2 tax logic
Key benefits of this approach:
Flexible tax calculation strategies
Easy to add new employer types
Follows Open/Closed Principle
Decouples tax calculation logic from employer class
name = $name;
ReplyDelete$this->id = $id;
$this->salary = $salary;
}
// Abstract method for tax calculation
abstract public function calculateTax();
// Getter methods
public function getName() {
return $this->name;
}
public function getId() {
return $this->id;
}
public function getSalary() {
return $this->salary;
}
}
// Concrete implementation for Type 1 Employer
class EmployerTypeOne extends Employer {
public function __construct($name, $id, $salary) {
parent::__construct($name, $id, $salary);
}
public function calculateTax() {
// Type 1 specific tax calculation
$taxableIncome = $this->salary ;
if ($taxableIncome <= 250000) {
return 0; // No tax for income below 250,000
} elseif ($taxableIncome <= 500000) {
return $taxableIncome * 0.05; // 5% tax
} elseif ($taxableIncome <= 1000000) {
return $taxableIncome * 0.20; // 20% tax
} else {
return $taxableIncome * 0.30; // 30% tax for higher incomes
}
}
}
// Concrete implementation for Type 2 Employer
class EmployerTypeTwo extends Employer {
public function __construct($name, $id, $salary ) {
parent::__construct($name, $id, $salary);
}
public function calculateTax() {
// Type 2 specific tax calculation
$taxableIncome = $this->salary ;
if ($taxableIncome <= 300000) {
return 0; // No tax for income below 300,000
} elseif ($taxableIncome <= 600000) {
return $taxableIncome * 0.10; // 10% tax
} elseif ($taxableIncome <= 1200000) {
return $taxableIncome * 0.15; // 15% tax
} else {
return $taxableIncome * 0.35; // 35% tax for higher incomes
}
}
}
$employer = new EmployerTypeOne(
"Sreekala",
"T1-001",
500000
);
print_r($employer->calculateTax() );