#include<vector>
#include<iostream>#include<list>
using namespace std;
class MenuItem
{
public:
MenuItem(){}
MenuItem(string name,string des,double pri)
{
this->name = name;
this->des = des;
this->pri = pri;
}
string getName()
{
return name;
}
string getDes()
{
return des;
}
double getPri()
{
return pri;
}
private:
string name;
string des;
double pri;
};
class MyIterator
{
public:
virtual bool hasNext(){}
virtual MenuItem next(){}
};
class ConcreteIterator:public MyIterator
{
private:
list<MenuItem>items;
list<MenuItem>::const_iterator iter;
public:
ConcreteIterator(list<MenuItem>item)
{
items = item;
iter = items.begin();
}
MenuItem next()
{
MenuItem menuItem = *iter;
++ iter;
return menuItem;
}
bool hasNext()
{
if(iter == items.end())
{
return false;
}
else
{
return true;
}
}
};
class Menu
{
public:
virtual MyIterator *createIterator(){}
};
class ConcreteMenu : public Menu
{
private:
list<MenuItem>items;
public:
ConcreteMenu()
{
addItem("kfs","good",10.2);
addItem("cc","bad",101.2);
}
void addItem(string name,string des,double pri)
{
MenuItem menu (name,des,pri);
items.push_back(menu);
}
MyIterator *createIterator()
{
return new ConcreteIterator(items);
}
};
class Waitress
{
private:
Menu *ConcreteMenu;
public:
Waitress(Menu *menu)
{
ConcreteMenu = menu;
}
void printMenu()
{
MyIterator *iter = ConcreteMenu->createIterator();
while(iter->hasNext())
{
MenuItem menu = iter->next();
cout<<"name="<<menu.getName();
cout<<"des="<<menu.getDes();
cout<<"pri="<<menu.getPri()<<endl;
}
}
};
int main()
{
Menu *menu = new ConcreteMenu();
Waitress wa(menu);
wa.printMenu();
}