/* * Payable.java * Any class that can produce a bill amount implements this. *public interface Payable { * double gst = 0.25; // interface fields are implicitly *public static final * double salestax = 0.20; * void generateInvoice(); // interface methods are *implicitly public abstract *} */ /* * TestInterfaces.java * --------------------------------------------------------------- * Simple test cases for: * - SortByPurchaseAmt implementing Comparator * - PreferredCustomer implementing Comparable (by billAmt) * - PreferredCustomer implementing Payable (generateInvoice() -> billAmt) * * HOW TO USE: * javac *.java * java TestInterfaces * --------------------------------------------------------------- */ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; public class TestInterfaces { static int total = 0; static int passed = 0; static void check(String description, double actual, double expected) { total++; if (Math.abs(actual - expected) < 0.01) { passed++; System.out.println("[PASS] " + description + " (got " + actual + ")"); } else { System.out.println("[FAIL] " + description + " -> expected " + expected + ", got " + actual); } } static void check(String description, boolean condition) { total++; if (condition) { passed++; System.out.println("[PASS] " + description); } else { System.out.println("[FAIL] " + description); } } static void section(String name) { System.out.println("\n-- " + name + " --"); } public static void main(String[] args) { section("SortByPurchaseAmt: Comparator on an ArrayList"); ArrayList customers = new ArrayList<>(); Customer c1 = new Customer("Asha", "Rao", "asha.rao@example.com", 101); c1.purchase(1200.0); Customer c2 = new Customer("Vikram", "Iyer", "vikram.iyer@example.com", 102); c2.purchase(300.0); Customer c3 = new Customer("Meera", "Nair", "meera.nair@example.com", 103); c3.purchase(4500.0); customers.add(c1); customers.add(c2); customers.add(c3); Collections.sort(customers, new SortByPurchaseAmt()); check("first customer after sort has the smallest purchaseAmt (Vikram, 300)", customers.get(0).getFirstName().equals("Vikram")); check("second customer after sort is Asha (1200)", customers.get(1).getFirstName().equals("Asha")); check("third customer after sort has the largest purchaseAmt (Meera, 4500)", customers.get(2).getFirstName().equals("Meera")); boolean ascending = true; for (int i = 0; i < customers.size() - 1; i++) { if (customers.get(i).getPurchaseAmt() > customers.get(i + 1).getPurchaseAmt()) { ascending = false; break; } } check("full list is in ascending purchaseAmt order", ascending); section("Payable: generateInvoice() computes billAmt correctly"); // purchaseAmt = 3200 -> discountLevel 3 (15%) -> findTotalAmt = 2720.0 // billAmt = 2720 + 2720*0.25 + 2720*0.20 = 2720 * 1.45 = 3944.0 PreferredCustomer p1 = new PreferredCustomer("Rohit", "Verma", "rohit.verma@example.com", 304); p1.purchase(3200.0); p1.setDiscountLevel(); double expectedTotal1 = p1.findTotalAmt(); p1.generateInvoice(); check("billAmt = findTotalAmt + gst(25%) + salestax(20%) [level 3 case]", p1.getBillAmt(), expectedTotal1 * 1.45); check("billAmt for purchaseAmt 3200 is exactly 3944.0", p1.getBillAmt(), 3944.0); // purchaseAmt = 300 -> discountLevel 0 (0%) -> findTotalAmt = 300.0 // billAmt = 300 * 1.45 = 435.0 PreferredCustomer p2 = new PreferredCustomer("Meera", "Nair", "meera.nair2@example.com", 305); p2.purchase(300.0); p2.setDiscountLevel(); p2.generateInvoice(); check("billAmt for a non-eligible (level 0) customer is purchaseAmt * 1.45", p2.getBillAmt(), 435.0); section("Comparable: natural ordering by billAmt, on an array"); // purchaseAmt = 6000 -> discountLevel 5 (25%) -> findTotalAmt = 4500.0 -> billAmt = 6525.0 PreferredCustomer p3 = new PreferredCustomer("Arjun", "Bose", "arjun.bose@example.com", 306); p3.purchase(6000.0); p3.setDiscountLevel(); p3.generateInvoice(); PreferredCustomer[] preferredArray = { p3, p1, p2 }; // deliberately out of order: billAmt 6525, 3944, 435 Arrays.sort(preferredArray); // uses compareTo() since no Comparator is passed check("first element after sort has the smallest billAmt (Meera, 435.0)", preferredArray[0].getFirstName().equals("Meera")); check("second element after sort is Rohit (3944.0)", preferredArray[1].getFirstName().equals("Rohit")); check("third element after sort has the largest billAmt (Arjun, 6525.0)", preferredArray[2].getFirstName().equals("Arjun")); boolean billAscending = true; for (int i = 0; i < preferredArray.length - 1; i++) { if (preferredArray[i].getBillAmt() > preferredArray[i + 1].getBillAmt()) { billAscending = false; break; } } check("full array is in ascending billAmt order", billAscending); System.out.println("\n============================"); System.out.println("Total: " + total + " Passed: " + passed + " Failed: " + (total - passed)); System.out.println("============================"); } }