Advertisement
YavorGrancharov

Exam_Shopping(dict)

Jul 8th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Exam_Shopping
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] input = Console.ReadLine().Split(' ').ToArray();
  12.             Dictionary<string, int> stock = new Dictionary<string, int>();
  13.  
  14.             while (input[0] != "shopping")
  15.             {
  16.                 string key = input[1];
  17.                 int val = int.Parse(input[2]);
  18.                 if (!stock.ContainsKey(key))
  19.                 {
  20.                     stock[key] = 0;
  21.                 }
  22.                 stock[key] += val;
  23.                 input = Console.ReadLine().Split(' ').ToArray();
  24.             }
  25.             input = Console.ReadLine().Split(' ').ToArray();
  26.             while (input[0] != "exam")
  27.             {
  28.                 string key = input[1];
  29.                 int val = int.Parse(input[2]);
  30.                 if (!stock.ContainsKey(key))
  31.                 {
  32.                     Console.WriteLine($"{key} doesn't exist");
  33.                 }
  34.                 else if (stock.ContainsKey(key))
  35.                 {
  36.                     if (stock[key] > 0)
  37.                     {
  38.                         stock[key] -= val;
  39.                     }
  40.                     else if (stock[key] <= 0)
  41.                     {
  42.                         stock[key] = 0;
  43.                         Console.WriteLine($"{key} out of stock");
  44.                     }
  45.                 }
  46.                 input = Console.ReadLine().Split(' ').ToArray();
  47.             }
  48.  
  49.             foreach (KeyValuePair<string, int> x in stock)
  50.             {
  51.                 if (x.Value > 0)
  52.                 {
  53.                     Console.WriteLine("{0} -> {1}", x.Key, x.Value);
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement