ThoughtWorks Interview Question
Senior Software Development EngineersCountry: India
Interview Type: Written Test
Decorator and Visitor seem relevant here. Decorator looks more suited out of the 2... as type of taxes can vary in nature.
// Abstract class Tax
Class Item {
String Category;
String Name;
double Price;
public:
Item () { Category="default"; Name="item"; Price=0.0; }
Item (String ct, String nm, double pr) { Category=ct; Name=nm; Price=pr; }
virtual double getPrice() { return Price;}
virtual double setPrice(double d) { this.Price = d;}
};
virtual double applyTax (Tax taxType) { };
}; // End of Item class
// Abstract class Tax
class Tax {
double rate;
public:
Tax () { }
Tax (double rt) { rate = rt;}
virtual Item getPriceAfterTax(Item& item) {
item.setPrice(item.getPrice*(1+(rate/100)));
return item;
}
};
This problem statement can be solved using with Abstract Factory and Strategy Design Pattern.
- GUL MD ERSHAD November 05, 2017Find full code in github : gmershad
Sales-Tax
https://github.com/gmershad/Sales-Tax.git