Advertisement
Guest User

Untitled

a guest
May 30th, 2020
393
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.Linq;
  4. using System.Text;
  5.  
  6. namespace Christmas
  7. {
  8. public class Bag
  9. {
  10. private List<Present> presents;
  11.  
  12. public Bag(string color, int capacity)
  13. {
  14. Color = color;
  15. Capacity = capacity;
  16. presents = new List<Present>();
  17. }
  18.  
  19. public string Color { get; set; }
  20. public int Capacity { get; set; }
  21. public void Add(Present present)
  22. {
  23. if (presents.Count < Capacity)
  24. {
  25. presents.Add(present);
  26. }
  27. }
  28.  
  29. public bool Remove(string name)
  30. {
  31. if (presents.Any(x => x.Name == name))
  32. {
  33. Present currentPresent = presents.FirstOrDefault(x => x.Name == name);
  34. presents.Remove(currentPresent);
  35. return true;
  36. }
  37. else
  38. {
  39. return false;
  40. }
  41. }
  42.  
  43. public Present GetHeaviestPresent()
  44. {
  45. Present currentPresent = presents.OrderByDescending(x => x.Weight).FirstOrDefault();
  46. return currentPresent;
  47. }
  48.  
  49. public Present GetPresent(string name)
  50. {
  51. Present currentPresent = presents.FirstOrDefault(x => x.Name == name);
  52. return currentPresent;
  53. }
  54. public int Count
  55. {
  56. get
  57. {
  58. return this.presents.Count;
  59. }
  60. }
  61.  
  62. public string Report()
  63. {
  64. StringBuilder stringBuilder = new StringBuilder();
  65. stringBuilder.AppendLine($"{Color} bag contains:");
  66.  
  67. foreach (Present present in presents)
  68. {
  69. stringBuilder.AppendLine($"Present {present.Name} for a {present.Gender}");
  70. }
  71.  
  72. return stringBuilder.ToString().TrimEnd();
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement