Last active
November 11, 2019 14:44
-
-
Save indyone/8021d6e5ad4835f02fad4b2556c49bd1 to your computer and use it in GitHub Desktop.
Sample Recipe Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This sample application is to quickly get started for the .NET training. | |
Using this code you can demostrate the: | |
- Creating a Project | |
- Basic Debugging | |
- Adding a Class Library (Move the `Recipe` class on its own project) | |
- Using a Nuget Package (eg. Humanizer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace RecipeApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Recipe burgerRecipe = new Recipe("Classic Cheeseburger"); | |
burgerRecipe.Servings = 4; | |
burgerRecipe.TotalTime = TimeSpan.FromMinutes(30); | |
burgerRecipe.AddItem("ground lean beef", 750, Measurement.Gram); | |
burgerRecipe.AddItem("hamburger buns", 4, Measurement.Piece); | |
burgerRecipe.AddItem("cheddar cheese", 8, Measurement.Piece); | |
burgerRecipe.AddItem("mayonnaise", 8, Measurement.Tablespoon); | |
burgerRecipe.AddItem("ketchup", 4, Measurement.Tablespoon); | |
Console.Write(burgerRecipe); | |
} | |
} | |
public class Recipe | |
{ | |
private readonly List<IngredientItem> _ingredientsList = new List<IngredientItem>(); | |
public string Title { get; } | |
public TimeSpan TotalTime { get; set; } | |
public int Servings { get; set; } | |
public IEnumerable<IngredientItem> IngredientsList => _ingredientsList; | |
public Recipe(string title) | |
{ | |
Title = title; | |
} | |
public void AddItem(string itemName, double quantityAmount, Measurement quantityMeasurement) | |
{ | |
IngredientItem item = new IngredientItem(); | |
item.Name = itemName; | |
item.QuantityAmount = quantityAmount; | |
item.QuantityMeasurement = quantityMeasurement; | |
_ingredientsList.Add(item); | |
} | |
public override string ToString() | |
{ | |
string output = $"Recipe: {Title}\nServings: {Servings}, Total Time: {TotalTime}\n\n" + | |
"Ingredient List\n"; | |
foreach (IngredientItem ingredientItem in IngredientsList) | |
{ | |
output += $"{ingredientItem.QuantityAmount} {ingredientItem.QuantityMeasurement} {ingredientItem.Name}\n"; | |
} | |
return output; | |
} | |
} | |
public class IngredientItem | |
{ | |
public string Name { get; set; } | |
public double QuantityAmount { get; set; } | |
public Measurement QuantityMeasurement { get; set; } | |
} | |
public enum Measurement | |
{ | |
Piece, | |
Gram, | |
Liter, | |
Tablespoon | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment