# Tutorial 3 tags: software systems, se time: 2025-12-06 07:18:10 ```java public class RentalService { // A "God Method" handling everything public String processRental(String type, int age, String license, String street, String city, String zip, double basePrice) { System.out.println("Processing rental for: " + type); // 1. Validation Logic (Mixed levels of abstraction) if (license == null || license.isEmpty()) { return "Error: No License"; } if (age < 18) { return "Error: Underage"; } // 2. Pricing Logic (Switch Statements - OO Abuser) double finalPrice = 0; if (type.equals("E-Bike")) { finalPrice = basePrice * 1.1; // 10% tax if (age < 25) { finalPrice += 5.0; // Insurance surcharge } } else if (type.equals("Scooter")) { finalPrice = basePrice * 1.2; if (age < 21) { return "Error: Too young for Scooter"; } } else if (type.equals("StandardBike")) { finalPrice = basePrice; } else { return "Error: Invalid Type"; } // 3. Receipt Generation (Feature Envy / Primitive Obsession) // Why is RentalService formatting the address? String addressLabel = street + "\n" + city + ", " + zip; String receipt = "RENTAL CONFIRMED\n" + addressLabel + "\nPrice: " + finalPrice; return receipt; } } ``` ```java // Extracted Method private double calculatePrice(String type, int age, double basePrice) { if (type.equals("E-Bike")) { return (basePrice * 1.1) + (age < 25 ? 5.0 : 0); } // ... rest of logic return basePrice; } ``` ```java // New Abstraction public class CustomerAddress { private String street; private String city; private String zip; // Logic moved here (Fixing Feature Envy) public String getFormattedLabel() { return street + "\n" + city + ", " + zip; } } ``` ```java public class RentalService { // Complexity dropped to 2 (Base + 1 validation check) public String processRental(String type, int age, String license, CustomerAddress addr, double basePrice) { if (!isValidDriver(license, age, type)) { return "Error: Validation Failed"; } double price = calculatePrice(type, age, basePrice); // Delegating address formatting to the Address object return "CONFIRMED\n" + addr.getFormattedLabel() + "\nPrice: " + price; } } ```