#include "InputManager.h" #include #include InputManager::InputManager(StorageEditor storageEditor) { this->editor = storageEditor; } InputManager::MenuOption InputManager::getSelectedOption() { return selectedOption; } void InputManager::setSelectedOption(MenuOption option) { this->selectedOption = option; } InputManager::MenuOption InputManager::askMenuOption(std::string question, std::vector answers) { this->displayDialog(question, answers); int input; std::cin >> input; return input; } void InputManager::displayDialog(std::string question, std::vector answers) { std::cout << question << std::endl; std::cout << "-------------------------------" << std::endl; int counter = 0; for(std::string answer:answers) { counter++; std::cout << counter << ") " << answer << std::endl; } std::cout << "-------------------------------" << std::endl; } InputManager::WareAndAmount InputManager::displayAddWareDialog() { std::string name; bool hasCooling; double width, height; int amount; name = getInput("Enter Ware Name:"); hasCooling = getNeedsCooling(); width = getInput("Enter Ware width:"); height = getInput("Enter Ware height:"); amount = getInput("Enter Ware amount:"); InputManager::WareAndAmount wareAndAmount; wareAndAmount.ware = Ware(name, hasCooling, width, height); wareAndAmount.amount = amount; return wareAndAmount; } InputManager::WareAndAmount InputManager::displayRemoveWareDialog() { std::string name; int amount; name = getInput("Enter Name of the Ware you want to remove:"); amount = getInput("Enter amount you want to remove:"); InputManager::WareAndAmount wareAndAmount; Ware ware = Ware(); ware.setName(name); wareAndAmount.ware = ware; wareAndAmount.amount = amount; return wareAndAmount; } void InputManager::displayMainMenu() { std::vector answers; std::string question = "Wie moechten Sie verfahren?"; std::string answer = "Ware/n hinzufuegen."; answers.push_back(answer); answer = "Ware/n entfernen."; answers.push_back(answer); answer = "Ware/n anzeigen."; answers.push_back(answer); answer = "Beenden."; answers.push_back(answer); selectedOption = askMenuOption(question, answers); switch(selectedOption) { case 1: { InputManager::WareAndAmount x = displayAddWareDialog(); editor.addWaren(x.ware, x.amount); break; } case 2: { InputManager::WareAndAmount x = displayRemoveWareDialog(); editor.removeWaren(x.ware.getName(), x.amount); break; } case 3: { std::vector waren = this->editor.getWarenStorage().getAllWaren(); displayWaren(waren); break; } case 4: { return; break; } } displayMainMenu(); } // if no more initialization needed --> refactor to setStorageEditor void InputManager::displayWaren(const std::vector waren) { std::cout << "Liste der sich im Lager befindlichen Waren:" << std::endl; std::vector sortedWarenList = sortAndGroupWaren(waren); if(sortedWarenList.empty()){ std::cout << "Das Warenlager ist leer." << std::endl; std::cout << "---------------------" << std::endl; return; } for(WareAndAmount wareAndAmount: sortedWarenList){ std::cout << "Warenname:" << std::endl; std::cout << wareAndAmount.ware.getName() << std::endl; std::cout << "Needs Cooling:" << std::endl; if(wareAndAmount.ware.getHasCooling()) { std::cout << "true" << std::endl; } else { std::cout << "false" << std::endl; } std::cout << "Width:" << std::endl; std::cout << wareAndAmount.ware.getSize().width << std::endl; std::cout << "Height:" << std::endl; std::cout << wareAndAmount.ware.getSize().height << std::endl; std::cout << "Amount:" << std::endl; std::cout << wareAndAmount.amount << std::endl; std::cout << "---------------------" << std::endl; } } //refactor std::vector InputManager::sortAndGroupWaren(const std::vector& waren) { std::vector sortedWaren; WareAndAmount wareAndAmount; wareAndAmount.amount = 0; for(Ware ware : waren) { if(ware.getType() == Ware::WareType::DefaultWare) { if(wareAndAmount.amount == 0) { wareAndAmount.ware = ware; wareAndAmount.amount++; } else if(wareAndAmount.ware.getName() == ware.getName()) { wareAndAmount.amount++; } else { sortedWaren.push_back(wareAndAmount); wareAndAmount.amount = 0; } } } if(!waren.empty() && wareAndAmount.amount > 0){ sortedWaren.push_back(wareAndAmount); wareAndAmount.amount = 0; } for(Ware ware : waren) { if(ware.getType() == Ware::WareType::CoolingWare) { if(wareAndAmount.amount == 0) { wareAndAmount.ware = ware; wareAndAmount.amount++; } else if(wareAndAmount.ware.getName() == ware.getName()) { wareAndAmount.amount++; } else { sortedWaren.push_back(wareAndAmount); wareAndAmount.amount = 0; } } } if(!waren.empty() && wareAndAmount.amount > 0){ sortedWaren.push_back(wareAndAmount); wareAndAmount.amount = 0; } for(Ware ware : waren) { if(ware.getType() == Ware::WareType::LargeWare) { if(wareAndAmount.amount == 0) { wareAndAmount.ware = ware; wareAndAmount.amount++; } else if(wareAndAmount.ware.getName() == ware.getName()) { wareAndAmount.amount++; } else { sortedWaren.push_back(wareAndAmount); wareAndAmount.amount = 0; } } } if(!waren.empty() && wareAndAmount.amount > 0){ sortedWaren.push_back(wareAndAmount); wareAndAmount.amount = 0; } return sortedWaren; } bool InputManager::getNeedsCooling() { std::string input; bool hasCooling; std::cout << "Does Ware need Cooling? (y/n)" << std::endl; try { std::cin >> input; if(input == "y") { hasCooling = true; } else if(input == "n") { hasCooling = false; } else { std::cout << "That didn't work. Please try again :)" << std::endl; getNeedsCooling(); } } catch(const std::exception&){ std::cout << "That didn't work. Please try again :)" << std::endl; getNeedsCooling(); } return hasCooling; } //std::istream template T InputManager::getInput(std::string message) const { std::cout << message << std::endl; T val; try { std::string input; std::cin >> input; std::istringstream ss(input); ss >> val; } catch(const std::exception&) { std::cout << "That didn't work. Please try again :)" << std::endl; getInput(message); } return val; }