Advertisement
bullit3189

SoftUniBarIncome-Regex

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