Advertisement
elena1234

Furniture*-regex(price!)

Nov 20th, 2020 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4.  
  5. namespace text
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             var regex = new Regex(@">>([A-Z][a-zA-Z]+)<<([0-9]+(?:\.[0-9]+)?)!([0-9]+)");
  12.             var listFurniture = new List<string>();
  13.             decimal totalPrice = 0;
  14.             string input = string.Empty;
  15.             while ((input = Console.ReadLine()) != "Purchase")
  16.             {
  17.                 Match match = regex.Match(input);
  18.                 if (match.Success)
  19.                 {
  20.                     string furniture = $"{ match.Groups[1].Value}";
  21.                     listFurniture.Add(furniture);
  22.                     string price = $"{match.Groups[2].Value}";
  23.                     decimal priceDecimal = decimal.Parse(price);
  24.                     string quantity = $"{match.Groups[3].Value}";
  25.                     decimal quantityDecimal = decimal.Parse(quantity);
  26.                     totalPrice += priceDecimal * quantityDecimal;
  27.                 }
  28.             }
  29.  
  30.             Console.WriteLine("Bought furniture:");
  31.             foreach (var furniture in listFurniture)
  32.             {
  33.                 Console.WriteLine(furniture);
  34.             }
  35.  
  36.             Console.WriteLine($"Total money spend: {totalPrice:f2}");
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement