Advertisement
elena1234

SoftUniBarIncome *-regex(price!)

Nov 22nd, 2020 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace text
  5. {
  6.     class MainClass
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             var regex = new Regex(@"%(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<quantity>[0-9]{1,})\|[^|$%.]*?(?<price>[0-9]+(?:\.[0-9]+)?)\$$");
  11.             decimal totalIncome = 0;
  12.             string input = string.Empty;
  13.             while ((input = Console.ReadLine()) != "end of shift")
  14.             {
  15.                 var matches = regex.Matches(input);
  16.                 if (regex.IsMatch(input))
  17.                 {
  18.                     foreach (Match match in matches)
  19.                     {
  20.                         string quantity = match.Groups["quantity"].Value;
  21.                         int quantityInt = int.Parse(quantity);
  22.                         string price = match.Groups["price"].Value;
  23.                         decimal priceDecimal = decimal.Parse(price);
  24.                         decimal totalPrice = quantityInt * priceDecimal;
  25.                         Console.WriteLine($"{match.Groups["name"]}: {match.Groups["product"]} - {totalPrice:F2}");
  26.                         totalIncome += totalPrice;
  27.                     }
  28.                 }
  29.             }
  30.  
  31.             Console.WriteLine($"Total income: {totalIncome:F2}");
  32.         }
  33.  
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement