Lektion 13: lösningsförslag

  1. // 1
  2. class Product
  3. {
  4. public string Name { get; set; }
  5. public double Price { get; set; }
  6. }
  7. static void Main(string[] args)
  8. {
  9. List<Product> cart = new List<Product>();
  10. string[] lines = File.ReadAllLines("Products.txt");
  11. foreach (string line in lines)
  12. {
  13. try
  14. {
  15. string[] parts = line.Split(',');
  16. string name = parts[0];
  17. double price = double.Parse(parts[1]);
  18. Product product = new Product
  19. {
  20. Name = name,
  21. Price = price
  22. };
  23. cart.Add(product);
  24. }
  25. catch
  26. {
  27. Console.WriteLine("Invalid line in product file");
  28. }
  29. }
  30. double total = 0;
  31. foreach (Product p in cart)
  32. {
  33. total += p.Price;
  34. }
  35. if (total < 300)
  36. {
  37. total += 29;
  38. }
  39. Console.WriteLine("Total: " + total);
  40. }
  41. // 2
  42. class Product
  43. {
  44. public string Name { get; set; }
  45. public double Price { get; set; }
  46. }
  47. static void Main(string[] args)
  48. {
  49. List<Product> cart = new List<Product>();
  50. string[] lines = File.ReadAllLines("Products.txt");
  51. foreach (string line in lines)
  52. {
  53. try
  54. {
  55. string[] parts = line.Split(',');
  56. string name = parts[0];
  57. double price = double.Parse(parts[1]);
  58. Product product = new Product
  59. {
  60. Name = name,
  61. Price = price
  62. };
  63. cart.Add(product);
  64. }
  65. catch
  66. {
  67. Console.WriteLine("Invalid line in product file");
  68. }
  69. }
  70. double total = 0;
  71. foreach (Product p in cart)
  72. {
  73. total += p.Price;
  74. }
  75. if (total < 300)
  76. {
  77. total += 29;
  78. }
  79. // We could reduce some duplication here by using custom methods and/or a do-while loop.
  80. string[] validCodes = File.ReadAllLines("Codes.txt");
  81. Console.Write("Please enter code: ");
  82. string userCode = Console.ReadLine();
  83. bool codeFound = false;
  84. while (userCode != "" && !codeFound)
  85. {
  86. foreach (string code in validCodes)
  87. {
  88. if (code == userCode)
  89. {
  90. codeFound = true;
  91. }
  92. }
  93. if (!codeFound)
  94. {
  95. Console.WriteLine("Invalid code entered.");
  96. Console.Write("Please enter code: ");
  97. userCode = Console.ReadLine();
  98. }
  99. }
  100. if (codeFound)
  101. {
  102. total = total * 0.8;
  103. }
  104. Console.WriteLine("Total: " + total);
  105. }
  106. // 3
  107. class Product
  108. {
  109. public string Name { get; set; }
  110. public double Price { get; set; }
  111. }
  112. static void Main(string[] args)
  113. {
  114. List<Product> products = new List<Product>();
  115. string[] lines = File.ReadAllLines("Products.txt");
  116. foreach (string line in lines)
  117. {
  118. try
  119. {
  120. string[] parts = line.Split(',');
  121. string name = parts[0];
  122. double price = double.Parse(parts[1]);
  123. Product product = new Product
  124. {
  125. Name = name,
  126. Price = price
  127. };
  128. products.Add(product);
  129. }
  130. catch
  131. {
  132. Console.WriteLine("Invalid line in product file");
  133. }
  134. }
  135. // As before, We could reduce some duplication here by using custom methods and/or a do-while loop.
  136. List<Product> cart = new List<Product>();
  137. Console.Write("Please enter product name: ");
  138. string productName = Console.ReadLine();
  139. while (productName != "")
  140. {
  141. bool productFound = false;
  142. foreach (Product p in products)
  143. {
  144. if (p.Name == productName)
  145. {
  146. productFound = true;
  147. cart.Add(p);
  148. }
  149. }
  150. if (!productFound)
  151. {
  152. Console.WriteLine("Product " + productName + " does not exist.");
  153. }
  154. Console.Write("Please enter product name: ");
  155. productName = Console.ReadLine();
  156. }
  157. double total = 0;
  158. foreach (Product p in cart)
  159. {
  160. total += p.Price;
  161. }
  162. if (total < 300)
  163. {
  164. total += 29;
  165. }
  166. string[] validCodes = File.ReadAllLines("Codes.txt");
  167. Console.Write("Please enter code: ");
  168. string userCode = Console.ReadLine();
  169. bool codeFound = false;
  170. while (userCode != "" && !codeFound)
  171. {
  172. foreach (string code in validCodes)
  173. {
  174. if (code == userCode)
  175. {
  176. codeFound = true;
  177. }
  178. }
  179. if (!codeFound)
  180. {
  181. Console.WriteLine("Invalid code entered.");
  182. Console.Write("Please enter code: ");
  183. userCode = Console.ReadLine();
  184. }
  185. }
  186. if (codeFound)
  187. {
  188. total = total * 0.8;
  189. }
  190. Console.WriteLine("Total: " + total);
  191. }