Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication2.cpp : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma.
- //
- #include <iostream>
- #include <limits>
- #include <functional>
- #include <string>
- #include <sstream> // std::stringstream
- #include <vector>
- #include <map>
- #include <windows.h>
- HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
- void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
- {
- if (size == 0) {
- size = 20; // default cursor size Changing to numbers from 1 to 20, decreases cursor width
- }
- CONSOLE_CURSOR_INFO lpCursor;
- lpCursor.bVisible = visible;
- lpCursor.dwSize = size;
- SetConsoleCursorInfo(console, &lpCursor);
- }
- class Portata {
- std::string _name;
- std::vector<std::string> goods;
- std::vector<std::string>::iterator _it;
- public:
- Portata(std::string name) {
- _it = goods.begin();
- _name = name;
- }
- void addGood(std::string good) {
- goods.insert(_it, good);
- _it++;
- }
- std::string getName() {
- return _name;
- }
- std::string getGood(int pos) {
- return goods.at(pos);
- }
- };
- class Comanda {
- std::vector<Portata>::iterator it;
- int nPortate = 0;
- public:
- std::vector<Portata> portate;
- public:
- Comanda() {
- portate.push_back(Portata("Beverage"));
- }
- void addPortata() {
- nPortate++;
- std::string nomePortata = "Portata #";
- nomePortata.append(std::to_string(nPortate));
- portate.push_back(Portata(nomePortata));
- std::cout << nomePortata << " creata con successo." << std::endl;
- }
- };
- class Table {
- int _nTable;
- int _nPeople = 1;
- Comanda _comanda;
- float _conto;
- public:
- Table(int nTable) {
- _nTable = nTable;
- }
- void setComanda(Comanda comanda) {
- _comanda = comanda;
- }
- Comanda getComanda() {
- return _comanda;
- }
- void setnPeople(int n) {
- _nPeople = n;
- }
- int getnTable() {
- return _nTable;
- }
- int getnPeople() {
- return _nPeople;
- }
- };
- void prendiComanda() {
- int nTable;
- int nPeople;
- std::string good;
- Comanda comanda;
- std::cout << "Scegli il numero del tavolo:" << std::endl;
- std::cin >> nTable;
- Table table(nTable);
- std::cout << "Quante persone sono sedute al tavolo? ";
- std::cin >> nPeople;
- table.setnPeople(nPeople);
- std::cin.ignore();
- std::cout << "Cosa prendono da bere? ";
- getline(std::cin, good);
- comanda.portate[0].addGood(good);
- good.clear();
- std::cout << "Gradite un aperitivo? Cosa prendete? ";
- getline(std::cin, good);
- comanda.addPortata();
- comanda.portate[1].addGood(good);
- table.setComanda(comanda);
- std::cout << "Resoconto comanda:" << std::endl;
- std::cout << "Tavolo n. " << table.getnTable() << std::endl;
- std::cout << "N. persone: " << table.getnPeople() << std::endl;
- std::cout << table.getComanda().portate.at(1).getName() << ":" << std::endl;
- std::cout << table.getComanda().portate.at(1).getGood(0) << std::endl;
- std::cout << table.getComanda().portate.at(0).getName() << ":" << std::endl;
- std::cout << table.getComanda().portate.at(0).getGood(0) << std::endl;
- }
- class Context {
- typedef std::function<void()> pFunc;
- typedef std::pair<std::string, pFunc> Pair;
- typedef std::map<std::string, pFunc> mapFunc;
- private:
- std::string name;
- std::vector<std::string> commands;
- mapFunc map;
- public:
- Context() {
- name = "PLEASE SELECT A CONTEXT";
- }
- Context(std::string n, std::initializer_list<std::string> commlist) {
- name = n;
- for (auto elem : commlist) {
- commands.push_back(elem);
- }
- }
- std::string getContextName() {
- return name;
- }
- std::vector<std::string> getCommands() {
- return commands;
- }
- void assignCommand(std::string command, pFunc pf) {
- bool isCommandFound = false;
- for (auto elem : commands) {
- if (elem == command) {
- isCommandFound = true;
- //Assegna funzione.
- map.insert(Pair(command, pf));
- }
- }
- if (isCommandFound == false) {
- std::cout << "Command not found in list." << std::endl;
- }
- }
- void help() {
- std::cout << "[" << name << "]" << std::endl;
- std::cout << "Commands available: ";
- for (auto elem : commands) {
- std::cout << elem;
- }
- }
- };
- void interLoop() {
- std::string line;
- std::string command;
- std::vector<std::string> tokens;
- std::vector<std::string> contextnames = { "SALA", "TABLE", "ORDER" };
- Context sala = Context("SALA", { "selcontext" , "seltable" , "addtable", "freetable", "help", "exit" });
- Context table = Context("TABLE", { "selcontext", "setpeople", "setorder", "showinfo", "help", "exit" });
- Context order = Context("ORDER", { "selcontext", "addcourse", "selcourse", "remcourse", "showinfo", "help", "exit" });
- Context currentContext = sala;
- //Entering loop.
- std::cout << "Welcome to this simple shell." << std::endl;
- std::cout << "Enter \"help\" to show available commands. Enter \"exit\" to quit the shell." << std::endl << std::endl;
- while (command != "exit") {
- std::stringstream ss;
- bool isCommandFound = false;
- std::cout << "[SIMPLE SHELL::" << currentContext.getContextName() << "]: ";
- std::getline(std::cin, line);
- ss << line;
- for (std::string each; std::getline(ss, each, ' '); tokens.push_back(each));
- command = tokens[0];
- for (auto elem : currentContext.getCommands()) {
- if (command == elem) {
- isCommandFound = true;
- if (command == "exit") {
- break;
- }
- if (command == "help") {
- for (auto elem : currentContext.getCommands()) {
- std::cout << elem << " ";
- }
- std::cout << std::endl;
- }
- else {
- std::cout << "Command not assigned yet." << std::endl;
- break;
- }
- }
- }
- if (isCommandFound == false) {
- std::cout << "Command not available. Enter \"help\" to show list of commands." << std::endl;
- }
- tokens.clear();
- }
- }
- int main() {
- setcursor(1, 10);
- //prendiComanda();
- interLoop();
- return 0;
- }
- // Per eseguire il programma: CTRL+F5 oppure Debug > Avvia senza eseguire debug
- // Per eseguire il debug del programma: F5 oppure Debug > Avvia debug
- // Suggerimenti per iniziare:
- // 1. Usare la finestra Esplora soluzioni per aggiungere/gestire i file
- // 2. Usare la finestra Team Explorer per connettersi al controllo del codice sorgente
- // 3. Usare la finestra di output per visualizzare l'output di compilazione e altri messaggi
- // 4. Usare la finestra Elenco errori per visualizzare gli errori
- // 5. Passare a Progetto > Aggiungi nuovo elemento per creare nuovi file di codice oppure a Progetto > Aggiungi elemento esistente per aggiungere file di codice esistenti al progetto
- // 6. Per aprire di nuovo questo progetto in futuro, passare a File > Apri > Progetto e selezionare il file con estensione sln
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement