BubaLazi

DictionariesEX04Shopping - Test 01

Jul 28th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 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.             SortedDictionary<string, int> productsInStock = new SortedDictionary<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.             List<string> DoNotExist = new List<string>();
  34.  
  35.             while(input[0] != "exam")
  36.             {
  37.                 string productName = input[1];
  38.                 int quantity = int.Parse(input[2]);
  39.  
  40.                 if(!productsInStock.ContainsKey(productName))
  41.                 {
  42.                     DoNotExist.Add(productName);                  
  43.                 }
  44.                 else
  45.                 {
  46.                     if (productsInStock[productName] > 0)
  47.                     {
  48.                         productsInStock[productName] -= quantity;
  49.                         if (productsInStock[productName] <= 0)
  50.                         {
  51.                             productsInStock.Remove(productName);
  52.  
  53.                         }
  54.                     }
  55.                     else if (productsInStock[productName] <= 0)
  56.                     {
  57.                         productsInStock.Remove(productName);                        
  58.                     }                    
  59.                 }                
  60.                 input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  61.             }
  62.  
  63.  
  64.             for( int i =0; i < DoNotExist.Count; i++)
  65.             {
  66.                 Console.WriteLine(string.Join(" ", DoNotExist[i] + " doesn't exist"));
  67.             }
  68.            
  69.             foreach (KeyValuePair<string,int> item in productsInStock)
  70.             {
  71.                 Console.WriteLine($"{item.Key} -> {item.Value}");
  72.             }
  73.         }  
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment