Advertisement
EvgeniVT

Healthy Heaven

Oct 23rd, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. namespace HealthyHeaven
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     public class StartUp
  9.     {
  10.         public static void Main()
  11.         {
  12.             // Initialize the repository
  13.             Restaurant restaurant = new Restaurant("Casa Domingo");
  14.  
  15.             // Initialize the entities
  16.             Vegetable tomato = new Vegetable("Tomato", 20);
  17.             Vegetable cucumber = new Vegetable("Cucumber", 15);
  18.  
  19.             Salad salad = new Salad("Tomatoes with cucumbers");
  20.  
  21.             salad.Add(tomato);
  22.             salad.Add(cucumber);
  23.  
  24.             Console.WriteLine(salad.GetTotalCalories()); // 35
  25.             Console.WriteLine(salad.GetProductCount());  // 2
  26.  
  27.             Console.WriteLine(salad.ToString());
  28.             // * Salad Tomatoes with cucumbers is 35 calories and have 2 products:
  29.             //  - Tomato have 20 calories
  30.             //  - Cucumber have 15 calories
  31.  
  32.             restaurant.Add(salad);
  33.  
  34.             Console.WriteLine(restaurant.Buy("Invalid salad")); // False
  35.  
  36.             // Initialize the second entities
  37.             Vegetable corn = new Vegetable("Corn", 90);
  38.             Salad casaDomingo = new Salad("Casa Domingo");
  39.  
  40.             casaDomingo.Add(tomato);
  41.             casaDomingo.Add(cucumber);
  42.             casaDomingo.Add(corn);
  43.  
  44.             restaurant.Add(casaDomingo);
  45.  
  46.             Console.WriteLine(restaurant.GetHealthiestSalad()); // Tomatoes with cucumbers
  47.  
  48.             Console.WriteLine(restaurant.GenerateMenu());
  49.             // Casa Domingo have 2 salads:
  50.             // * Salad Tomatoes with cucumbers is 35 calories and have 2 products:
  51.             //  - Tomato have 20 calories
  52.             //  - Cucumber have 15 calories
  53.             // * Salad Casa Domingo is 125 calories and have 3 products:
  54.             //  - Tomato have 20 calories
  55.             //  - Cucumber have 15 calories
  56.             //  - Corn have 90 calories
  57.         }
  58.     }
  59.     public class Vegetable
  60.     {
  61.         private int calories;
  62.  
  63.         public Vegetable(string name, int calories)
  64.         {
  65.             this.Name = name;
  66.             this.Calories = calories;
  67.         }
  68.  
  69.         public string Name { get; set; }
  70.         public int Calories
  71.         {
  72.             get => calories;
  73.             set
  74.             {
  75.                 if (value > 0)
  76.                     calories = value;
  77.             }
  78.         }
  79.         public override string ToString()
  80.         {
  81.             return $" - {this.Name} have {this.Calories} calories";
  82.         }
  83.     }
  84.     public class Salad
  85.     {
  86.         private HashSet<Vegetable> products;
  87.  
  88.         public HashSet<Vegetable> Products
  89.         {
  90.             get { return products; }
  91.             set { products = value; }
  92.         }
  93.  
  94.         public string Name { get; set; }
  95.         public Salad(string name)
  96.         {
  97.             this.Products = new HashSet<Vegetable>();
  98.             this.Name = name;
  99.         }
  100.         public int GetTotalCalories()
  101.         {
  102.             return this.Products.Sum(x => x.Calories);
  103.         }
  104.         public int GetProductCount()
  105.         {
  106.             return this.Products.Count;
  107.         }
  108.         public void Add(Vegetable product)
  109.         {
  110.             this.Products.Add(product);
  111.         }
  112.         public override string ToString()
  113.         {
  114.             var sb = new StringBuilder();
  115.             sb.AppendLine($"* Salad {this.Name} is {GetTotalCalories()} calories and have {GetProductCount()} products:");
  116.             foreach (var product in this.Products)
  117.             {
  118.                 sb.AppendLine($"{product.ToString()}");
  119.             }
  120.             return sb.ToString().TrimEnd();
  121.         }
  122.     }
  123.     public class Restaurant
  124.     {
  125.         private HashSet<Salad> data;
  126.         public string Name { get; set; }
  127.         public HashSet<Salad> Data
  128.         {
  129.             get { return data; }
  130.             set { data = value; }
  131.         }
  132.         public Restaurant(string name)
  133.         {
  134.             this.Data = new HashSet<Salad>();
  135.             this.Name = name;
  136.         }
  137.         public void Add(Salad salad)
  138.         {
  139.             this.Data.Add(salad);
  140.         }
  141.         public bool Buy(string name)
  142.         {
  143.             for (int i = 0; i < this.Data.Count; i++)
  144.             {
  145.                 if (this.Data.ElementAt(i).Name == name)
  146.                 {
  147.                     this.Data.Remove(this.Data.ElementAt(i));
  148.                     return true;
  149.                 }
  150.             }
  151.             return false;
  152.         }
  153.         public Salad GetHealthiestSalad()
  154.         {
  155.             return this.Data
  156.                 .OrderBy(x => x.Products.Sum(y => y.Calories))
  157.                 .FirstOrDefault();
  158.         }
  159.         public string GenerateMenu()
  160.         {
  161.             var sb = new StringBuilder();
  162.             sb.AppendLine($"{this.Name} have {this.Data.Count} salads:");
  163.             foreach (var item in this.Data)
  164.             {
  165.                 sb.AppendLine($"{item.ToString()}");
  166.             }
  167.             return sb.ToString().TrimEnd();
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement