Advertisement
gospod1978

Regular Expression/Furniture

Oct 11th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Text.RegularExpressions;
  8.  
  9.  
  10. namespace Orders
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             string pattern = @">>(?<name>.+)<<(?<price>\d+\.?\d*)!(?<quantity>\d+)";
  17.  
  18.             List<string> furniture = new List<string>();
  19.             string input;
  20.             decimal totalSpendMoney = 0;
  21.             decimal sum = 0;
  22.  
  23.             while ((input = Console.ReadLine()) != "Purchase")
  24.             {
  25.                 Match match = Regex.Match(input, pattern);
  26.  
  27.                 if (match.Success)
  28.                 {
  29.                     string name = match.Groups["name"].Value;
  30.                     decimal price = decimal.Parse(match.Groups["price"].Value);
  31.                     int quantity = int.Parse(match.Groups["quantity"].Value);
  32.  
  33.                     furniture.Add(name);
  34.                     sum = price * quantity;
  35.                     totalSpendMoney += sum;
  36.                 }
  37.             }
  38.  
  39.             Console.WriteLine("Bought furniture:");
  40.  
  41.             foreach (var furnitures in furniture)
  42.             {
  43.                 Console.WriteLine(furnitures);
  44.             }
  45.  
  46.             Console.WriteLine("Total money spend: {0:f2}", totalSpendMoney);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement