Skip to content

Commit

Permalink
refactor finance module data
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed May 29, 2024
1 parent 3aa05ba commit fc5579b
Show file tree
Hide file tree
Showing 12 changed files with 967 additions and 971 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ set(FAKER_SOURCES
src/modules/date/Date.cpp
src/modules/date/DateData.cpp
src/modules/finance/Finance.cpp
src/modules/finance/FinanceData.cpp
src/modules/food/Food.cpp
src/modules/git/Git.cpp
src/modules/hacker/Hacker.cpp
Expand Down
19 changes: 10 additions & 9 deletions include/faker-cxx/Finance.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <optional>
#include <string>
#include <string_view>

#include "types/Country.h"
#include "types/Precision.h"
Expand All @@ -10,9 +11,9 @@ namespace faker
{
struct Currency
{
std::string name;
std::string code;
std::string symbol;
std::string_view name;
std::string_view code;
std::string_view symbol;
};

class Finance
Expand All @@ -38,7 +39,7 @@ class Finance
* Finance::currencyName() // "US Dollar"
* @endcode
*/
static std::string currencyName();
static std::string_view currencyName();

/**
* @brief Returns a random currency code.
Expand All @@ -49,7 +50,7 @@ class Finance
* Finance::currencyCode() // "USD"
* @endcode
*/
static std::string currencyCode();
static std::string_view currencyCode();

/**
* @brief Returns a random currency symbol.
Expand All @@ -60,7 +61,7 @@ class Finance
* Finance::currencySymbol() // "$"
* @endcode
*/
static std::string currencySymbol();
static std::string_view currencySymbol();

/**
* @brief Returns a random account type.
Expand All @@ -71,7 +72,7 @@ class Finance
* Finance::accountType() // "Savings"
* @endcode
*/
static std::string accountType();
static std::string_view accountType();

/**
* @brief Generates a random amount between the given bounds (inclusive).
Expand Down Expand Up @@ -164,7 +165,7 @@ class Finance
* Finance::bic(BicCountry::Poland) // "BREXPLPWMUL"
* @endcode
*/
static std::string bic(std::optional<BicCountry> country = std::nullopt);
static std::string_view bic(std::optional<BicCountry> country = std::nullopt);

/**
* Generates a random account number.
Expand Down Expand Up @@ -290,6 +291,6 @@ class Finance
* Finance::creditCardType() // "Visa"
* @endcode
*/
static std::string creditCardType();
static std::string_view creditCardType();
};
}
98 changes: 37 additions & 61 deletions src/modules/finance/Finance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,38 @@

#include "../../common/FormatHelper.h"
#include "../../common/PrecisionMapper.h"
#include "data/AccountTypes.h"
#include "data/BankIndentifiersCodes.h"
#include "data/CreditCardsFormats.h"
#include "data/CreditCardTypeNames.h"
#include "data/Currencies.h"
#include "data/IbanFormats.h"
#include "faker-cxx/Date.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"
#include "faker-cxx/String.h"
#include "FinanceData.h"

namespace faker
{
namespace
{
const std::vector<Finance::BicCountry> supportedBicCountries{
Finance::BicCountry::Poland, Finance::BicCountry::UnitedStates, Finance::BicCountry::UnitedKingdom,
Finance::BicCountry::Germany, Finance::BicCountry::Romania, Finance::BicCountry::France,
Finance::BicCountry::Italy, Finance::BicCountry::Spain, Finance::BicCountry::Netherlands,
Finance::BicCountry::India};

const std::vector<Finance::IbanCountry> supportedIbanCountries{
Finance::IbanCountry::Austria, Finance::IbanCountry::Belgium, Finance::IbanCountry::Bulgaria,
Finance::IbanCountry::Croatia, Finance::IbanCountry::Cyprus, Finance::IbanCountry::Czechia,
Finance::IbanCountry::Denmark, Finance::IbanCountry::Estonia, Finance::IbanCountry::Finland,
Finance::IbanCountry::France, Finance::IbanCountry::Germany, Finance::IbanCountry::Greece,
Finance::IbanCountry::Hungary, Finance::IbanCountry::Ireland, Finance::IbanCountry::Italy,
Finance::IbanCountry::Latvia, Finance::IbanCountry::Lithuania, Finance::IbanCountry::Luxembourg,
Finance::IbanCountry::Malta, Finance::IbanCountry::Netherlands, Finance::IbanCountry::Poland,
Finance::IbanCountry::Portugal, Finance::IbanCountry::Romania, Finance::IbanCountry::Slovakia,
Finance::IbanCountry::Slovenia, Finance::IbanCountry::Spain, Finance::IbanCountry::Sweden,
};

const std::unordered_map<Finance::CreditCardType, std::vector<std::string>> creditCardTypeToNumberFormats{
{Finance::CreditCardType::AmericanExpress, americanExpressCreditCardFormats},
{Finance::CreditCardType::Discover, discoverCreditCardFormats},
{Finance::CreditCardType::MasterCard, masterCardCreditCardFormats},
{Finance::CreditCardType::Visa, visaCreditCardFormats},
};

const std::vector<Finance::CreditCardType> creditCardTypes{
Finance::CreditCardType::AmericanExpress, Finance::CreditCardType::Discover, Finance::CreditCardType::MasterCard,
Finance::CreditCardType::Visa};
}

Currency Finance::currency()
{
return Helper::arrayElement<Currency>(currencies);
return Helper::arrayElement(currencies);
}

std::string Finance::currencyName()
std::string_view Finance::currencyName()
{
return Helper::arrayElement<Currency>(currencies).name;
return Helper::arrayElement(currencies).name;
}

std::string Finance::currencyCode()
std::string_view Finance::currencyCode()
{
return Helper::arrayElement<Currency>(currencies).code;
return Helper::arrayElement(currencies).code;
}

std::string Finance::currencySymbol()
std::string_view Finance::currencySymbol()
{
return Helper::arrayElement<Currency>(currencies).symbol;
return Helper::arrayElement(currencies).symbol;
}

std::string Finance::accountType()
std::string_view Finance::accountType()
{
return Helper::arrayElement<std::string>(accountTypes);
return Helper::arrayElement(accountTypes);
}

std::string Finance::amount(double min, double max, Precision precision, const std::string& symbol)
Expand All @@ -89,7 +53,7 @@ std::string Finance::amount(double min, double max, Precision precision, const s

std::string Finance::iban(std::optional<Finance::IbanCountry> country)
{
const auto ibanCountry = country ? *country : Helper::arrayElement<Finance::IbanCountry>(supportedIbanCountries);
const auto ibanCountry = country ? *country : Helper::arrayElement(ibanCountries);

const auto& ibanFormat = ibanFormats.at(ibanCountry);

Expand All @@ -102,7 +66,8 @@ std::string Finance::iban(std::optional<Finance::IbanCountry> country)
const auto& ibanFormatEntry = ibanFormat[i];

const auto ibanFormatEntryDataType = ibanFormatEntry[ibanFormatEntry.size() - 1];
const auto ibanFormatEntryDataLength = std::stoi(ibanFormatEntry.substr(0, ibanFormatEntry.size() - 1));
const auto ibanFormatEntryDataLength =
std::stoi(static_cast<const std::string>(ibanFormatEntry.substr(0, ibanFormatEntry.size() - 1)));

if (ibanFormatEntryDataType == 'a')
{
Expand All @@ -121,11 +86,11 @@ std::string Finance::iban(std::optional<Finance::IbanCountry> country)
return iban;
}

std::string Finance::bic(std::optional<Finance::BicCountry> country)
std::string_view Finance::bic(std::optional<Finance::BicCountry> country)
{
const auto bicCountry = country ? *country : Helper::arrayElement<Finance::BicCountry>(supportedBicCountries);
const auto bicCountry = country ? *country : Helper::arrayElement(bicCountries);

return Helper::arrayElement<std::string>(bankIdentifiersCodesMapping.at(bicCountry));
return Helper::arrayElement(bicCountriesCodes.at(bicCountry));
}

std::string Finance::accountNumber(unsigned int length)
Expand All @@ -145,14 +110,24 @@ std::string Finance::routingNumber()

std::string Finance::creditCardNumber(std::optional<Finance::CreditCardType> creditCardType)
{
const auto creditCardTargetType =
creditCardType ? *creditCardType : Helper::arrayElement<Finance::CreditCardType>(creditCardTypes);

const auto& creditCardFormats = creditCardTypeToNumberFormats.at(creditCardTargetType);
const auto creditCardTargetType = creditCardType ? *creditCardType : Helper::arrayElement(creditCardTypes);

const auto creditCardFormat = Helper::arrayElement<std::string>(creditCardFormats);
switch (creditCardTargetType)
{
case CreditCardType::AmericanExpress:
return Helper::replaceCreditCardSymbols(
static_cast<std::string>(Helper::arrayElement(americanExpressCreditCardFormats)));
case CreditCardType::Discover:
return Helper::replaceCreditCardSymbols(
static_cast<std::string>(Helper::arrayElement(discoverCreditCardFormats)));
case CreditCardType::MasterCard:
return Helper::replaceCreditCardSymbols(
static_cast<std::string>(Helper::arrayElement(masterCardCreditCardFormats)));
case CreditCardType::Visa:
return Helper::replaceCreditCardSymbols(static_cast<std::string>(Helper::arrayElement(visaCreditCardFormats)));
}

return Helper::replaceCreditCardSymbols(creditCardFormat);
return "";
}

std::string Finance::creditCardCvv()
Expand All @@ -164,7 +139,7 @@ std::string Finance::bitcoinAddress()
{
const unsigned addressLength = Number::integer(26u, 33u);

auto address = Helper::arrayElement<std::string>(std::vector<std::string>{"1", "3"});
auto address = Helper::arrayElement(std::vector<std::string>{"1", "3"});

address += String::alphanumeric(addressLength, StringCasing::Mixed, "0OIl");

Expand All @@ -175,7 +150,7 @@ std::string Finance::litecoinAddress()
{
const unsigned addressLength = Number::integer(26u, 33u);

auto address = Helper::arrayElement<std::string>(std::vector<std::string>{"L", "M", "3"});
auto address = Helper::arrayElement(std::vector<std::string>{"L", "M", "3"});

address += String::alphanumeric(addressLength, StringCasing::Mixed, "0OIl");

Expand All @@ -190,12 +165,13 @@ std::string Finance::ethereumAddress()
std::string Finance::creditCardExpirationDate()
{
const auto expirationDate = Date::futureDate(3);

return expirationDate.substr(5, 2) + "/" + expirationDate.substr(2, 2);
}

std::string Finance::creditCardType()
std::string_view Finance::creditCardType()
{
return Helper::arrayElement<std::string>(creditCardTypeNames);
return Helper::arrayElement(creditCardNames);
}

}
Loading

0 comments on commit fc5579b

Please sign in to comment.