Add created and set up the CSVReader.h file
This commit is contained in:
3548
CM2005 Object Oriented Programming/Topic 3/3.4.2/20200317.csv
Normal file
3548
CM2005 Object Oriented Programming/Topic 3/3.4.2/20200317.csv
Normal file
File diff suppressed because it is too large
Load Diff
15
CM2005 Object Oriented Programming/Topic 3/3.4.2/CSVReader.h
Normal file
15
CM2005 Object Oriented Programming/Topic 3/3.4.2/CSVReader.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class CSVReader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CSVReader();
|
||||||
|
static std::vector<OrderBookEntry> readCSV(std::string csvFile);
|
||||||
|
private:
|
||||||
|
static std::vector<std::string> tokenise(std::string csvLine, char separator);
|
||||||
|
static OrderBookEntry stringsToBE(std::vector<std::string> strings);
|
||||||
|
};
|
||||||
129
CM2005 Object Oriented Programming/Topic 3/3.4.2/MerkelMain.cpp
Normal file
129
CM2005 Object Oriented Programming/Topic 3/3.4.2/MerkelMain.cpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
#include "MerkelMain.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
|
||||||
|
MerkelMain::MerkelMain()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::init()
|
||||||
|
{
|
||||||
|
loadOrderBook();
|
||||||
|
int input;
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
printMenu();
|
||||||
|
input = getUserOption();
|
||||||
|
processUserOption(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::loadOrderBook()
|
||||||
|
{
|
||||||
|
|
||||||
|
orders.push_back( OrderBookEntry{1000,
|
||||||
|
0.02,
|
||||||
|
"2020/03/17 17:01:24.884492",
|
||||||
|
"BTC/USDT",
|
||||||
|
OrderBookType::bid} );
|
||||||
|
|
||||||
|
orders.push_back( OrderBookEntry{2000,
|
||||||
|
0.02,
|
||||||
|
"2020/03/17 17:01:24.884492",
|
||||||
|
"BTC/USDT",
|
||||||
|
OrderBookType::bid} );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printMenu()
|
||||||
|
{
|
||||||
|
// 1 print help
|
||||||
|
std::cout << "1: Print help " << std::endl;
|
||||||
|
// 2 print exchange stats
|
||||||
|
std::cout << "2: Print exchange stats" << std::endl;
|
||||||
|
// 3 make an offer
|
||||||
|
std::cout << "3: Make an offer " << std::endl;
|
||||||
|
// 4 make a bid
|
||||||
|
std::cout << "4: Make a bid " << std::endl;
|
||||||
|
// 5 print wallet
|
||||||
|
std::cout << "5: Print wallet " << std::endl;
|
||||||
|
// 6 continue
|
||||||
|
std::cout << "6: Continue " << std::endl;
|
||||||
|
|
||||||
|
std::cout << "============== " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printHelp()
|
||||||
|
{
|
||||||
|
std::cout << "Help - your aim is to make money. Analyse the market and make bids and offers. " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printMarketStats()
|
||||||
|
{
|
||||||
|
std::cout << "OrderBook contains : " << orders.size() << "entries" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::enterOffer()
|
||||||
|
{
|
||||||
|
std::cout << "Mark and offer - enter the amount " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::enterBid()
|
||||||
|
{
|
||||||
|
std::cout << "Make a bid - enter the amount" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::printWallet()
|
||||||
|
{
|
||||||
|
std::cout << "Your wallet is empty. " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::gotoNextTimeframe()
|
||||||
|
{
|
||||||
|
std::cout << "Going to next time frame. " << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int MerkelMain::getUserOption()
|
||||||
|
{
|
||||||
|
int userOption;
|
||||||
|
|
||||||
|
std::cout << "Type in 1-6" << std::endl;
|
||||||
|
std::cin >> userOption;
|
||||||
|
std::cout << "You chose: " << userOption << std::endl;
|
||||||
|
return userOption;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MerkelMain::processUserOption(int userOption)
|
||||||
|
{
|
||||||
|
if (userOption == 0) // bad input
|
||||||
|
{
|
||||||
|
std::cout << "Invalid choice. Choose 1-6" << std::endl;
|
||||||
|
}
|
||||||
|
if (userOption == 1)
|
||||||
|
{
|
||||||
|
printHelp();
|
||||||
|
}
|
||||||
|
if (userOption == 2)
|
||||||
|
{
|
||||||
|
printMarketStats();
|
||||||
|
}
|
||||||
|
if (userOption == 3)
|
||||||
|
{
|
||||||
|
enterOffer();
|
||||||
|
}
|
||||||
|
if (userOption == 4)
|
||||||
|
{
|
||||||
|
enterBid();
|
||||||
|
}
|
||||||
|
if (userOption == 5)
|
||||||
|
{
|
||||||
|
printWallet();
|
||||||
|
}
|
||||||
|
if (userOption == 6)
|
||||||
|
{
|
||||||
|
gotoNextTimeframe();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
|
||||||
|
class MerkelMain
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MerkelMain();
|
||||||
|
/** Call this to start the sim */
|
||||||
|
void init();
|
||||||
|
private:
|
||||||
|
void loadOrderBook();
|
||||||
|
void printMenu();
|
||||||
|
void printHelp();
|
||||||
|
void printMarketStats();
|
||||||
|
void enterOffer();
|
||||||
|
void enterBid();
|
||||||
|
void printWallet();
|
||||||
|
void gotoNextTimeframe();
|
||||||
|
int getUserOption();
|
||||||
|
void processUserOption(int userOption);
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<OrderBookEntry> orders;
|
||||||
|
|
||||||
|
};
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
#include "OrderBookEntry.h"
|
||||||
|
|
||||||
|
OrderBookEntry::OrderBookEntry( double _price,
|
||||||
|
double _amount,
|
||||||
|
std::string _timestamp,
|
||||||
|
std::string _product,
|
||||||
|
OrderBookType _orderType)
|
||||||
|
: price(_price),
|
||||||
|
amount(_amount),
|
||||||
|
timestamp(_timestamp),
|
||||||
|
product(_product),
|
||||||
|
orderType(_orderType)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
enum class OrderBookType{bid, ask};
|
||||||
|
|
||||||
|
class OrderBookEntry
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
OrderBookEntry( double _price,
|
||||||
|
double _amount,
|
||||||
|
std::string _timestamp,
|
||||||
|
std::string _product,
|
||||||
|
OrderBookType _orderType);
|
||||||
|
|
||||||
|
|
||||||
|
double price;
|
||||||
|
double amount;
|
||||||
|
std::string timestamp;
|
||||||
|
std::string product;
|
||||||
|
OrderBookType orderType;
|
||||||
|
};
|
||||||
BIN
CM2005 Object Oriented Programming/Topic 3/3.4.2/a.exe
Normal file
BIN
CM2005 Object Oriented Programming/Topic 3/3.4.2/a.exe
Normal file
Binary file not shown.
53
CM2005 Object Oriented Programming/Topic 3/3.4.2/main.cpp
Normal file
53
CM2005 Object Oriented Programming/Topic 3/3.4.2/main.cpp
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include "OrderBookEntry.h"
|
||||||
|
#include "MerkelMain.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
MerkelMain app{};
|
||||||
|
app.init();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// std::vector<OrderBookEntry> orders;
|
||||||
|
|
||||||
|
// orders.push_back( OrderBookEntry{1000,
|
||||||
|
// 0.02,
|
||||||
|
// "2020/03/17 17:01:24.884492",
|
||||||
|
// "BTC/USDT",
|
||||||
|
// OrderBookType::bid} );
|
||||||
|
|
||||||
|
// orders.push_back( OrderBookEntry{2000,
|
||||||
|
// 0.02,
|
||||||
|
// "2020/03/17 17:01:24.884492",
|
||||||
|
// "BTC/USDT",
|
||||||
|
// OrderBookType::bid} );
|
||||||
|
|
||||||
|
|
||||||
|
// // std::cout << "The price is " << orders[1].price << std::endl;
|
||||||
|
|
||||||
|
// for (OrderBookEntry& order : orders)
|
||||||
|
// {
|
||||||
|
// std::cout << "The price is " << order.price << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (unsigned int i = 0; i < orders.size() ; ++i)
|
||||||
|
// {
|
||||||
|
// std::cout << "The price is " << orders[i].price << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (unsigned int i = 0; i < orders.size() ; ++i)
|
||||||
|
// {
|
||||||
|
// std::cout << "The price is " << orders.at(i).price << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BIN
CM2005 Object Oriented Programming/Topic 3/3.4.2/merklerex
Normal file
BIN
CM2005 Object Oriented Programming/Topic 3/3.4.2/merklerex
Normal file
Binary file not shown.
72
CM2005 Object Oriented Programming/Topic 3/3.4.2/test.cpp
Normal file
72
CM2005 Object Oriented Programming/Topic 3/3.4.2/test.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
std::vector<std::string> Tokenise(std::string csvLine, char separator)
|
||||||
|
{
|
||||||
|
std::vector<std::string> tokens;
|
||||||
|
signed int start, end;
|
||||||
|
std::string token;
|
||||||
|
start = csvLine.find_first_not_of(separator, 0);
|
||||||
|
do
|
||||||
|
{
|
||||||
|
end = csvLine.find_first_of(separator, start);
|
||||||
|
if(start == csvLine.length() || start == end) break;
|
||||||
|
if(end >= 0)
|
||||||
|
token = csvLine.substr(start, end - start);
|
||||||
|
else
|
||||||
|
token = csvLine.substr(start, csvLine.length() - start);
|
||||||
|
|
||||||
|
tokens.push_back(token);
|
||||||
|
start = end + 1;
|
||||||
|
} while (end > 0);
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::string filename = "20200317.csv";
|
||||||
|
std::string line;
|
||||||
|
std::ifstream csvFile{filename};
|
||||||
|
|
||||||
|
std::vector<std::string> tokens;
|
||||||
|
|
||||||
|
if(csvFile.is_open())
|
||||||
|
{
|
||||||
|
std::cout << "File open" << std::endl;
|
||||||
|
while(std::getline(csvFile, line))
|
||||||
|
{
|
||||||
|
std::cout << "Read line: " << line << std::endl;
|
||||||
|
tokens = Tokenise(line, ',');
|
||||||
|
if(tokens.size() != 5) //bad
|
||||||
|
{
|
||||||
|
std::cout << "Bad line" << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
double price{};
|
||||||
|
double amount{};
|
||||||
|
// We checked and we have 5 tokens
|
||||||
|
try
|
||||||
|
{
|
||||||
|
price = std::stod(tokens[3]);
|
||||||
|
amount = std::stod(tokens[4]);
|
||||||
|
std::cout << price << std::endl;
|
||||||
|
std::cout << amount << std::endl;
|
||||||
|
}
|
||||||
|
catch(std::exception& e)
|
||||||
|
{
|
||||||
|
//error reading file
|
||||||
|
std::cout << "ERROR" << std::endl;
|
||||||
|
}
|
||||||
|
break; // can be removed to read all lines in the csv
|
||||||
|
}
|
||||||
|
csvFile.close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Could not open file" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user