Advertisement
Guest User

Untitled

a guest
May 12th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Startup
  6. {
  7.     public static void Main()
  8.     {
  9.         List<Person> personsCollection = new List<Person>();
  10.         List<Product> productsCollection = new List<Product>();
  11.         Dictionary<string, List<string>> finalResults = new Dictionary<string, List<string>>();
  12.  
  13.         try
  14.         {
  15.             string[] currentPerson = Console.ReadLine().Split(new char[] { '=', ';' }, StringSplitOptions.RemoveEmptyEntries);
  16.             string[] currentProduct = Console.ReadLine().Split(new char[] { '=', ';' }, StringSplitOptions.RemoveEmptyEntries);
  17.  
  18.             for (int i = 0; i < currentPerson.Length; i += 2)
  19.             {
  20.                 string name = currentPerson[i];
  21.                 decimal money = decimal.Parse(currentPerson[i + 1]);
  22.  
  23.                 Person person = new Person(name, money);
  24.                 personsCollection.Add(person);
  25.             }
  26.             for (int i = 0; i < currentProduct.Length; i += 2)
  27.             {
  28.                 string name = currentProduct[i];
  29.                 decimal cost = decimal.Parse(currentProduct[i + 1]);
  30.  
  31.                 Product product = new Product(name, cost);
  32.                 productsCollection.Add(product);
  33.             }
  34.  
  35.  
  36.             string operation = Console.ReadLine();
  37.             while (operation != "END")
  38.             {
  39.                 string[] commandLine = operation.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  40.                 string username = commandLine[0];
  41.                 string product = commandLine[1];
  42.                 decimal result = 0;
  43.                 foreach (var person in personsCollection.Where(p => p.Name == username))
  44.                 {
  45.                     foreach (var item in productsCollection.Where(p => p.Name == product))
  46.                     {
  47.                         result = person.BuyProduct(item.Cost);
  48.                     }
  49.                 }
  50.                 if (result == -1)
  51.                 {
  52.                     Console.WriteLine($"{username} can't afford {product}");
  53.                     PutEmptyBag(finalResults, username);
  54.                 }
  55.                 else
  56.                 {
  57.                     Console.WriteLine($"{username} bought {product}");
  58.                     PutItemsInBag(finalResults, username, product);
  59.                 }
  60.  
  61.                 operation = Console.ReadLine();
  62.             }
  63.         }
  64.         catch (Exception ex)
  65.         {
  66.             Console.WriteLine(ex.Message);
  67.         }
  68.  
  69.         foreach (var kvp in finalResults)
  70.         {
  71.             if (kvp.Value.Count == 0)
  72.             {
  73.                 Console.WriteLine($"{kvp.Key} - Nothing bought");
  74.             }
  75.             else
  76.             {
  77.                 Console.WriteLine($"{kvp.Key} - {string.Join(", ", kvp.Value)}");
  78.             }  
  79.         }
  80.  
  81.     }
  82.  
  83.     private static void PutEmptyBag(Dictionary<string, List<string>> finalResults, string username)
  84.     {
  85.         if (!finalResults.ContainsKey(username))
  86.         {
  87.             finalResults.Add(username, new List<string>());
  88.         }
  89.     }
  90.  
  91.     private static void PutItemsInBag(Dictionary<string, List<string>> finalResults, string username, string product)
  92.     {
  93.         if (!finalResults.ContainsKey(username))
  94.         {
  95.             finalResults.Add(username, new List<string>() {product});
  96.         }
  97.         else
  98.         {
  99.             finalResults[username].Add(product);
  100.         }
  101.  
  102.     }
  103. }
  104.  
  105. public class Product
  106. {
  107.     private string name;
  108.     private decimal cost;
  109.  
  110.     public Product(string name, decimal cost)
  111.     {
  112.         Name = name;
  113.         Cost = cost;
  114.     }
  115.  
  116.     public string Name
  117.     {
  118.         get
  119.         {
  120.             return this.name;
  121.         }
  122.         private set
  123.         {
  124.             if (string.IsNullOrWhiteSpace(value))
  125.             {
  126.                 throw new ArgumentException("Name cannot be empty");
  127.             }
  128.             this.name = value;
  129.         }
  130.     }
  131.     public decimal Cost
  132.     {
  133.         get
  134.         {
  135.             return this.cost;
  136.         }
  137.         private set
  138.         {
  139.             if (value < 0)
  140.             {
  141.                 throw new ArgumentException("Price cannot be negative");
  142.             }
  143.             this.cost = value;
  144.         }
  145.     }
  146. }
  147.  
  148. public class Person
  149. {
  150.     private string name;
  151.     private decimal money;
  152.  
  153.     public Person(string name, decimal money)
  154.     {
  155.         Name = name;
  156.         Money = money;
  157.     }
  158.  
  159.     public string Name
  160.     {
  161.         get
  162.         {
  163.             return this.name;
  164.         }
  165.         protected set
  166.         {
  167.             if (string.IsNullOrWhiteSpace(value))
  168.             {
  169.                 throw new ArgumentException("Name cannot be empty");
  170.             }
  171.             this.name = value;
  172.         }
  173.     }
  174.     public decimal Money
  175.     {
  176.         get
  177.         {
  178.             return this.money;
  179.         }
  180.         protected set
  181.         {
  182.             if (value < 0)
  183.             {
  184.                 throw new ArgumentException("Money cannot be negative");
  185.             }
  186.             this.money = value;
  187.         }
  188.     }
  189.     public decimal BuyProduct(decimal cost)
  190.     {
  191.         return this.money >= cost ? this.money -= cost :  -1;
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement