Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace HealthyHeaven
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class StartUp
- {
- public static void Main()
- {
- // Initialize the repository
- Restaurant restaurant = new Restaurant("Casa Domingo");
- // Initialize the entities
- Vegetable tomato = new Vegetable("Tomato", 20);
- Vegetable cucumber = new Vegetable("Cucumber", 15);
- Salad salad = new Salad("Tomatoes with cucumbers");
- salad.Add(tomato);
- salad.Add(cucumber);
- Console.WriteLine(salad.GetTotalCalories()); // 35
- Console.WriteLine(salad.GetProductCount()); // 2
- Console.WriteLine(salad.ToString());
- // * Salad Tomatoes with cucumbers is 35 calories and have 2 products:
- // - Tomato have 20 calories
- // - Cucumber have 15 calories
- restaurant.Add(salad);
- Console.WriteLine(restaurant.Buy("Invalid salad")); // False
- // Initialize the second entities
- Vegetable corn = new Vegetable("Corn", 90);
- Salad casaDomingo = new Salad("Casa Domingo");
- casaDomingo.Add(tomato);
- casaDomingo.Add(cucumber);
- casaDomingo.Add(corn);
- restaurant.Add(casaDomingo);
- Console.WriteLine(restaurant.GetHealthiestSalad()); // Tomatoes with cucumbers
- Console.WriteLine(restaurant.GenerateMenu());
- // Casa Domingo have 2 salads:
- // * Salad Tomatoes with cucumbers is 35 calories and have 2 products:
- // - Tomato have 20 calories
- // - Cucumber have 15 calories
- // * Salad Casa Domingo is 125 calories and have 3 products:
- // - Tomato have 20 calories
- // - Cucumber have 15 calories
- // - Corn have 90 calories
- }
- }
- public class Vegetable
- {
- private int calories;
- public Vegetable(string name, int calories)
- {
- this.Name = name;
- this.Calories = calories;
- }
- public string Name { get; set; }
- public int Calories
- {
- get => calories;
- set
- {
- if (value > 0)
- calories = value;
- }
- }
- public override string ToString()
- {
- return $" - {this.Name} have {this.Calories} calories";
- }
- }
- public class Salad
- {
- private HashSet<Vegetable> products;
- public HashSet<Vegetable> Products
- {
- get { return products; }
- set { products = value; }
- }
- public string Name { get; set; }
- public Salad(string name)
- {
- this.Products = new HashSet<Vegetable>();
- this.Name = name;
- }
- public int GetTotalCalories()
- {
- return this.Products.Sum(x => x.Calories);
- }
- public int GetProductCount()
- {
- return this.Products.Count;
- }
- public void Add(Vegetable product)
- {
- this.Products.Add(product);
- }
- public override string ToString()
- {
- var sb = new StringBuilder();
- sb.AppendLine($"* Salad {this.Name} is {GetTotalCalories()} calories and have {GetProductCount()} products:");
- foreach (var product in this.Products)
- {
- sb.AppendLine($"{product.ToString()}");
- }
- return sb.ToString().TrimEnd();
- }
- }
- public class Restaurant
- {
- private HashSet<Salad> data;
- public string Name { get; set; }
- public HashSet<Salad> Data
- {
- get { return data; }
- set { data = value; }
- }
- public Restaurant(string name)
- {
- this.Data = new HashSet<Salad>();
- this.Name = name;
- }
- public void Add(Salad salad)
- {
- this.Data.Add(salad);
- }
- public bool Buy(string name)
- {
- for (int i = 0; i < this.Data.Count; i++)
- {
- if (this.Data.ElementAt(i).Name == name)
- {
- this.Data.Remove(this.Data.ElementAt(i));
- return true;
- }
- }
- return false;
- }
- public Salad GetHealthiestSalad()
- {
- return this.Data
- .OrderBy(x => x.Products.Sum(y => y.Calories))
- .FirstOrDefault();
- }
- public string GenerateMenu()
- {
- var sb = new StringBuilder();
- sb.AppendLine($"{this.Name} have {this.Data.Count} salads:");
- foreach (var item in this.Data)
- {
- sb.AppendLine($"{item.ToString()}");
- }
- return sb.ToString().TrimEnd();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement