Advertisement
Aliendreamer

topings

Oct 29th, 2018
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5.  
  6. namespace PizzaCalories
  7. {
  8.     class Pizza
  9.     {
  10.         private string name;
  11.         private Dough dough;
  12.  
  13.         public Pizza(string name)
  14.         {
  15.             this.Name = name;
  16.             this.Toppings = new List<Topping>();
  17.            
  18.         }
  19.  
  20.         public string Name
  21.         {
  22.             get => name;
  23.             set
  24.             {
  25.                 if (value.Length<1 || value.Length>15)
  26.                 {
  27.                     Exception ex = new ArgumentException("Pizza name should be between 1 and 15 symbols.");
  28.                     Console.WriteLine(ex.Message);
  29.                     Environment.Exit(0);
  30.                 }
  31.                 name = value;
  32.             }
  33.         }
  34.  
  35.         private List<Topping> Toppings{get;set;}
  36.  
  37.         public Dough Dough { get => dough; set => dough = value; }
  38.  
  39.         public void Add(Topping topping)
  40.         {
  41.             if (this.Toppings.Count > 10)
  42.                 {
  43.                     Exception ex = new ArgumentException("Number of toppings should be in range [0..10].");
  44.                     Console.WriteLine(ex.Message);
  45.                     Environment.Exit(0);
  46.                 }
  47.             this.Toppings.Add(topping);
  48.         }
  49.  
  50.         private double GetCalories()
  51.         {
  52.             double doughCalories = this.Dough.DoughCalories;
  53.             double toppingsCalories = this.Toppings.Sum(c=>c.ToppingCalories);
  54.             return doughCalories + toppingsCalories;
  55.         }
  56.  
  57.         public override string ToString()
  58.         {
  59.             return $"{this.Name} - {this.GetCalories():f2} Calories.";
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement