---- PreDescription ----
Started programming back in 2015-2016 using C++ and then got lost in many other programming languages. Python, Java, C#, JS, HTML just to name a few lol.
I have decided I will only focus on C++ and python. Someone else can take care of the rest lol.
---- Description ----
I'm looking for feedback :). straight up blunt honesty lol and criticism with my code anything is welcome XD :D.
---- Current Project ----
Currency Exchanger (Will evolve to Stock Market Analyzer/Scanner/Tool)also forex, crypto...
1. Practicing with interfaces.
2. Wanted to introduce Commands Pattern(unsure if its a fit) but wanted to practice.
hence the (Exchange, Deposit, Withdraw, Exit) feedback on what to look for or anything is welcome :)
---- Reason ----
To practice on SWD Patterns, and C++ concepts.
-- CODE --
Main [CurrencyExchanger.cpp]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// CurrencyExchanger.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include "ExchangeOne.h"
using namespace std;
int main()
{
ExchangeOne BankOne;
BankOne.setCompanyName("BankOne Bank");
BankOne.start();
return 0;
}
|
ExchangeOne.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#pragma once
#include "ExchangeInterface.h"
class ExchangeOne : public ExchangeInterface {
public:
double deposit();
double withdraw();
void exit();
double applyFee();
private:
void HandlePromptLoop();
void HandlePrompt();
int USER_CHOICE;
};
|
ExchangeOne.cpp (method definition)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include "ExchangeOne.h"
double ExchangeOne::deposit() {
return 0.0;
}
double ExchangeOne::withdraw() {
return 0.0;
}
double ExchangeOne::applyFee() {
return 0.0;
}
void ExchangeOne::exit() {
std::cout << "Thank you for your business from " << CompanyName << std::endl;
}
void ExchangeOne::HandlePromptLoop() {
if (CompanyName == "") {
std::cout << "Missing Company Name, unable to continue.\n";
return;
}
do {
prompt();
std::cin >> USER_CHOICE;
HandlePrompt();
} while (USER_CHOICE != EXIT_CODE);
}
void ExchangeOne::HandlePrompt() {
double x;
std::string currencyToExchange;
switch (USER_CHOICE)
{
case EXCHANGE:
std::cout << "To what currency would you like to exchange: ";
std::cin >> currencyToExchange;
std::cout << "How much would you like to exchange: ";
double userInput;
std::cin >> userInput;
x = exchangePlease(currencyToExchange, userInput);
std::cout << "Your " << userInput << " Euros will be " << x << " " << " USD." << std::endl;
break;
case DEPOSIT:
deposit();
break;
case WITHDRAW:
withdraw();
break;
case EXIT_CODE:
exit();
break;
default:
break;
}
}
|
ExchangeInterface.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#pragma once
#include <string>
#include <iostream>
#include "Currency.h"
#define EXCHANGE 1
#define DEPOSIT 2
#define WITHDRAW 3
#define EXIT_CODE 4
class ExchangeInterface : private Currency {
public:
virtual double exchangePlease(std::string toCurrency, double amount) {
return exchange(toCurrency, amount);
}
virtual double applyFee() = 0;
virtual double deposit() = 0;
virtual double withdraw() = 0;
virtual void exit() = 0;
virtual void start() {
HandlePromptLoop();
}
void setCompanyName(std::string companyName) {
CompanyName = companyName;
}
protected:
virtual void HandlePromptLoop() = 0;
void prompt() {
std::cout << "Welcome to " << CompanyName << ", What can I do?\n";
std::cout << "1. Exchange\n2. Deposit\n3. Withdraw\n4. Exit\n";
std::cout << "User Choice: ";
};
int numberOfTrades;
std::string CompanyName;
};
|
Currency.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#pragma once
#include <fstream>
#include <string>
#include <unordered_map>
#include <iostream>
class Currency {
protected:
virtual double exchange(std::string currency, double qty) {
if (RatesBaseEuro.empty()) {
loadLatestRateFile();
}
if (!RatesBaseEuro.count(currency)) {
std::cout << "Key does not exist\n";
}
return std::stod(RatesBaseEuro.at(currency)) * qty;
}
private:
std::unordered_map<std::string, std::string> RatesBaseEuro;
void loadLatestRateFile() {
std::ifstream latestRateFile("latest_rates.rt");
int index = 1;
std::string prevCurrencyCode;
if (latestRateFile.is_open()) {
while (latestRateFile.good()) {
std::string line;
getline(latestRateFile, line);
tokenize(line, ",");
}
latestRateFile.close();
} else {
std::cout << "Error: File could not be opened\n" << std::endl;
}
}
void tokenize(std::string word, std::string delimeter = " ") {
int start, end = -1 * delimeter.size();
std::string toReturnArray[2];
int index = 0;
do {
start = end + delimeter.size();
end = word.find(delimeter, start);
//std::cout << word.substr(start, end - start);
toReturnArray[index++] = word.substr(start, end - start);
} while (end != -1);
RatesBaseEuro.insert(std::pair<std::string, std::string>(toReturnArray[0], toReturnArray[1]));
}
};
|
---- Code Explained [in my head] ----
ExchangeOne -> ExchangeInterface -> Currency
anyExchange can use the interface and apply their own fees.
The interface acts as a facade or (api?) for the developer using the "tools"
Main just makes an object of ExchangeOne as if I was using the tools,
Exchange, ApplyFees and what not.