Advertisement
Spocoman

03. SoftUni Bar Income

Apr 20th, 2023
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.13 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace SoftUniBarIncome
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Regex pattern = new Regex(@"%(?<name>[A-Z][a-z]*)%[^|$%.]*?<(?<product>\w+)>[^|$%.]*?\|(?<quantity>\d+)\|[^|$%.]*?(?<price>\d+(\.\d+)?)\$");
  12.  
  13.             double total = 0;
  14.  
  15.             string command;
  16.  
  17.             while ((command = Console.ReadLine()) != "end of shift")
  18.             {
  19.                 if (pattern.IsMatch(command))
  20.                 {
  21.                     var info = pattern.Match(command);
  22.  
  23.                     string name = info.Groups["name"].Value;
  24.                     string product = info.Groups["product"].Value;
  25.                     int quantity = int.Parse(info.Groups["quantity"].Value);
  26.                     double price = double.Parse(info.Groups["price"].Value);
  27.  
  28.                     Console.WriteLine($"{name}: {product} - {price * quantity:f2}");
  29.                     total += price * quantity;
  30.                 }
  31.             }
  32.  
  33.             Console.WriteLine($"Total income: {total:f2}");
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement