You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.5 KiB

2 years ago
  1. #include "InputManager.h"
  2. #include <exception>
  3. #include <sstream>
  4. InputManager::InputManager(StorageEditor storageEditor) {
  5. this->editor = storageEditor;
  6. }
  7. InputManager::MenuOption InputManager::getSelectedOption() {
  8. return selectedOption;
  9. }
  10. void InputManager::setSelectedOption(MenuOption option) {
  11. this->selectedOption = option;
  12. }
  13. InputManager::MenuOption InputManager::askMenuOption(std::string question, std::vector<std::string> answers) {
  14. this->displayDialog(question, answers);
  15. int input;
  16. std::cin >> input;
  17. return input;
  18. }
  19. void InputManager::displayDialog(std::string question, std::vector<std::string> answers) {
  20. std::cout << question << std::endl;
  21. std::cout << "-------------------------------" << std::endl;
  22. int counter = 0;
  23. for(std::string answer:answers) {
  24. counter++;
  25. std::cout << counter << ") " << answer << std::endl;
  26. }
  27. std::cout << "-------------------------------" << std::endl;
  28. }
  29. InputManager::WareAndAmount InputManager::displayAddWareDialog() {
  30. std::string name;
  31. bool hasCooling;
  32. double width, height;
  33. int amount;
  34. name = getInput<std::string>("Enter Ware Name:");
  35. hasCooling = getNeedsCooling();
  36. width = getInput<double>("Enter Ware width:");
  37. height = getInput<double>("Enter Ware height:");
  38. amount = getInput<int>("Enter Ware amount:");
  39. InputManager::WareAndAmount wareAndAmount;
  40. wareAndAmount.ware = Ware(name, hasCooling, width, height);
  41. wareAndAmount.amount = amount;
  42. return wareAndAmount;
  43. }
  44. InputManager::WareAndAmount InputManager::displayRemoveWareDialog() {
  45. std::string name;
  46. int amount;
  47. name = getInput<std::string>("Enter Name of the Ware you want to remove:");
  48. amount = getInput<int>("Enter amount you want to remove:");
  49. InputManager::WareAndAmount wareAndAmount;
  50. Ware ware = Ware();
  51. ware.setName(name);
  52. wareAndAmount.ware = ware;
  53. wareAndAmount.amount = amount;
  54. return wareAndAmount;
  55. }
  56. void InputManager::displayMainMenu() {
  57. std::vector<std::string> answers;
  58. std::string question = "Wie moechten Sie verfahren?";
  59. std::string answer = "Ware/n hinzufuegen.";
  60. answers.push_back(answer);
  61. answer = "Ware/n entfernen.";
  62. answers.push_back(answer);
  63. answer = "Ware/n anzeigen.";
  64. answers.push_back(answer);
  65. answer = "Beenden.";
  66. answers.push_back(answer);
  67. selectedOption = askMenuOption(question, answers);
  68. switch(selectedOption) {
  69. case 1: {
  70. InputManager::WareAndAmount x = displayAddWareDialog();
  71. editor.addWaren(x.ware, x.amount);
  72. break;
  73. }
  74. case 2: {
  75. InputManager::WareAndAmount x = displayRemoveWareDialog();
  76. editor.removeWaren(x.ware.getName(), x.amount);
  77. break;
  78. }
  79. case 3: {
  80. std::vector<Ware> waren = this->editor.getWarenStorage().getAllWaren();
  81. displayWaren(waren);
  82. break;
  83. }
  84. case 4: {
  85. return;
  86. break;
  87. }
  88. }
  89. displayMainMenu();
  90. }
  91. // if no more initialization needed --> refactor to setStorageEditor
  92. void InputManager::displayWaren(const std::vector<Ware> waren) {
  93. std::cout << "Liste der sich im Lager befindlichen Waren:" << std::endl;
  94. std::vector<InputManager::WareAndAmount> sortedWarenList = sortAndGroupWaren(waren);
  95. if(sortedWarenList.empty()){
  96. std::cout << "Das Warenlager ist leer." << std::endl;
  97. std::cout << "---------------------" << std::endl;
  98. return;
  99. }
  100. for(WareAndAmount wareAndAmount: sortedWarenList){
  101. std::cout << "Warenname:" << std::endl;
  102. std::cout << wareAndAmount.ware.getName() << std::endl;
  103. std::cout << "Needs Cooling:" << std::endl;
  104. if(wareAndAmount.ware.getHasCooling()) {
  105. std::cout << "true" << std::endl;
  106. }
  107. else {
  108. std::cout << "false" << std::endl;
  109. }
  110. std::cout << "Width:" << std::endl;
  111. std::cout << wareAndAmount.ware.getSize().width << std::endl;
  112. std::cout << "Height:" << std::endl;
  113. std::cout << wareAndAmount.ware.getSize().height << std::endl;
  114. std::cout << "Amount:" << std::endl;
  115. std::cout << wareAndAmount.amount << std::endl;
  116. std::cout << "---------------------" << std::endl;
  117. }
  118. }
  119. //refactor
  120. std::vector<InputManager::WareAndAmount> InputManager::sortAndGroupWaren(const std::vector<Ware>& waren) {
  121. std::vector<WareAndAmount> sortedWaren;
  122. WareAndAmount wareAndAmount;
  123. wareAndAmount.amount = 0;
  124. for(Ware ware : waren) {
  125. if(ware.getType() == Ware::WareType::DefaultWare) {
  126. if(wareAndAmount.amount == 0) {
  127. wareAndAmount.ware = ware;
  128. wareAndAmount.amount++;
  129. }
  130. else if(wareAndAmount.ware.getName() == ware.getName()) {
  131. wareAndAmount.amount++;
  132. }
  133. else {
  134. sortedWaren.push_back(wareAndAmount);
  135. wareAndAmount.amount = 0;
  136. }
  137. }
  138. }
  139. if(!waren.empty() && wareAndAmount.amount > 0){
  140. sortedWaren.push_back(wareAndAmount);
  141. wareAndAmount.amount = 0;
  142. }
  143. for(Ware ware : waren) {
  144. if(ware.getType() == Ware::WareType::CoolingWare) {
  145. if(wareAndAmount.amount == 0) {
  146. wareAndAmount.ware = ware;
  147. wareAndAmount.amount++;
  148. }
  149. else if(wareAndAmount.ware.getName() == ware.getName()) {
  150. wareAndAmount.amount++;
  151. }
  152. else {
  153. sortedWaren.push_back(wareAndAmount);
  154. wareAndAmount.amount = 0;
  155. }
  156. }
  157. }
  158. if(!waren.empty() && wareAndAmount.amount > 0){
  159. sortedWaren.push_back(wareAndAmount);
  160. wareAndAmount.amount = 0;
  161. }
  162. for(Ware ware : waren) {
  163. if(ware.getType() == Ware::WareType::LargeWare) {
  164. if(wareAndAmount.amount == 0) {
  165. wareAndAmount.ware = ware;
  166. wareAndAmount.amount++;
  167. }
  168. else if(wareAndAmount.ware.getName() == ware.getName()) {
  169. wareAndAmount.amount++;
  170. }
  171. else {
  172. sortedWaren.push_back(wareAndAmount);
  173. wareAndAmount.amount = 0;
  174. }
  175. }
  176. }
  177. if(!waren.empty() && wareAndAmount.amount > 0){
  178. sortedWaren.push_back(wareAndAmount);
  179. wareAndAmount.amount = 0;
  180. }
  181. return sortedWaren;
  182. }
  183. bool InputManager::getNeedsCooling() {
  184. std::string input;
  185. bool hasCooling;
  186. std::cout << "Does Ware need Cooling? (y/n)" << std::endl;
  187. try {
  188. std::cin >> input;
  189. if(input == "y") {
  190. hasCooling = true;
  191. }
  192. else if(input == "n") {
  193. hasCooling = false;
  194. }
  195. else {
  196. std::cout << "That didn't work. Please try again :)" << std::endl;
  197. getNeedsCooling();
  198. }
  199. }
  200. catch(const std::exception&){
  201. std::cout << "That didn't work. Please try again :)" << std::endl;
  202. getNeedsCooling();
  203. }
  204. return hasCooling;
  205. }
  206. //std::istream
  207. template <typename T>
  208. T InputManager::getInput(std::string message) const {
  209. std::cout << message << std::endl;
  210. T val;
  211. try {
  212. std::string input;
  213. std::cin >> input;
  214. std::istringstream ss(input);
  215. ss >> val;
  216. }
  217. catch(const std::exception&) {
  218. std::cout << "That didn't work. Please try again :)" << std::endl;
  219. getInput<T>(message);
  220. }
  221. return val;
  222. }