Skip to main content

Oops concept code test

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


Comments

  1. name = $name;
    $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() );

    ReplyDelete

Post a Comment

Popular posts from this blog

MySQL's ACID compliance

Mysql acid compliance ACID is an acronym that stands for four key properties of database transactions: Atomicity Ensures that a transaction is treated as a single, indivisible unit of work Either all operations within a transaction are completed successfully, or none are If any part of the transaction fails, the entire transaction is rolled back to its previous state Prevents partial updates that could leave the database in an inconsistent state Consistency Guarantees that a transaction brings the database from one valid state to another valid state All data written to the database must adhere to defined rules, constraints, cascades, triggers, and other database integrity mechanisms Ensures that any transaction will not break the database's predefined rules Isolation Determines how and when changes made by one transaction become visible to other transactions Prevents interference between concurrent transactions MySQL provides different isolation levels: Read Uncommitted Read Commit...

Interview questions related to Laravel 8 updates- Laravel Interview questions

 Laravel 8 brought several updates and features to the framework. If you are preparing for an interview and expecting questions related to Laravel 8 updates, here are some potential questions: 1. What are the major features introduced in Laravel 8? Laravel Jetstream: A new application scaffolding for Laravel, providing teams with a starting point for building robust applications. Laravel Breeze: A lightweight and minimalistic front-end starter kit. Model Factory Classes: Introduction of factory classes for model factories, allowing for better organization of data seeding logic. Job Batching: A feature that allows you to easily run a batch of jobs and then perform some action when all the jobs have completed. Dynamic Blade Components: The ability to render Blade components dynamically. 2. Explain the improvements made to the Laravel job queue in version 8. Laravel 8 introduced Job Batching, which allows you to group multiple jobs into a batch and perform actions upon the completion ...