3
public class OrderCalculator (
public double calculateTotalPrice (double[] itemPrices, String customerType, boolean isVIP, String discountCode) {
if (itemPrices null || itemPrices.length) {
throw new IllegalArgumentException("No items in the order.");
double totalPrice = 0.0;
for (double price itemPrices) ( if (price <0){
throw new IllegalArgumentException("Iten price must be greater than zero.");
totalPrice price;
double discount = 0.0;
discount 0.20; // 20% discount for VIP customers
} else if (customerType.equalsIgnoreCase("Regular")) {
10
11
12
13
14
15
日
if (isVIP) (
16
17
18
19
20
21
22
23
24
25
discount 0.05; // 5% discount for regular customers
if (discountCode != null && !discountCode.isEmpty()) {
if (discountCode.equals("SALE10")) {
discount+= 0.10; // Additional 10% discount for SALE10 code
) else if (discountCode.equals("WELCOMES")) ( discount + 0.05; // Additional 5% discount for WELCOMES code
double finalPrice totalPrice (1 discount):
return finalPrice <? finalPrice; // Ensuring price does not go below zero
Sample JUnit Test Class for Completion
The following is a partially completed JUnit test class for the OrderCalculator method.Students are required to complete this class by adding the necessary test cases to achieve full equivalence partitioning, boundary value analysis, and branch coverage for the
calculate TotalPrice method.
35 public class OrderCalculatorTest {
Test (expected IllegalArgumentException.class)
OrderCalculator calculator new OrderCalculator();
calculator.calculateTotalPrice (new double()(), "VIP", false, null); // No itens in order
36
37
38
public void testNoltensInOrder() {
39
40
41
}
42
43
87est
44
public void testVIPCustomerWithNoDiscountCode() {
45
46
OrderCalculator calculator new OrderCalculator();
double total calculator.calculateTotalPrice (new double[] (100, 200), "VIP", true, null); // Branch 1: VIP customer assertEquals (240.0, total, 0.01); // 20% discount
48
Zoom
+ 100%
Close