Advertisement
BubaLazi

DictionariesEX04Shopping - Test

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