Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the solution to Apply Discount Every N Orders #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Java/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1676076077872</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
40 changes: 40 additions & 0 deletions Java/src/ApplyDiscountEveryNOrders.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer, Integer> 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);
*/