Informatics - Making Minimarket Discount Program in C++ Version 2 (week 13)

Are you ready for version two? Don't worry it's only needed a little addition and I'm sure you can do it by yourself! Your boss will be proud of you and maybe you'll get bonus? So, without any further do, let's start! 

So, the first version is only for counting the discount and the total price. But what if your minimarket wants to give a bonus item for a specific threshold? So, in second version I will show you how to add the bonus item for specific threshold. 

We start from the first code that we have made:

//program minimarket_discount_v1.cpp

#include <iostream>
#include <iomanip>

using namespace std;

int main (){

double totalPurchase, discount = 0.0, finalPrice;

cout << "Please enter the amount of purchase (in Rupiah): Rp";
cin >> totalPurchase;

if (totalPurchase >= 1000000){
    discount = 0.2;
}
else if (totalPurchase >= 500000){
    discount = 0.1;
}
else if (totalPurchase >= 200000){
    discount = 0.05;
}
else if (totalPurchase >= 100000){
    discount = 0.25;
}
    double discountAmount = totalPurchase * discount;
    finalPrice = totalPurchase - discountAmount;

cout << " --Shopping bill-- " << endl;
cout << "Total purchase: Rp" << totalPurchase << endl;
cout << "Discount: Rp" << discountAmount << endl;
cout << "Final price: Rp" << finalPrice << endl;
}

After you have your first version of the program, you only need to make addition of the bonus item below the total discount in each conditional statement. After that, don't forget to add the bonus item in the output statement so there'll be the statement of what kind of the bonus item the customer got in the output. So here the adjustment for the version 2:

//program minimarket_discount_v2.cpp

#include <iostream>
#include <iomanip>

using namespace std;

int main (){

double totalPurchase, discount = 0.0, finalPrice;
string bonusItem;

cout << "Please enter the amount of purchase: Rp";
cin >> totalPurchase;

if (totalPurchase >= 1000000){
    discount = 0.2;
    bonusItem = "a carton of eggs";
}
else if (totalPurchase >= 500000){
    discount = 0.1;
    bonusItem = "a bottle of milk";
}
else if (totalPurchase >= 200000){
    discount = 0.05;
    bonusItem = "an exclusive KPD MiniMarket pen";
}
else if (totalPurchase >= 100000){
    discount = 0.025;
    bonusItem = "sorry you need to buy more";
}

    double discountAmount = totalPurchase * discount;
    finalPrice = totalPurchase - discountAmount;

cout << " --Shopping bill-- " << endl;
cout << "Total purchase: Rp" << totalPurchase << endl;
cout << "Discount: Rp" << discountAmount << endl;
cout << "Final price: Rp" << finalPrice << endl;
cout << "Secret bonus: " << bonusItem << endl;
}

In this code, the string bonusItem is used to store and display the bonus gift that a customer gets based on their purchase amount. Since a string holds text, it allows the program to store different messages like "a carton of eggs" or "a bottle of milk" and then display them later.

When the user enters their purchase amount, the program checks the amount using if-else conditions. Depending on the amount, bonusItem is updated with a specific text value (like "a bottle of milk" for purchases above Rp 500,000). At the end of the program, the stored value of bonusItem is printed as the customer's "Secret bonus." 

Here, I'll show you the output of this program!  
As an example, I input Rp400000 as my total purchase. It will count the discount, final price, and showing what secret bonus your customer got. Here the customer got an exclusive KPD MiniMarket pen because the total purchase is Rp400000 which is more than 200000 and less than 500000.

So that's all for version 2! It's pretty easy right? So, good luck!



Komentar

Postingan populer dari blog ini

Informatics - Making a Main C++ Program to Run all the Calculator Program (week 11)

Informatics - Triangles (week 18)

Informatics - Making Minimarket Discount Program in C++ version 1 (Week 12)