JulianJulianov

06.RegularExpressionsExercise-SoftUni Bar Income

May 6th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. 03. SoftUni Bar Income
  2.  Let`s take a break and visit the game bar at SoftUni. It is about time for the people behind the bar to go home and you are the person
  3.  who has to draw the line and calculate the money from the products that were sold throughout the day. Until you receive a line with
  4.  text "end of shift" you will be given lines of input. But before processing that line you have to do some validations first.
  5. Each valid order should have a customer, product, count and a price:
  6. • Valid customer's name should be surrounded by '%' and must start with a capital letter, followed by lower-case letters
  7. • Valid product contains any word character and must be surrounded by '<' and '>'
  8. • Valid count is an integer, surrounded by '|'
  9. • Valid price is any real number followed by '$'
  10. The parts of a valid order should appear in the order given: customer, product, count and a price.
  11. Between each part there can be other symbols, except ('|', '$', '%' and '.')
  12. For each valid line print on the console: "{customerName}: {product} - {totalPrice}"
  13.  When you receive "end of shift" print the total amount of money for the day rounded to 2 decimal places in the following format:
  14.  "Total income: {income}".
  15. Input / Constraints
  16. • Strings that you have to process until you receive text "end of shift".
  17. Output
  18. • Print all of the valid lines in the format "{customerName}: {product} - {totalPrice}"
  19. • After receiving "end of shift" print the total amount of money for the day rounded to 2 decimal places in the following format:
  20. "Total income: {income}"
  21. • Allowed working time / memory: 100ms / 16MB.
  22. Examples
  23. Input                                Output                              Comment
  24. %George%<Croissant>|2|10.3$          George: Croissant - 20.60           Each line is valid, so we print each order,
  25. %Peter%<Gum>|1|1.3$                  Peter: Gum - 1.30                   calculating the total price of the product bought.
  26. %Maria%<Cola>|1|2.4$                 Maria: Cola - 2.40                  At the end we print the total income for the day
  27. end of shift                         Total income: 24.30
  28.  
  29.  
  30. %InvalidName%<Croissant>|2|10.3$     Valid: Valid - 200.00               On the first line, the customer name isn`t valid,
  31. %Peter%<Gum>1.3$                     Total income: 200.00                so we skip that line.
  32. %Maria%<Cola>|1|2.4                                                      The second line is missing product count.
  33. %Valid%<Valid>valid|10|valid20$                                          The third line don`t have a valid price.
  34. end of shift                                                             And only the forth line is valid
  35.    
  36.  
  37. using System;
  38. using System.Text.RegularExpressions;
  39. using System.Collections.Generic;
  40.  
  41. public class Program
  42. {
  43.    public static void Main()
  44.    {
  45.        var pattern = @"%(?<Customer>[A-Z][a-z]+)%[^|$%.]*<(?<Product>\w+)>[^|$%.]*\|(?<Count>\d+)\|[^|$%.]*?(?<Price>\d+(.\d+)?)\$";
  46.        var listValidOrders = new List<string>();
  47.        var totalIncome = 0m;
  48.        var input = "";
  49.        while ((input = Console.ReadLine()) != "end of shift")
  50.        {
  51.            var matchInput = Regex.Matches(input, pattern);  
  52.            if (matchInput.Success)
  53.            {                                                                  //група 1=(.\d+)
  54.                var customerName = matchInput.Groups["Customer"/*2*/].Value;   //група 2=(?<Customer>[A-Z][a-z]+)
  55.                var product = matchInput.Groups["Product"/*3*/].Value;         //група 3=(?<Product>\w+)
  56.                var count = matchInput.Groups["Count"/*4*/].Value;             //група 4=(?<Count>\d+)
  57.                var price = matchInput.Groups["Price"/*5*/].Value;             //група 5=(?<Price>\d+(.\d+)?)
  58.                var totalPrice = int.Parse(count) * decimal.Parse(price);
  59.                totalIncome += totalPrice;
  60.                var validOrder = $"{customerName}: {product} - {totalPrice:F2}";
  61.                listValidOrders.Add(validOrder);
  62.            }
  63.        }
  64.        Console.WriteLine(string.Join("\n", listValidOrders));
  65.        Console.WriteLine($"Total income: {totalIncome:F2}");
  66.    }
  67. }
Add Comment
Please, Sign In to add comment