I applied for a Junior .NET Developer Position recently, and I was asked to solve a problem (Sales Tax Program) and I was rejected for some reason. The question goes like this:
Basic sales tax is applicable at a rate of 10% on all goods, except food, medicines and books. For all imported goods, a tax rate of additional 5% is applied irrespective of their type. Generate a receipt which shows names of all the items purchased and their prices (including taxes), finishing with total cost of the items, and total amount sales tax 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 nearest 0.05) amount of sales tax. Write an application to generate receipt with all these details.
They gave me the input to take and output should come:
Input 3:
- 1 imported bottle of perfume at 27.99
- 1 bottle of perfume at 18.99
- 1 packet of headache pills at 9.75
- 1 box of imported chocolates at 11.25
Output 3:
- 1 imported bottle of perfume: 32.19
- 1 bottle of perfume: 20.89
- 1 packet of headache pills: 9.75
- 1 imported box of chocolates: 11.85
- Sales Taxes: 6.70
- Total: 74.68
They gave two more sets of input, but I just didn't put that to save some time.
Please review my code and let me know what's wrong with that and what are the concepts I need to focus on to solve a problem like this.
namespace Products
{
class Program
{
static void Main(string[] args)
{
try
{
// calling the Generatereciept class and invoking Generate method to display all the product details, price, taxes and total after taxes
GenerateReciept generate = new GenerateReciept();
generate.Generate();
}
catch (Exception ex)
{
// writing the exception to console.. we can log it to a database or eventviewer based on the requirements
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
/// <summary>
/// enum for determining whether a product is imported or not
/// </summary>
public enum ProdType
{
Imported = 1,
NonImported = 2
}
/// <summary>
/// enum for determining whether product is tax exempted or not
/// </summary>
public enum TaxStatus
{
Taxable = 1,
NonTaxable = 2
}
/// <summary>
/// contains the product details
/// </summary>
class Product
{
private string _Name;
private TaxStatus _TaxStatus;
private decimal _Price;
private ProdType _Typeofproduct;
public string Name
{
get
{
return _Name;
}
}
public TaxStatus TaxStatus
{
get
{
return _TaxStatus;
}
}
public decimal Price
{
get
{
return _Price;
}
}
public ProdType Typeofproduct
{
get
{
return _Typeofproduct;
}
}
/// <summary>
/// Initializes the fields
/// </summary>
/// <param name="name"></param>
/// <param name="taxStatus"></param>
/// <param name="price"></param>
/// <param name="typeOfProduct"></param>
public Product(string name, TaxStatus taxStatus, decimal price, ProdType typeOfProduct)
{
_Name = name;
_TaxStatus = taxStatus;
_Price = price;
_Typeofproduct = typeOfProduct;
}
}
/// <summary>
/// maintains product list
/// </summary>
class ProductsList
{
/// <summary>
/// Based on the input given, added products and their respective info to the list
/// </summary>
/// <returns></returns>
public List<Product> GetProducts()
{
List<Product> list = new List<Product>
{
//input 1
new Product("Book", TaxStatus.NonTaxable, 12.49M, ProdType.NonImported),
new Product("MusicCD", TaxStatus.Taxable, 14.99M, ProdType.NonImported),
new Product("ChocolateBar", TaxStatus.NonTaxable, 0.85M, ProdType.NonImported),
////input 2
//new Product("Imported box of Chocolates", TaxStatus.NonTaxable, 10.00M, ProdType.Imported),
//new Product("Imported bottle of Perfume", TaxStatus.Taxable, 47.50M, ProdType.Imported),
////input 3
//new Product("Imported bottle of Perfume", TaxStatus.Taxable, 27.99M, ProdType.Imported),
//new Product("Bottle of Perfume", TaxStatus.Taxable, 18.99M, ProdType.NonImported),
//new Product("Packet of headache Pills", TaxStatus.NonTaxable, 9.75M, ProdType.NonImported),
//new Product("Box of Imported Chocolates", TaxStatus.NonTaxable, 11.25M, ProdType.Imported)
};
return list;
}
}
/// <summary>
/// declares a method for calculating basic sales tax
/// </summary>
interface IbasicTax
{
decimal CalculateBasicTax();
}
/// <summary>
/// declares an method for calculating full tax( basic sales tax + additional tax for imported goods)
/// </summary>
interface IFullTax
{
decimal CalculateFullTax();
}
/// <summary>
/// declares method for calculating imported tax only( non taxable imported products)
/// </summary>
interface IImportedTax
{
decimal CalculateAdditionalTax();
}
/// <summary>
/// class inheriting from all the interfaces and implementing them
/// </summary>
class Tax : IFullTax, IbasicTax, IImportedTax
{
decimal tax = 0;
decimal _price;
public Tax(decimal price)
{
_price = price;
}
public decimal CalculateBasicTax()
{
return tax += 0.1M * _price;
}
public decimal CalculateAdditionalTax()
{
return tax += 0.05M * _price;
}
public decimal CalculateFullTax()
{
return tax += 0.15M * _price;
}
}
class GenerateReciept
{
decimal TotalSum = 0, TotalTaxes = 0;
/// <summary>
/// Generates the products reciept based on the taxes imposed on the products... calculates and displays in an order suggested
/// </summary>
public void Generate()
{
// Program program = new Program();
ProductsList list = new ProductsList();
List<Product> products = list.GetProducts();
foreach (Product p in products)
{
decimal a = 0;
decimal Taxamount = 0;
if (p.TaxStatus == TaxStatus.Taxable)
{
if (p.Typeofproduct == ProdType.Imported)
{
IFullTax tax = new Tax(p.Price);
a = tax.CalculateFullTax();
}
else
{
IbasicTax tax = new Tax(p.Price);
a = tax.CalculateBasicTax();
}
}
else
{
if (p.Typeofproduct == ProdType.Imported)
{
IImportedTax tax = new Tax(p.Price);
a = tax.CalculateAdditionalTax();
}
}
Taxamount = Math.Ceiling(a * 20) / 20;
TotalTaxes += Taxamount;
TotalSum += Taxamount + p.Price;
Console.WriteLine("1 {0} : {1}", p.Name, Taxamount + p.Price);
}
Console.WriteLine("Total Sales Tax : {0}", TotalTaxes);
Console.WriteLine("Total after taxes: {0}", TotalSum);
}
}
}
I have got an output but for some reason it was rejected. Please let me know what are the flaws in my Code. Even if it is completely flawed, please let me know. I want to correct myself and try not to repeat the same mistake again.