# Apply Discount Every n Orders in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Suppose there is a sale in a supermarket, there will be a discount every n customer. Consider there are some products in the supermarket where the id of the i-th product is products[i] and the price per unit of this product is prices[i]. Here the system will count the number of customers and when the n-th customer arrives he/she will have a discount on the bill. Then the system will start counting customers again. The customer orders a certain amount of each product where product[i] is the id of the i-th product the customer ordered and amount[i] is the number of units the customer ordered of that product. So we have to implement this system. The Cashier class will have these methods

• Cashier(int n, int discount, int[] products, int[] prices) This constructor is used to Initializes the object with n, the discount, the products, and their prices.

• double getBill(int[] product, int[] amount) this will return the value of the bill and apply the discount if needed. Answers within 10^-5 of the actual value will be accepted as correct.

For example, initialize Cashier using Cashier(3, 50, [1,2,3,4,5,6,7], [100,200,300,400,300,200,100]), now call getBill methods −

getBill([1,2],[1,2]), getBill([3,7],[10,10]), getBill([1,2,3,4,5,6,7],[1,1,1,1,1,1,1]), getBill([4],[10]), getBill([7,3],[10,10]), getBill([7,5,3,1,6,4,2],[10,10,10,9,9,9,7]), getBill([2,3,5],[5,3,2]), then the outputs will be [500.0, 4000.0, 800.0, 4000.0, 4000.0, 7350.0, 2500.0]

To solve this, we will follow these steps −

• Define a map called order

• Cashier will work as follows −

• curr := 0

• for i in range 0 to size of prices array

• order[produce[i]] := prices[i]

• set discount as given discount rate

• The getBill method will work as follows −

• increase curr by 1, set flag := true, if curr = n, otherwise false

• if curr = n, then set curr := 0

• ret := 0

• for i in range 0 to size of product array – 1

• x := pro[i]

• cost := order[x]

• y :=amount[i]

• increase ret by cost * y

• if flag is set, then ret := ret – (ret * discount) / 100

• return ret

## Example (C++)

Let us see the following implementation to get better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Cashier {
public:
int curr;
map <double, double> order;
int n;
int discount;
Cashier(int n, int discount, vector<int>& pro, vector<int>& p) {
curr = 0;
for(int i = 0; i < p.size(); i++){
order[pro[i]] = p[i];
}
this->n = n;
this->discount = discount;
}
double getBill(vector<int> pro, vector<int> am) {
curr++;
bool flag = curr == n;
if(curr == n){
curr = 0;
}
double ret = 0;
for(int i = 0; i < pro.size(); i++){
double x = pro[i];
double cost = order[x];
double y = am[i];
ret += (cost * y);
}
if(flag) ret = ret - (ret * discount) / 100;
return ret;
}
};
main(){
vector<int> v1 = {1,2,3,4,5,6,7}, v2 =
{100,200,300,400,300,200,100};
Cashier ob(3, 50, v1, v2);
v1 = {1,2}, v2 = {1,2};
cout << (ob.getBill(v1, v2)) << endl;
v1 = {3,7}, v2 = {10,10};
cout << (ob.getBill(v1, v2)) << endl;
v1 = {1,2,3,4,5,6,7}, v2 = {1,1,1,1,1,1,1};
cout << (ob.getBill(v1, v2)) << endl;
v1 = {4}, v2 = {10};
cout << (ob.getBill(v1, v2)) << endl;
v1 = {7,3}, v2 = {10,10};
cout << (ob.getBill(v1, v2)) << endl;
v1 = {7,5,3,1,6,4,2}, v2 = {10,10,10,9,9,9,7};
cout << (ob.getBill(v1, v2)) << endl;
v1 = {2,3,5}, v2 = {5,2,3};
cout << (ob.getBill(v1, v2)) << endl;
}

## Input

See the main function

## Output

500
4000
800
4000
4000
7350
2500
Updated on 29-Apr-2020 12:49:55