मैं C++ में एकाधिक चीज़ों की विशेषता कैसे बदल सकता हूँ?


मैं वर्तमान में C++ में एक सरल प्रबंधन प्रणाली पर काम कर रहा हूं जो श्रमिकों, वस्तुओं और भंडारण को संभाल सकती है। मेरे पास वर्तमान में एक चेंजआइटम() विधि है जो किसी आइटम की विशेषता को बदल देती है (आइटम बन्स, कागज के टुकड़े इत्यादि जैसी कुछ भी हैं)। विशेषताओं में से एक स्थान है. स्थान एक भंडारण (बॉक्स, फ्रिज, आदि) का नाम है।

जब मैं यह परिवर्तन करता हूं (आइटम को स्टोरेज1 से स्टोरेज2 में ले जाता हूं), तो स्टोरेज1 में शेष स्थान स्थानांतरित मात्रा से बढ़ जाना चाहिए। और स्टोरेज2 की शेष जगह को स्थानांतरित की गई राशि से कम किया जाना चाहिए।

संकट: हालाँकि, जब ऐसा होता है, तो विशेषताओं में परिवर्तन नहीं होता है और स्टोरेज1 को शेष स्थान नहीं मिलता है।

स्पष्टता नोट: जब आप कोई आइटम बनाते हैं, तो यह एक स्टोरेज या एकाधिक स्टोरेज में संग्रहीत हो जाता है। फिर आप इसे आवश्यकतानुसार किसी अन्य स्टोरेज में ले जा सकते हैं। जब ऐसा होता है, तो स्थानांतरित की गई राशि के अनुसार शेषस्पेस विशेषता बदलनी चाहिए।

आइटम प्रबंधित करें.hpp:

#ifndef MANAGEITEMS_HPP
#define MANAGEITEMS_HPP

struct item {
    string name;
    string arrivalDate;
    string expirationDate;
    string location;
    double price;
    double quantity;
};

vector<item> itemData;

// Function to move items to storage
bool moveItemToStorage(item& newItem, const string& storageName, bool updateSpace = true) {
    bool storageFound = false;
    double remainingQuantity = newItem.quantity;

    // Check if the specified storage exists
    for (auto& storageItem : storageData) {
        if (storageItem.name == storageName) {
            storageFound = true;

            // Check if the specified storage has enough space
            if (newItem.quantity <= storageItem.remainingSpace) {
                if (updateSpace) {
                    storageItem.remainingSpace -= newItem.quantity; // Update remaining space for specified storage
                    remainingQuantity = 0;
                }    
            } else {
                remainingQuantity = newItem.quantity - storageItem.remainingSpace;
                
                if (updateSpace) {
                    storageItem.remainingSpace = 0; // Update remaining space for specified storage
                }    
            }

            break;
        }
    }

    // If specified storage doesn't exist or doesn't have enough space, try alternative storage
    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; // Update remaining space for alternative storage
                    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; // Update remaining space for alternative storage
                    remainingQuantity -= spaceAvailable;
                }
            }
        }
    }

    // If there are remaining items not stored, print a message
    if (remainingQuantity > 0) {
        cout << remainingQuantity << " " << newItem.name << " remain outside of storage" << endl;
        return false;
    }

    return true;
}

// Display all item names
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;

    //Check if the item name exists
    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;
    
    // Check if the storage exists
    bool storageFound = false;
    
    for (auto& storageItem : storageData) {
        //If the storage name input is found
        if (storageItem.name == storageName) {
            storageFound = true;
            cout << "Enter the quantity of items received: ";
            cin >> newItem.quantity;

            // Move the item to the specified storage
            moveItemToStorage(newItem, storageName);
            break;
        }
    }
    
    //Storage name input is not found
    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);
}

// Function to modify attributes of an item
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;

    // Find the item with the given name
    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;
    }
    
    //Choose what they wish to change
    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) {
            // Change arrival date
            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;
            
            // Change expiration date
            cout << "Old Expiration Date: " << it->expirationDate << endl;
            cout << "Enter new Expiration Date (Month Day, Year): ";
            getline(cin, it->expirationDate);
        } else if (choice == 2) {
            // Change expiration date
            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) {
            // Change location of item
            cout << "Old Location: " << it->location << endl;
            string newLocation;
            cout << "Enter new Location: ";
            cin >> newLocation;

            // Move the item to the new location
            moveItemToStorage(*it, newLocation);
        } else if (choice == 4) {
            // Change the price
            cout << "Old Price: $" << it->price << endl;
            cout << "Enter new Price: ";
            cin >> it->price;
        } else if (choice == 5) {
            // Change the quantity of the item
            cout << "Old Quantity: " << it->quantity << endl;
            cout << "Enter new Quantity: ";
            double oldQuantity = it->quantity;
            cin >> it->quantity;

            // Update remaining space for the storage
            double quantityDifference = it->quantity - oldQuantity;
            
            if (quantityDifference != 0) {
                // If quantity is increased, reduce remaining space; if decreased, increase remaining space
                for (auto& storage : storageData) {
                    if (storage.name == it->location) {
                        if (quantityDifference > 0) {
                            // Decrease remaining space
                            storage.remainingSpace -= quantityDifference;
                        } else {
                            // Increase remaining space
                            storage.remainingSpace += (-quantityDifference);
                        }
                        
                        break;
                    }
                }
            }
        } else if (choice == 6) {
            // Stop editing the attributes
            break;
        } else {
            cout << "Invalid option" << endl << endl;
            return;
        }
    }    

    cout << "Attribute(s) changed successfully" << endl << endl;
}

#endif

मैनेजस्टोरेज.hpp:

सी++
<pre>#ifndef MANAGESTORAGE_HPP
#define MANAGESTORAGE_HPP

struct storage {
    string name;
    double temperature;
    int maxCapacity;
    int remainingSpace;
};

vector<storage> storageData;

void displayStorageNames() {
    // Display all storage names
    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;
    
    //Check if the storage name already exists
    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; // Initialize remaining space
    storageData.push_back(newStorage);
    cout << newStorage.name << " successfully added to storages array" << endl << endl;
}
#endif

मैंने क्या प्रयास किया है:

मैंने moveItemToStorage() विधि के साथ खिलवाड़ करने का प्रयास किया, लेकिन मैं अभी भी इसे काम में नहीं ला सका। शायद यह बहुत आसान है और मैं इसे आवश्यकता से अधिक कठिन बना रहा हूँ।

समाधान 1

अपने ‘चेंजआइटम’ फ़ंक्शन में किसी आइटम का स्थान एक से दूसरे में बदलते समय आप जिस तरह शेष स्थान को संभालते हैं वह गलत है।

जब आप आइटम का स्थान बदलते हैं, तो आप ‘moveItemToStorage’ फ़ंक्शन को कॉल करते हैं, लेकिन आप यह जांच नहीं कर रहे हैं कि स्थानांतरण किया गया था या नहीं। यदि स्थानांतरण की गणना नहीं की जाती है, तो पुराने संग्रहण के लिए शेष स्थान अद्यतन नहीं किया जाएगा।

आपको एक बूलियन लौटाने के लिए ‘moveItemToStorage’ फ़ंक्शन को संशोधित करना चाहिए, जिससे यह पता चले कि स्थानांतरण किया गया था या नहीं। अपने ‘चेंजआइटम’ फ़ंक्शन में, पुराने स्टोरेज के लिए शेष स्थान को अपडेट करने से पहले रिटर्न वैल्यू की जांच करें, इस भाग के साथ अपना कोड अपडेट करें –

सी++
} else if (choice == 3) {
    cout << "Old Location: " << it->location << endl;
    string newLocation;
    cout << "Enter new Location: ";
    cin >> newLocation;

    //Move your item to the new location...
    bool moveSuccess = moveItemToStorage(*it, newLocation);

    if (moveSuccess) {
        //If moved, update remaining space for the old storage...
        for (auto& storage : storageData) {
            if (storage.name == it->location) {
                storage.remainingSpace += it->quantity; 
                //Increase the remaining space...
                break;
            }
        }
    } else {
        cout << "Move to new location failed. Remaining space not updated." << endl;
    }
}

コメント

タイトルとURLをコピーしました