I am currently working on a simple management system in C++ that can handle workers, items, and storage. I currently have a changeItem() method that changes an attribute of an item (items are anything like buns, pieces of paper, etc.). One of the attributes is location. Location is the name of a storage (box, fridge, etc.).
When I make this change (move the item from storage1 to storage2), the remaining space in storage1 should increase by the amount moved. And storage2's remaining space should be decreased by the amount moved.
Problem: However, when this happens, the changes in the attributes do not occur and storage1 does not gain remaining space.
Clarity note: When You create an item, it gets stored into a storage or multiple storages. Then you can move it to another storage as needed. When this happens, the remainingSpace attribute should change by the amount moved.
ManageItems.hpp:
#ifndef MANAGEITEMS_HPP
#define MANAGEITEMS_HPP
struct item {
string name;
string arrivalDate;
string expirationDate;
string location;
double price;
double quantity;
};
vector<item> itemData;
bool moveItemToStorage(item& newItem, const string& storageName, bool updateSpace = true) {
bool storageFound = false;
double remainingQuantity = newItem.quantity;
for (auto& storageItem : storageData) {
if (storageItem.name == storageName) {
storageFound = true;
if (newItem.quantity <= storageItem.remainingSpace) {
if (updateSpace) {
storageItem.remainingSpace -= newItem.quantity;
remainingQuantity = 0;
}
} else {
remainingQuantity = newItem.quantity - storageItem.remainingSpace;
if (updateSpace) {
storageItem.remainingSpace = 0;
}
}
break;
}
}
if ((!storageFound || remainingQuantity > 0) && updateSpace) {
for (auto& altStorage : storageData) {
if (altStorage.name != storageName && altStorage.remainingSpace > 0) {
double spaceAvailable = altStorage.remainingSpace;
if (remainingQuantity <= spaceAvailable) {
cout << "Alternatively, you can store the remaining quantity of " << newItem.name << " in " << altStorage.name << endl;
altStorage.remainingSpace -= remainingQuantity;
remainingQuantity = 0;
return true;
} else {
cout << "Alternatively, you can store the remaining quantity of " << newItem.name << " in " << altStorage.name << " (" << spaceAvailable << " available)" << endl;
altStorage.remainingSpace = 0;
remainingQuantity -= spaceAvailable;
}
}
}
}
if (remainingQuantity > 0) {
cout << remainingQuantity << " " << newItem.name << " remain outside of storage" << endl;
return false;
}
return true;
}
void displayItemNames() {
for (int i = 0; i < itemData.size(); i++) {
cout << itemData[i].name;
if (i != itemData.size() - 1) {
cout << ", ";
} else {
cout << endl << endl;
}
}
}
void createItem() {
item newItem;
cout << "Enter the name of the item: ";
cin >> newItem.name;
for (const auto& itemEntry : itemData) {
if (itemEntry.name == newItem.name) {
cout << "Error: Item name already exists" << endl;
return;
}
}
cout << "Enter the day of arrival (Month dd, yyyy): ";
cin.ignore();
getline(cin, newItem.arrivalDate);
if (!isDateValid(newItem.arrivalDate)) {
cout << "Error: Invalid arrival date format" << endl << endl;
return;
}
cout << "Enter the expiration date (Month dd, yyyy): ";
getline(cin, newItem.expirationDate);
if (!isDateValid(newItem.expirationDate)) {
cout << "Error: Invalid expiration date format" << endl << endl;
return;
}
if (!isDateBefore(newItem.arrivalDate, newItem.expirationDate)) {
cout << "Error: Arrival date must be before expiration date" << endl << endl;
return;
}
string storageName;
cout << "Enter the storage where the item shall be stored: ";
cin >> storageName;
newItem.location = storageName;
bool storageFound = false;
for (auto& storageItem : storageData) {
if (storageItem.name == storageName) {
storageFound = true;
cout << "Enter the quantity of items received: ";
cin >> newItem.quantity;
moveItemToStorage(newItem, storageName);
break;
}
}
if (!storageFound) {
cout << "Storage does not exist" << endl << endl;
newItem.location = "No Storage Location";
cout << "Enter the quantity of the item incoming: ";
cin >> newItem.quantity;
cout << endl << "Enter the price of the items: ";
cin >> newItem.price;
cout << endl;
}
cout << newItem.name << " successfully added to items array" << endl << endl;
itemData.push_back(newItem);
}
void changeItem() {
if (itemData.empty()) {
cout << "No items found" << endl << endl;
return;
}
cout << "Current items: ";
displayItemNames();
string itemName;
cout << "Enter the name of the item you want to edit: ";
cin >> itemName;
auto it = find_if(itemData.begin(), itemData.end(), [&](const item& i) {
return i.name == itemName;
});
if (it == itemData.end()) {
cout << "Item not found" << endl << endl;
return;
}
while (true) {
cout << "Choose the attribute to change:" << endl;
cout << "1. Arrival Date" << endl;
cout << "2. Expiration Date" << endl;
cout << "3. Location" << endl;
cout << "4. Price" << endl;
cout << "5. Quantity" << endl;
cout << "6. End editing" << endl;
int choice;
cin >> choice;
if (choice == 1) {
cout << "Old Arrival Date: " << it->arrivalDate << endl;
cout << "Enter new Arrival Date (Month Day, Year): ";
cin.ignore();
getline(cin, it->arrivalDate);
cout << endl << "You must also change the expiration date" << endl;
cout << "Old Expiration Date: " << it->expirationDate << endl;
cout << "Enter new Expiration Date (Month Day, Year): ";
getline(cin, it->expirationDate);
} else if (choice == 2) {
cout << "Old Expiration Date: " << it->expirationDate << endl;
cout << "Enter new Expiration Date (Month Day, Year): ";
cin.ignore();
getline(cin, it->expirationDate);
} else if (choice == 3) {
cout << "Old Location: " << it->location << endl;
string newLocation;
cout << "Enter new Location: ";
cin >> newLocation;
moveItemToStorage(*it, newLocation);
} else if (choice == 4) {
cout << "Old Price: $" << it->price << endl;
cout << "Enter new Price: ";
cin >> it->price;
} else if (choice == 5) {
cout << "Old Quantity: " << it->quantity << endl;
cout << "Enter new Quantity: ";
double oldQuantity = it->quantity;
cin >> it->quantity;
double quantityDifference = it->quantity - oldQuantity;
if (quantityDifference != 0) {
for (auto& storage : storageData) {
if (storage.name == it->location) {
if (quantityDifference > 0) {
storage.remainingSpace -= quantityDifference;
} else {
storage.remainingSpace += (-quantityDifference);
}
break;
}
}
}
} else if (choice == 6) {
break;
} else {
cout << "Invalid option" << endl << endl;
return;
}
}
cout << "Attribute(s) changed successfully" << endl << endl;
}
#endif
ManageStorage.hpp:
<pre>#ifndef MANAGESTORAGE_HPP
#define MANAGESTORAGE_HPP
struct storage {
string name;
double temperature;
int maxCapacity;
int remainingSpace;
};
vector<storage> storageData;
void displayStorageNames() {
for (int i = 0; i < storageData.size(); i++) {
cout << storageData[i].name;
if (i != storageData.size() - 1) {
cout << ", ";
} else {
cout << endl << endl;
}
}
}
void createStorage() {
storage newStorage;
cout << "Enter the name of the new storage container: ";
cin >> newStorage.name;
for (const auto& storageItem : storageData) {
if (storageItem.name == newStorage.name) {
cout << "Error: Storage name already exists" << endl << endl;
return;
}
}
cout << "Enter the average temperature of the storage in fahrenheit: ";
cin >> newStorage.temperature;
cout << "Enter the maximum amount items the storage can handle: ";
cin >> newStorage.maxCapacity;
newStorage.remainingSpace = newStorage.maxCapacity; storageData.push_back(newStorage);
cout << newStorage.name << " successfully added to storages array" << endl << endl;
}
#endif
What I have tried:
I tried messing with the moveItemToStorage() method, but I still haven't gotten it to work. Maybe it's super easy and I am making it harder than it needs to be.