I really did not want to post this question, but I have no ideas for few days already. In my university I was given task to use design patterns to "solve" some tasks.
Here is task I'm stuck on (translation):
The kiosk has 2 vending machines with cocktails. One contains several alcoholic cocktails, the other one - their non-alcoholic alternatives. Depending on the age of buyer dispense alcoholic or non-alcoholic cocktail.
I've already made 2 solutions, but none of them has design patterns. I was thinking about creational patterns, but from examples of its use I don't think that it is good choice. Also thought about strategy, mediator, chain of responsibility, facade and bridge, but as it seemed to me it is doesn't fit here too.
Also, additional problem is I started learning patterns only 2 weeks ago, so still pretty bad at using them.
I don't even care already about quality of code, I just want to finish this task with appropriate pattern and continue my normal studying without forcing me to use any kind of things without real need in them.
What design pattern can I use in my situation?
Here is my last attempt
public class Kiosk
{
private readonly Dispenser _alcoholicDispenser = new Dispenser(), _nonAlcoholicDispenser = new Dispenser();
public void AddCocktail(string name, CocktailRecipe alcoholicRecipe, CocktailRecipe nonAlcoholicRecipe)
{
_alcoholicDispenser.AddCocktail(new Cocktail(name, true, alcoholicRecipe));
_nonAlcoholicDispenser.AddCocktail(new Cocktail(name, false, nonAlcoholicRecipe));
}
public bool RemoveCocktail(string name)
{
if (!_alcoholicDispenser.RemoveCocktail(name))
return false;
_nonAlcoholicDispenser.RemoveCocktail(name);
return true;
}
public Cocktail GetCocktail(Person person, string cocktailName)
{
if (person.Age < 18)
return _nonAlcoholicDispenser.Dispence(cocktailName);
else
return _alcoholicDispenser.Dispence(cocktailName);
}
}
public class Dispenser
{
private readonly Dictionary<string, Cocktail> _cocktails = new Dictionary<string, Cocktail>();
public void AddCocktail(Cocktail cocktail)
{
_cocktails.Add(cocktail.Name, cocktail);
}
public bool RemoveCocktail(string cocktailName)
{
return _cocktails.Remove(cocktailName);
}
public Cocktail Dispence(string cocktailName)
{
return _cocktails[cocktailName];
}
}
public class Cocktail
{
public string Name { get; set; }
public bool Alcoholic { get; set; }
public CocktailRecipe Recipe { get; set; }
public Cocktail(string name, bool alcoholic, CocktailRecipe recipe)
{
Name = name;
Alcoholic = alcoholic;
Recipe = recipe;
}
public override string ToString()
{
string res = "Name:" + Name + "(" + (Alcoholic ? "alcoholic" : "non-alcoholic") + ")";
return res;
}
}
public class CocktailRecipe
{
public List<Ingredient> Ingredients { get; set; }
}
public class Ingredient
{
public string Name { get; set; }
public double Volume { get; set; }
public Ingredient(string name, double volume)
{
Name = name;
Volume = volume;
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main(string[] args)
{
var kiosk = new Kiosk();
kiosk.AddCocktail("Mojito", new CocktailRecipe(), new CocktailRecipe());
var majorPerson = new Person("Tom", 20);
var minorPerson = new Person("Jimmy", 17);
Console.WriteLine(kiosk.GetCocktail(majorPerson, "Mojito"));
Console.WriteLine(kiosk.GetCocktail(minorPerson, "Mojito"));
Console.ReadKey();
}
}
Aucun commentaire:
Enregistrer un commentaire