Skip to content

Update shippingCost with class and SRP #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update shippingCost with class and SRP
  • Loading branch information
Its-Kal committed May 18, 2025
commit 2beeb888e47688969b3eadc67a11d43aa45e101d
155 changes: 85 additions & 70 deletions programming-I/05-shippingCost/shippingCost.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,92 @@
//*****************************************************************************************************
// Online Purchase Cost Calculator
//
// This program calculates the total cost of an online purchase based on the item's price and
// weight.
//
//*****************************************************************************************************

#include <iomanip>
#include <iostream>
#include <iomanip>
using namespace std;

//*****************************************************************************************************

// Class untuk menghitung biaya pengiriman
class ShippingCalculator {
public:
float calculateShipping(float weight) {
const float LESS_ONE_LB = 10,
LESS_FIVE_LB = 7,
LESS_EIGHT_LB = 5,
LESS_TEN_LB = 3,
LESS_TWENTY_LB = 2,
ABOVE_TWENTY = 1;

if (weight < 1)
return LESS_ONE_LB * weight;
else if (weight < 5)
return LESS_FIVE_LB * weight;
else if (weight < 8)
return LESS_EIGHT_LB * weight;
else if (weight < 10)
return LESS_TEN_LB * weight;
else if (weight < 20)
return LESS_TWENTY_LB * weight;
else
return ABOVE_TWENTY * weight;
}
};

// Class untuk menghitung pajak
class TaxCalculator {
public:
float calculateTax(float price) {
const float TAXRATE = 4.225 / 100;
return price * TAXRATE;
}
};

// Class untuk menghitung total biaya
class TotalCostCalculator {
public:
float calculateTotalCost(float price, float tax, float shipping) {
return price + tax + shipping;
}
};

// Class untuk menangani input pengguna
class PurchaseInput {
public:
void getInput(float &price, float &weight) {
cout << "Enter the item's price: ";
cin >> price;
cout << "Enter the item's weight: ";
cin >> weight;
}
};

// Class utama untuk mengkoordinasi proses perhitungan
class OnlinePurchaseCalculator {
public:
void calculate() {
float price, weight, tax, shipping, totalCost;

// Mengambil input
PurchaseInput input;
input.getInput(price, weight);

// Menghitung biaya pengiriman
ShippingCalculator shippingCalc;
shipping = shippingCalc.calculateShipping(weight);

// Menghitung pajak
TaxCalculator taxCalc;
tax = taxCalc.calculateTax(price);

// Menghitung total biaya
TotalCostCalculator totalCalc;
totalCost = totalCalc.calculateTotalCost(price, tax, shipping);

// Menampilkan hasil
cout << "\nTotal price: " << fixed << setprecision(2) << totalCost << endl;
}
};

// Program utama
int main() {
const float TAXRATE = 4.225 / 100,
LESS_ONE_LB = 10,
LESS_FIVE_LB = 7,
LESS_EIGHT_LB = 5,
LESS_TEN_LB = 3,
LESS_TWENTY_LB = 2,
ABOVE_TWENTY = 1;
float price,
tax,
shipping,
totalCost,
weight;

cout << "Calculate the total cost of an online purchase" << endl;

cout << "\nEnter the item's price: ";
cin >> price;

cout << "Enter the item's weight: ";
cin >> weight;

if (weight < 1)
shipping = LESS_ONE_LB * weight;
else if (weight < 5)
shipping = LESS_FIVE_LB * weight;
else if (weight < 8)
shipping = LESS_EIGHT_LB * weight;
else if (weight < 10)
shipping = LESS_TEN_LB * weight;
else if (weight < 20)
shipping = LESS_TWENTY_LB * weight;
else
shipping = ABOVE_TWENTY * weight;

tax = price * TAXRATE;
totalCost = price + tax + shipping;

cout << "\nTotal price: " << fixed << setprecision(2) << totalCost << endl;
OnlinePurchaseCalculator calculator;
calculator.calculate();

return 0;
}

//*****************************************************************************************************
/*

Calculate the total cost of an online purchase

Enter the item's price: 10
Enter the item's weight: 50

Total price: 60.42

*****************************************************************************************************

Calculate the total cost of an online purchase

Enter the item's price: 900
Enter the item's weight: 45.50

Total price: 983.53

*/