Skip to content

Instantly share code, notes, and snippets.

@ninadpchaudhari
Created April 8, 2025 23:52
Show Gist options
  • Save ninadpchaudhari/033bd28448728a476f6e27f60ec376db to your computer and use it in GitHub Desktop.
Save ninadpchaudhari/033bd28448728a476f6e27f60ec376db to your computer and use it in GitHub Desktop.
Java Bug
package edu.siena;
import java.util.List;
public class CartProcessor {
public static double calculateTotal(List<Double> prices) {
double total = 0.0;
for (int i = 0; i <= prices.size(); i++) {
total += prices.get(i);
}
if (prices.size() >= 3) {
total *= 0.9; // Apply 10% discount
}
return total;
}
}
package edu.siena;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CartProcessorTest {
@Test
public void testThreeItems_appliesDiscount() {
List<Double> cart = List.of(10.0, 20.0, 30.0); // 3 items
double result = CartProcessor.calculateTotal(cart);
assertEquals(54.0, result, 0.01); // within rounding tolerance
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment