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;
- namespace Inventory_Manager
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- Dictionary<string, List<Item>> items = new Dictionary<string, List<Item>>();
- var set = new HashSet<Item>();
- StringBuilder str = new StringBuilder();
- while (input != "end")
- {
- string[] parameters = input.Split(' ');
- if (parameters[0] == "add")
- {
- var item = new Item(parameters[1], double.Parse(parameters[2]), parameters[3]);
- if (!set.Add(item))
- {
- str.AppendLine($"Error: Item {parameters[1]} already exists");
- input = Console.ReadLine();
- continue;
- }
- else
- {
- // add jeans 10.99 clothes
- // add pants 10.99 clothes
- if (items.ContainsKey(parameters[3]))
- {
- items[parameters[3]].Add(item);
- }
- else
- {
- items.Add(parameters[3], new List<Item> { item });
- }
- str.AppendLine($"Ok: Item {parameters[1]} added successfully");
- input = Console.ReadLine();
- continue;
- }
- }
- if (parameters[0] == "filter")
- {
- var criteria = parameters[2];
- if (criteria == "type")
- {
- //filter by type clothes
- if (!items.ContainsKey(parameters[3]))
- {
- str.AppendLine($"Error: Type {parameters[3]} does not exists");
- input = Console.ReadLine();
- continue;
- }
- var msg = $"Ok: {string.Join(", ", items[parameters[3]].OrderBy(p => p.Price).ThenBy(p => p.Name)/*.ThenBy(p => p.Type)*/.Take(10))}";
- str.AppendLine(msg);
- input = Console.ReadLine();
- continue;
- }
- else if (criteria == "price")
- {
- if (parameters[3] == "from")
- {
- if (parameters.Length > 5)
- {
- str.AppendLine($"Ok: {string.Join(", ", set.Where(p => p.Price > double.Parse(parameters[4]) && p.Price < double.Parse(parameters[6])).OrderBy(p => p.Price).ThenBy(p => p.Name).ThenBy(p => p.Type).Take(10)/*.Select(p => p.ToString())*/)}");
- input = Console.ReadLine();
- continue;
- }
- str.AppendLine($"Ok: {string.Join(", ", set.Where(p => p.Price > double.Parse(parameters[4])).OrderBy(p => p.Price).ThenBy(p => p.Name).ThenBy(p => p.Type).Take(10)/*.Select(p => p.ToString())*/)}");
- input = Console.ReadLine();
- continue;
- }
- else if (parameters[3] == "to")
- {
- str.AppendLine($"Ok: {string.Join(", ", set.Where(p => p.Price < double.Parse(parameters[4])).OrderBy(p => p.Price).ThenBy(p => p.Name).ThenBy(p => p.Type).Take(10)/*.Select(p => p.ToString())*/)}");
- input = Console.ReadLine();
- continue;
- }
- }
- }
- }
- Console.WriteLine(str);
- }
- }
- public class Item
- {
- double price;
- string name;
- string type;
- public Item(string name, double price, string type)
- {
- this.Name = name;
- this.Price = price;
- this.Type = type;
- }
- public double Price { get => this.price; set => this.price = value; }
- public string Name { get => this.name; set => this.name = value; }
- public string Type { get => this.type; set => this.type = value; }
- public override string ToString()
- {
- return $"{this.Name}({this.price})";
- }
- public override bool Equals(object obj)
- {
- if (obj == null || !this.GetType().Equals(obj.GetType()))
- {
- return false;
- }
- var comp = obj as Item;
- return this.Name.Equals(comp.Name);
- }
- public override int GetHashCode()
- {
- return Name.GetHashCode();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement