Advertisement
svetlyoek

Untitled

Jun 17th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. namespace HealthyHeaven
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. public class Salad
  8. {
  9. private string name;
  10.  
  11. private List<Vegetable> products;
  12.  
  13. public Salad(string name)
  14. {
  15. this.Name = name;
  16. this.Products = new List<Vegetable>();
  17. }
  18.  
  19. public string Name
  20. {
  21. get { return this.name; }
  22. set { this.name = value; }
  23. }
  24.  
  25. public List<Vegetable> Products
  26. {
  27. get { return this.products; }
  28. set { this.products = value; }
  29. }
  30.  
  31. public int GetTotalCalories()
  32. {
  33. int sum = 0;
  34.  
  35. foreach(var veg in products)
  36. {
  37. sum += veg.Calories;
  38. }
  39.  
  40. return sum;
  41. }
  42.  
  43. public int GetProductCount()
  44. {
  45. return this.Products.Count;
  46. }
  47.  
  48. public void Add(Vegetable product)
  49. {
  50. if (!this.Products.Contains(product))
  51. {
  52. this.Products.Add(product);
  53. }
  54. }
  55.  
  56. public override string ToString()
  57. {
  58. StringBuilder sb = new StringBuilder();
  59.  
  60. sb.AppendLine($"* Salad {this.Name} is {this.GetTotalCalories()} calories and have {this.GetProductCount()} products: ");
  61.  
  62. foreach (var salad in this.products)
  63. {
  64. sb.AppendLine(salad.ToString());
  65. }
  66.  
  67. return sb.ToString().TrimEnd();
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement