JulianJulianov

04.RegularExpressionsExercise-Furniture

May 1st, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1.                                                      Exercise: Regular Expressions
  2. Problems for exercise and homework for the "C#  Fundamentals" course @ SoftUni
  3. You can check your solutions in Judge
  4. 01. Furniture
  5. Write a program to calculate the total cost of different types of furniture. You will be given some lines of input until you receive the line "Purchase". For the line to be valid it should be in the following format:
  6. ">>{furniture name}<<{price}!{quantity}"
  7. The price can be floating point number or whole number. Store the names of the furniture and the total price. At the end print the each bought furniture on separate line in the format:
  8. "Bought furniture:
  9. {1st name}
  10. {2nd name}
  11. …"
  12. And on the last line print the following: "Total money spend: {spend money}" formatted to the second decimal point.
  13. Examples
  14. Input                             Output                            Comment
  15. >>Sofa<<312.23!3                  Bought furniture:                 Only the Sofa and the TV are valid, for each of them we
  16. >>TV<<300!5                       Sofa                              multiply the price by the quantity and print the result
  17. >Invalid<<!5                      TV
  18. Purchase                          Total money spend: 2436.69
  19.    
  20. >table<!3                         Bought furniture:  
  21. Purchase                          Total money spend: 0.00
  22.  
  23.      
  24. using System;
  25. using System.Text.RegularExpressions;
  26. using System.Collections.Generic;
  27.  
  28. public class Program
  29. {
  30.     public static void Main()
  31.     {
  32.           var pattern = @">>(?<Furniture>[A-Z\sa-z]*)<<(?<Price>\d+(.\d+)?)!(?<Quantity>\d+)";
  33.             var boughtFurniture = "";
  34.             var totalSum = 0.0;
  35.  
  36.             var listFurniture = new List<string>();
  37.             while ((boughtFurniture = Console.ReadLine()) != "Purchase")
  38.             {
  39.                 var matchFurniture = Regex.Matches(boughtFurniture, pattern);
  40.                 if (matchFurniture.Count == 0)//Може и без форийчване чрез if (matchFurniture.Success), който връща True или False
  41.                 {                                                        //{var nameFurniture = matchFurniture.Groups[2].Value;
  42.                     continue;                                            //var price = double.Parse(matchFurniture.Groups[3].Value);
  43.                 }                                                        //var quantity = int.Parse(matchFurniture.Groups[4].Value);
  44.                 foreach (Match type in matchFurniture)                   //...........
  45.                 {                                                        //else{continue;}    //група 1 = (.\d+)
  46.                     var nameFurniture = type.Groups[2/*"Furniture"*/].Value;                  //група 2 = (?<Furniture>[A-Z\sa-z]*)
  47.                     listFurniture.Add(furniture);
  48.                     var price = type.Groups[3/*"Price"*/].Value;                              //група 3 = (?<Price>\d+(.\d+)?)
  49.                     var quantity = type.Groups[4/*"Quantity"*/].Value;                        //група 4 = (?<Quantity>\d+)
  50.                     totalSum += double.Parse(price) * double.Parse(quantity);
  51.                 }
  52.             }
  53.              if (listFurniture.Count > 0)
  54.             {
  55.                 Console.WriteLine($"Bought furniture:\n{string.Join("\n", listFurniture)}\nTotal money spend: {totalSum:F2}");
  56.             }
  57.             else
  58.             {
  59.                 Console.WriteLine($"Bought furniture:\nTotal money spend: {totalSum:F2}");
  60.             }
  61.             //Console.WriteLine($"Bought furniture:");   Друг вариант на завършек.
  62.             //if (listFurniture.Count > 0)
  63.             //{
  64.                 //Console.WriteLine($"{string.Join(Environment.NewLine, listFurniture)}");
  65.             //}
  66.             //Console.WriteLine($"Total money spend: {totalSum:F2}");
  67.     }
  68. }
Add Comment
Please, Sign In to add comment