From 7774d61d30b5e7f356f528d44ca519cbce66c134 Mon Sep 17 00:00:00 2001 From: aadrummond261 Date: Mon, 9 Mar 2026 22:39:31 -0400 Subject: [PATCH] Made a test class and tested code --- src/main/java/amanidrummond/CarRental.java | 83 ++++++++++++ .../java/amanidrummond/test/CarRental.java | 127 ++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 src/main/java/amanidrummond/CarRental.java create mode 100644 src/test/java/amanidrummond/test/CarRental.java diff --git a/src/main/java/amanidrummond/CarRental.java b/src/main/java/amanidrummond/CarRental.java new file mode 100644 index 0000000..07b130b --- /dev/null +++ b/src/main/java/amanidrummond/CarRental.java @@ -0,0 +1,83 @@ +package amanidrummond; + +public class CarRental { + + private String customerName; + private String carModel; + private int rentalDays; + private double dailyRate; + private boolean luxury; + + public CarRental(String customerName, String carModel, int rentalDays, double dailyRate, boolean luxury) { + if (rentalDays <= 0) { + throw new IllegalArgumentException("Rental days must be greater than 0"); + } + if (dailyRate <= 0) { + throw new IllegalArgumentException("Daily rate must be greater than 0"); + } + + this.customerName = customerName; + this.carModel = carModel; + this.rentalDays = rentalDays; + this.dailyRate = dailyRate; + this.luxury = luxury; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + + public String getCarModel() { + return carModel; + } + + public void setCarModel(String carModel) { + this.carModel = carModel; + } + + public int getRentalDays() { + return rentalDays; + } + + public void setRentalDays(int rentalDays) { + if (rentalDays <= 0) { + throw new IllegalArgumentException("Rental days must be greater than 0"); + } + this.rentalDays = rentalDays; + } + + public double getDailyRate() { + return dailyRate; + } + + public void setDailyRate(double dailyRate) { + if (dailyRate <= 0) { + throw new IllegalArgumentException("Daily rate must be greater than 0"); + } + this.dailyRate = dailyRate; + } + + public boolean isLuxury() { + return luxury; + } + + public void setLuxury(boolean luxury) { + this.luxury = luxury; + } + + public double calculateTotalCost() { + double total = rentalDays * dailyRate; + if (luxury) { + total += 100; + } + return total; + } + + public boolean qualifiesForDiscount() { + return rentalDays >= 7; + } +} diff --git a/src/test/java/amanidrummond/test/CarRental.java b/src/test/java/amanidrummond/test/CarRental.java new file mode 100644 index 0000000..8575225 --- /dev/null +++ b/src/test/java/amanidrummond/test/CarRental.java @@ -0,0 +1,127 @@ +package amanidrummond.test; + +import amanidrummond.CarRental; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class CarRentalTest { + + @Test + void constructorHappyPath() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 5, 40.0, false); + + // Act / Assert + assertEquals("Alex", rental.getCustomerName()); + assertEquals("Toyota Camry", rental.getCarModel()); + assertEquals(5, rental.getRentalDays()); + assertEquals(40.0, rental.getDailyRate()); + assertFalse(rental.isLuxury()); + } + + @Test + void constructorThrowsWhenRentalDaysIsZero() { + // Arrange / Act / Assert + assertThrows(IllegalArgumentException.class, () -> { + new CarRental("Alex", "Toyota Camry", 0, 40.0, false); + }); + } + + @Test + void constructorThrowsWhenDailyRateIsZero() { + // Arrange / Act / Assert + assertThrows(IllegalArgumentException.class, () -> { + new CarRental("Alex", "Toyota Camry", 5, 0, false); + }); + } + + @Test + void settersAndGettersWork() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 5, 40.0, false); + + // Act + rental.setCustomerName("Jordan"); + rental.setCarModel("Honda Civic"); + rental.setRentalDays(10); + rental.setDailyRate(55.0); + rental.setLuxury(true); + + // Assert + assertEquals("Jordan", rental.getCustomerName()); + assertEquals("Honda Civic", rental.getCarModel()); + assertEquals(10, rental.getRentalDays()); + assertEquals(55.0, rental.getDailyRate()); + assertTrue(rental.isLuxury()); + } + + @Test + void setRentalDaysThrowsForNegativeValue() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 5, 40.0, false); + + // Act / Assert + assertThrows(IllegalArgumentException.class, () -> { + rental.setRentalDays(-2); + }); + } + + @Test + void setDailyRateThrowsForNegativeValue() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 5, 40.0, false); + + // Act / Assert + assertThrows(IllegalArgumentException.class, () -> { + rental.setDailyRate(-10.0); + }); + } + + @Test + void calculateTotalCostWithoutLuxuryFee() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 5, 40.0, false); + + // Act + double total = rental.calculateTotalCost(); + + // Assert + assertEquals(200.0, total); + } + + @Test + void calculateTotalCostWithLuxuryFee() { + // Arrange + CarRental rental = new CarRental("Alex", "BMW 7 Series", 3, 100.0, true); + + // Act + double total = rental.calculateTotalCost(); + + // Assert + assertEquals(400.0, total); + } + + @Test + void qualifiesForDiscountReturnsTrueWhenRentalDaysIsSevenOrMore() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 7, 40.0, false); + + // Act + boolean result = rental.qualifiesForDiscount(); + + // Assert + assertTrue(result); + } + + @Test + void qualifiesForDiscountReturnsFalseWhenRentalDaysIsLessThanSeven() { + // Arrange + CarRental rental = new CarRental("Alex", "Toyota Camry", 3, 40.0, false); + + // Act + boolean result = rental.qualifiesForDiscount(); + + // Assert + assertFalse(result); + } +}