Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8.  
  9. namespace Задача_1
  10. {
  11. public class Item
  12. {
  13. public string Name { get; }
  14. public double Price { get; }
  15. public int Quantity { get; }
  16.  
  17.  
  18.  
  19. public Item(string itemName, double itemPrice, int itemQuantity)
  20. {
  21. Name = itemName;
  22. Price = itemPrice;
  23. Quantity = itemQuantity;
  24. }
  25. public override string ToString()
  26. {
  27. return $"{Name,10} {Price,14} {Quantity,14}";
  28. }
  29. }
  30. public class ShoppingCart //Зачем делать класс публичным?
  31. {
  32. private int _itemCount;
  33. private double _totalPrice;
  34. private int _capacity;
  35. private Item[] _cart = new Item[5];
  36. private int i;
  37. public ShoppingCart()
  38. {
  39. i = 0;
  40. _itemCount = 0;
  41. _totalPrice = 0.0;
  42. _capacity = _cart.Length;
  43. }
  44. public void AddToCart(string itemName, double price, int quantity)
  45. {
  46. if(!IfCartIsNotFull())
  47. {
  48. IncreaseSize();
  49. }
  50. _totalPrice += price * quantity;
  51.  
  52. _cart[_itemCount] = new Item(itemName, price, quantity);
  53. _itemCount++;
  54. }
  55. private void IncreaseSize()
  56. {
  57. _capacity += 3;
  58. Array.Resize(ref _cart, _capacity);
  59. }
  60. public bool IfCartIsNotFull()
  61. {
  62. if (_capacity - _itemCount > 0)
  63. return true;
  64. else
  65. return false;
  66. }
  67. public override string ToString()
  68. {
  69. string contents = "\nShopping Cart\n";
  70. contents += " Item Unit Price Quantity\n";
  71. for (int i = 0; i < _itemCount; i++)
  72. {
  73. contents += _cart[i] + "\n";
  74. }
  75. contents += $"\nПожалуйста заплатите: {_totalPrice}\n";
  76. return contents;
  77. }
  78. }
  79. class Program
  80. {
  81. static string EnterStr()
  82. {
  83. Console.Write("Введите название предмета: ");
  84. string str = Console.ReadLine();
  85. return str;
  86. }
  87. static int EnterCount()
  88. {
  89. int n;
  90. do
  91. {
  92. Console.WriteLine("Введите количество: ");
  93. } while (!int.TryParse(Console.ReadLine(), out n));
  94. return n;
  95. }
  96. static int EnterMoney()
  97. {
  98. int n;
  99. do
  100. {
  101. Console.WriteLine("Введите число денег: ");
  102. } while (!int.TryParse(Console.ReadLine(), out n));
  103. return n;
  104. }
  105. static void Main()
  106. {
  107. ShoppingCart shop = new ShoppingCart();
  108. ConsoleKeyInfo key;
  109. int i = 1;
  110. do
  111. {
  112. Console.WriteLine("Кладу в корзину в " + i + " раз!");
  113. shop.AddToCart(EnterStr(), EnterMoney(), EnterCount());
  114.  
  115. i++;
  116.  
  117. Console.WriteLine("\nВведите Esc если больше не будете ничего покупать!");
  118. key = Console.ReadKey(true);
  119. } while (key.Key != ConsoleKey.Escape);
  120.  
  121. Console.WriteLine(shop);
  122. Console.ReadLine();
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement