Advertisement
social1986

Untitled

Feb 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class StartUp
  5. {
  6.     public static void Main()
  7.     {        
  8.         var persons = new Dictionary<string, Person>();
  9.         var personsInput = Console.ReadLine()
  10.             .Split(new[] { '=', ';' },StringSplitOptions.RemoveEmptyEntries);
  11.  
  12.         for (int i = 0; i < personsInput.Length; i+=2)
  13.         {
  14.             try
  15.             {
  16.                 var name = personsInput[i];
  17.                 var money = decimal.Parse(personsInput[i + 1]);
  18.                 var person = new Person(name, money);
  19.  
  20.                 if (!persons.ContainsKey(name))
  21.                 {
  22.                     persons[name] = person;
  23.                 }
  24.             }
  25.             catch (ArgumentException ex)
  26.             {
  27.                 Console.WriteLine(ex.Message);
  28.                 Environment.Exit(0);
  29.             }
  30.         }
  31.  
  32.         var productsInput = Console.ReadLine()
  33.             .Split(new[] { ';', '=' }, StringSplitOptions.RemoveEmptyEntries);
  34.  
  35.         var products = new Dictionary<string, Product>();
  36.  
  37.         for (int i = 0; i < productsInput.Length; i+=2)
  38.         {
  39.             try
  40.             {
  41.                 var productName = productsInput[i];
  42.                 var cost = decimal.Parse(productsInput[i + 1]);
  43.                 var product = new Product(productName, cost);
  44.                 if (!products.ContainsKey(productName))
  45.                 {
  46.                     products[productName] = product;
  47.                 }
  48.             }
  49.             catch (ArgumentException ex)
  50.             {
  51.                 Console.WriteLine(ex.Message);
  52.                 Environment.Exit(0);
  53.             }
  54.            
  55.         }
  56.  
  57.         string input;
  58.  
  59.         while ((input = Console.ReadLine()) != "END")
  60.         {
  61.             try
  62.             {
  63.                 var currentPersonToBuy = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  64.                 var personsName = currentPersonToBuy[0];
  65.                 var productToBuy = currentPersonToBuy[1];
  66.                 persons[personsName].AddProduct(products[productToBuy]);
  67.                 Console.WriteLine($"{personsName} bought {productToBuy}");
  68.             }
  69.             catch (ArgumentException ex)
  70.             {
  71.                 Console.WriteLine(ex.Message);
  72.             }            
  73.         }
  74.  
  75.         foreach (var person in persons.Values)
  76.         {
  77.             if (person.Products.Count > 0)
  78.             {
  79.                 Console.WriteLine(person);
  80.             }
  81.             else
  82.             {
  83.                 Console.WriteLine($"{person.Name} - Nothing bought");
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement