Advertisement
yanchevilian

03. Cocktail Party - Advanced Retake Exam - 14 April 2021

Dec 12th, 2021
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.37 KB | None | 0 0
  1. namespace CocktailParty
  2. {
  3.     using System;
  4.  
  5.     public class StartUp
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             //Sample Code Usage:
  10.  
  11.             //Initialize Cocktail
  12.             Cocktail cocktail = new Cocktail("Pina Colada", 3, 10);
  13.  
  14.             //Initialize Ingredient
  15.             Ingredient rum = new Ingredient("Rum", 2, 3);
  16.  
  17.             //Print rum
  18.             Console.WriteLine(rum.ToString());
  19.  
  20.             //Ingredient: Rum
  21.             //Quantity: 3
  22.             //Alcohol: 2
  23.  
  24.             //Add rum
  25.             cocktail.Add(rum);
  26.  
  27.             //Remove rum
  28.             Console.WriteLine(cocktail.Remove("Rum")); // true
  29.  
  30.             Ingredient vodka = new Ingredient("Vodka", 2, 5);
  31.             Ingredient milk = new Ingredient("Milk", 0, 5);
  32.  
  33.             //Add ingredients
  34.             cocktail.Add(vodka);
  35.             cocktail.Add(milk);
  36.  
  37.             //GetMostAlcoholicIngredient
  38.             Console.WriteLine(cocktail.GetMostAlcoholicIngredient());
  39.             //Ingredient: Vodka
  40.             //Quantity: 5
  41.             //Alcohol: 2
  42.  
  43.             //CurrentAlcoholLevel
  44.             Console.WriteLine(cocktail.CurrentAlcoholLevel);
  45.             //2
  46.  
  47.             //Print Cocktail report
  48.             Console.WriteLine(cocktail.Report());
  49.  
  50.             //Cocktail: Pina Colada - Current Alcohol Level: 2
  51.             //Ingredient: Vokda
  52.             //Quantity: 5
  53.             //Alcohol: 2
  54.             //Ingredient: Milk
  55.             //Quantity: 5
  56.             //Alcohol: 0
  57.  
  58.         }
  59.     }
  60.     public class Ingredient
  61.     {
  62.         public Ingredient(string name, int alcohol, int quantity)
  63.         {
  64.             this.Name = name;
  65.             this.Alcohol = alcohol;
  66.             this.Quantity = quantity;
  67.         }
  68.         public string Name { get; set; }
  69.         public int Alcohol { get; set; }
  70.         public int Quantity { get; set; }
  71.  
  72.         public override string ToString()
  73.         {
  74.             StringBuilder sb = new StringBuilder();
  75.             sb.AppendLine($"Ingredient: {this.Name}");
  76.             sb.AppendLine($"Quantity: {this.Quantity}");
  77.             sb.AppendLine($"Alcohol: {this.Alcohol}");
  78.  
  79.             return sb.ToString().TrimEnd();
  80.         }
  81.     }
  82.  public class Cocktail
  83.     {
  84.         public Cocktail(string name, int capacity, int maxAlcoholLevel)
  85.         {
  86.             this.Name = name;
  87.             this.Capacity = capacity;
  88.             this.MaxAlcoholLevel = maxAlcoholLevel;
  89.             this.Ingredients = new List<Ingredient>();
  90.         }
  91.         //•   Name: string
  92.         //•   Capacity: int - the maximum allowed number of ingredients in the cocktail
  93.         //•   MaxAlcoholLevel: int - the maximum allowed amount of alcohol in the cocktail
  94.         public string Name { get; set; }
  95.         public int Capacity { get; set; }
  96.         public int MaxAlcoholLevel { get; set; }
  97.         public List<Ingredient> Ingredients { get; set; }
  98.  
  99.         //•   Method Add(Ingredient ingredient) - adds the entity if there isn't an Ingredient with the same name and if there is enough space in terms of quantity and alcohol.
  100.         public void Add(Ingredient ingredient)
  101.         {
  102.             if (this.Ingredients.Count < this.Capacity && this.MaxAlcoholLevel > ingredient.Alcohol &&
  103.                 this.Ingredients.Any(x => x.Name == ingredient.Name) == false)
  104.             {
  105.                 this.Ingredients.Add(ingredient);
  106.             }
  107.         }
  108.         //•   Method Remove(string name) - removes an Ingredient from the cocktail with the given name, if such exists and returns bool if the deletion is successful.
  109.         public bool Remove(string name)
  110.         {
  111.             if (this.Ingredients.Any(x => x.Name == name))
  112.             {
  113.                 this.Ingredients.Remove(this.Ingredients.First(x => x.Name == name));
  114.                 return true;
  115.             }
  116.  
  117.             return false;
  118.         }
  119.         //•   Method FindIngredient(string name) - returns an Ingredient with the given name.If it doesn't exist, return null.
  120.         public Ingredient FindIngredient(string name)
  121.         {
  122.             if (this.Ingredients.Any(x => x.Name == name))
  123.             {
  124.                 return this.Ingredients.First(x => x.Name == name);
  125.             }
  126.  
  127.             return null;
  128.         }
  129.         //•   Method GetMostAlcoholicIngredient() – returns the Ingredient with most Alcohol.
  130.         public Ingredient GetMostAlcoholicIngredient()
  131.         {
  132.             return this.Ingredients.OrderByDescending(x => x.Alcohol).First();
  133.         }
  134.         //•   Getter CurrentAlcoholLevel – returns the amount of alcohol currently in the cocktail.
  135.         public int CurrentAlcoholLevel => this.Ingredients.Sum(x => x.Alcohol);
  136.  
  137.         //    •   Method Report() - returns information about the Cocktail and the Ingredients inside it in the following format:
  138.         //"Cocktail: {name} - Current Alcohol Level: {CurrentAlcoholLevel}
  139.         //{Ingredient1
  140.         //}
  141.         //{Ingredient2
  142.         //}
  143.         //… "
  144.         public string Report()
  145.         {
  146.             StringBuilder sb = new StringBuilder();
  147.             sb.AppendLine($"Cocktail: {this.Name} - Current Alcohol Level: {CurrentAlcoholLevel}");
  148.             foreach (var ingredient in this.Ingredients)
  149.             {
  150.                 sb.AppendLine(ingredient.ToString());
  151.             }
  152.  
  153.             return sb.ToString().TrimEnd();
  154.         }
  155.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement