-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathPriceCalculations.java
More file actions
41 lines (31 loc) · 1.33 KB
/
PriceCalculations.java
File metadata and controls
41 lines (31 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.codedifferently.ReceiptApp;
public class PriceCalculations {
//sum list of doubles
public double subtotal(double[] prices){
double subtotal = 0.0;
for (int i=0; i<prices.length; i++){
subtotal = subtotal + prices[i];
}
return subtotal;
}
//calculate cost of tax
public double tax(double billPrice, double taxPercent){
return billPrice*(taxPercent/100);
}
//get cost after factoring in a fee or discount. cannot be charged a fee and discount at the same time by default
public double extraValues(double subTotal, boolean discount, double fee, double discountPercent){
if (discount){
return subTotal - (subTotal* discountPercent/100) ;
}else{
return subTotal + fee;
}
}
//sum tax and values after factoring in discount, fees and taxes
public double finalTotal(double subtotal, boolean discount, double fee, double taxPercent, double discountPercent){
//round
double subtotalPostFee = extraValues(subtotal, discount, fee, discountPercent);
double taxValue = tax(subtotalPostFee, taxPercent);
//multiply value of math.round by 100.0, then divide by 100.0 to get rounded value at 2 decimals
return Math.round((subtotalPostFee + taxValue) * 100.0) / 100.0;
}
}