🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

Hey guys, I am back with another cool project. This is going to be a simple inventory management system where the program allows the user to manage the inventory of a shop or store by adding, updating, and deleting products. Also, features like searching for different products by name or number, calculating the inventory's total product value, and saving and loading inventory data from files will be available. Let's go build this project.
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
// Product class
class Product {
public:
int id;
string name;
int quantity;
double price;
Product(int id, const string& name, int quantity, double price)
: id(id), name(name), quantity(quantity), price(price) {}
};
// Inventory Management Class
class Inventory {
private:
vector<Product> products;
int nextID;
public:
Inventory() : nextID(1) {}
// Add a new product
void addProduct() {
string name;
int quantity;
double price;
cout << "Enter product name: ";
cin.ignore();
getline(cin, name);
cout << "Enter quantity: ";
cin >> quantity;
cout << "Enter price: ";
cin >> price;
products.emplace_back(nextID++, name, quantity, price);
cout << "Product added successfully!\n";
}
// Update a product
void updateProduct() {
int id;
cout << "Enter product ID to update: ";
cin >> id;
for (auto& product : products) {
if (product.id == id) {
cout << "Enter new name (current: " << product.name << "): ";
cin.ignore();
getline(cin, product.name);
cout << "Enter new quantity (current: " << product.quantity << "): ";
cin >> product.quantity;
cout << "Enter new price (current: " << product.price << "): ";
cin >> product.price;
cout << "Product updated successfully!\n";
return;
}
}
cout << "Product with ID " << id << " not found.\n";
}
// Delete a product
void deleteProduct() {
int id;
cout << "Enter product ID to delete: ";
cin >> id;
for (auto it = products.begin(); it != products.end(); ++it) {
if (it->id == id) {
products.erase(it);
cout << "Product deleted successfully!\n";
return;
}
}
cout << "Product with ID " << id << " not found.\n";
}
// Search for a product by name
void searchProduct() const {
string name;
cout << "Enter product name to search: ";
cin.ignore();
getline(cin, name);
for (const auto& product : products) {
if (product.name == name) {
cout << "Product found:\n";
cout << "ID: " << product.id << ", Name: " << product.name
<< ", Quantity: " << product.quantity << ", Price: " << product.price << "\n";
return;
}
}
cout << "Product with name \"" << name << "\" not found.\n";
}
// Calculate total inventory value
void calculateTotalValue() const {
double totalValue = 0;
for (const auto& product : products) {
totalValue += product.quantity * product.price;
}
cout << "Total inventory value: " << totalValue << "\n";
}
// Save inventory to a file
void saveToFile() const {
ofstream outFile("inventory.txt");
if (!outFile) {
cout << "Error saving inventory data.\n";
return;
}
for (const auto& product : products) {
outFile << product.id << "\n"
<< product.name << "\n"
<< product.quantity << "\n"
<< product.price << "\n";
}
outFile.close();
cout << "Inventory saved successfully!\n";
}
};
int main() {
Inventory inventory;
inventory.loadFromFile();
int choice;
do {
cout << "\nSimple Inventory Management\n";
cout << "1. Add Product\n";
cout << "2. Update Product\n";
cout << "3. Delete Product\n";
cout << "4. Search Product\n";
cout << "5. Calculate Total Value\n";
cout << "6. Display All Products\n";
cout << "7. Save and Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
inventory.addProduct();
break;
case 2:
inventory.updateProduct();
break;
case 3:
inventory.deleteProduct();
break;
case 4:
inventory.searchProduct();
break;
case 5:
inventory.calculateTotalValue();
break;
case 6:
inventory.displayProducts();
break;
case 7:
inventory.saveToFile();
cout << "Exiting program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 7);
return 0;
}
Inventory systems are crucial for retail stores and supermarkets. They allow managers to track stock levels, details of products, and place reorders on time to prevent stock-out issues.
Large distribution centers and warehouses utilize similar mechanisms for tracking hundreds of products efficiently.
Virtual stores depend on inventory systems to update products in real-time, handle large orders, and manage pricing effectively.
Comments
Post a Comment