Guest User

Untitled

a guest
Oct 22nd, 2019
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ConsoleApplication2.cpp : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma.
  2. //
  3.  
  4. #include <iostream>
  5. #include <limits>
  6. #include <functional>
  7. #include <string>
  8. #include <sstream> // std::stringstream
  9. #include <vector>
  10. #include <map>
  11. #include <windows.h>
  12.  
  13. HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
  14.  
  15. void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
  16. {
  17. if (size == 0) {
  18. size = 20; // default cursor size Changing to numbers from 1 to 20, decreases cursor width
  19. }
  20. CONSOLE_CURSOR_INFO lpCursor;
  21. lpCursor.bVisible = visible;
  22. lpCursor.dwSize = size;
  23. SetConsoleCursorInfo(console, &lpCursor);
  24. }
  25.  
  26. class Portata {
  27. std::string _name;
  28. std::vector<std::string> goods;
  29. std::vector<std::string>::iterator _it;
  30.  
  31. public:
  32. Portata(std::string name) {
  33. _it = goods.begin();
  34. _name = name;
  35. }
  36.  
  37. void addGood(std::string good) {
  38. goods.insert(_it, good);
  39. _it++;
  40. }
  41.  
  42. std::string getName() {
  43. return _name;
  44. }
  45.  
  46. std::string getGood(int pos) {
  47. return goods.at(pos);
  48. }
  49. };
  50.  
  51. class Comanda {
  52. std::vector<Portata>::iterator it;
  53. int nPortate = 0;
  54.  
  55. public:
  56. std::vector<Portata> portate;
  57.  
  58. public:
  59. Comanda() {
  60. portate.push_back(Portata("Beverage"));
  61. }
  62.  
  63. void addPortata() {
  64. nPortate++;
  65. std::string nomePortata = "Portata #";
  66.  
  67. nomePortata.append(std::to_string(nPortate));
  68. portate.push_back(Portata(nomePortata));
  69.  
  70. std::cout << nomePortata << " creata con successo." << std::endl;
  71. }
  72. };
  73.  
  74. class Table {
  75. int _nTable;
  76. int _nPeople = 1;
  77. Comanda _comanda;
  78. float _conto;
  79.  
  80. public:
  81. Table(int nTable) {
  82. _nTable = nTable;
  83. }
  84.  
  85. void setComanda(Comanda comanda) {
  86. _comanda = comanda;
  87. }
  88.  
  89. Comanda getComanda() {
  90. return _comanda;
  91. }
  92.  
  93. void setnPeople(int n) {
  94. _nPeople = n;
  95. }
  96.  
  97. int getnTable() {
  98. return _nTable;
  99. }
  100.  
  101. int getnPeople() {
  102. return _nPeople;
  103. }
  104. };
  105.  
  106. void prendiComanda() {
  107. int nTable;
  108. int nPeople;
  109. std::string good;
  110. Comanda comanda;
  111.  
  112. std::cout << "Scegli il numero del tavolo:" << std::endl;
  113. std::cin >> nTable;
  114. Table table(nTable);
  115.  
  116. std::cout << "Quante persone sono sedute al tavolo? ";
  117. std::cin >> nPeople;
  118. table.setnPeople(nPeople);
  119.  
  120. std::cin.ignore();
  121.  
  122. std::cout << "Cosa prendono da bere? ";
  123. getline(std::cin, good);
  124. comanda.portate[0].addGood(good);
  125. good.clear();
  126.  
  127. std::cout << "Gradite un aperitivo? Cosa prendete? ";
  128. getline(std::cin, good);
  129. comanda.addPortata();
  130. comanda.portate[1].addGood(good);
  131.  
  132. table.setComanda(comanda);
  133.  
  134. std::cout << "Resoconto comanda:" << std::endl;
  135. std::cout << "Tavolo n. " << table.getnTable() << std::endl;
  136. std::cout << "N. persone: " << table.getnPeople() << std::endl;
  137. std::cout << table.getComanda().portate.at(1).getName() << ":" << std::endl;
  138. std::cout << table.getComanda().portate.at(1).getGood(0) << std::endl;
  139. std::cout << table.getComanda().portate.at(0).getName() << ":" << std::endl;
  140. std::cout << table.getComanda().portate.at(0).getGood(0) << std::endl;
  141.  
  142.  
  143. }
  144.  
  145. class Context {
  146.  
  147. typedef std::function<void()> pFunc;
  148. typedef std::pair<std::string, pFunc> Pair;
  149. typedef std::map<std::string, pFunc> mapFunc;
  150.  
  151. private:
  152. std::string name;
  153. std::vector<std::string> commands;
  154. mapFunc map;
  155.  
  156. public:
  157. Context() {
  158. name = "PLEASE SELECT A CONTEXT";
  159. }
  160.  
  161. Context(std::string n, std::initializer_list<std::string> commlist) {
  162. name = n;
  163. for (auto elem : commlist) {
  164. commands.push_back(elem);
  165. }
  166. }
  167.  
  168. std::string getContextName() {
  169. return name;
  170. }
  171.  
  172. std::vector<std::string> getCommands() {
  173. return commands;
  174. }
  175.  
  176. void assignCommand(std::string command, pFunc pf) {
  177. bool isCommandFound = false;
  178. for (auto elem : commands) {
  179. if (elem == command) {
  180. isCommandFound = true;
  181. //Assegna funzione.
  182. map.insert(Pair(command, pf));
  183. }
  184. }
  185. if (isCommandFound == false) {
  186. std::cout << "Command not found in list." << std::endl;
  187. }
  188. }
  189.  
  190. void help() {
  191. std::cout << "[" << name << "]" << std::endl;
  192. std::cout << "Commands available: ";
  193.  
  194. for (auto elem : commands) {
  195. std::cout << elem;
  196. }
  197. }
  198. };
  199.  
  200.  
  201. void interLoop() {
  202. std::string line;
  203. std::string command;
  204. std::vector<std::string> tokens;
  205. std::vector<std::string> contextnames = { "SALA", "TABLE", "ORDER" };
  206.  
  207. Context sala = Context("SALA", { "selcontext" , "seltable" , "addtable", "freetable", "help", "exit" });
  208. Context table = Context("TABLE", { "selcontext", "setpeople", "setorder", "showinfo", "help", "exit" });
  209. Context order = Context("ORDER", { "selcontext", "addcourse", "selcourse", "remcourse", "showinfo", "help", "exit" });
  210.  
  211. Context currentContext = sala;
  212.  
  213. //Entering loop.
  214. std::cout << "Welcome to this simple shell." << std::endl;
  215. std::cout << "Enter \"help\" to show available commands. Enter \"exit\" to quit the shell." << std::endl << std::endl;
  216.  
  217.  
  218. while (command != "exit") {
  219. std::stringstream ss;
  220. bool isCommandFound = false;
  221. std::cout << "[SIMPLE SHELL::" << currentContext.getContextName() << "]: ";
  222.  
  223. std::getline(std::cin, line);
  224. ss << line;
  225. for (std::string each; std::getline(ss, each, ' '); tokens.push_back(each));
  226. command = tokens[0];
  227.  
  228. for (auto elem : currentContext.getCommands()) {
  229. if (command == elem) {
  230. isCommandFound = true;
  231. if (command == "exit") {
  232. break;
  233. }
  234. if (command == "help") {
  235. for (auto elem : currentContext.getCommands()) {
  236. std::cout << elem << " ";
  237. }
  238. std::cout << std::endl;
  239. }
  240. else {
  241. std::cout << "Command not assigned yet." << std::endl;
  242. break;
  243. }
  244.  
  245. }
  246.  
  247. }
  248. if (isCommandFound == false) {
  249. std::cout << "Command not available. Enter \"help\" to show list of commands." << std::endl;
  250. }
  251. tokens.clear();
  252. }
  253. }
  254.  
  255. int main() {
  256.  
  257. setcursor(1, 10);
  258.  
  259. //prendiComanda();
  260.  
  261. interLoop();
  262.  
  263. return 0;
  264. }
  265.  
  266. // Per eseguire il programma: CTRL+F5 oppure Debug > Avvia senza eseguire debug
  267. // Per eseguire il debug del programma: F5 oppure Debug > Avvia debug
  268.  
  269. // Suggerimenti per iniziare:
  270. // 1. Usare la finestra Esplora soluzioni per aggiungere/gestire i file
  271. // 2. Usare la finestra Team Explorer per connettersi al controllo del codice sorgente
  272. // 3. Usare la finestra di output per visualizzare l'output di compilazione e altri messaggi
  273. // 4. Usare la finestra Elenco errori per visualizzare gli errori
  274. // 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
  275. // 6. Per aprire di nuovo questo progetto in futuro, passare a File > Apri > Progetto e selezionare il file con estensione sln
RAW Paste Data