Advertisement
grubcho

Exam shopping - Dictionaries

Jul 12th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 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 Exam_shopping
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<string, int> dict = new Dictionary<string, int>();
  14.             string stock = Console.ReadLine();
  15.             while (stock != "shopping time")
  16.             {
  17.                 string[] stockAdd = stock.Split(' ').ToArray();
  18.                 string stockItem = stockAdd[1];
  19.                 int stockItemQuantity = int.Parse(stockAdd[2]);
  20.                 if (!dict.ContainsKey(stockItem))
  21.                 {
  22.                     dict[stockItem] = stockItemQuantity;
  23.                 }
  24.                 else
  25.                 {
  26.                     dict[stockItem] += stockItemQuantity;
  27.                 }                
  28.                 stock = Console.ReadLine();
  29.             }
  30.             string buy = Console.ReadLine();
  31.             while (buy != "exam time")
  32.             {
  33.                 string[] buying = buy.Split(' ').ToArray();
  34.                 string buyItem = buying[1];
  35.                 int buyItemQuantity = int.Parse(buying[2]);
  36.                 if (!dict.ContainsKey(buyItem))
  37.                 {
  38.                     Console.WriteLine($"{buyItem} doesn't exist");
  39.                 }
  40.                 else
  41.                 {
  42.                     if (dict[buyItem] == 0)
  43.                     {
  44.                         Console.WriteLine($"{buyItem} out of stock");
  45.                     }
  46.                     else if (buyItemQuantity >= dict[buyItem])
  47.                     {
  48.                         dict[buyItem] = 0;
  49.                     }
  50.                     else
  51.                     {                        
  52.                         dict[buyItem] -= buyItemQuantity;
  53.                     }                                      
  54.                 }
  55.                 buy = Console.ReadLine();
  56.             }
  57.  
  58.             foreach (KeyValuePair<string, int> item in dict)
  59.             {
  60.                 if (item.Value > 0)
  61.                 {
  62.                     Console.WriteLine($"{item.Key} -> {item.Value}");
  63.                 }              
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement