0% found this document useful (0 votes)
2 views8 pages

Lab Assignment 2 023

The document contains two Java class implementations: LibraryBookRental and RideSharingService, each with methods for calculating costs and retrieving details. The LibraryBookRental class manages book rentals, while the RideSharingService class handles ride-sharing information. Both classes include corresponding test files with unit tests to validate their functionality.

Uploaded by

Maaz Anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Lab Assignment 2 023

The document contains two Java class implementations: LibraryBookRental and RideSharingService, each with methods for calculating costs and retrieving details. The LibraryBookRental class manages book rentals, while the RideSharingService class handles ride-sharing information. Both classes include corresponding test files with unit tests to validate their functionality.

Uploaded by

Maaz Anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB ASSIGNMENT#2

SUBMITTED BY:MAAZ ANWAR


REGISTRATION # SP22-BSE-023
Date:28-03-25
Question 1: Create a class named LibraryBookRental that stores information
about book rentals in a library.

public class LibraryBookRental {


private String title;
private String author;
private int rentalDuration;
private String rentalID;
private String bookType;
private double rentalCost;

// 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();
}

// Method to calculate rental cost


public double calculateRentalCost() {
if (bookType.equalsIgnoreCase("Regular")) {
return (rentalDuration <= 7) ? 5.0
: 5.0 + (rentalDuration - 7) *
1.0;
} else if (bookType.equalsIgnoreCase("New Release")) {
return (rentalDuration <= 3) ? 10.0
: 10.0 + (rentalDuration - 3)
* 3.0;
}
return 0.0; // Default case (invalid book type)
}

// Method to get rental details


public String getRentalDetails() {
return "Title: " + title + "\nAuthor: " + author + "\nRental
Duration:" + rentalDuration + " days" +
"\nRental ID: " + rentalID + "\nBook Type: " + bookType +
"\nRental Cost: $" + rentalCost;
}

// 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;

public class LibraryBookRentalTest {


@Test
void testRegularBookDuration() {
LibraryBookRental book = new LibraryBookRental("Book B", "Author Y",
10, "R101", "Regular");
assertEquals(8, book.calculateRentalCost());
}

@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

public class RideSharingService {

private String ridetype;


private int distanceInKm;
private String rideID;
private String destination;
private int fare;

// 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();
}

// calculate fare public method


public int calculateFare() {
if (ridetype.equalsIgnoreCase("city ride")) {
return (distanceInKm <= 10) ? 15 : 15 + (distanceInKm - 10) * 2;
} else if (ridetype.equalsIgnoreCase("outstation Ride")) {
return (distanceInKm <= 50) ? 100 : 100 + (distanceInKm - 50) * 5;
} else {
}
return 0;
}

// print details method


public String getRideDetails() {
return "Ride Type: " + ridetype + "\nDiatance In Km: " + distanceInKm
+ "\nRide Id: " + rideID
+ "\nDestination: " + destination + "\nfare: " + "$" + fare;
}

// 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.*;

public class RideSharingServiceTest {


@Test
void testCityRideWithin10Km() {
RideSharingService ride = new RideSharingService("City Ride", 10,
"R001", "Downtown");
assertEquals(15, ride.calculateFare(), "City ride within 10km should
have a fare of $15");
}

@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");
}

// Parametrized test for caluclate fare method


@ParameterizedTest
@CsvSource({
"City Ride,5, R001, Destination1,15",
"City Ride,10, R002, Destination2,15",
"City Ride,15, R003, Destination3,25",
"Outstation Ride,30, R004, Destination4,100",
"Outstation Ride,50, R005, Destination5,100",
"Outstation Ride,60, R006, Destination6,150",
"Unknown Ride,25, R007, Destination7,0"
})
void testCalculateFareParameterized(String rideType, int distance, String
rideId, String destination,
int expectedFare) {
RideSharingService ride = new RideSharingService(rideType, distance,
rideId, destination);
assertEquals(expectedFare, ride.calculateFare(), "Fare calculation
should match expected value");
}
}

OUTPUT:

You might also like