Implement a rudimentary market for cryptocurrencies in C++. A class, name "Exchange",…
- User
Implement a rudimentary market for cryptocurrencies in C++. A class, name "Exchange", has public member functions that allow for the deposit and withdrawl of various assets (e.g. Bitcoin, Etherum and US Dollars). Orders to buy and sell assets at a specified price can also be made and the exchange will perform trades as possible to fulfill orders. Lastly, the exchange has a member function dedicated to printing out the current status of the exchange (i.e. what is in each user’s portfolio, what orders have been opened and filled, what trades have taken place, and what the current prices are for the assets being traded. The starter code has three header files and 4 implementation files. Some of these files have content in them already, but you are welcome to add, remove or change any part of the code given. Each test case will compile the implementation files and include the exchange.hpp and/or utility.hpp if it makes use of a class declared within. The useraccount files are not directly tested in the test cases, but they can be optionally used to store and manipulate information associated with a user’s account (as helper class(es) for the exchange).
Here are the starter code files:
exhcange.hpp #pragma once #include #include
#include "useraccount.hpp" #include "utility.hpp"
class Exchange { public: void MakeDeposit(const std::string &username, const std::string &asset, int amount); void PrintUserPortfolios(std::ostream &os) const; bool MakeWithdrawal(const std::string &username, const std::string &asset, int amount); bool AddOrder(const Order &order); void PrintUsersOrders(std::ostream &os) const; void PrintTradeHistory(std::ostream &os) const; void PrintBidAskSpread(std::ostream &os) const; };
utility.hpp #pragma once #include #include
struct Order { std::string username; std::string side; // Can be "Buy" or "Sell" std::string asset; int amount; int price; };
struct Trade { std::string buyer_username; std::string seller_username; std::string asset; int amount; int price; };
First write this following member function: • MakeDeposit: this function should take a username (e.g. “Prof”), an asset (e.g. “BTC”), and an amount (e.g. 100). The function does not return anything, but should store the information that the deposit occurred to a user’s portfolio. A portfolio is a record of the assets a user possesses on the exchange that are not involved in any open orders. In the example, Prof is depositing 100 BTC (Bitcoins) into the exchange.
- Assistant
void Exchange::MakeDeposit(const std::string &username, const std::string &asset, int amount) { // Find the user's account UserAccount *user_account = FindUserAccount(username); if (user_account == nullptr) { std::cerr << "Error: User account not found\n"; return; }
// Update the user's portfolio user_account->AddToPortfolio(asset, amount);
// Print a message indicating the deposit was successful std::cout << "Deposit of " << amount << " " << asset << " successful for user " << username << "\n"; }
- User
Can you tell me specifically what code goes in what file
- Assistant
Sure!
The
MakeDepositfunction should be added to theExchangeclass inexchange.hpp.The
FindUserAccountfunction (which wasn't included in the starter code) should be added to theExchangeclass, and defined inexchange.cpp. It should take a stringusernameas a parameter and return a pointer to aUserAccountobject if the account is found, ornullptrif not.The
AddToPortfoliofunction is a member function of theUserAccountclass, defined inuseraccount.cpp, so you'll need to includeuseraccount.hppinexchange.cpp.std::coutandstd::cerrare part of the iostream library, so you'll need to include<iostream>inexchange.hpp.I hope that helps!
- User
Can you change the code so we don't need a FindUserAccount?
- Assistant
Certainly! One way to do this is to change our
UserAccountobjects from a map of assets to an unordered_map of asset names to amounts (i.e. how many units of each asset the user has). Then, we can create a newUserAccountobject automatically in theMakeDepositfunction if one doesn't already exist for the given username. Here's the modified code for theMakeDepositfunction:codevoid Exchange::MakeDeposit(const std::string &username, const std::string &asset, int amount) { // Check if the user already has an account on the exchange auto user_account_iter = users.find(username); if (user_account_iter == users.end()) { // Create a new account for the user users[username] = UserAccount(); std::cout << "Created new account for user " << username << "\n"; } // Update the user's portfolio users[username].AddToPortfolio(asset, amount); // Print a message indicating the deposit was successful std::cout << "Deposit of " << amount << " " << asset << " successful for user " << username << "\n"; }