Sales Tax case study design.

Do we have any design for sales tax problem of thoughtworks. Basically I would like to know design aspects (applying OO principles along with design patterns) approaching the problem.
case-studies.txt
ramanursAsked:
Who is Participating?
 
DarrenDConnect With a Mentor Commented:
Hi,

I would not have a class called TaxableProduct. All products have a tax so you don't need a specialised class to handle it. E.g. You may have a Medical Product with no tax but the tax value is simply 0.00. If the Medical Product is Imported then the sales Tax is applied. So I would create a tax field withing the class to store the tax in the base class. Also an 'IsImport' field can be put into the Product class.

Usually a derived class will be a special type of product so you could have products as seperate classes such as Book, Food, Medical, Clothing, Technical but this would be to show the different properties that each product would have E.g Book as an Author, ISBN etc.. and Clothing will have Style, Size, colour etc...

Then you can use your interface 'ITax' that is implemented by a Tax class which has a CalculateTax method which accepts a Product class as a parameter. Then based on the Product you can calculate the sales tax and importation tax on it.

Does this make sense to you?

Hope this helps,

Darren
0
 
DarrenDCommented:
Hi,

I've read your question and your attachment. What exactly do you require?

Design Patterns:
There are lots of design patterns defined for various scenarios. For example for your problem maybe look at Model View controller (MVP) pattern.

OO Principles:
You will have to look up some examples on the web as there is too much material already on the web for this.

There are books written on the two areas mentioned above so you need to refine your question

Hope this helps,

Darren
0
 
ramanursAuthor Commented:
Thanks Darren for your reply. I have read many articles and books regarding OO principles and design patterns, but the point here is how do you approach solving any problem using this concepts. Hence I have chosen this sales tax problem and expecting a design to it. It would be good enough if I get a domain level class diagram like example as per the problem we can see Good has a class and sub class TaxableGood to represent Goods that will be taxed. I hope I am clear enough about the question and answer I am looking for.

Thanks,
RR
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DarrenDCommented:
Hi,

As this appears to be homework I am not going to give you any answers as such but I will try to help provided you do most of the work and I will simply point out where you are going right or wrong. Does this suit?

Firstly, based on the text document I would say that you would have a product class opposed to a good class. Not that it really matters what you call it

I don't think that you need a taxablegood class. Maybe you could have a taxable property of the good / product  class?

What properties do you have so far for good / product (as I call it)

Cheers,

Darren
class Product
{
  String taxable;
 
  Product() 
  {
    taxable = String.Empty
  }
}

Open in new window

0
 
ramanursAuthor Commented:
I am ok with it.

As you said lets say it has Product. As per the problem it shows two types of taxes, but it is always possible (change in requirements or new requirement) that a new type of tax can be applicable to any type of Product. As some Products have taxes and some don't, the way we calculate cost of Product differs. Hence I thought it would be a good idea to separate it out. Each class has its own responsibility to calculate the cost now.

Regards,
RR

 
class Product
{
    protected int id;
    protected String name;
    protected double price;
    protected ProductTypeEnum type;
    private boolean taxesApplied ;
 
    public Product(int id, String name, double price, ProductTypeEnum type, boolean taxesApplied)
    {
        this.id = id;
        this.name = name;
        this.price = price;
        this.type = type;
        this.taxesApplied = taxesApplied;
    }
 
    public double computeCost()
    {
        return price;
    }
}
 
interface Taxable
{
    public double getTax();
}
 
class TaxableProduct extends Product implements Taxable
{
    private List<Tax> applicableTaxes;
    
    public TaxableProduct(int id, String name, double price, ProductTypeEnum type)
    {
        super(id, name, price, type, true);
        applicableTaxes = new ArrayList<Tax>();
    }
    
    public double getTax()
    {
        int size = applicableTaxes.size();
        double taxAmount = 0f;
        Tax taxType = null;
        for( int i=0; i<size; i++)
        {
            taxType = applicableTaxes.get(i);
            taxAmount += taxType.computeTax();
        }
        return Utilities.currencyRound(taxAmount, 2);
    }
    
    public void addApplicableTax(Tax tax)
    {
        applicableTaxes.add(tax);
    }
 
    @Override
    public double computeCost()
    {
        return getPrice() + getTax();
    }
}

Open in new window

0
 
ramanursAuthor Commented:
I feel its kind of wrong doing just based on some value if tax is applicable or not. And more over if we have to apply multiple taxes and want to know what tax type it is and tax value individually we need it. Saying that I also agree if we can pull taxes into Product class itself instead of TaxableProduct, the only point I had in mind was that are we unnecessary loading non-taxable product objects with these properties?

Anyways thanks.

Regards,
RR
0
 
DarrenDCommented:
Hi,

In answer to your question I would think that every product has a tax value. My reasoning behind this is that all of the products are taxable depending on whether they are imported or not.  I don't think that you are 'loading non-taxable product objects with these properties' by having a tax field. It's definitely less overhead than creating a taxable class for every taxable product.

Would you then have another class for ImportedProduct and another for an ImportedTaxableProduct?

These are just my suggestions...

Cheers,

Darren

0
 
ramanursAuthor Commented:
Answering the question "Would you then have another class for ImportedProduct and another for an ImportedTaxableProduct?" TaxableProduct class has a property "List<Tax> applicableTaxes;" along with addTax(Tax aTax); api. By this it is easily scalable to new type of taxes. You just need to add new type of tax in TaxTypeEnum for sake of tight type casting by value.

Thanks Darren, I am also looking for suggestions and more kind of technical discussion to figure out any best possible solution.

Thanks,
RR
0
 
Sudipto80Commented:
Hi ramanurs,
                    Looks like you wanted to implement "Decorator Pattern" for the problem. Could you please give the definitions for the following classes

Tax
Utilities.currencyRound

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.