Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,17 @@
/subprojects/Catch2-*/
/subprojects/packagecache/
/cmake-build-*/

# CMake build artifacts
CMakeCache.txt
CMakeFiles/
_deps/
Makefile
cmake_install.cmake
CTestTestfile.cmake

# Binary files
sample_test_catch2
sample_test_gtest
*.a
*.o
93 changes: 59 additions & 34 deletions c/src/supermarket.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,23 @@ void handle_offers(struct cart_t* cart, struct receipt_t* receipt, struct specia
double quantity = cart->quantities[i];
if (strcmp(offer->product->name, product.name) == 0) {
double unitPrice = unit_price(catalog, &product);
int quantityAsInt = (int)quantity;
struct discount_t* discount = NULL;
int x = 1;

if (offer->type == ThreeForTwo) {
x = 3;
} else if (offer->type == TwoForAmount) {
x = 2;
if (quantityAsInt >= 2) {
double total = offer->argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice;
double discountN = unitPrice * quantity - total;
char description[MAX_NAME_LENGTH];
sprintf(description, "2 for %f", offer->argument);
discount = discount_create(description, -discountN, &product);
}
} if (offer->type == FiveForAmount) {
x = 5;
}
int numberOfXs = quantityAsInt / x;
if (offer->type == ThreeForTwo && quantityAsInt > 2) {
double discountAmount = quantity * unitPrice - ((numberOfXs * 2 * unitPrice) + quantityAsInt % 3 * unitPrice);
char description[MAX_NAME_LENGTH];
sprintf(description, "3 for 2");
discount = discount_create(description, -discountAmount, &product);
}
if (offer->type == TenPercentDiscount) {
char description[MAX_NAME_LENGTH];
sprintf(description, "%.0f%% off", offer->argument);
discount = discount_create(description, -quantity * unitPrice * offer->argument / 100.0, &product);

switch (offer->type) {
case ThreeForTwo:
discount = calculate_three_for_two_discount(&product, quantity, unitPrice);
break;
case TwoForAmount:
discount = calculate_two_for_amount_discount(&product, quantity, unitPrice, offer->argument);
break;
case TenPercentDiscount:
discount = calculate_ten_percent_discount(&product, quantity, unitPrice, offer->argument);
break;
case FiveForAmount:
discount = calculate_five_for_amount_discount(&product, quantity, unitPrice, offer->argument);
break;
}
if (offer->type == FiveForAmount && quantityAsInt >= 5) {
double discountTotal = unitPrice * quantity - (offer->argument * numberOfXs + quantityAsInt % 5 * unitPrice);
char description[MAX_NAME_LENGTH];
sprintf(description, "%d for %f", x, offer->argument);
discount = discount_create(description, -discountTotal, &product);
}

if (discount != NULL) {
receipt->discounts[receipt->discountCount] = *discount;
receipt->discountCount++;
Expand Down Expand Up @@ -149,3 +129,48 @@ double unit_price(struct catalog_t* catalog, struct product_t *product) {
}
return 0;
}

struct discount_t* calculate_three_for_two_discount(struct product_t* product, double quantity, double unitPrice) {
int quantityAsInt = (int)quantity;
if (quantityAsInt <= 2) {
return NULL;
}

int numberOfThrees = quantityAsInt / 3;
double discountAmount = quantity * unitPrice - ((numberOfThrees * 2 * unitPrice) + quantityAsInt % 3 * unitPrice);
char description[MAX_NAME_LENGTH];
sprintf(description, "3 for 2");
return discount_create(description, -discountAmount, product);
}

struct discount_t* calculate_two_for_amount_discount(struct product_t* product, double quantity, double unitPrice, double amount) {
int quantityAsInt = (int)quantity;
if (quantityAsInt < 2) {
return NULL;
}

double total = amount * (quantityAsInt / 2) + quantityAsInt % 2 * unitPrice;
double discountN = unitPrice * quantity - total;
char description[MAX_NAME_LENGTH];
sprintf(description, "2 for %f", amount);
return discount_create(description, -discountN, product);
}

struct discount_t* calculate_ten_percent_discount(struct product_t* product, double quantity, double unitPrice, double percentage) {
char description[MAX_NAME_LENGTH];
sprintf(description, "%.0f%% off", percentage);
return discount_create(description, -quantity * unitPrice * percentage / 100.0, product);
}

struct discount_t* calculate_five_for_amount_discount(struct product_t* product, double quantity, double unitPrice, double amount) {
int quantityAsInt = (int)quantity;
if (quantityAsInt < 5) {
return NULL;
}

int numberOfFives = quantityAsInt / 5;
double discountTotal = unitPrice * quantity - (amount * numberOfFives + quantityAsInt % 5 * unitPrice);
char description[MAX_NAME_LENGTH];
sprintf(description, "5 for %f", amount);
return discount_create(description, -discountTotal, product);
}
7 changes: 7 additions & 0 deletions c/src/supermarket.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ struct discount_t* discount_create(char* description, double discount, struct pr
struct receipt_t* check_out_articles(struct teller_t* teller, struct cart_t* cart);
double total_price(struct receipt_t* receipt);
double unit_price(struct catalog_t* catalog, struct product_t *product);

// Discount calculation functions
struct discount_t* calculate_three_for_two_discount(struct product_t* product, double quantity, double unitPrice);
struct discount_t* calculate_two_for_amount_discount(struct product_t* product, double quantity, double unitPrice, double amount);
struct discount_t* calculate_ten_percent_discount(struct product_t* product, double quantity, double unitPrice, double percentage);
struct discount_t* calculate_five_for_amount_discount(struct product_t* product, double quantity, double unitPrice, double amount);

void handle_offers(struct cart_t* cart, struct receipt_t* receipt, struct special_offer_t* offer, struct catalog_t* catalog);

#endif //SAMPLE_H
85 changes: 57 additions & 28 deletions csharp-ms/SupermarketReceipt/ShoppingCart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,76 @@ public void HandleOffers(Receipt receipt, Dictionary<Product, Offer> offers, Sup
foreach (var p in _productQuantities.Keys)
{
var quantity = _productQuantities[p];
var quantityAsInt = (int) quantity;
if (offers.ContainsKey(p))
{
var offer = offers[p];
var unitPrice = catalog.GetUnitPrice(p);
Discount discount = null;
var x = 1;
if (offer.OfferType == SpecialOfferType.ThreeForTwo)
{
x = 3;
}
else if (offer.OfferType == SpecialOfferType.TwoForAmount)
{
x = 2;
if (quantityAsInt >= 2)
{
var total = offer.Argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice;
var discountN = unitPrice * quantity - total;
discount = new Discount(p, "2 for " + offer.Argument, -discountN);
}
}

if (offer.OfferType == SpecialOfferType.FiveForAmount) x = 5;
var numberOfXs = quantityAsInt / x;
if (offer.OfferType == SpecialOfferType.ThreeForTwo && quantityAsInt > 2)
{
var discountAmount = quantity * unitPrice - (numberOfXs * 2 * unitPrice + quantityAsInt % 3 * unitPrice);
discount = new Discount(p, "3 for 2", -discountAmount);
}

if (offer.OfferType == SpecialOfferType.TenPercentDiscount) discount = new Discount(p, offer.Argument + "% off", -quantity * unitPrice * offer.Argument / 100.0);
if (offer.OfferType == SpecialOfferType.FiveForAmount && quantityAsInt >= 5)
switch (offer.OfferType)
{
var discountTotal = unitPrice * quantity - (offer.Argument * numberOfXs + quantityAsInt % 5 * unitPrice);
discount = new Discount(p, x + " for " + offer.Argument, -discountTotal);
case SpecialOfferType.ThreeForTwo:
discount = CalculateThreeForTwoDiscount(p, quantity, unitPrice);
break;
case SpecialOfferType.TwoForAmount:
discount = CalculateTwoForAmountDiscount(p, quantity, unitPrice, offer.Argument);
break;
case SpecialOfferType.TenPercentDiscount:
discount = CalculateTenPercentDiscount(p, quantity, unitPrice, offer.Argument);
break;
case SpecialOfferType.FiveForAmount:
discount = CalculateFiveForAmountDiscount(p, quantity, unitPrice, offer.Argument);
break;
}

if (discount != null)
receipt.AddDiscount(discount);
}
}
}

private Discount CalculateThreeForTwoDiscount(Product product, double quantity, double unitPrice)
{
var quantityAsInt = (int) quantity;
if (quantityAsInt <= 2)
{
return null;
}

var numberOfThrees = quantityAsInt / 3;
var discountAmount = quantity * unitPrice - (numberOfThrees * 2 * unitPrice + quantityAsInt % 3 * unitPrice);
return new Discount(product, "3 for 2", -discountAmount);
}

private Discount CalculateTwoForAmountDiscount(Product product, double quantity, double unitPrice, double amount)
{
var quantityAsInt = (int) quantity;
if (quantityAsInt < 2)
{
return null;
}

var total = amount * (quantityAsInt / 2) + quantityAsInt % 2 * unitPrice;
var discountN = unitPrice * quantity - total;
return new Discount(product, "2 for " + amount, -discountN);
}

private Discount CalculateTenPercentDiscount(Product product, double quantity, double unitPrice, double percentage)
{
return new Discount(product, percentage + "% off", -quantity * unitPrice * percentage / 100.0);
}

private Discount CalculateFiveForAmountDiscount(Product product, double quantity, double unitPrice, double amount)
{
var quantityAsInt = (int) quantity;
if (quantityAsInt < 5)
{
return null;
}

var numberOfFives = quantityAsInt / 5;
var discountTotal = unitPrice * quantity - (amount * numberOfFives + quantityAsInt % 5 * unitPrice);
return new Discount(product, "5 for " + amount, -discountTotal);
}
}
}
85 changes: 57 additions & 28 deletions csharp/SupermarketReceipt/ShoppingCart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,48 +40,77 @@ public void HandleOffers(Receipt receipt, Dictionary<Product, Offer> offers, Sup
foreach (var p in _productQuantities.Keys)
{
var quantity = _productQuantities[p];
var quantityAsInt = (int) quantity;
if (offers.ContainsKey(p))
{
var offer = offers[p];
var unitPrice = catalog.GetUnitPrice(p);
Discount discount = null;
var x = 1;
if (offer.OfferType == SpecialOfferType.ThreeForTwo)
{
x = 3;
}
else if (offer.OfferType == SpecialOfferType.TwoForAmount)
{
x = 2;
if (quantityAsInt >= 2)
{
var total = offer.Argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice;
var discountN = unitPrice * quantity - total;
discount = new Discount(p, "2 for " + PrintPrice(offer.Argument), -discountN);
}
}

if (offer.OfferType == SpecialOfferType.FiveForAmount) x = 5;
var numberOfXs = quantityAsInt / x;
if (offer.OfferType == SpecialOfferType.ThreeForTwo && quantityAsInt > 2)
{
var discountAmount = quantity * unitPrice - (numberOfXs * 2 * unitPrice + quantityAsInt % 3 * unitPrice);
discount = new Discount(p, "3 for 2", -discountAmount);
}

if (offer.OfferType == SpecialOfferType.TenPercentDiscount) discount = new Discount(p, offer.Argument + "% off", -quantity * unitPrice * offer.Argument / 100.0);
if (offer.OfferType == SpecialOfferType.FiveForAmount && quantityAsInt >= 5)
switch (offer.OfferType)
{
var discountTotal = unitPrice * quantity - (offer.Argument * numberOfXs + quantityAsInt % 5 * unitPrice);
discount = new Discount(p, x + " for " + PrintPrice(offer.Argument), -discountTotal);
case SpecialOfferType.ThreeForTwo:
discount = CalculateThreeForTwoDiscount(p, quantity, unitPrice);
break;
case SpecialOfferType.TwoForAmount:
discount = CalculateTwoForAmountDiscount(p, quantity, unitPrice, offer.Argument);
break;
case SpecialOfferType.TenPercentDiscount:
discount = CalculateTenPercentDiscount(p, quantity, unitPrice, offer.Argument);
break;
case SpecialOfferType.FiveForAmount:
discount = CalculateFiveForAmountDiscount(p, quantity, unitPrice, offer.Argument);
break;
}

if (discount != null)
receipt.AddDiscount(discount);
}
}
}

private Discount CalculateThreeForTwoDiscount(Product product, double quantity, double unitPrice)
{
var quantityAsInt = (int) quantity;
if (quantityAsInt <= 2)
{
return null;
}

var numberOfThrees = quantityAsInt / 3;
var discountAmount = quantity * unitPrice - (numberOfThrees * 2 * unitPrice + quantityAsInt % 3 * unitPrice);
return new Discount(product, "3 for 2", -discountAmount);
}

private Discount CalculateTwoForAmountDiscount(Product product, double quantity, double unitPrice, double amount)
{
var quantityAsInt = (int) quantity;
if (quantityAsInt < 2)
{
return null;
}

var total = amount * (quantityAsInt / 2) + quantityAsInt % 2 * unitPrice;
var discountN = unitPrice * quantity - total;
return new Discount(product, "2 for " + PrintPrice(amount), -discountN);
}

private Discount CalculateTenPercentDiscount(Product product, double quantity, double unitPrice, double percentage)
{
return new Discount(product, percentage + "% off", -quantity * unitPrice * percentage / 100.0);
}

private Discount CalculateFiveForAmountDiscount(Product product, double quantity, double unitPrice, double amount)
{
var quantityAsInt = (int) quantity;
if (quantityAsInt < 5)
{
return null;
}

var numberOfFives = quantityAsInt / 5;
var discountTotal = unitPrice * quantity - (amount * numberOfFives + quantityAsInt % 5 * unitPrice);
return new Discount(product, "5 for " + PrintPrice(amount), -discountTotal);
}

private string PrintPrice(double price)
{
Expand Down
4 changes: 2 additions & 2 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>22</source>
<target>22</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
Expand Down
Loading