/* * TestInheritance.java * --------------------------------------------------------------- * Simple test cases for the Person / Customer / PreferredCustomer * inheritance exercise. No external libraries needed (no JUnit) -- * just plain Java, so it compiles and runs anywhere. * * HOW TO USE: * 1. Put this file in the same folder as your Person.java, * Customer.java, and PreferredCustomer.java * 2. Compile everything together: * javac *.java * 3. Run the tests: * java TestInheritance * 4. Read the PASS/FAIL lines. A summary is printed at the end. * --------------------------------------------------------------- */ public class TestInheritance { static int total = 0; static int passed = 0; // Compares two doubles allowing a tiny rounding error 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, int actual, int expected) { total++; if (actual == expected) { passed++; System.out.println("[PASS] " + description + " (got " + actual + ")"); } else { System.out.println("[FAIL] " + description + " -> expected " + expected + ", got " + actual); } } static void check(String description, boolean actual, boolean expected) { total++; if (actual == expected) { passed++; System.out.println("[PASS] " + description + " (got " + actual + ")"); } else { System.out.println("[FAIL] " + description + " -> expected " + expected + ", got " + actual); } } static void check(String description, String actual, String expected) { total++; if (actual != null && actual.equals(expected)) { passed++; System.out.println("[PASS] " + description + " (got \"" + actual + "\")"); } else { System.out.println("[FAIL] " + description + " -> expected \"" + expected + "\", got \"" + actual + "\""); } } static void section(String name) { System.out.println("\n-- " + name + " --"); } public static void main(String[] args) { section("Person basics (via Customer, since Person is abstract)"); Customer c0 = new Customer("Asha", "Rao", "asha.rao@example.com", 101); check("getFirstName() returns what the constructor set", c0.getFirstName(), "Asha"); check("getLastName() returns what the constructor set", c0.getLastName(), "Rao"); check("getEmailId() returns the valid email set at construction", c0.getEmailId(), "asha.rao@example.com"); section("validateEmailId()"); check("a normal email is valid", c0.validateEmailId("test@example.com"), true); check("an email with no '@' is invalid", c0.validateEmailId("testexample.com"), false); check("an email with no '.' after '@' is invalid", c0.validateEmailId("test@examplecom"), false); check("an email with a space is invalid", c0.validateEmailId("test @example.com"), false); check("an empty string is invalid", c0.validateEmailId(""), false); section("Customer.setEmailId() rejects invalid input"); c0.setEmailId("not-a-valid-email"); check("email is unchanged after an invalid setEmailId() call", c0.getEmailId(), "asha.rao@example.com"); c0.setEmailId("asha.new@example.com"); check("email is updated after a valid setEmailId() call", c0.getEmailId(), "asha.new@example.com"); section("Customer fields and purchase()"); Customer c1 = new Customer("Vikram", "Iyer", "vikram.iyer@example.com", 202); check("custId is set correctly by the constructor", c1.getCustId(), 202); check("purchaseAmt starts at 0", c1.getPurchaseAmt(), 0.0); c1.purchase(1000.0); check("purchaseAmt after one purchase()", c1.getPurchaseAmt(), 1000.0); c1.purchase(500.0); check("purchaseAmt accumulates across multiple purchase() calls", c1.getPurchaseAmt(), 1500.0); section("PreferredCustomer discount levels (based on purchaseAmt)"); PreferredCustomer p1 = new PreferredCustomer("Meera", "Nair", "meera.nair@example.com", 301); p1.purchase(300.0); p1.setDiscountLevel(); check("purchaseAmt = 300 (<= 500) -> discountLevel 0", p1.getDiscountLevel(), 0); PreferredCustomer p2 = new PreferredCustomer("Karan", "Shah", "karan.shah@example.com", 302); p2.purchase(1000.0); p2.setDiscountLevel(); check("purchaseAmt = 1000 (500-1500) -> discountLevel 1", p2.getDiscountLevel(), 1); PreferredCustomer p3 = new PreferredCustomer("Divya", "Menon", "divya.menon@example.com", 303); p3.purchase(2000.0); p3.setDiscountLevel(); check("purchaseAmt = 2000 (1500-2500) -> discountLevel 2", p3.getDiscountLevel(), 2); PreferredCustomer p4 = new PreferredCustomer("Rohit", "Verma", "rohit.verma@example.com", 304); p4.purchase(3200.0); p4.setDiscountLevel(); check("purchaseAmt = 3200 (2500-3500) -> discountLevel 3", p4.getDiscountLevel(), 3); PreferredCustomer p5 = new PreferredCustomer("Sneha", "Pillai", "sneha.pillai@example.com", 305); p5.purchase(4000.0); p5.setDiscountLevel(); check("purchaseAmt = 4000 (3500-4500) -> discountLevel 4", p5.getDiscountLevel(), 4); PreferredCustomer p6 = new PreferredCustomer("Arjun", "Bose", "arjun.bose@example.com", 306); p6.purchase(6000.0); p6.setDiscountLevel(); check("purchaseAmt = 6000 (> 4500) -> discountLevel 5", p6.getDiscountLevel(), 5); section("PreferredCustomer.calculateDiscount() and findTotalAmt()"); // purchaseAmt = 3200 -> level 3 -> 15% discount -> discount amount 480.0, total 2720.0 check("calculateDiscount() for level 3 (15% of 3200)", p4.calculateDiscount(), 480.0); check("findTotalAmt() = purchaseAmt - discount", p4.findTotalAmt(), 2720.0); // purchaseAmt = 6000 -> level 5 -> 25% discount -> discount amount 1500.0, total 4500.0 check("calculateDiscount() for level 5 (25% of 6000)", p6.calculateDiscount(), 1500.0); check("findTotalAmt() = purchaseAmt - discount", p6.findTotalAmt(), 4500.0); // purchaseAmt = 300 -> level 0 -> 0% discount -> discount amount 0.0, total unchanged check("calculateDiscount() for level 0 (not eligible)", p1.calculateDiscount(), 0.0); check("findTotalAmt() equals purchaseAmt when discount is 0", p1.findTotalAmt(), 300.0); section("PreferredCustomer inherits Customer/Person behaviour"); check("getFirstName() works on a PreferredCustomer (inherited from Person)", p4.getFirstName(), "Rohit"); p4.setEmailId("bad-email"); check("PreferredCustomer.setEmailId() also rejects invalid input", p4.getEmailId(), "rohit.verma@example.com"); System.out.println("\n============================"); System.out.println("Total: " + total + " Passed: " + passed + " Failed: " + (total - passed)); System.out.println("============================"); } }