Advertisement
bullit3189

SoftUniBarIncome-RegexWithClass

Mar 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class Person
  7. {
  8. public string Name {get;set;}
  9. public string Product {get;set;}
  10. public double TotalPrice {get;set;}
  11. }
  12. public class Program
  13. {
  14. public static void Main()
  15. {
  16. string namePattern = @"%(?<name>[A-Z][a-z]+)%";
  17. string productPattern = @"<(?<product>\w+)>";
  18. string quantityPattern = @"\|(?<quantity>\d+)\|";
  19. string pricePattern = @"(?<price>\d+(\.\d+)?)\$";
  20.  
  21. double totalIncome =0;
  22.  
  23. List<Person> personsData = new List<Person>();
  24.  
  25. while (true)
  26. {
  27. string input = Console.ReadLine();
  28.  
  29. if (input == "end of shift")
  30. {
  31. break;
  32. }
  33.  
  34. string name = string.Empty;
  35. string product = string.Empty;
  36. int quantity =0;
  37. double price =0;
  38.  
  39. if (Regex.IsMatch(input,namePattern))
  40. {
  41. Match matchedName = Regex.Match(input,namePattern);
  42. name = matchedName.Groups["name"].Value;
  43. }
  44.  
  45. if (name == string.Empty)
  46. {
  47. continue;
  48. }
  49.  
  50. if (Regex.IsMatch(input,productPattern))
  51. {
  52. Match matchedProduct = Regex.Match(input,productPattern);
  53. product = matchedProduct.Groups["product"].Value;
  54. }
  55.  
  56. if (product == string.Empty)
  57. {
  58. continue;
  59. }
  60.  
  61. if (Regex.IsMatch(input,quantityPattern))
  62. {
  63. Match matchedQuantity = Regex.Match(input,quantityPattern);
  64. quantity =int.Parse(matchedQuantity.Groups["quantity"].Value);
  65. }
  66.  
  67. if (quantity==0)
  68. {
  69. continue;
  70. }
  71.  
  72. if (Regex.IsMatch(input,pricePattern))
  73. {
  74. Match matchedPrice = Regex.Match(input,pricePattern);
  75. price = double.Parse(matchedPrice.Groups["price"].Value);
  76. }
  77.  
  78. if (price==0)
  79. {
  80. continue;
  81. }
  82.  
  83. double totalPrice = price * quantity;
  84.  
  85. personsData.Add(new Person
  86. {
  87. Name=name,
  88. Product=product,
  89. TotalPrice = totalPrice
  90. });
  91. }
  92.  
  93. foreach (var person in personsData)
  94. {
  95. Console.WriteLine("{0}: {1} - {2:f2}",person.Name,person.Product,person.TotalPrice);
  96. totalIncome += person.TotalPrice;
  97. }
  98. Console.WriteLine("Total income: {0:f2}",totalIncome);
  99.  
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement