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

Image
  🌿  Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms" 🧭  1. Introduction: Farming in the Age of Climate Change In a world where clean water is more precious than gold, efficient  rainwater harvesting and plant care systems  are no longer optional — they’re essential. Smart farming doesn’t mean just automating irrigation. It means combining  robotic drones, environmental sensors, and intelligent scheduling  to build a garden that practically takes care of itself. In this guide, we build a  fully functional Garden Manager System  using  C++  that: Captures and conserves rainwater Uses  robotic drones and sensors  to monitor crop health Integrates a  real-time UI  with progress bars and alerts Includes  timers  for scheduling plant growth and drone tasks Plays  interactive sounds  based on crop state and events Whether you'r...

C++ Projects: Database Management System

C++ Projects: Database Management System

A database management system in C++ involves creating various functionalities to manage, store, update, retrieve, and delete data from a database. File handling is used to simplify different functions, while the C++ standard library aids in processing.

Creating a Basic DBMS in C++

Definition of Data Structure

Storage of records can be implemented using a class or a similar structure to represent database entries.

File Handling

To save data persistently, file I/O operations are utilized.

CRUD Operations

CRUD refers to:

  • Create: Adding new records to the database.
  • Read: Fetching records based on criteria.
  • Update: Modifying existing data.
  • Delete: Removing records.

User Interface

Providing an interface for interacting with the database management system.

The Code to Build the Desired Project

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

// Definition of a structure to store records
struct Record {
    int id;
    string name;
    string email;

    void display() const {
        cout << setw(5) << id << setw(20) << name << setw(30) << email << endl;
    }
};

// Function prototypes
void addRecord();
void viewRecords();
void updateRecord();
void deleteRecord();

// Helper functions
Record parseRecord(const string& line);
void saveRecords(const vector<Record>& records);
vector<Record> loadRecords();

int main() {
    int choice;
    do {
        cout << "\nDatabase Management System\n";
        cout << "1. Add Record\n";
        cout << "2. View Records\n";
        cout << "3. Update Record\n";
        cout << "4. Delete Record\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
            case 1: addRecord(); break;
            case 2: viewRecords(); break;
            case 3: updateRecord(); break;
            case 4: deleteRecord(); break;
            case 5: cout << "Exiting...\n"; break;
            default: cout << "Invalid choice. Try again.\n";
        }
    } while (choice != 5);

    return 0;
}

void addRecord() {
    Record newRecord;
    cout << "Enter ID: ";
    cin >> newRecord.id;
    cin.ignore(); // Clear input buffer
    cout << "Enter Name: ";
    getline(cin, newRecord.name);
    cout << "Enter Email: ";
    getline(cin, newRecord.email);

    ofstream file("database.txt", ios::app);
    if (file) {
        file << newRecord.id << "," << newRecord.name << "," << newRecord.email << "\n";
        file.close();
        cout << "Record added successfully.\n";
    } else {
        cerr << "Error opening file for writing.\n";
    }
}

void viewRecords() {
    vector<Record> records = loadRecords();
    for (const auto& record : records) {
        record.display();
    }
}

void deleteRecord() {
    vector<Record> records = loadRecords();
    int id;
    cout << "Enter the ID of the record to delete: ";
    cin >> id;

    auto it = remove_if(records.begin(), records.end(), [id](const Record& record) {
        return record.id == id;
    });

    if (it != records.end()) {
        records.erase(it, records.end());
        saveRecords(records);
        cout << "Record deleted successfully.\n";
    } else {
        cout << "Record not found.\n";
    }
}

Record parseRecord(const string& line) {
    Record record;
    size_t pos1 = line.find(',');
    size_t pos2 = line.rfind(',');

    record.id = stoi(line.substr(0, pos1));
    record.name = line.substr(pos1 + 1, pos2 - pos1 - 1);
    record.email = line.substr(pos2 + 1);

    return record;
}

vector<Record> loadRecords() {
    vector<Record> records;
    ifstream file("database.txt");

    if (file) {
        string line;
        while (getline(file, line)) {
            records.push_back(parseRecord(line));
        }
        file.close();
    } else {
        cerr << "Error opening file for reading.\n";
    }
    return records;
}

void saveRecords(const vector<Record>& records) {
    ofstream file("database.txt", ios::trunc);
    if (file) {
        for (const auto& record : records) {
            file << record.id << "," << record.name << "," << record.email << "\n";
        }
        file.close();
    } else {
        cerr << "Error opening file for writing.\n";
    }
}

Explanation

Definition of Structure

Each record's data is stored using a structure with an id, name, and email.

File Handling

The file database.txt stores all records. ofstream is used to append or truncate records.

Interface

The system provides different options for CRUD operations.

Functions

  • addRecord: Appends a new record to the file.
  • viewRecords: Reads and displays all records.
  • updateRecord: Modifies a specific record.
  • deleteRecord: Removes a specific record.

Helper Functions

  • parseRecord: Converts file lines into records.
  • loadRecords: Reads records into a vector.
  • saveRecords: Writes a vector of records back to the file.

Real-World Examples

SQLite

  • Relevance: SQLite stores data in flat files, similar to this DBMS but with advanced features like indexing and transactions.
  • Code Connection: Extend saveRecords() with transaction support using temporary files for atomic writing.

Firebase Realtime Database in Embedded Systems

  • Relevance: Used in IoT devices for local storage.
  • Code Connection: Add a timestamp to the Record struct for time-series data logging.

Legacy dBase Systems

  • Relevance: Early databases like dBase used .dbf flat files, similar to this approach but without binary optimization.

Case Study: SQLite's ACID Compliance

  • Challenge: File corruption during power failures.
  • Solution: SQLite uses write-ahead logging to ensure atomicity.

This C++ DBMS is a stepping stone to more advanced database solutions.

Comments

Popular posts from this blog

C++ Projects: Basic Traffic Management System

C++ Projects: Book Shop Management System

C++ Projects: Password Manager