TheBulgarianWolf

Shopping Spree

Jan 16th, 2021
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ShoppingSpree
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<Person> peopleList = new List<Person>();
  12.             List<Product> productsList = new List<Product>();
  13.             Console.WriteLine("Enter the people and their money in the format person=money and separate them with ;   :");
  14.             string[] people= Console.ReadLine().Split(";");
  15.             Console.WriteLine("Enter the products and their price in the format product=price and separate them with ;   :");
  16.             string[] products = Console.ReadLine().Split(";");
  17.             foreach(string person in people)
  18.             {
  19.                 string[] personInfo = person.Split("=");
  20.                 string personName = personInfo[0];
  21.                 int personMoney = int.Parse(personInfo[1]);
  22.                 Person newPerson = new Person(personName, personMoney);
  23.                 peopleList.Add(newPerson);
  24.             }
  25.             foreach (string product in products)
  26.             {
  27.                 string[] productInfo = product.Split("=");
  28.                 string productName = productInfo[0];
  29.                 int productPrice = int.Parse(productInfo[1]);
  30.                 Product newProduct = new Product(productName, productPrice);
  31.                 productsList.Add(newProduct);
  32.             }
  33.             string input;
  34.             while((input = Console.ReadLine()) != "END")
  35.             {
  36.                 string[] inputInfo = input.Split(" ");
  37.                 Person selectedPerson = peopleList.Find(p => p.Name == inputInfo[0]);
  38.                 Product selectedProduct = productsList.Find(pr => pr.ProductName == inputInfo[1]);
  39.                 if(selectedPerson.Money >= selectedProduct.Price)
  40.                 {
  41.                     selectedPerson.Money -= selectedProduct.Price;
  42.                     selectedPerson.ProductsBag.Add(selectedProduct.ProductName);
  43.                     Console.WriteLine($"{selectedPerson.Name} bought {selectedProduct.ProductName}");
  44.                 }
  45.                 else
  46.                 {
  47.                     Console.WriteLine($"{selectedPerson.Name} can't afford {selectedProduct.ProductName}");
  48.                 }
  49.             }
  50.  
  51.             foreach(Person person1 in peopleList)
  52.             {
  53.                 if(person1.ProductsBag.Count > 0)
  54.                 {
  55.                     Console.Write($"{person1.Name} - {string.Join(",", person1.ProductsBag)}");
  56.                 }
  57.                 else
  58.                 {
  59.                     Console.WriteLine($"{person1.Name} - Nothing bought");
  60.                 }
  61.                
  62.             }
  63.         }
  64.     }
  65.  
  66.     class Person
  67.     {
  68.         public string Name { get; set; }
  69.         public int Money { get; set; }
  70.  
  71.         public List<string> ProductsBag { get; set; }
  72.  
  73.         public Person(string name,int money)
  74.         {
  75.             Name = name;
  76.             Money = money;
  77.             ProductsBag = new List<string>();
  78.         }
  79.     }
  80.  
  81.     class Product
  82.     {
  83.         public string ProductName { get; set; }
  84.         public int Price { get; set; }
  85.  
  86.         public Product(string name,int price)
  87.         {
  88.             ProductName = name;
  89.             Price = price;
  90.         }
  91.     }
  92. }
  93.  
Add Comment
Please, Sign In to add comment