diff --git a/programming-I/05-shippingCost/shippingCost.cpp b/programming-I/05-shippingCost/shippingCost.cpp index cd38960..01bd4a0 100644 --- a/programming-I/05-shippingCost/shippingCost.cpp +++ b/programming-I/05-shippingCost/shippingCost.cpp @@ -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 #include +#include 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 - -*/ \ No newline at end of file