Pages

April 26, 2016

Part 2: Scenario Based Java Design Pattern Interview Questions & Answers

Scenario 1

  • A company named XYZ Retail is in the business of selling Books, CD's and Cosmetics. 
  • Books are sales tax exempt and CDs and Cosmetics have a sales tax of 10%. 
  • CD's can be imported and attracts an import tax of 5%. 
  • Write a simple shopping basket program, which will calculate extended price (qty * (unitprice + tax)) inclusive of tax for each item in the basket?

Java Solution
We will first create the TaxCalculator interface and its implementation TaxCalculatorImpl.



Goods class is abstract as it implements the common behavior of Book, CDs, and Cosmetics. Also it composes (i.e composition) 'TaxCalculator' to perform the tax calculation based on if the good is imported and if sales tax is applicable.

Now, we will create Book, CDs, and Cosmetics classes that extends the shared behavior (inherits the shared behavior of Goods). The isTaxable & isImported are specific to the product. We can add new products in the future with specific taxable & importable behaviors.



-K Himaanshu Shuklaa

No comments:

Post a Comment