Skip to content

Commit

Permalink
refactor commerce module data (cieslarmichal#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored and 00thirdeye00 committed Jun 20, 2024
1 parent d6661e5 commit a87a965
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 180 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(FAKER_SOURCES
src/modules/color/Color.cpp
src/modules/color/ColorData.cpp
src/modules/commerce/Commerce.cpp
src/modules/commerce/CommerceData.cpp
src/modules/company/Company.cpp
src/modules/computer/Computer.cpp
src/modules/crypto/Crypto.cpp
Expand Down
26 changes: 13 additions & 13 deletions include/faker-cxx/Commerce.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <string>
#include <string_view>

namespace faker
{
Expand All @@ -16,7 +16,7 @@ class Commerce
* Commerce::department() // "Books"
* @endcode
*/
static std::string department();
static std::string_view department();

/**
* @brief Generates a random price between the given bounds (inclusive).
Expand Down Expand Up @@ -51,7 +51,7 @@ class Commerce
* Commerce::productAdjective() // "Handcrafted"
* @endcode
*/
static std::string productAdjective();
static std::string_view productAdjective();

/**
* @brief Returns a random product material.
Expand All @@ -62,7 +62,7 @@ class Commerce
* Commerce::productMaterial() // "Wooden"
* @endcode
*/
static std::string productMaterial();
static std::string_view productMaterial();

/**
* @brief Returns a random product short name.
Expand All @@ -73,7 +73,7 @@ class Commerce
* Commerce::productName() // "Computer"
* @endcode
*/
static std::string productName();
static std::string_view productName();

/**
* @brief Returns a random product full name.
Expand Down Expand Up @@ -150,7 +150,7 @@ class Commerce
* Commerce::paymentType() // "Credit Card"
* @endcode
*/
static std::string paymentType();
static std::string_view paymentType();

/**
* @brief Returns a random payment provider.
Expand All @@ -161,7 +161,7 @@ class Commerce
* Commerce::paymentProvider() // "Paypal"
* @endcode
*/
static std::string paymentProvider();
static std::string_view paymentProvider();

/**
* @brief Returns a random product description.
Expand All @@ -172,7 +172,7 @@ class Commerce
* Commerce::productDescription() // "Elevate your lifestyle with premium quality product."
* @endcode
*/
static std::string productDescription();
static std::string_view productDescription();

/**
* @brief Returns a random product category.
Expand All @@ -183,7 +183,7 @@ class Commerce
* Commerce::productCategory() // "Electronics"
* @endcode
*/
static std::string productCategory();
static std::string_view productCategory();

/**
* @brief Returns a random product review.
Expand All @@ -194,7 +194,7 @@ class Commerce
* Commerce::productReview() // "Unfortunately, it broke shortly after I started using it."
* @endcode
*/
static std::string productReview();
static std::string_view productReview();

/**
* @brief Returns a random product rating (0-5).
Expand All @@ -216,7 +216,7 @@ class Commerce
* Commerce::discountType() // "percentage"
* @endcode
*/
static std::string discountType();
static std::string_view discountType();

/**
* @brief Returns random discount code within the specified range of 6 to 12 characters.
Expand Down Expand Up @@ -271,7 +271,7 @@ class Commerce
* Commerce::orderStatus() // "shipped"
* @endcode
*/
static std::string orderStatus();
static std::string_view orderStatus();

/**
* @brief Returns a random shipping carrier.
Expand All @@ -282,6 +282,6 @@ class Commerce
* Commerce::shippingMethod() // "FedEx"
* @endcode
*/
static std::string shippingCarrier();
static std::string_view shippingCarrier();
};
}
2 changes: 2 additions & 0 deletions src/modules/color/ColorData.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <array>
#include <string_view>

Expand Down
72 changes: 39 additions & 33 deletions src/modules/commerce/Commerce.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "faker-cxx/Commerce.h"

#include "../../common/FormatHelper.h"
#include "data/Commerce.h"
#include "CommerceData.h"
#include "faker-cxx/Finance.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/String.h"

namespace faker
{
std::string Commerce::department()
std::string_view Commerce::department()
{
return Helper::arrayElement<std::string>(departments);
return Helper::arrayElement(departments);
}

std::string Commerce::price(double min, double max)
Expand All @@ -23,19 +23,19 @@ std::string Commerce::sku(unsigned int length)
return String::numeric(length, false);
}

std::string Commerce::productAdjective()
std::string_view Commerce::productAdjective()
{
return Helper::arrayElement<std::string>(productAdjectives);
return Helper::arrayElement(productAdjectives);
}

std::string Commerce::productMaterial()
std::string_view Commerce::productMaterial()
{
return Helper::arrayElement<std::string>(productMaterials);
return Helper::arrayElement(productMaterials);
}

std::string Commerce::productName()
std::string_view Commerce::productName()
{
return Helper::arrayElement<std::string>(productNames);
return Helper::arrayElement(productNames);
}

std::string Commerce::productFullName()
Expand All @@ -45,9 +45,10 @@ std::string Commerce::productFullName()

std::string Commerce::EAN13()
{
std::string ean13 = String::numeric(12, false);
const auto ean13 = String::numeric(12, false);

int sum = 0;

for (size_t i = 0; i < 12; i++)
{
if (i % 2 == 0)
Expand All @@ -72,9 +73,10 @@ std::string Commerce::EAN13()

std::string Commerce::EAN8()
{
std::string ean8 = String::numeric(7, false);
const auto ean8 = String::numeric(7, false);

int sum = 0;

for (size_t i = 0; i < 7; i++)
{
if (i % 2 == 0)
Expand All @@ -99,9 +101,10 @@ std::string Commerce::EAN8()

std::string Commerce::ISBN13()
{
std::string isbn13 = String::numeric(12, true);
const auto isbn13 = String::numeric(12, true);

int sum = 0;

for (size_t i = 0; i < 12; i++)
{
if (i % 2 == 0)
Expand All @@ -126,9 +129,10 @@ std::string Commerce::ISBN13()

std::string Commerce::ISBN10()
{
std::string isbn10 = String::numeric(9, true);
const auto isbn10 = String::numeric(9, true);

int sum = 0, weight = 10;

for (size_t i = 0; i < 9; i++)
{
sum += (isbn10[i] - '0') * weight;
Expand All @@ -155,29 +159,29 @@ std::string Commerce::productId()
return String::alphanumeric(10, StringCasing::Upper, "");
}

std::string Commerce::paymentType()
std::string_view Commerce::paymentType()
{
return Helper::arrayElement<std::string>(paymentTypes);
return Helper::arrayElement(paymentTypes);
}

std::string Commerce::paymentProvider()
std::string_view Commerce::paymentProvider()
{
return Helper::arrayElement<std::string>(paymentProviders);
return Helper::arrayElement(paymentProviders);
}

std::string Commerce::productDescription()
std::string_view Commerce::productDescription()
{
return Helper::arrayElement<std::string>(productDescriptions);
return Helper::arrayElement(productDescriptions);
}

std::string Commerce::productCategory()
std::string_view Commerce::productCategory()
{
return Helper::arrayElement<std::string>(productCategoryNames);
return Helper::arrayElement(productCategoryNames);
}

std::string Commerce::productReview()
std::string_view Commerce::productReview()
{
return Helper::arrayElement<std::string>(productReviews);
return Helper::arrayElement(productReviews);
}

double Commerce::productRating()
Expand All @@ -186,28 +190,30 @@ double Commerce::productRating()
return std::ceil(ratingValue * 100) / 100;
}

std::string Commerce::discountType()
std::string_view Commerce::discountType()
{
return Helper::arrayElement<std::string>(discountTypes);
return Helper::arrayElement(discountTypes);
}

std::string Commerce::discountCode()
{
const std::integral auto codeLength = Number::integer<unsigned int>(kMinDiscountCodeLength, kMaxDiscountCodeLength);
const std::integral auto codeLength = Number::integer<unsigned int>(minDiscountCodeLength, maxDiscountCodeLength);

return String::alphanumeric(codeLength, StringCasing::Upper);
}

double Commerce::discountAmount()
{
const std::floating_point auto amountValue =
Number::decimal<double>(kMinDiscountAmountValue, kMaxDiscountAmountValue);
const std::floating_point auto amountValue = Number::decimal<double>(minDiscountAmount, maxDiscountAmount);

return std::ceil(amountValue * 100) / 100;
}

double Commerce::discountPercentage()
{
const std::floating_point auto percentageValue =
Number::decimal<double>(kMinDiscountPercentageValue, kMaxDiscountPercentageValue);
Number::decimal<double>(minDiscountPercentage, maxDiscountPercentage);

return std::ceil(percentageValue * 100) / 100;
}

Expand All @@ -216,14 +222,14 @@ std::string Commerce::orderNumber()
return String::numeric(7, true);
}

std::string Commerce::orderStatus()
std::string_view Commerce::orderStatus()
{
return Helper::arrayElement<std::string>(orderStatuses);
return Helper::arrayElement(orderStatuses);
}

std::string Commerce::shippingCarrier()
std::string_view Commerce::shippingCarrier()
{
return Helper::arrayElement<std::string>(shippingCarriers);
return Helper::arrayElement(shippingCarriers);
}

}
Loading

0 comments on commit a87a965

Please sign in to comment.