The Muggy Weather Robotics Duo

Image
 The Muggy Weather Robotics Duo A C++ System That Thinks, Feels (Sensors!), and Acts Humidity is like the quiet character in the weather story that actually runs the show. On muggy days, everything feels heavier—breathing, drying laundry, running machines, even keeping a data center cool. For people, it’s about comfort and health; for machines, it’s about performance and reliability; for plants and buildings, it’s about moisture balance and mold risk. In robotics and automation, muggy weather isn’t just a nuisance—it’s a signal . It tells your systems when to ventilate, when to dehumidify, when to throttle physically demanding tasks, and when to take preventative maintenance actions. Today, we’ll build a two-program C++ system that “understands” muggy weather: Program A — sensor_hub.cpp A sensor-side program that generates (or ingests) a live stream of environmental data (temperature, relative humidity, pressure, CO₂, VOCs). Think of it as your robotic nose and skin , con...

C++ Multi-Module Smart Banking System with AI Fraud Detection

C++ Multi-Module Smart Banking System with AI Fraud Detection

This project integrates a Core Banking System with an AI Fraud Detection System, combining traditional banking operations with machine learning-based fraud detection. The system is CLI-based and utilizes MySQL for database management.


Phase 1: Core Banking System (CLI-Based)

The Core Banking System is responsible for:

  • User Authentication (Registration & Login)
  • Managing Transactions (Deposits & Withdrawals)
  • Balance Checking
  • Database Management (MySQL Integration)

Database Setup (MySQL)

Before running the banking system, set up the MySQL database and tables.

SQL Commands to Create the Database and Tables

CREATE DATABASE banking_system;
USE banking_system;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(100) NOT NULL,
    balance DOUBLE DEFAULT 0.0
);

CREATE TABLE transactions (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT, 
    amount DOUBLE,
    type ENUM ('deposit', 'withdrawal'),
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    fraud_flag BOOLEAN DEFAULT 0,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

C++ Core Banking System (CLI)

Dependencies

Before compiling the C++ program, install the MySQL connector:

sudo apt install libmysqlcppconn-dev

Core Banking System Code (banking_system.cpp)

#include <iostream>
#include <mysql/mysql.h>
#include <string>

using namespace std;

MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES* res;

const char* HOST = "localhost";
const char* USER = "root";
const char* PASSWORD = "your_mysql_password";
const char* DATABASE = "banking_system";

// Function to connect to MySQL
bool connectDB() {
    conn = mysql_init(0);
    conn = mysql_real_connect(conn, HOST, USER, PASSWORD, DATABASE, 3306, NULL, 0);
    return conn != NULL;
}

// Function to register a user
void registerUser() {
    string username, password;
    cout << "Enter username: ";
    cin >> username;
    cout << "Enter password: ";
    cin >> password;

    string query = "INSERT INTO users (username, password) VALUES ('" + username + "', '" + password + "')";
    if (mysql_query(conn, query.c_str()) == 0) {
        cout << "User registered successfully!\n";
    } else {
        cout << "Error: " << mysql_error(conn) << endl;
    }
}

// Function to log in a user
int loginUser() {
    string username, password;
    cout << "Enter username: ";
    cin >> username;
    cout << "Enter password: ";
    cin >> password;

    string query = "SELECT id FROM users WHERE username='" + username + "' AND password='" + password + "'";
    if (mysql_query(conn, query.c_str()) == 0) {
        res = mysql_store_result(conn);
        if (mysql_num_rows(res) > 0) {
            row = mysql_fetch_row(res);
            int user_id = atoi(row[0]);
            mysql_free_result(res);
            return user_id;
        }
    }
    cout << "Invalid credentials!\n";
    return -1;
}

// Function to deposit money
void deposit(int user_id) {
    double amount;
    cout << "Enter deposit amount: ";
    cin >> amount;

    string query = "UPDATE users SET balance = balance + " + to_string(amount) + " WHERE id = " + to_string(user_id);
    string transQuery = "INSERT INTO transactions(user_id, amount, type) VALUES (" + to_string(user_id) + ", " + to_string(amount) + ", 'deposit')";

    if (mysql_query(conn, query.c_str()) == 0 && mysql_query(conn, transQuery.c_str()) == 0) {
        cout << "Deposit successful!\n";
    } else {
        cout << "Error: " << mysql_error(conn) << endl;
    }
}

// Function to withdraw money
void withdraw(int user_id) {
    double amount;
    cout << "Enter withdrawal amount: ";
    cin >> amount;

    string balanceQuery = "SELECT balance FROM users WHERE id=" + to_string(user_id);
    mysql_query(conn, balanceQuery.c_str());
    res = mysql_store_result(conn);
    row = mysql_fetch_row(res);
    double balance = atof(row[0]);
    mysql_free_result(res);

    if (amount > balance) {
        cout << "Insufficient funds!\n";
        return;
    }

    string query = "UPDATE users SET balance = balance - " + to_string(amount) + " WHERE id = " + to_string(user_id);
    string transQuery = "INSERT INTO transactions(user_id, amount, type) VALUES (" + to_string(user_id) + ", " + to_string(amount) + ", 'withdrawal')";

    if (mysql_query(conn, query.c_str()) == 0 && mysql_query(conn, transQuery.c_str()) == 0) {
        cout << "Withdrawal successful!\n";
    } else {
        cout << "Error: " << mysql_error(conn) << endl;
    }
}

// Function to check balance
void checkBalance(int user_id) {
    string query = "SELECT balance FROM users WHERE id=" + to_string(user_id);
    mysql_query(conn, query.c_str());
    res = mysql_store_result(conn);
    row = mysql_fetch_row(res);
    cout << "Current balance: $" << row[0] << endl;
    mysql_free_result(res);
}

int main() {
    if (!connectDB()) {
        cout << "Database connection failed!\n";
        return 1;
    }

    int choice, user_id = -1;

    while (true) {
        cout << "\n1. Register\n2. Login\n3. Deposit\n4. Withdraw\n5. Check Balance\n6. Exit\nEnter choice: ";
        cin >> choice;

        if (choice == 1) {
            registerUser();
        } else if (choice == 2) {
            user_id = loginUser();
        } else if (choice == 3 && user_id != -1) {
            deposit(user_id);
        } else if (choice == 4 && user_id != -1) {
            withdraw(user_id);
        } else if (choice == 5 && user_id != -1) {
            checkBalance(user_id);
        } else if (choice == 6) {
            break;
        } else {
            cout << "Invalid choice or login required!\n";
        }
    }

    mysql_close(conn);
    return 0;
}

Phase 2: AI Fraud Detection Module

How Fraud Detection Works

  • Fetches transaction history from MySQL
  • Computes Z-score for each transaction
  • Flags transactions with Z-score > 3 as suspicious

Z-score Formula

Z=XμσZ = \frac{X - \mu}{\sigma}

Where:

  • X = Transaction Amount
  • μ (Mean) = Average Transaction Amount
  • σ (Standard Deviation) = Spread of Transactions

Database Modification

Add a fraud_flag column to mark suspicious transactions:

ALTER TABLE transactions ADD COLUMN fraud_flag BOOLEAN DEFAULT 0;

Phase 3: Integrating AI with Core Banking System

  • The banking system will trigger the fraud detection module after every transaction.
  • If fraud is detected, the user will be alerted.

Integration Code in banking_system.cpp

#include <cstdlib> // For system()

void runFraudDetection() {
    system("./fraud_detector"); // Run fraud detection system
}

// Call fraud detection after transactions
if (mysql_query(conn, transQuery.c_str()) == 0) {
    cout << "Transaction successful!\n";
    runFraudDetection(); // Run fraud detection after transaction
}

Final Compilation & Execution

g++ banking_system.cpp -o banking_system -lmysqlclient
g++ fraud_detector.cpp -o fraud_detector -lmysqlclient -lm
./banking_system

Final Features

User Registration & Login
Deposit & Withdraw Funds
Balance Check
AI-Based Fraud Detection
Automated Fraud Alerts

This modular banking system provides secure transactions with real-time fraud detection using AI-powered analytics.

Comments

Popular posts from this blog

C++ Projects: Basic Traffic Management System

C++ Projects: Book Shop Management System

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