-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCalculations.java
More file actions
46 lines (32 loc) · 1.22 KB
/
Calculations.java
File metadata and controls
46 lines (32 loc) · 1.22 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
42
43
package org.codedifferently;
import java.util.Random;
public class Calculations {
//build methods
Random random = new Random();
public double generateSubtotal(double price, double price2, double price3) {
//builds subtotal for 3 random prices
return Math.round((price + price2 + price3) * 100) / 100;
}
//calculate tax
public double calculateTax(double subtotal, double tax1) {
return Math.round(subtotal * tax1 * 100.0) / 100.0;
}
//apply discount
public double applyDiscount(double subtotal, double discountRate, double minAmount) {
if (subtotal >= minAmount) {
double discount = subtotal * discountRate;
return Math.round((subtotal - discount) * 100.0) / 100.0;
}
return subtotal;
}
public double calculateFinalTotal(
double subtotal,
double discountRate,
double minAmount,
double taxRate
) {
double discountedSubtotal = applyDiscount(subtotal, discountRate, minAmount);
double tax = calculateTax(discountedSubtotal, taxRate);
return Math.round((discountedSubtotal + tax) * 100.0) / 100.0;
}
}