Advertisement
markovood

Untitled

Mar 25th, 2020
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace PizzaCalories
  5. {
  6.     public class Pizza
  7.     {
  8.         private string name;
  9.         private List<Topping> toppings;
  10.  
  11.         public Pizza(string name, Dough dough)
  12.         {
  13.             this.Name = name;
  14.             this.Dough = dough;
  15.             this.toppings = new List<Topping>();
  16.         }
  17.  
  18.         public string Name
  19.         {
  20.             get => this.name;
  21.             private set
  22.             {
  23.                 if (string.IsNullOrWhiteSpace(value) || value.Length > 15)
  24.                 {
  25.                     throw new ArgumentException("Pizza name should be between 1 and 15 symbols.");
  26.                 }
  27.  
  28.                 this.name = value;
  29.             }
  30.         }
  31.  
  32.         private Dough Dough { get; set; }
  33.  
  34.         public IReadOnlyList<Topping> Toppings
  35.         {
  36.             get => this.toppings.AsReadOnly();
  37.         }
  38.  
  39.         public double TotalCalories
  40.         {
  41.             get => this.CalculateCalories();
  42.         }
  43.  
  44.         public void SetDough(Dough dough)
  45.         {
  46.             this.Dough = dough;
  47.         }
  48.  
  49.         public void AddTopping(Topping topping)
  50.         {
  51.             if (this.toppings.Count >= 10)
  52.             {
  53.                 throw new ArgumentException("Number of toppings should be in range [0..10].");
  54.             }
  55.  
  56.             this.toppings.Add(topping);
  57.         }
  58.  
  59.         public override string ToString()
  60.         {
  61.             return $"{this.Name} - {this.TotalCalories:F2} Calories.";
  62.         }
  63.  
  64.         private double CalculateCalories()
  65.         {
  66.             double toppingsTotalCalories = GetTotalToppingsCalories(this.toppings);
  67.             return (this.Dough.WeightGR * this.Dough.CaloriesPerGR) + toppingsTotalCalories;
  68.         }
  69.  
  70.         private double GetTotalToppingsCalories(List<Topping> toppings)
  71.         {
  72.             double calories = 0;
  73.             foreach (var topping in toppings)
  74.             {
  75.                 calories += topping.WeightGR * topping.CaloriesPerGR;
  76.             }
  77.  
  78.             return calories;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement