From c9c016c8a350d04619b767a14a6c2db53c05c735 Mon Sep 17 00:00:00 2001
From: Damian <92489118+BuzzardDev@users.noreply.github.com>
Date: Fri, 10 Feb 2023 16:47:16 -0800
Subject: [PATCH] Adds the solution to Apply Discount Every N Orders
---
Java/.project | 11 +++++++
Java/src/ApplyDiscountEveryNOrders.java | 40 +++++++++++++++++++++++++
2 files changed, 51 insertions(+)
create mode 100644 Java/src/ApplyDiscountEveryNOrders.java
diff --git a/Java/.project b/Java/.project
index 4fdb69a93e..f8689af274 100644
--- a/Java/.project
+++ b/Java/.project
@@ -14,4 +14,15 @@
org.eclipse.jdt.core.javanature
+
+
+ 1676076077872
+
+ 30
+
+ org.eclipse.core.resources.regexFilterMatcher
+ node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__
+
+
+
diff --git a/Java/src/ApplyDiscountEveryNOrders.java b/Java/src/ApplyDiscountEveryNOrders.java
new file mode 100644
index 0000000000..d4c6b834ef
--- /dev/null
+++ b/Java/src/ApplyDiscountEveryNOrders.java
@@ -0,0 +1,40 @@
+import java.util.HashMap;
+import java.util.Map;
+
+class ApplyDiscountEveryNOrders {
+ private int n;
+ private int discount;
+ private int curr;
+ private Map lookup;
+ public ApplyDiscountEveryNOrders(int n, int discount, int[] products, int[] prices) {
+ this.n = n;
+ this.discount = discount;
+ this.curr = 0;
+ this.lookup = new HashMap<>();
+
+ // Map lookup = new HashMap<>();
+ for (int i = 0; i < products.length; i++) {
+ lookup.put(products[i], prices[i]);
+ }
+
+ }
+
+ public double getBill(int[] product, int[] amount) {
+ this.curr = (this.curr+1) % this.n;
+ double result = 0.0;
+ for (int i = 0; i < product.length; i++) {
+ int p = product[i];
+ Integer pp = this.lookup.get(p);
+ if(pp != null){
+ result += pp * amount[i];
+ }
+ }
+ return result * (curr == 0 ? (1.0 - (double)discount/100.0) : 1.0);
+ }
+}
+
+/**
+ * Your Cashier object will be instantiated and called as such:
+ * Cashier obj = new Cashier(n, discount, products, prices);
+ * double param_1 = obj.getBill(product,amount);
+ */
\ No newline at end of file