Advertisement
alexbancheva

Santa's Bag of Presents_class Bag

Jun 19th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace Christmas
  7. {
  8.     public class Bag
  9.     {
  10.         private readonly List<Present> data;
  11.  
  12.         public Bag(string color, int capacity)
  13.  
  14.         {
  15.             this.Color = color;
  16.             this.Capacity = capacity;
  17.             this.data = new List<Present>();
  18.         }
  19.  
  20.         public string Color { get; set; }
  21.  
  22.         public int Capacity { get; set; }
  23.  
  24.         public int Count
  25.         {
  26.             get
  27.             {
  28.                return this.data.Count;
  29.             }
  30.         }
  31.  
  32.         public void Add(Present present)
  33.         {
  34.             if (this.data.Count + 1 <= this.Capacity)
  35.             {
  36.                 this.data.Add(present);
  37.             }
  38.         }
  39.  
  40.         public bool Remove(string name)
  41.         {
  42.             Present present = this.data.FirstOrDefault(p => p.Name == name);
  43.  
  44.             if (present != null)
  45.             {
  46.                 this.data.Remove(present);
  47.                 return true;
  48.             }
  49.             return false;
  50.         }
  51.  
  52.         public Present GetHeaviestPresent()
  53.         {
  54.             Present heaviestPresent = this.data
  55.                     .OrderByDescending(p=>p.Weight)
  56.                     .First();
  57.  
  58.             return heaviestPresent;
  59.         }
  60.  
  61.         public Present GetPresent(string name)
  62.         {
  63.             Present present = this.data.FirstOrDefault(p=>p.Name == name);
  64.  
  65.             return present;
  66.         }
  67.  
  68.         public string Report()
  69.         {
  70.             StringBuilder sb = new StringBuilder();
  71.  
  72.             sb.AppendLine($"{this.Color} bag contains:");
  73.             foreach (var present in this.data)
  74.             {
  75.                 sb.AppendLine(present.ToString());
  76.             }
  77.  
  78.             return sb.ToString().TrimEnd();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement