petarkobakov

Ad Astra

Aug 15th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Reflection.Metadata;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Ad_Astra
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string text = Console.ReadLine();
  14. Regex pattern = new Regex(@"((?<separator>[#]|[\|]){1})(?<item>[A-Za-z\s]+)(\1)(?<expiration>[0-9]{2}\/[0-9]{2}\/[0-9]{2})(\1)(?<calories>[0-9]+)(\1)");
  15.  
  16. int calPerDay = 2000;
  17.  
  18. MatchCollection matches = pattern.Matches(text);
  19.  
  20. List<Items> itemlist = new List<Items>();
  21. int caloriesSum = 0;
  22.  
  23.  
  24. if (pattern.IsMatch(text))
  25. {
  26. foreach (Match match in matches)
  27. {
  28. string item = match.Groups["item"].Value;
  29. string expiration = match.Groups["expiration"].Value;
  30. int calories = int.Parse(match.Groups["calories"].Value);
  31. Items currentItem = new Items(item,expiration,calories);
  32. itemlist.Add(currentItem);
  33.  
  34. }
  35. }
  36.  
  37. foreach (var item in itemlist)
  38. {
  39. caloriesSum += item.Calories;
  40. }
  41.  
  42. int days = caloriesSum/calPerDay;
  43.  
  44. Console.WriteLine($"You have food to last you for: {days} days!");
  45.  
  46. foreach (var item in itemlist)
  47. {
  48. Console.WriteLine($"Item: {item.Item}, Best before: {item.Expiration}, Nutrition: {item.Calories}");
  49. }
  50. }
  51. }
  52.  
  53. public class Items
  54. {
  55. public string Item { get; set; }
  56. public string Expiration { get; set; }
  57. public int Calories { get; set; }
  58.  
  59. public Items(string item, string expiration, int calories)
  60. {
  61. Item = item;
  62. Expiration = expiration;
  63. Calories = calories;
  64. }
  65. }
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment