patterns/iterator/simpleiterator/waitress.d

80 lines
1.6 KiB
D
Raw Normal View History

2022-12-05 07:38:19 +00:00
module waitress;
import menu, iterator, menuitem;
import std.stdio : writeln, write;
class Waitress
{
private Menu dinerMenu;
this(Menu dinerMenu)
{
this.dinerMenu = dinerMenu;
}
void printMenu()
{
writeln("MENU\n----\nLUNCH");
printMenu(dinerMenu.createIterator());
}
private void printMenu(Iterator iterator)
{
while (iterator.hasNext())
{
MenuItem menuItem = iterator.next();
write(menuItem.getName(), ", ");
write(menuItem.getPrice(), " -- ");
writeln(menuItem.getDescription());
}
}
void printVegetarianMenu()
{
writeln("MENU\n----\nVEGETARIAN");
printVegetarianMenu(dinerMenu.createIterator());
}
// bool isItemVegetarian(string name)
// {
// Iterator dinnerIterator = dinerMenu.createIterator();
// if (isVegetarian(name, dinnerIterator))
// {
// return true;
// }
// return false;
// }
private void printVegetarianMenu(Iterator iterator)
{
while (iterator.hasNext())
{
MenuItem menuItem = iterator.next();
if (menuItem.isVegetarian())
{
write(menuItem.getName());
writeln("\t\t", menuItem.getPrice());
writeln("\t", menuItem.getDescription());
}
}
}
// private bool isVegetarian(string name, Iterator iterator)
// {
// while (iterator.hasNext())
// {
// MenuItem menuItem = iterator.next();
// if (menuItem.getName() == name)
// {
// if (menuItem.isVegetarian())
// {
// return true;
// }
// }
// }
// return false;
// }
}