Advertisement
gospod1978

Regular Expression/ SoftUni Bar Income

Oct 11th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Text.RegularExpressions;
  8.  
  9.  
  10. namespace SoftUniBarIncome
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string input = string.Empty;
  17.  
  18.             string pattern = @"^%(?<name>[A-Z][a-z]+)%[^|$%.]*<(?<product>\w+)>[^|$%.]*\|(?<count>\d+)\|[^|$%.]*?(?<price>[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\$";
  19.  
  20.             double total = 0;
  21.  
  22.             while ((input = Console.ReadLine()) != "end of shift")
  23.             {
  24.                 if (Regex.IsMatch(input, pattern))
  25.                 {
  26.                     Match digit = Regex.Match(input, pattern);
  27.  
  28.                     string name = digit.Groups["name"].Value;
  29.                     string product = digit.Groups["product"].Value;
  30.                     int count = int.Parse(digit.Groups["count"].Value);
  31.                     double price = double.Parse(digit.Groups["price"].Value);
  32.  
  33.                     double money = price * count;
  34.  
  35.                     Console.WriteLine($"{name}: {product} - {money:f2}");
  36.  
  37.                     total += money;
  38.                 }
  39.             }
  40.  
  41.             Console.WriteLine($"Total income: {total:f2}");
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement