Aborigenius

ExamShopping

Jul 13th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ExamShopping
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, int> products = new Dictionary<string, int>();
  14.             string[] input = Console.ReadLine().Replace("buy", "").Split(' ');
  15.             int price = 0;
  16.             while (input[0] != "shopping")
  17.             {
  18.                 string name = input[1];
  19.                 string quantity = input[2];
  20.  
  21.                 if (int.TryParse(quantity, out price))
  22.                 {
  23.                     try
  24.                     {
  25.                         products[name] += price;
  26.                     }
  27.                     catch
  28.                     {
  29.                         products[name] = price;
  30.                     }
  31.                 }
  32.                 input = Console.ReadLine().Replace("buy", "").Split(' ');
  33.  
  34.             }
  35.             input = Console.ReadLine().Replace("buy", "").Split(' ');
  36.  
  37.             while (input[0] != "exam")
  38.             {
  39.                 string name = input[1];
  40.                 string quantity = input[2];
  41.                
  42.                 if (!products.ContainsKey(name))
  43.                 {
  44.                     Console.WriteLine($"{name} doesn't exist");
  45.                 }
  46.                 else if (products.ContainsKey(name))
  47.                 {
  48.                     int stock = products[name];
  49.                     if (stock <= 0)
  50.                     {
  51.                         Console.WriteLine($"{name} out of stock");
  52.                     }
  53.                     else if (int.TryParse(quantity, out price))
  54.                     {
  55.                         products[name] -= price;
  56.                     }
  57.  
  58.                 }
  59.                 input = Console.ReadLine().Replace("buy", "").Split(' ');
  60.             }
  61.             foreach (KeyValuePair<string, int> item in products)
  62.             {
  63.                 if (item.Value > 0)
  64.                 {
  65.                     Console.WriteLine($"{item.Key} -> {item.Value}");
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Add Comment
Please, Sign In to add comment