diff --git a/inventory_management_system.cpp b/inventory_management_system.cpp index a7d4e347d98b6ad225ac33ad8d069e578004b098..caacdfff93a8cb404909d32fd0d0edca9e81794e 100644 --- a/inventory_management_system.cpp +++ b/inventory_management_system.cpp @@ -42,12 +42,133 @@ public: class Inventory { private: + sqlite3* db; // SQLite database handle vector<Toy> toys; public: - // Add a new toy to the inventory + Inventory() { + int rc = sqlite3_open("inventory.db", &db); + if (rc) { + cerr << "Can't open database: " << sqlite3_errmsg(db) << endl; + sqlite3_close(db); + exit(1); + } + // Create the "toys" table if it doesn't exist + const char* createTableSQL = + "CREATE TABLE IF NOT EXISTS toys (" + "id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT NOT NULL," + "material TEXT NOT NULL," + "description TEXT," + "manufacturingPrice REAL NOT NULL," + "quantityInStock INTEGER NOT NULL" + ");"; + + rc = sqlite3_exec(db, createTableSQL, 0, 0, 0); + if (rc) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + sqlite3_close(db); + exit(1); + } + } + + ~Inventory() { + sqlite3_close(db); + } + // hkaur28 methods added + // Add a new toy to the inventory and the database void addToy(const Toy& toy) { toys.push_back(toy); + + const char* insertSQL = "INSERT INTO toys (name, material, description, manufacturingPrice, quantityInStock) " + "VALUES (?, ?, ?, ?, ?);"; + + sqlite3_stmt* stmt; + int rc = sqlite3_prepare_v2(db, insertSQL, -1, &stmt, 0); + if (rc != SQLITE_OK) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + return; + } + + sqlite3_bind_text(stmt, 1, toy.getName().c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 2, toy.getMaterial().c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 3, toy.getDescription().c_str(), -1, SQLITE_STATIC); + sqlite3_bind_double(stmt, 4, toy.getManufacturingPrice()); + sqlite3_bind_int(stmt, 5, toy.getQuantityInStock()); + + rc = sqlite3_step(stmt); + if (rc != SQLITE_DONE) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + } + + sqlite3_finalize(stmt); + } + + // Delete a toy from the inventory and the database by its name + void deleteToy(const string& name) { + auto it = toys.begin(); + while (it != toys.end()) { + if (it->getName() == name) { + it = toys.erase(it); + } else { + ++it; + } + } + + const char* deleteSQL = "DELETE FROM toys WHERE name = ?;"; + + sqlite3_stmt* stmt; + int rc = sqlite3_prepare_v2(db, deleteSQL, -1, &stmt, 0); + if (rc != SQLITE_OK) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + return; + } + + sqlite3_bind_text(stmt, 1, name.c_str(), -1, SQLITE_STATIC); + + rc = sqlite3_step(stmt); + if (rc != SQLITE_DONE) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + } + + sqlite3_finalize(stmt); + } + + // Update a toy's information in the inventory and the database by its name + void updateToy(const string& name, const Toy& newToy) { + auto it = toys.begin(); + while (it != toys.end()) { + if (it->getName() == name) { + *it = newToy; + break; + } else { + ++it; + } + } + + const char* updateSQL = "UPDATE toys SET name = ?, material = ?, description = ?, " + "manufacturingPrice = ?, quantityInStock = ? WHERE name = ?;"; + + sqlite3_stmt* stmt; + int rc = sqlite3_prepare_v2(db, updateSQL, -1, &stmt, 0); + if (rc != SQLITE_OK) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + return; + } + + sqlite3_bind_text(stmt, 1, newToy.getName().c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 2, newToy.getMaterial().c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(stmt, 3, newToy.getDescription().c_str(), -1, SQLITE_STATIC); + sqlite3_bind_double(stmt, 4, newToy.getManufacturingPrice()); + sqlite3_bind_int(stmt, 5, newToy.getQuantityInStock()); + sqlite3_bind_text(stmt, 6, name.c_str(), -1, SQLITE_STATIC); + + rc = sqlite3_step(stmt); + if (rc != SQLITE_DONE) { + cerr << "SQL error: " << sqlite3_errmsg(db) << endl; + } + + sqlite3_finalize(stmt); } // Display all toys in the inventory @@ -69,7 +190,20 @@ int main() { inventory.addToy(toy1); inventory.addToy(toy2); - // Display the inventory + // Display the initial inventory + inventory.displayInventory(); + + // Update a toy + Toy updatedToy("Toy1", "Metal", "An upgraded version of Toy1", 7.99, 60); + inventory.updateToy("Toy1", updatedToy); + + // Display the updated inventory + inventory.displayInventory(); + + // Delete a toy + inventory.deleteToy("Toy2"); + + // Display the updated inventory after deletion inventory.displayInventory(); return 0;