I applied for an Application Developer position. They require all their applicants to complete 1 of 3 programming assignments. I picked one for sales tax calculation. It was quite simple.
Write a program the feeds in prices. If the item is a Book, Food or Medical product there is no tax, if not there is a 10% tax (other item). If the item is an import there is a 5% tax (it's possible to have an import and "other item" type of item with 15% tax (10 + 5)). The program needs to print receipts like this...
- Book: 9.12 (2 @ 4.56)
- Imported dog food: 6.52
- Perfume: 6.23
- Sales Taxes: 1.65
- Total: 21.87
You can see in this case the application can "group" items and provide tax calculation including total tax.
To describe it my application had a menu the continuing prompting the user with information until they broke from the menu. The product information was placed into a List
. A lambda expression is written against the list to group and get the duplicate items. An if
statement nested within a foreach
statement fires the tax calculation to print out the receipts.
namespace Cash
{
public class product
{
public string prod_name { get; set; }
public decimal prod_price { get; set; }
public bool is_import { get; set; }
public bool is_other { get; set; }
}
class Program
{
static void Main(string[] args)
{
Program prog = new Program();
prog.menu();
}
public void menu()
{
string men1;
string prod_name=null;
bool is_import;
bool is_other;
decimal prod_price;
var prod_list = new List<product>();
while (true)
{
Console.WriteLine("Sales Tax");
Console.WriteLine();
Console.WriteLine("***Please Select the Item Type***");
Console.WriteLine("1. Book, Food or Medical Product");
Console.WriteLine("2. Other");
Console.WriteLine("Please select a menu option or 0 to end:");
men1 = Console.ReadLine();
if (men1.Equals("0"))
{
Print(prod_list);
break;
}
Console.WriteLine("Is this product an import? (Y/N)");
is_import = (Console.ReadLine().ToUpper().Equals("Y")) ? true : false;
is_other = (men1.Equals("2")) ? true : false;
Console.WriteLine("***Please enter the price***");
prod_price = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the descriptive name of the product");
prod_name = Console.ReadLine();
prod_list.Add(new product { is_import = is_import, prod_name = prod_name, prod_price = prod_price, is_other = is_other });
}
}
public void Print(List<product> prod_list)
{
var result = prod_list.GroupBy(x => new { x.prod_name, x.prod_price, x.is_other, x.is_import })
.Select(x => new
{
prod_name = x.Key.prod_name,
sum = x.Sum(z => z.prod_price),
count = x.Count(),
prod_price = x.Key.prod_price,
is_other = x.Key.is_other,
is_import = x.Key.is_import,
});
foreach (var item in result)
{
if (item.count > 1)
{
Console.WriteLine(item.prod_name + ": " + string.Format("{0:0.00}", CalcTax(item.sum, item.is_other, item.is_import)) + " (" + item.count + " @ " + item.prod_price + ")");
}
else
{
Console.WriteLine(item.prod_name + ": " + string.Format("{0:0.00}", CalcTax(item.sum, item.is_other, item.is_import)));
}
}
Console.WriteLine("Sales Taxes: " + string.Format("{0:0.00}", Total_tax));
Console.WriteLine("Total: " + string.Format("{0:0.00}", prod_list.Sum(item => item.prod_price) + Total_tax));
Console.ReadLine();
}
public decimal CalcTax(decimal price, bool is_other, bool is_import)
{
decimal _price=0;
if (is_import == true && is_other == true)
{
_price = customRound((price * 15) / 100) + price;
Total_tax = customRound((_price - price));
}
if(is_import == true && is_other == false)//
{
_price = customRound((price * 5) / 100) + price;
Total_tax = customRound((_price - price));
}
if (is_import == false && is_other == true)
{
_price = customRound((price * 10) / 100) + price;
Total_tax = customRound((_price - price));
}
if (is_import == false && is_other == false)
{
_price = price;
Total_tax = customRound((_price - price));
}
return _price;
}
private decimal _Total_tax;
public decimal Total_tax
{
get
{
return this._Total_tax;
}
set
{
this._Total_tax = value + Total_tax;
}
}
public decimal customRound(decimal num)
{
return Math.Round(num * 20.0M, MidpointRounding.AwayFromZero) / 20.0M;
}
}
}
After submitting this to the company the recruiter came back and informed me that the company indicated that this "was not object oriented". So either I'm pretty way off or I could make some adjustments to meet their requirements. Anyway, I performed the work in 2 days (time limit).
How could this be written to follow OOP concepts? I'm not asking someone to "re-write" this for me. I think I have the right frame of mind. I just need someone to "nudge" me in the right direction.