Java Exp 1.2-1
Java Exp 1.2-1
Experiment 1.2
Aim: Design and implement a simple inventory control system for a small video rental store
Objective: To design and implement a user-friendly inventory control system for a small
video rental store, enabling efficient management of video inventory, including functionalities
for adding, renting, and returning videos.
Algorithm:
Define Classes:
Video: To represent each video, with attributes such as video ID, title, genre, and
availability status.
Inventory: To manage the list of videos, including adding and removing videos from
the inventory.
Customer: To represent customers, with attributes such as customer ID, name, and
rented videos.
RentalSystem: To control the process of renting and returning videos.
Video Class:
Define the video with attributes such as videoID, title, genre, and isAvailable.
Define methods to mark the video as rented and returned.
Inventory Class:
Customer Class:
RentalSystem Class:
Handle the main functionality: list available videos, allow customers to rent and return
videos, and display the inventory status.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
import java.util.ArrayList;
import java.util.Scanner;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
@Override
public String toString() {
return "Title: " + title + " | Available: " + (isAvailable ? "Yes" : "No");
}
}
public VideoStore() {
inventory = new ArrayList<>();
}
// Rent a video
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
// Return a video
public void returnVideo(String title) {
for (Video video : inventory) {
if (video.getTitle().equalsIgnoreCase(title)) {
if (!video.isAvailable()) {
video.returnVideo();
System.out.println("You returned: " + title);
} else {
System.out.println("Error: Video was not rented.");
}
return;
}
}
System.out.println("Error: Video not found in inventory.");
}
}
while (true) {
System.out.println("\n--- Video Rental Store ---");
System.out.println("1. Add Video");
System.out.println("2. List Inventory");
System.out.println("3. Rent Video");
System.out.println("4. Return Video");
System.out.println("5. Exit");
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
} else {
System.out.println("Invalid choice. Please enter a number.");
scanner.next(); // Consume invalid input
continue;
}
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter video title to add: ");
String titleToAdd = scanner.nextLine().trim();
store.addVideo(titleToAdd);
break;
case 2:
store.listInventory();
break;
case 3:
System.out.print("Enter video title to rent: ");
String titleToRent = scanner.nextLine().trim();
store.rentVideo(titleToRent);
break;
case 4:
System.out.print("Enter video title to return: ");
String titleToReturn = scanner.nextLine().trim();
store.returnVideo(titleToReturn);
break;
case 5:
System.out.println("Exiting the system. Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Learning Outcomes:
Object-Oriented Design: Learn to create and use classes for real-world entities.
Core Programming Skills: Practice loops, conditionals, and methods for inventory
operations.
Data Structure Usage: Use ArrayList to manage dynamic data effectively.
User-Friendly Systems: Design intuitive interfaces and handle errors smoothly.