Advertisement
Guest User

Furniture

a guest
Jul 11th, 2019
1,601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 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 regex = @">>(?<name>[A-z]+)<<(?<price>\d+\.?\d*)!(?<quant>\d+)";
  12.  
  13.             string input = Console.ReadLine();
  14.             var items = new List<string>();
  15.             double totalPrice = 0;
  16.  
  17.             while (input != "Purchase")
  18.             {
  19.                 MatchCollection matched = Regex.Matches(input, regex);
  20.                 foreach (Match m in matched)
  21.                 {
  22.                     var name = m.Groups["name"].Value;
  23.                     var price = double.Parse(m.Groups["price"].Value);
  24.                     var quant = int.Parse(m.Groups["quant"].Value);
  25.                     items.Add(name);
  26.                     totalPrice += price * quant;
  27.                 }
  28.                 input = Console.ReadLine();
  29.             }
  30.             Console.WriteLine($"Bought furniture:{Environment.NewLine}{string.Join(Environment.NewLine, items)}");
  31.             Console.WriteLine($"Total money spend: {totalPrice:F2}");
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement