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...

PHP OOPs exercise - Basic Oops

  Here are key PHP OOP (Object-Oriented Programming) exercise questions with solutions: Basic Class and Object Exercise: // Create a simple bank account class class BankAccount {     private $accountNumber;     private $balance;     public function __construct($accountNumber, $initialBalance = 0) {         $this->accountNumber = $accountNumber;         $this->balance = $initialBalance;     }     public function deposit($amount) {         if ($amount > 0) {             $this->balance += $amount;             return true;         }         return false;  ...

Interview questions for Senior PHP Developer particle41.com

1.Self Introduction 2.Basic questions on session and cookie. 3.Where is session stored? 4.Difference between Cookie and session. 5.Will there be any session before session start? 6.Post Max execution time.How can we modify it? 7.We have a string, "BJFSJK".Without any php function reverse it with half the string length.   To reverse the string with half the string length without using any PHP functions, you can implement a simple algorithm to achieve the desired result. Here's how you can do it: Initialize two pointers, one at the beginning of the string and the other at the midpoint of the string. Swap characters between these two pointers iteratively, moving the pointers towards each other until they meet or cross each other. Here's the PHP code to implement this algorithm:  <?php $string = "ABC100"; $length = strlen($string); // Calculate the midpoint of the string $midpoint = (int)($length / 2); // Initialize pointers $start = 0; $end = $length - 1; //...