Browse Source

fix memory copying issues

master
leo 3 years ago
parent
commit
56462a851b
  1. 39
      src/InputManager/InputManager.cpp
  2. 8
      src/InputManager/InputManager.h
  3. 4
      src/StorageEditor/StorageEditor.cpp
  4. 11
      src/StorageEditor/StorageEditor.h
  5. 2
      src/WarenStorage/WarenStorage.cpp
  6. 6
      src/WarenStorage/WarenStorage.h
  7. 2
      src/main.cpp

39
src/InputManager/InputManager.cpp

@ -14,15 +14,16 @@ void InputManager::setSelectedOption(MenuOption option) {
this->selectedOption = option;
}
InputManager::MenuOption InputManager::askMenuOption(std::string question, std::vector<std::string> answers) {
InputManager::MenuOption InputManager::askMenuOption(const std::string& question, const 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) {
void InputManager::displayDialog(const std::string& question, const std::vector<std::string>& answers) {
std::cout << question << std::endl;
std::cout << "-------------------------------" << std::endl;
int counter = 0;
@ -34,16 +35,11 @@ void InputManager::displayDialog(std::string question, std::vector<std::string>
}
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:");
std::string name = getInput<std::string>("Enter Ware Name:");
bool hasCooling = getNeedsCooling();
double width = getInput<double>("Enter Ware width:");
double height = getInput<double>("Enter Ware height:");
int amount = getInput<int>("Enter Ware amount:");
InputManager::WareAndAmount wareAndAmount;
wareAndAmount.ware = Ware(name, hasCooling, width, height);
@ -53,11 +49,8 @@ InputManager::WareAndAmount InputManager::displayAddWareDialog() {
}
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:");
std::string name = getInput<std::string>("Enter Name of the Ware you want to remove:");
int amount = getInput<int>("Enter amount you want to remove:");
InputManager::WareAndAmount wareAndAmount;
@ -100,7 +93,7 @@ void InputManager::displayMainMenu() {
break;
}
case 3: {
std::vector<Ware> waren = this->editor->getWarenStorage().getAllWaren();
std::vector<Ware> waren = this->editor->getWarenStorage()->getAllWaren();
displayWaren(waren);
break;
}
@ -108,13 +101,17 @@ void InputManager::displayMainMenu() {
return;
break;
}
default: {
return;
break;
}
}
displayMainMenu();
}
// if no more initialization needed --> refactor to setStorageEditor
void InputManager::displayWaren(const std::vector<Ware> waren) {
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);
@ -222,7 +219,7 @@ std::vector<InputManager::WareAndAmount> InputManager::sortAndGroupWaren(const s
bool InputManager::getNeedsCooling() {
std::string input;
bool hasCooling;
bool hasCooling = false;
std::cout << "Does Ware need Cooling? (y/n)" << std::endl;
try {
@ -247,7 +244,7 @@ bool InputManager::getNeedsCooling() {
//std::istream
template <typename T>
T InputManager::getInput(std::string message) const {
T InputManager::getInput(const std::string& message) const {
std::cout << message << std::endl;
T val;
try {

8
src/InputManager/InputManager.h

@ -12,9 +12,9 @@ class InputManager {
MenuOption selectedOption;
MenuOption askMenuOption(std::string question, std::vector<std::string> answers);
MenuOption askMenuOption(const std::string& question, const std::vector<std::string>& answers);
void displayDialog(std::string question, std::vector<std::string> answers);
void displayDialog(const std::string& question, const std::vector<std::string>& answers);
struct WareAndAmount {
Ware ware;
@ -27,7 +27,7 @@ class InputManager {
void displayMainMenu();
void displayWaren(const std::vector<Ware> waren);
void displayWaren(const std::vector<Ware>& waren);
WareAndAmount displayAddWareDialog();
@ -39,7 +39,7 @@ class InputManager {
void setSelectedOption(MenuOption option);
template <typename T>
T getInput(std::string message) const;
T getInput(const std::string& message) const;
bool getNeedsCooling();

4
src/StorageEditor/StorageEditor.cpp

@ -2,7 +2,7 @@
StorageEditor::StorageEditor() { }
StorageEditor::StorageEditor(const WarenStorage storage) {
StorageEditor::StorageEditor(const std::shared_ptr<WarenStorage> storage) {
this->storage = storage;
}
@ -22,7 +22,7 @@ void StorageEditor::addWaren(const Ware ware, int amount) {
}
void StorageEditor::removeWaren(std::string name, const int amount) {
void StorageEditor::removeWaren(const std::string& name, const int amount) {
for(auto i = 0; i < amount; i++) {
storage.removeWare(name);
}

11
src/StorageEditor/StorageEditor.h

@ -1,18 +1,19 @@
#include "../pch.h"
#include <memory>
#include "../WarenStorage/WarenStorage.h"
class StorageEditor {
private:
WarenStorage storage;
std::shared_ptr<WarenStorage> storage;
public:
StorageEditor();
StorageEditor(WarenStorage storage);
StorageEditor(std::shared_ptr<WarenStorage> storage);
WarenStorage getWarenStorage() const;
std::shared_ptr<WarenStorage> getWarenStorage() const;
void setWarenStorage(const WarenStorage storage);
@ -20,7 +21,7 @@ class StorageEditor {
void addWaren(const Ware ware, int amount);
void removeWare(std::string name);
void removeWare(const std::string& name);
void removeWaren(std::string name, int amount);
void removeWaren(const std::string& name, int amount);
};

2
src/WarenStorage/WarenStorage.cpp

@ -63,7 +63,7 @@ void WarenStorage::addWareAndGenerateId(Ware ware) {
}
}
void WarenStorage::removeWare(const std::string name) {
void WarenStorage::removeWare(const std::string& name) {
std::vector<Ware> allWaren = getAllWaren();
for(size_t i = 0; i < allWaren.size(); i++) {
if(allWaren[i].getName() == name) {

6
src/WarenStorage/WarenStorage.h

@ -39,9 +39,5 @@ class WarenStorage {
void addWareAndGenerateId(Ware ware);
void removeWare(const std::string name);
//const frage:
//
//wie kann ich die get...Waren() getter wieder als const definieren, ohne dass ich damit StorageEditor die möglichkeit nehme sie zu Editieren.
void removeWare(const std::string& name);
};

2
src/main.cpp

@ -6,7 +6,7 @@ int main () {
std::cout << "Kaufland:" << std::endl;
StorageEditor editor;
std::shared_ptr<StorageEditor> editor;
InputManager inputManager(editor);
inputManager.displayMainMenu();

Loading…
Cancel
Save