Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Text;
- namespace DSA_InventoryManager
- {
- class Program
- {
- static void Main(string[] args)
- {
- HashSet<string> itemsByType = new HashSet<string>();
- Dictionary<string, Item> items = new Dictionary<string, Item>();
- Dictionary<string, List<string>> typeItemNames = new Dictionary<string, List<string>>();
- StringBuilder outputMessages = new StringBuilder();
- string[] commandParameters;
- bool alive = true;
- string type;
- do
- {
- commandParameters = Console.ReadLine().Split(" ");
- switch (commandParameters[0])
- {
- case "add":
- string name = commandParameters[1];
- decimal price = decimal.Parse(commandParameters[2]);
- type = commandParameters[3];
- if (!items.ContainsKey(name))
- {
- if (typeItemNames.ContainsKey(type))
- {
- typeItemNames[type].Add(name);
- }
- else
- {
- typeItemNames.Add(type, new List<string>() { name });
- }
- items.Add(name, new Item(name, price, type));
- outputMessages.AppendLine($"Ok: Item {name} added successfully");
- }
- else
- {
- outputMessages.AppendLine($"Error: Item {name} already exists");
- }
- break;
- case "filter":
- string sortBy = commandParameters[2];
- if (sortBy == "type")
- {
- type = commandParameters[3];
- if (typeItemNames.ContainsKey(type))
- {
- outputMessages.Append("Ok: ");
- foreach (string item in typeItemNames[type])
- {
- outputMessages.Append(ItemToString(items[item]));
- }
- outputMessages.AppendLine();
- }
- else
- {
- outputMessages.AppendLine($"Error: Type {type} does not exists");
- }
- }
- else if(sortBy == "price")
- {
- if(commandParameters.Length == 5)
- {
- decimal pricee = decimal.Parse(commandParameters[4]);
- string range = commandParameters[3];
- if (range == "from")
- {
- List<string> tmpItems = items.Where(item => item.Value.Price > pricee)
- .OrderBy(item => item.Value.Price)
- .Take(10)
- .Select(item => ItemToString(item.Value)).ToList();
- outputMessages.Append("OK: ");
- outputMessages.AppendLine(string.Join("", tmpItems));
- }
- else
- {
- List<string> tmpItems = items.Where(item => item.Value.Price <= pricee)
- .OrderBy(item => item.Value.Price)
- .Take(10)
- .Select(item => ItemToString(item.Value)).ToList();
- outputMessages.Append("OK: ");
- outputMessages.AppendLine(string.Join("", tmpItems));
- }
- }
- else
- {
- decimal fromPrice = decimal.Parse(commandParameters[4]);
- decimal toPrice = decimal.Parse(commandParameters[6]);
- List<string> tmpItems = items.Where(item => item.Value.Price > fromPrice &&
- item.Value.Price < toPrice)
- .OrderBy(item => item.Value.Price)
- .Take(10)
- .Select(item => ItemToString(item.Value)).ToList();
- outputMessages.Append("OK: ");
- outputMessages.AppendLine(string.Join("", tmpItems));
- }
- }
- break;
- case "end":
- alive = false;
- break;
- }
- } while (alive);
- Console.WriteLine(outputMessages.ToString().Trim(' ').Trim(','));
- }
- public static string ItemToString(Item item)
- {
- return $"{item.Name}({item.Price}), ";
- }
- }
- public class Item
- {
- private string name;
- private decimal price;
- private string type;
- public Item(string name, decimal price, string type)
- {
- this.Name = name;
- this.Price = price;
- this.Type = type;
- }
- public string Name
- {
- get => this.name;
- set => this.name = value;
- }
- public decimal Price
- {
- get => this.price;
- set => this.price = value;
- }
- public string Type
- {
- get => this.type;
- set => this.type = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement