Trading System in C++

              Trading system deals with finance management. It gets the input from the console and display the output in the console.

It has the following components.

  • A structure ‘T_order’ with 3 elements. It has t_type,t_quantity and t_price.
  • A class ‘TradingSystemEg  creates a vector and a variable ‘t_balance’. ‘placeIt()’, ‘showIt()’ are the two functions to buy the units and displayed.
  • The constructor TradingSystemEg() is used to initialise the variable values.
  • ‘placeIt()’ – it gets the three values as function input. This function checks the balance is greater than quantity ,then the type is “BUYIT”, the balance is reduced as quantity and multiplied with the price value.
  • ‘showIt()’ – it displays the balance value and unit value.
  • ‘main()’ – it creates the object for TradingSystemEg. The object calls the function.

Code:

#include <iostream>

#include <vector>

#include <string>

struct T_Order {

    std::string t_type;

    int t_quantity;

    double t_price;

};

class TradingSystemEg {

    std::vector<T_Order> o1;

    double t_balance;

public:

    TradingSystemEg(double t_initialBalance) : t_balance(t_initialBalance) {}

    void placeIt(std::string type1, int t_qty, double t_price) {

        if (type1 == "BUYIT" && t_balance < t_qty * t_price) {

            std::cout << "Sorry. Balace is not enough!\n";

            return;

        }

        o1.push_back({type1, t_qty, t_price});

        if (type1 == "BUYIT") t_balance -= t_qty * t_price;

        else t_balance += t_qty * t_price;

    }

    void showIt() {

        std::cout << "The Balance is: " << t_balance << "\n";

        std::cout << "The Orders are executed: " << o1.size() << "\n";

    }

};

int main() {

    TradingSystemEg ts1(1000000);

    ts1.placeIt("BUYIT", 100, 50);

    ts1.showIt();

    return 0;

}

Output:

The Balance is: 995600

The Orders are executed: 2

That’s all the C++ program to display the trading system for buying units was done. Hope, this code is useful to you. Keep Coding!!!

Comments

Popular posts from this blog

How to create a XML DTD for displaying student details

Java NIO examples to illustrate channels and buffers.

How to write your first XML program?