Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Shop
- {
- class Shop
- {
- //полета
- private Dictionary<string, Product> availableProducts;
- //методи
- public Shop()
- {
- availableProducts = new Dictionary<string, Product>();
- }
- public Dictionary<string, Product> AvailableProducts
- {
- get
- {
- return availableProducts;
- }
- set
- {
- availableProducts = value;
- }
- }
- //sell
- public void Sell(string code, double quantity)
- {
- if (availableProducts.ContainsKey(code))
- {
- //имам продукта
- Product product = availableProducts[code];
- if(product.Quantity >= quantity)
- {
- //имаме количество
- product.Quantity = product.Quantity - quantity;
- availableProducts[code] = product;
- }
- else
- {
- //нямаме количество
- throw new Exception("Not enough quantity.");
- }
- }
- else
- {
- //нямам продукта
- throw new Exception("Not available product.");
- }
- }
- public void Add (string code, string name, double price, double quantity)
- {
- if (availableProducts.ContainsKey(code))
- {
- //имаме го вече
- Product product = new Product(name, code, price, quantity);
- availableProducts[code] = product;
- }
- else
- {
- //нямаме го и трябва да добавим нов продукт
- Product product = new Product(name, code, price, quantity);
- availableProducts.Add(code, product);
- }
- }
- public void Update (string code, double quantity)
- {
- if (availableProducts.ContainsKey(code))
- {
- //имам наличен продукта
- Product product = availableProducts[code];
- product.Quantity = product.Quantity + quantity;
- availableProducts[code] = product;
- }
- else
- {
- //нямам наличен продукта
- throw new Exception("Please add your product first!");
- }
- }
- public void Print()
- {
- foreach (var pair in availableProducts)
- {
- Product product = pair.Value;
- Console.WriteLine($"Product name: {product.Name}, Code: {product.Code}, " +
- $"Price: {product.Price}, Quantity: {product.Quantity}");
- }
- }
- public double Calculate()
- {
- double sum = 0;
- foreach(var pair in availableProducts)
- {
- Product product = pair.Value;
- sum += product.Quantity * product.Price;
- }
- return sum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement