Advertisement
NelIfandieva

SoftUni Bar Income

Sep 30th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. namespace BeforeExam
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             //string a = "sdska%Radoslav%vkksa<Vafli>rsa76sau|5|dasla1.20dik$";
  12.             var input = "";
  13.             decimal totalIncome = 0.00m;
  14.             while (input != "end of shift")
  15.             {
  16.                 input = Console.ReadLine();
  17.                 if (input == "end of shift")
  18.                 {
  19.                     break;
  20.                 }
  21.                 string[] splitByPercent = input.Split(new[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
  22.                 string customer = "";
  23.                 Regex checkCustomer = new Regex(@"^[A-Z][a-z]*$");
  24.                 bool validCustomer = false;
  25.                 foreach (string word in splitByPercent)
  26.                 {
  27.                     if (checkCustomer.IsMatch(word))
  28.                     {
  29.                         customer = word;
  30.                         validCustomer = true;
  31.                         break;
  32.                     }
  33.                 }
  34.                 if (!validCustomer)
  35.                 {
  36.                     continue;
  37.                 }
  38.                 string[] splitForProduct = input.Split(new[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries);
  39.                 string product = "";
  40.                 Regex checkProduct = new Regex(@"^[A-Za-z]*$");
  41.                 bool validProduct = false;
  42.                 foreach (string word in splitForProduct)
  43.                 {
  44.                     if (checkProduct.IsMatch(word))
  45.                     {
  46.                         product = word;
  47.                         validProduct = true;
  48.                         break;
  49.                     }
  50.                 }
  51.                 if (!validProduct)
  52.                 {
  53.                     continue;
  54.                 }
  55.                 string[] splitForQuantityAndPrice = input.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  56.                 int quantity = 0;
  57.                 Regex checkQuantity = new Regex(@"^[0-9]*$");
  58.                 bool validQuantity = false;
  59.                 string rawPrice = "";
  60.                 foreach (var word in splitForQuantityAndPrice)
  61.                 {
  62.                     if (checkQuantity.IsMatch(word))
  63.                     {
  64.                         if (int.TryParse(word, out quantity))
  65.                         {
  66.                             validQuantity = true;
  67.                             //break;
  68.                         }
  69.                     }
  70.                     else if (word.EndsWith("$"))
  71.                     {
  72.                         rawPrice = word;
  73.                     }
  74.                 }
  75.                 if (!validQuantity)
  76.                 {
  77.                     continue;
  78.                 }
  79.                 rawPrice = rawPrice.Trim('$');
  80.                 char[] rawPriceToCharArr = rawPrice.ToCharArray();
  81.                 rawPriceToCharArr = rawPriceToCharArr.Where(n => n >= (char)48 && n <= (char)57 || n == (char)46).ToArray();
  82.                 rawPrice = string.Join("", rawPriceToCharArr);
  83.                 decimal price = 0.00m;
  84.                 Regex checkPrice = new Regex(@"^(\d*\.)?\d+$");
  85.                 bool validPrice = false;
  86.                 if (checkPrice.IsMatch(rawPrice))
  87.                 {
  88.                     validPrice = true;
  89.                 }
  90.                 if (validPrice)
  91.                 {
  92.                     price = decimal.Parse(rawPrice);
  93.                 }
  94.                 else
  95.                 {
  96.                     continue;
  97.                 }
  98.                 decimal toPay = quantity * price;
  99.                 totalIncome += toPay;
  100.                 Console.WriteLine($"{customer}: {product} - {toPay:f2}");
  101.             }
  102.             Console.WriteLine($"Total income: {totalIncome:f2}");
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement