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;
}
//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."
So that's all for version 2! It's pretty easy right? So, good luck!
.png)
Komentar
Posting Komentar