Skip to content

Instantly share code, notes, and snippets.

@PhiHuyHoang
Created October 3, 2018 18:51
Show Gist options
  • Save PhiHuyHoang/3ca1e83b26019386e002721676ad223f to your computer and use it in GitHub Desktop.
Save PhiHuyHoang/3ca1e83b26019386e002721676ad223f to your computer and use it in GitHub Desktop.
public class Food
{
private string name;
private int price;
private List<string> ingredient;
private int somethingMore;
//Builder Pattern
public string Name(string name) { this.name = name; return name; }
public int Price(int price) { this.price = price; return price; }
public List<string> Ingredient(List<string> ingredient) { this.ingredient = ingredient; return ingredient; }
public int SomethingMore(int somethingMore) { this.somethingMore = somethingMore; return somethingMore; }
public int ReadsomethingMore() { return somethingMore; }
public Food() {}
//Constructor
public Food(string name, int price, List<string> ingredient)
{
this.name = name;
this.price = price;
this.ingredient = ingredient;
}
public override string ToString()
{
return $"Food: {name}, Price: {price}" +
$" Ingredient: {String.Join(",", ingredient)}";
}
}
class Program
{
static void Main(string[] args)
{
List<Food> menu = new List<Food>();
Food A = new Food();
A.Name("Food A");
A.Price(1000);
A.Ingredient(new List<string> { "ingredient A", "ingredient B" });
A.SomethingMore(5000);
Food B = new Food();
B.Name("Food B");
B.Price(2000);
B.Ingredient(new List<string> { "ingredient B", "ingredient C" });
Food C = new Food("Food C", 3000, new List<string> { "ingredient C", "ingredient D" });
Food D = new Food("Food D", 4000, new List<string> { "ingredient D", "ingredient E" });
menu.AddRange(new List<Food> { A, B, C, D });
foreach (Food f in menu)
{
Console.WriteLine(f.ToString());
}
Console.WriteLine($"Something More of A: {A.ReadsomethingMore()}");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment