Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

Recently I was trying for a company ‘x’. They send me some set of questions and told me to solve only one.

The problem is like this -

Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions. When I purchase items I receive a receipt which lists the name of all the items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid. The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest 0.05) amount of sales tax.

“They told me, they are interested in the DESIGN ASPECT of your solution and would like to evaluate my OBJECT ORIENTED PROGRAMMING SKILLS.”

So I provided below code – you can just copy paste code and run in VS.
C#
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                double totalBill = 0, salesTax = 0;
                List<Product> productList = getProductList();
                foreach (Product prod in productList)
                {
                    double tax = prod.ComputeSalesTax();
                    salesTax += tax;
                    totalBill += tax + (prod.Quantity * prod.ProductPrice);
                    Console.WriteLine(string.Format("Item = {0} : Quantity = {1} : Price = {2} : Tax = {3}", prod.ProductName, prod.Quantity, prod.ProductPrice + tax, tax));
                }
                Console.WriteLine("Total Tax : " + salesTax);
                Console.WriteLine("Total Bill : " + totalBill);                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        private static List<Product> getProductList()
        {
            List<Product> lstProducts = new List<Product>();
            //input 1
            lstProducts.Add(new Product("Book", 12.49, 1, ProductType.ExemptedProduct, false));
            lstProducts.Add(new Product("Music CD", 14.99, 1, ProductType.TaxPaidProduct, false));
            lstProducts.Add(new Product("Chocolate Bar", .85, 1, ProductType.ExemptedProduct, false));

            //input 2
            //lstProducts.Add(new Product("Imported Chocolate", 10, 1, ProductType.ExemptedProduct,true));
            //lstProducts.Add(new Product("Imported Perfume", 47.50, 1, ProductType.TaxPaidProduct,true));

            //input 3
            //lstProducts.Add(new Product("Imported Perfume", 27.99, 1, ProductType.TaxPaidProduct,true));
            //lstProducts.Add(new Product("Perfume", 18.99, 1, ProductType.TaxPaidProduct,false));
            //lstProducts.Add(new Product("Headache Pills", 9.75, 1, ProductType.ExemptedProduct,false));
            //lstProducts.Add(new Product("Imported Chocolate", 11.25, 1, ProductType.ExemptedProduct,true));
            return lstProducts;
        }
    }

    public enum ProductType
    {
        ExemptedProduct=1,
        TaxPaidProduct=2,
        //ImportedProduct=3
    }

    class Product
    {
        private ProductType _typeOfProduct = ProductType.TaxPaidProduct;
        private string _productName = string.Empty;
        private double _productPrice;
        private int _quantity;
        private bool _isImportedProduct = false;

        public string ProductName { get { return _productName; } }
        public double ProductPrice { get { return _productPrice; } }
        public int Quantity { get { return _quantity; } }

        public Product(string productName, double productPrice,int quantity, ProductType type, bool isImportedProduct)
        {
            _productName = productName;
            _productPrice = productPrice;
            _quantity = quantity;
            _typeOfProduct = type;
            _isImportedProduct = isImportedProduct;
        }

        public double ComputeSalesTax()
        {
            double tax = 0;
            if(_isImportedProduct) //charge 5% tax directly
                tax+=_productPrice*.05;
            switch (_typeOfProduct)
            {
                case ProductType.ExemptedProduct: break;
                case ProductType.TaxPaidProduct:
                    tax += _productPrice * .10;
                    break;
            }
            return Math.Round(tax, 2);
            //round result before returning
        }
    }

you can uncomment input and run for different inputs.

I provided the solution but I was rejected.

"They said, they are unable to consider me for our current open positions because code solution is not satisfactory."

Please guide me what is missing here. Is this solution is not a good OOAD solution.
How can I improve my OOAD skills.
My seniors also says perfect OOAD application will also not work practically.

Thanks
Posted
Updated 17-Feb-12 20:15pm
v3
Comments
BillWoodruff 18-Feb-12 6:09am    
Wish I could help here, but what an unknown company was looking for in your rendering this question into OO design: I can't guess. Did they give you any clues beyond what you report here ? Is it possible they wanted you to come back with a multi-tier design that reflected a back-end database of product types (name, cost, exempt, imported), which was then accessed by some mid-level code entity, and, finally, presented in some kind of UI which allowed one to quickly select a product, specify the quantity, add the product/quantity to some kind of "shopping cart" ui, and then present a total. Did they mention using .NET, WinForms, ASP.NET, use of an intranet, or access to a Cloud based database ?
sunder.tinwar 19-Feb-12 12:28pm    
Hi Bill,

thanks for reply, this what they(their own words) said in mail when they asked this question -

For the solution, we would want you use either Java, Ruby or C#.

·We are interested in the DESIGN ASPECT of your solution and would like to evaluate your

OBJECT ORIENTED PROGRAMMING SKILLS.

·You may use external libraries or tools for building or testing purposes. Specifically, you may use

unit testing libraries or build tools available for your chosen language (e.g., JUnit, Ant, NUnit, NAnt,

Test::Unit, Rake etc.)

·Optionally, you may also include a brief explanation of your design and assumptions along with

your code.

·Kindly note that we are NOT expecting a web-based application or a comprehensive UI. Rather,

we are expecting a simple, console based application and interested in your source code.

1 solution

Being fair i'd say it's not the nicest code i've seen an i but it seems to do as requested. Now the difficulty, when you are writing code for someone to evaluate what you're level might be, i'd draw up a rather extensive model with e.g. a data manager, calculation engine and more stuff you might thing of. I would also make damn sure all was properly unit tested and provide the test code, the test code would certainly have 100% coverage.
The goal in this case is not to show that you can meet the minimal requirements but also not to overdo it to much either.
One hint; use design patterns and keep to the naming conventions.

I guess these are my two cents for now.

Hope it helps,

Cheers, AT
 
Share this answer
 
Comments
sunder.tinwar 25-Feb-12 9:05am    
thanks for reply....it was my first code.....

how is this, as i corrected it with a friends comments on my design

public abstract class Product
{
protected string _productName = string.Empty;
protected double _productPrice;

public string ProductName { get { return _productName; } }
public double ProductPrice { get { return _productPrice; } }
}

public class ExemptedProduct : Product
{
public ExemptedProduct(string productName, double price)
{
this._productName = productName;
this._productPrice = price;
}
public double ComputeSalesTax()
{
return _productPrice * .05;
}
}

public class TaxPaidProduct : Product
{
public TaxPaidProduct(string productName, double price)
{
this._productName = productName;
this._productPrice = price;
}
public double ComputeSalesTax()
{
return _productPrice * .15;
}
}

public class ShoppingCart
{
List<shoppingcartdatasource> _productList = new List<shoppingcartdatasource>();

class ShoppingCartDataSource
{
private Product _product;
private int _quantity;
private bool _isImportedProduct;

public Product CartProduct { get { return _product; } }
public int Quantity { get { return _quantity; } }
public bool IsImportedProduct { get { return _isImportedProduct; } }

public ShoppingCartDataSource(Product product, int quantity, bool isImportedProduct)
{
_product = product;
_quantity = quantity;
_isImportedProduct = isImportedProduct;
}
}

public void AddProduct(Product product, int quantity, bool isImportedProduct)
{
_productList.Add(new ShoppingCartDataSource(product,quantity,isImportedProduct));
}

public void GenerateCartBill()
{
double totalTax = 0;
double netTotal = 0;
foreach (var product in _productList)
{
double tax=computeSalesTax(product);
Console.WriteLine(product.CartProduct.ProductName + " | " + (product.CartProduct.ProductPrice + tax) + " | " + product.Quantity);
totalTax += tax;
netTotal += product.CartProduct.ProductPrice + tax;
}
Console.WriteLine("Total Tax : " + totalTax);
Console.WriteLine("Total : " + netTotal);
}

private double computeSalesTax(ShoppingCartDataSource cartItem)
{
double tax = 0;
if (cartItem.CartProduct is ExemptedProduct)
{
if (cartItem.IsImportedProduct)
tax = (cartItem.Quantity * cartItem.CartProduct.ProductPrice) * .05;
}
else if (cartItem.CartProduct is TaxPaidProduct)
{
if (cartItem.IsImportedProduct)
tax = (cartItem.Quantity * cartItem.CartProduct.ProductPrice) * .15;
else
tax = (cartItem.Quantity * cartItem.CartProduct.ProductPrice) * .10;
}
return Math.Round(tax,2);
}
}

public class Program
{
public static void Main()
{
ShoppingCart cart = new ShoppingCart();
cart.AddProduct(new TaxPaidProduct("Imported Perfume", 27.99), 1, true);
cart.AddProduct(new TaxPaidProduct("Perfume", 18.99), 1, false);
cart.AddProduct(new ExemptedProduct("Headache Pills", 9.75), 1, false);
cart.AddProduct(new ExemptedProduct("Imported Chocolate", 11.25), 1, true);

cart.GenerateCartBill();
}
}

Please guide how can i improve my OOAD skills....thanks.
Addy Tas 25-Feb-12 17:21pm    
I think this really is a lot better than the previous attempt, showing this i'd give you a try. Naturally you could still extend the example using the factory pattern (know the GOF!!!) but that might take it over the top. As another suggestion that is not related to any OO but more to the coding styles I'd make const's of the magic numbers, taking the magic out, use CamelCase on all the method names (computeSalesTax). I really don't like classes in classes but that might be just me. You may like the internal class declaration here.
One thing that could still use a brush-up is that the shopping cart currently takes care of it's presentation, seperating the BL and it's presentation is always a step forward(unless you don't have to maintain the code :) )

My two cents for now,

Cheers, AT

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900