Advertisement
dragonbs

03. SoftUni Bar Income

Mar 20th, 2023
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5.  
  6. class ValidOrder
  7. {
  8.     public string Name { get; set; }
  9.  
  10.     public string Product { get; set; }
  11.  
  12.     public int Count { get; set; }
  13.  
  14.     public double Price { get; set; }
  15.  
  16.     public ValidOrder(string name, string product, int count, double price)
  17.     {
  18.         this.Name = name;
  19.         this.Product = product;
  20.         this.Price = price;
  21.         this.Count = count;
  22.     }
  23.  
  24.     public override string ToString()
  25.     => $"{this.Name}: {this.Product} - {this.Price * this.Count:f2}";
  26. }
  27.  
  28. internal class Program
  29. {
  30.     static void Main()
  31.     {
  32.         var validOrders = new List<ValidOrder>();
  33.         string regexName = @"%(?<name>[A-Z][a-z]+)%";
  34.         string regexProduct = @"<(?<product>\w+)>";
  35.         string regexCount = @"[|](?<count>\d+)[|]";
  36.         string regexPrice = @"(?<price>\d+[.]?\d*?)[$]";
  37.         string input;
  38.  
  39.         while ((input = Console.ReadLine()) != "end of shift")
  40.         {
  41.             MatchCollection matchName = Regex.Matches(input, regexName);
  42.             MatchCollection matchProduct = Regex.Matches(input, regexProduct);
  43.             MatchCollection matchCount = Regex.Matches(input, regexCount);
  44.             MatchCollection matchPrice = Regex.Matches(input, regexPrice);
  45.  
  46.             if (matchName.Count > 0 && matchProduct.Count > 0 && matchPrice.Count > 0 && matchCount.Count > 0)
  47.             {
  48.                 string name = matchName[0].Groups["name"].Value;
  49.                 string product = matchProduct[0].Groups["product"].Value;
  50.                 int count = int.Parse(matchCount[0].Groups["count"].Value);
  51.                 double price = double.Parse(matchPrice[0].Groups["price"].Value);
  52.  
  53.                 validOrders.Add(new ValidOrder(name, product, count, price));
  54.             }
  55.         }
  56.  
  57.         foreach (ValidOrder validOrder in validOrders)
  58.             Console.WriteLine(validOrder.ToString());
  59.  
  60.         double totalIncome = validOrders.Sum(x => x.Price * x.Count);
  61.         Console.WriteLine($"Total income: {totalIncome:f2}");
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement