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

#include "InputManager.h"
#include <exception>
#include <sstream>
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<std::string> answers) {
this->displayDialog(question, answers);
int input;
std::cin >> input;
return input;
}
void InputManager::displayDialog(std::string question, std::vector<std::string> 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<std::string>("Enter Ware Name:");
hasCooling = getNeedsCooling();
width = getInput<double>("Enter Ware width:");
height = getInput<double>("Enter Ware height:");
amount = getInput<int>("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<std::string>("Enter Name of the Ware you want to remove:");
amount = getInput<int>("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<std::string> 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<Ware> 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<Ware> waren) {
std::cout << "Liste der sich im Lager befindlichen Waren:" << std::endl;
std::vector<InputManager::WareAndAmount> 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::WareAndAmount> InputManager::sortAndGroupWaren(const std::vector<Ware>& waren) {
std::vector<WareAndAmount> 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 <typename T>
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<T>(message);
}
return val;
}