Apply Discount Every n Orders LeetCode solution in Java

1
class Cashier {
2
private final int[] prices;
3
private final int n;
4
private final int discount;
5
private int customerNumber;
6

7
public Cashier(int n, int discount, int[] products, int[] prices) {
8
this.prices = new int[200];
9

10
for (int i = 0; i < products.length; ++i) this.prices[products[i] - 1] = prices[i];
11

12
this.n = n;
13
this.discount = discount;
14
this.customerNumber = 1;
15
}
16

17
public double getBill(int[] product, int[] amount) {
18
double sum = 0;
19

20
for (int i = 0; i < product.length; ++i) sum += this.prices[product[i] - 1] * amount[i];
21

22
if (this.customerNumber != 0 && this.customerNumber % n == 0)
23
sum *= (double) (100 - this.discount) / 100;
24

25
this.customerNumber++;
26

27
return sum;
28
}
29
}
30

31
/**
32
* Your Cashier object will be instantiated and called as such: Cashier obj = new Cashier(n,
33
* discount, products, prices); double param_1 = obj.getBill(product,amount);
34
*/

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0