fork download
  1. using System;
  2.  
  3. namespace FoodItems
  4. {
  5. class Product
  6. {
  7. private string name;
  8. private decimal cost, tax, quant;
  9. private bool food;
  10.  
  11. public Product(string itemName, decimal itemCost, bool foodItem)
  12. {
  13. name = itemName;
  14. cost = itemCost;
  15. food = foodItem;
  16. }
  17.  
  18. public decimal CalculateProduct(decimal quantity)
  19. {
  20. quant = quantity;
  21.  
  22. if (food)
  23. {
  24. tax = 0.12m;
  25. }
  26. else
  27. {
  28. tax = 0.25m;
  29. }
  30.  
  31. decimal finalprice = cost * quant;
  32. finalprice = finalprice + (finalprice * tax);
  33. finalprice = Math.Round(finalprice, 2);
  34.  
  35. return finalprice;
  36. }
  37. }
  38.  
  39. class GroceryStore
  40. {
  41. public static void Main(string[] args)
  42. {
  43. bool productFood;
  44.  
  45. Console.Write("Enter the product name: ");
  46. string productName = Console.ReadLine();
  47.  
  48. Console.Write("Net unit price: ");
  49. string userInput = Console.ReadLine();
  50. decimal productPrice = Convert.ToDecimal(userInput);
  51.  
  52. Console.Write("Food item (y/n): ");
  53. string boolRead = Console.ReadLine();
  54.  
  55. if (boolRead == "y")
  56. {
  57. productFood = true;
  58. }
  59. else
  60. {
  61. productFood = false;
  62. }
  63.  
  64.  
  65. Console.Write("Quantity: ");
  66. userInput = Console.ReadLine();
  67. decimal quantity = Convert.ToDecimal(userInput);
  68.  
  69. Product Apple = new Product(productName, productPrice, productFood);
  70. decimal price = Apple.CalculateProduct(quantity);
  71.  
  72. Console.WriteLine("Price: {0}", price);
  73. Console.Read();
  74. }
  75. }
  76. }
Success #stdin #stdout 0.04s 33952KB
stdin
Standard input is empty
stdout
Enter the product name: Net unit price: Food item (y/n): Quantity: Price: 0.00