Untitled

 avatar
unknown
plain_text
2 years ago
2.3 kB
6
Indexable
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

struct Order
{
    std::string datetime;
    std::string cryptocurrency;
    std::string action; // Either chooses bid or ask depending on row
    double high;
    double low;
};

class OrderBook
{
private:
    std::vector<Order> orders;

public:
    void addOrder(const std::string &datetime, const std::string &crypto, const std::string &action, double high, double low)
    {
        orders.push_back({datetime, crypto, action, high, low});
    }

    void displayTradeInfo(int row) const
    {
        if (row >= 0 && row < orders.size())
        {
            const Order &trade = orders[row];
            std::cout << "Row " << row << ":\n"
                      << "Date and Time: " << trade.datetime << "\n"
                      << "Cryptocurrency: " << trade.cryptocurrency << "\n"
                      << "Action: " << trade.action << "\n"
                      << "High: " << trade.high << "\n"
                      << "Low: " << trade.low << "\n";
        }
        else
        {
            std::cout << "Invalid row position.\n";
        }
    }

    void getOrdersFromCSV(const std::string &filename)
    {
        std::ifstream file(filename);
        if (!file.is_open())
        {
            std::cout << "Failed to open file: " << filename << "\n";
            return;
        }

        std::string line;
        while (std::getline(file, line))
        {
            std::istringstream iss(line);
            std::vector<std::string> tokens;

            // Using Commas to split our csv
            std::string token;
            while (std::getline(iss, token, ','))
            {
                tokens.push_back(token);
            }

            // Making sure we have enough tokens
            if (tokens.size() == 5)
            {
                addOrder(tokens[0], tokens[1], tokens[2], std::stod(tokens[3]), std::stod(tokens[4]));
            }
            else
            {
                std::cout << "Error parsing line: " << line << "\n";
            }
        }

        file.close();
    }
};

int main()
{
    OrderBook orderBook;
    orderBook.getOrdersFromCSV("20231124.csv");
    orderBook.displayTradeInfo(5);

    return 0;
}
Editor is loading...
Leave a Comment