Advertisement
WindFell

So

Jul 12th, 2018
1,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. class SoftUniBarIncome
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         string pattern = @"\%(?<customer>[A-Z][a-z]+)\%[^|$%.]*\<(?<product>\w+)\>[^|$%.]*\|(?<count>\d+)\|[^|$%.]*?(?<price>\d+([.]\d+)?)\$";
  11.         List<Order> orders = new List<Order>();
  12.  
  13.         string input;
  14.  
  15.         while ((input = Console.ReadLine()) != "end of shift")
  16.         {
  17.             Match match = Regex.Match(input, pattern);
  18.  
  19.             if (Regex.IsMatch(input, pattern) == false)
  20.             {
  21.                 continue;
  22.             }
  23.  
  24.             string customerName = match.Groups["customer"].Value;
  25.             string product = match.Groups["product"].Value;
  26.             int quantity = int.Parse(match.Groups["count"].Value);
  27.             decimal price = decimal.Parse(match.Groups["price"].Value);
  28.  
  29.             Order order = new Order(customerName, product, quantity, price);
  30.             orders.Add(order);
  31.         }
  32.  
  33.         foreach (Order order in orders)
  34.         {
  35.             Console.WriteLine(order);
  36.         }
  37.  
  38.         decimal totalIncome = orders.Sum(o => o.TotalPrice);
  39.  
  40.         string total = $"Total income: {totalIncome:F2}";
  41.  
  42.         Console.WriteLine(total);
  43.     }
  44. }
  45.  
  46. class Order
  47. {
  48.     public Order(string customerName, string product, int quantity, decimal price)
  49.     {
  50.         this.CustomerName = customerName;
  51.         this.Product = product;
  52.         this.Quantity = quantity;
  53.         this.Price = price;
  54.     }
  55.  
  56.     public string CustomerName { get; }
  57.  
  58.     public string Product { get; }
  59.  
  60.     public int Quantity { get; }
  61.  
  62.     public decimal Price { get; }
  63.  
  64.     public decimal TotalPrice => this.Quantity * this.Price;
  65.  
  66.     public override string ToString()
  67.     {
  68.         string result = $"{this.CustomerName}: {this.Product} - {this.TotalPrice:F2}";
  69.         return result;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement