Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace BeforeExam
- {
- class Program
- {
- static void Main()
- {
- //string a = "sdska%Radoslav%vkksa<Vafli>rsa76sau|5|dasla1.20dik$";
- var input = "";
- decimal totalIncome = 0.00m;
- while (input != "end of shift")
- {
- input = Console.ReadLine();
- if (input == "end of shift")
- {
- break;
- }
- string[] splitByPercent = input.Split(new[] { '%' }, StringSplitOptions.RemoveEmptyEntries);
- string customer = "";
- Regex checkCustomer = new Regex(@"^[A-Z][a-z]*$");
- bool validCustomer = false;
- foreach (string word in splitByPercent)
- {
- if (checkCustomer.IsMatch(word))
- {
- customer = word;
- validCustomer = true;
- break;
- }
- }
- if (!validCustomer)
- {
- continue;
- }
- string[] splitForProduct = input.Split(new[] { '<', '>' }, StringSplitOptions.RemoveEmptyEntries);
- string product = "";
- Regex checkProduct = new Regex(@"^[A-Za-z]*$");
- bool validProduct = false;
- foreach (string word in splitForProduct)
- {
- if (checkProduct.IsMatch(word))
- {
- product = word;
- validProduct = true;
- break;
- }
- }
- if (!validProduct)
- {
- continue;
- }
- string[] splitForQuantityAndPrice = input.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
- int quantity = 0;
- Regex checkQuantity = new Regex(@"^[0-9]*$");
- bool validQuantity = false;
- string rawPrice = "";
- foreach (var word in splitForQuantityAndPrice)
- {
- if (checkQuantity.IsMatch(word))
- {
- if (int.TryParse(word, out quantity))
- {
- validQuantity = true;
- //break;
- }
- }
- else if (word.EndsWith("$"))
- {
- rawPrice = word;
- }
- }
- if (!validQuantity)
- {
- continue;
- }
- rawPrice = rawPrice.Trim('$');
- char[] rawPriceToCharArr = rawPrice.ToCharArray();
- rawPriceToCharArr = rawPriceToCharArr.Where(n => n >= (char)48 && n <= (char)57 || n == (char)46).ToArray();
- rawPrice = string.Join("", rawPriceToCharArr);
- decimal price = 0.00m;
- Regex checkPrice = new Regex(@"^(\d*\.)?\d+$");
- bool validPrice = false;
- if (checkPrice.IsMatch(rawPrice))
- {
- validPrice = true;
- }
- if (validPrice)
- {
- price = decimal.Parse(rawPrice);
- }
- else
- {
- continue;
- }
- decimal toPay = quantity * price;
- totalIncome += toPay;
- Console.WriteLine($"{customer}: {product} - {toPay:f2}");
- }
- Console.WriteLine($"Total income: {totalIncome:f2}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement