Skip to content

Instantly share code, notes, and snippets.

@Geal
Last active February 21, 2018 10:34
Show Gist options
  • Save Geal/e7a7de1f8a5442dd1c32dad63a9f8e86 to your computer and use it in GitHub Desktop.
Save Geal/e7a7de1f8a5442dd1c32dad63a9f8e86 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
class Dish {
public:
virtual string name() =0;
virtual void eat() {
cout << "eating " << name() << endl;
};
};
class Fries: public Dish {
public:
string name() {
return string("fries");
}
};
class Poutine: public Dish {
string name() {
return string("poutine");
}
void eat() {
cout << "eating some " << name() << endl;
};
};
class IceCream: public Dish {
public:
string name() {
return string("ice cream");
}
};
class Tacos: public Dish {
public:
string name() {
return string("Tacos");
}
};
class Waffles: public Dish {
public:
string name() {
return string("waffles");
}
};
class Pizza: public Dish {
public:
string name() {
return string("pizza");
}
};
class DishFactory {
public:
virtual string name() =0;
virtual Dish* create() =0;
};
class FriesFactory: public DishFactory {
public:
string name() {
return string("frites");
}
Dish* create() {
return new Fries();
}
};
class TacosFactory: public DishFactory {
public:
string name() {
return string("tacos");
}
Dish* create() {
return new Tacos();
}
};
class PoutineFactory: public DishFactory {
public:
string name() {
return string("poutine");
}
Dish* create() {
return new Poutine();
}
};
class IceCreamFactory: public DishFactory {
public:
string name() {
return string("glace");
}
Dish* create() {
return new IceCream();
}
};
class WafflesFactory: public DishFactory {
public:
string name() {
return string("gauffres");
}
Dish* create() {
return new Waffles();
}
};
class PizzaFactory: public DishFactory {
public:
string name() {
return string("pizza");
}
Dish* create() {
return new Pizza();
}
};
class Service {
vector<string> menu;
vector<int> choices;
vector<Dish*> plates;
public:
Service();
void show_menu();
bool check_choice(int choice);
void show_choice(int choice);
int order();
void present_plates();
void set_menu(vector<string> m);
vector<int>& get_choices();
void set_plates(vector<Dish*> p);
};
Service::Service() {
}
void Service::set_menu(vector<string> m) {
menu = m;
}
vector<int>& Service::get_choices() {
return choices;
}
void Service::set_plates(vector<Dish*> p) {
plates = p;
}
void Service::present_plates() {
if(plates.size() > 0) {
cout << "Voici les plats:" << endl;
for(int i = 0; i < plates.size(); i++) {
cout << i << "- " << plates[i]->name() << endl;
}
for(int i = 0; i < plates.size(); i++) {
Dish* p = plates.back();
plates.pop_back();
delete p;
}
}
}
void Service::show_menu() {
cout << "Voici le menu:" << endl;
for(int i = 0; i < menu.size(); i++) {
cout << i << "- " << menu[i] << endl;
}
}
bool Service::check_choice(int choice) {
return choice >= 0 && choice < menu.size();
}
void Service::show_choice(int choice) {
cout << "voici votre " << menu[choice] << endl;
}
int Service::order() {
int choice = 0;
do {
cin >> choice;
if(! check_choice(choice)) {
cout << "choix invalide" << endl;
}
} while ( ! check_choice(choice) );
choices.push_back(choice);
return choice;
}
class Kitchen {
Service& service;
queue<int> orders;
vector<DishFactory*> dishes;
vector<Dish*> plates;
void send_menu();
void receive_commands();
void prepare_commands();
void send_plates();
public:
Kitchen(Service& service);
void do_stuff();
};
Kitchen::Kitchen(Service& s) : service(s) {
dishes.push_back(new IceCreamFactory());
dishes.push_back( (DishFactory*) new PizzaFactory());
dishes.push_back(new FriesFactory());
dishes.push_back(new TacosFactory());
dishes.push_back(new WafflesFactory());
dishes.push_back(new PoutineFactory());
dishes.push_back(new TacosFactory());
send_menu();
}
void Kitchen::send_menu() {
vector<string> menu;
for (int i = 0; i < dishes.size(); i++) {
menu.push_back(dishes[i]->name());
}
service.set_menu(menu);
}
void Kitchen::do_stuff() {
receive_commands();
prepare_commands();
send_plates();
}
void Kitchen::receive_commands() {
vector<int>& choices = service.get_choices();
for(int i = 0; i < choices.size(); i++) {
orders.push(choices[i]);
}
choices.clear();
}
void Kitchen::prepare_commands() {
int size = min(3, (int)orders.size());
for(int i= 0; i < size; i++) {
int o = orders.front();
orders.pop();
Dish* plate = dishes[o]->create();
plates.push_back(plate);
}
}
void Kitchen::send_plates() {
service.set_plates(plates);
plates.clear();
}
int main() {
int nb_places = 10;
Service waiter;
Kitchen kitchen(waiter);
while(true) {
kitchen.do_stuff();
waiter.present_plates();
cout << "Bonjour!" << endl;
cout << "Combien êtes-vous?" << endl;
int nb_clients;
cin >> nb_clients;
vector<int> choices;
if (nb_clients > 0 && nb_clients <= nb_places) {
nb_places = nb_places - nb_clients;
cout << "Asseyez-vous svp." << endl;
waiter.show_menu();
for(int i = 0; i < nb_clients; i++) {
cout << "Client " << i <<
", que choisissez-vous?" << endl;
int choice = waiter.order();
}
} else if (nb_clients > 0) {
cout << "il n'y a pas assez de places." << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment