Lab Assignment 2 023
Lab Assignment 2 023
// Constructor
public LibraryBookRental(String title, String author, int rentalDuration,
String rentalID, String bookType) {
this.title = title;
this.author = author;
this.rentalDuration = rentalDuration;
this.rentalID = rentalID;
this.bookType = bookType;
this.rentalCost = calculateRentalCost();
}
// Main method
public static void main(String[] args) {
}
}
Test File:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@Test
void testNewReleaseBookDuration() {
LibraryBookRental book = new LibraryBookRental("Book C", "Author Z",
3, "R102", "New Release");
assertEquals(10, book.calculateRentalCost());
}
@Test
void testInvalidBookType() {
LibraryBookRental book = new LibraryBookRental("Book E", "Author V",
5, "R104", "InvalidType");
assertEquals(0, book.calculateRentalCost());
}
@Test
public void testGetRentalDetails() {
LibraryBookRental book = new LibraryBookRental("Alice in wonderland",
"Maaz Anwar", 8, "R003", "Regular");
String expectedDetails = "Title: Alice in wonderland\n" +
"Author: Maaz Anwar\n" +
"Rental Duration:8 days\n" +
"Rental ID: R003\n" +
"Book Type: Regular\n" +
"Rental Cost: $6.0";
assertEquals(expectedDetails, book.getRentalDetails());
}
@ParameterizedTest
@CsvSource({
"Regular, 5, 5",
"Regular, 10, 8",
"New Release, 2, 10",
"New Release, 5, 16",
"InvalidType, 5, 0"
})
void testCalculateRentalCost(String bookType, int rentalDuration, int
expectedCost) {
LibraryBookRental book = new LibraryBookRental("Gangs of waseypur",
"sharma", rentalDuration, "HP008",
bookType);
assertEquals(expectedCost, book.calculateRentalCost());
}
}
OUTPUT:
Question 2: Create a class named RideSharingService that manages information
about rides in a ride sharing application
// Constructor
public RideSharingService(String ridetype, int distance, String id, String
destination) {
this.ridetype = ridetype;
this.distanceInKm = distance;
this.rideID = id;
this.destination = destination;
this.fare = calculateFare();
}
// Main method
public static void main(String[] args) {
}
}
TestFile:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;
@Test
void testCityRideAbove10Km() {
RideSharingService ride = new RideSharingService("City Ride", 15,
"R002", "Uptown");
assertEquals(25, ride.calculateFare(), "City ride of 15km should have
a fare of $25 (15 + 5 * 2)");
}
@Test
void testOutstationRideWithin50Km() {
RideSharingService ride = new RideSharingService("Outstation Ride",
50, "R003", "Neighboring City");
assertEquals(100, ride.calculateFare(), "Outstation ride within 50km
should have a fare of $100");
}
@Test
void testOutstationRideAbove50Km() {
RideSharingService ride = new RideSharingService("Outstation Ride",
60, "R004", "Distant City");
assertEquals(150, ride.calculateFare(), "Outstation ride of 60km
should have a fare of $150 (100 + 10 * 5)");
}
@Test
void testInvalidRideType() {
RideSharingService ride = new RideSharingService("Unknown Ride", 30,
"R005", "Nowhere");
assertEquals(0, ride.calculateFare(), "Invalid ride type should return
fare of $0");
}
@Test
void testGetRideDetails() {
RideSharingService ride = new RideSharingService("City Ride", 8,
"R006", "Karachi");
String expectedDetails = "Ride Type: City Ride\nDiatance In Km:
8\nRide Id: R006\nDestination: Karachi\nfare: $15";
assertEquals(expectedDetails, ride.getRideDetails(), "Ride details
should be correctly formatted");
}
OUTPUT: