Advertisement
ralichka

09.Regex-01.Furniture

Nov 29th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _01.Furniture
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.             decimal totalPrice = 0;
  13.             List<string> furn = new List<string>();
  14.  
  15.             while (input != "Purchase")
  16.             {
  17.                 Regex pattern = new Regex(@">>(?<furniture12>[A-Za-z]+)<<(?<price>\d+\.?\d*)!(?<count>\d+)");
  18.                 Match match = pattern.Match(input);
  19.  
  20.                 if (match.Success)
  21.                 {
  22.                     string furniture = match.Groups["furniture12"].Value;
  23.                     decimal price = decimal.Parse(match.Groups["price"].Value);
  24.                     int count = int.Parse(match.Groups["count"].Value);
  25.  
  26.                     furn.Add(furniture);
  27.                     totalPrice += count * price;
  28.                 }
  29.  
  30.                 input = Console.ReadLine();
  31.             }
  32.             Console.WriteLine("Bought furniture:");
  33.  
  34.             foreach (var furniture in furn)
  35.             {
  36.                 Console.WriteLine(furniture);
  37.             }
  38.             //Console.WriteLine(string.Join("\n", furn));
  39.             Console.WriteLine($"Total money spend: {totalPrice:f2}");
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement