anizko

04. Orders

Jul 8th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace _04._Orders
  7. {
  8. class Product
  9.     {
  10.         public int Quantity { get; set; }
  11.         public string Name { get; set; }
  12.         public double Price { get; set; }
  13.  
  14.         public Product(int quantity, double price)
  15.         {
  16.             Quantity = quantity;
  17.             Price = price;
  18.         }
  19.  
  20.  
  21.     }
  22.     class Program
  23.     {
  24.         static void Main(string[] args)
  25.         {
  26.             Dictionary<string, Product> dict = new Dictionary<string, Product>();
  27.  
  28.             string comand = Console.ReadLine();
  29.  
  30.  
  31.             while (comand != "buy")
  32.             {
  33.                 string[] input = comand.Split();
  34.  
  35.                 string name = input[0];
  36.                 double price = double.Parse(input[1]);
  37.                 int quantity = int.Parse(input[2]);
  38.  
  39.                 if (!dict.ContainsKey(name))
  40.                 {
  41.                     dict[name] = new Product(quantity, price);
  42.                 }
  43.                 else
  44.                 {
  45.                     dict[name].Quantity += quantity;
  46.                     dict[name].Price = price;
  47.                 }
  48.                 comand = Console.ReadLine();
  49.             }
  50.  
  51.  
  52.  
  53.             foreach (var item in dict)
  54.             {
  55.                 Console.WriteLine($"{item.Key} -> {(item.Value.Price * item.Value.Quantity):f2}");
  56.             }
  57.  
  58.  
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment