exp_2
exp_2
Experiment- 2
Student Name: Aakanksha Sharma UID: 22BCS16783
Branch: BE-CSE Section/Group: IOT_632/A
Semester: 6th Date of Performance: 17-01-2025
Subject Name: Project Based Learning Subject Code: 22CSH-359
in Java with Lab
1. Aim: Design and implement a simple inventory control system for a small video
rental store.
2. Objective:
➢ To learn about objects and classes in Java: The program uses the Video and
VideoStore classes to encapsulate data and behavior, demonstrating the fundamental
concepts of object-oriented programming.
➢ To understand method overriding and overloading: The program uses method
overriding to define toString for custom string representation of video objects.
➢ To understand the concept of encapsulation and access specifiers in Java: The use
of private fields and public methods in the Video class illustrates how encapsulation
and access control ensure data integrity.
3. Problem Statement:
Design and implement a simple inventory control system for a small video rental store. The
system should allow the store to efficiently manage its collection of videos and support the
following functionalities:
1. Add new videos to the store's inventory.
2. Rent out videos by marking them as checked out.
3. Return videos to make them available for renting again.
4. Accept user ratings for videos and calculate the average rating for each video.
5. Display the current inventory, including the title, checked-out status, and average rating of
each video.
4. Algorithm:
➢ Initialize Inventory: Create a VideoStore object to manage the inventory, using a list to store
video objects.
➢ Add Video: Accept a video title as input. Create a Video object with the title and add it to the
inventory.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
➢ Rent Video:
• Search the inventory for the given video title.
• If found and not already checked out, mark it as checked out.
• If already checked out or not found, display an appropriate message.
➢ Return Video:
• Search the inventory for the given video title.
• If found and checked out, mark it as returned.
• If not checked out or not found, display an appropriate message.
➢ Add Rating:
• Search the inventory for the given video title.
• If found, accept a rating (1-5) and update the average rating for the video.
• If not found or the rating is invalid, display an appropriate message.
➢ List Inventory:
• Iterate through all videos in the inventory.
• For each video, display the title, checked-out status, and average rating.
➢ Exit Program:
• Provide an option to exit the program gracefully.
5. Implementation/Code:
import java.util.ArrayList;
//class Video
class Video
{
private String title;
private boolean isCheckedOut;
private double totalRating;
private int ratingCount;
public Video(String title) {
this.title = title;
this.isCheckedOut = false;
this.totalRating = 0.0;
this.ratingCount = 0;
}
public String getTitle() {
return title;
}
public boolean isCheckedOut() {
return isCheckedOut;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
public double getAverageRating() {
return (ratingCount == 0) ? 0.0 : totalRating / ratingCount;
}
public void checkOut() {
if (!isCheckedOut) {
isCheckedOut = true;
System.out.println("Checked out: " + title);
}
else {
System.out.println(title + " is already checked out.");
}
}
public void returnVideo() {
if (isCheckedOut) {
isCheckedOut = false;
System.out.println("Returned: " + title);
} else {
System.out.println(title + " was not checked out.");
}
}
public void addRating(int rating) {
if (rating >= 1 && rating <= 5) {
totalRating += rating;
ratingCount++;
System.out.println("Rating added for " + title + ": " + rating);
} else {
System.out.println("Invalid rating. Please provide a rating between 1 and 5.");
}
}
@Override
public String toString() {
return "Title: " + title +
", Checked Out: " + isCheckedOut +
", Average Rating: " + getAverageRating();
}
}
//class VideoStore
class VideoStore {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
private ArrayList<Video> inventory;
public VideoStore() {
this.inventory = new ArrayList<>();
}
public void addVideo(String title) {
inventory.add(new Video(title));
System.out.println("Video added to inventory: " + title);
}
// Step 4: List inventory after "Godfather II" has been rented out
store.listInventory();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Output:
7. Learning Outcomes:
• Designed a functional system to manage video rentals, demonstrating the use
of classes and objects in Java.
• Implemented methods for operations like adding videos, renting out, returning,
and recording user ratings.
• Applied arrays to store and efficiently manage the video inventory within the
store.
• Learned to integrate multiple classes and enable seamless interaction among
them in a structured program.