Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Задача_1
- {
- public class Item
- {
- public string Name { get; }
- public double Price { get; }
- public int Quantity { get; }
- public Item(string itemName, double itemPrice, int itemQuantity)
- {
- Name = itemName;
- Price = itemPrice;
- Quantity = itemQuantity;
- }
- public override string ToString()
- {
- return $"{Name,10} {Price,14} {Quantity,14}";
- }
- }
- public class ShoppingCart //Зачем делать класс публичным?
- {
- private int _itemCount;
- private double _totalPrice;
- private int _capacity;
- private Item[] _cart = new Item[5];
- private int i;
- public ShoppingCart()
- {
- i = 0;
- _itemCount = 0;
- _totalPrice = 0.0;
- _capacity = _cart.Length;
- }
- public void AddToCart(string itemName, double price, int quantity)
- {
- if(!IfCartIsNotFull())
- {
- IncreaseSize();
- }
- _totalPrice += price * quantity;
- _cart[_itemCount] = new Item(itemName, price, quantity);
- _itemCount++;
- }
- private void IncreaseSize()
- {
- _capacity += 3;
- Array.Resize(ref _cart, _capacity);
- }
- public bool IfCartIsNotFull()
- {
- if (_capacity - _itemCount > 0)
- return true;
- else
- return false;
- }
- public override string ToString()
- {
- string contents = "\nShopping Cart\n";
- contents += " Item Unit Price Quantity\n";
- for (int i = 0; i < _itemCount; i++)
- {
- contents += _cart[i] + "\n";
- }
- contents += $"\nПожалуйста заплатите: {_totalPrice}\n";
- return contents;
- }
- }
- class Program
- {
- static string EnterStr()
- {
- Console.Write("Введите название предмета: ");
- string str = Console.ReadLine();
- return str;
- }
- static int EnterCount()
- {
- int n;
- do
- {
- Console.WriteLine("Введите количество: ");
- } while (!int.TryParse(Console.ReadLine(), out n));
- return n;
- }
- static int EnterMoney()
- {
- int n;
- do
- {
- Console.WriteLine("Введите число денег: ");
- } while (!int.TryParse(Console.ReadLine(), out n));
- return n;
- }
- static void Main()
- {
- ShoppingCart shop = new ShoppingCart();
- ConsoleKeyInfo key;
- int i = 1;
- do
- {
- Console.WriteLine("Кладу в корзину в " + i + " раз!");
- shop.AddToCart(EnterStr(), EnterMoney(), EnterCount());
- i++;
- Console.WriteLine("\nВведите Esc если больше не будете ничего покупать!");
- key = Console.ReadKey(true);
- } while (key.Key != ConsoleKey.Escape);
- Console.WriteLine(shop);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement