Branch: BE-CSE Section/Group: KRG_SC_1 Semester: 6th Date of Performance: 17/01/2024 Subject Name: Project Based Learning in Java Subject Code: 21CSH-319
1. Aim: Design and implement a simple inventory control system for a small video rental store.
2. Objective: • To learn about Classes. • To learn about Encapsulation, Aggregation concept in java.
3. Script and Output:
class Video { private String title; private boolean checkedOut; private double averageRating; private int numberOfRatings; public Video(String title) { this.title = title; this.checkedOut = false; this.averageRating = 0.0; this.numberOfRatings = 0; } public String getTitle() { return title; } public boolean isCheckedOut() { return checkedOut; } public void checkOut() { checkedOut = true; } public void returnVideo() { checkedOut = false; } public void receiveRating(int rating) { averageRating = (averageRating * numberOfRatings + rating) / (numberOfRatings + 1); numberOfRatings++; } public double getAverageRating() { return averageRating; } } class VideoStore { private Video[] inventory; public VideoStore() { this.inventory = new Video[10]; } public void addVideo(String title) { for (int i = 0; i < inventory.length; i++) { if (inventory[i] == null) { inventory[i] = new Video(title); System.out.println("Video '" + title + "' added to the inventory."); return; } } System.out.println("Error: Inventory is full. Cannot add video '" + title + "'."); } public void checkOut(String title) { for (Video video : inventory) { if (video != null && video.getTitle().equals(title) && !video.isCheckedOut()) { video.checkOut(); System.out.println("Video '" + title + "' checked out."); return; } } System.out.println("Error: Video '" + title + "' not found or already checked out."); } public void returnVideo(String title) { for (Video video : inventory) { if (video != null && video.getTitle().equals(title) && video.isCheckedOut()) { video.returnVideo(); System.out.println("Video '" + title + "' returned."); return; } } System.out.println("Error: Video '" + title + "' not found or not checked out."); } public void receiveRating(String title, int rating) { for (Video video : inventory) { if (video != null && video.getTitle().equals(title)) { video.receiveRating(rating); System.out.println("Rating of " + rating + " received for video '" + title + "'."); return; } } System.out.println("Error: Video '" + title + "' not found."); } public void listInventory() { System.out.println("Current Inventory:"); for (Video video : inventory) { if (video != null) { System.out.println("Title: " + video.getTitle() + ", Checked Out: " + video.isCheckedOut() + ", Average Rating: " + video.getAverageRating()); } } } } public class Main { public static void main(String[] args) { VideoStore videoStore = new VideoStore(); videoStore.addVideo("The Matrix"); videoStore.addVideo("Godfather II"); videoStore.addVideo("Star Wars Episode IV: A New Hope"); videoStore.receiveRating("The Matrix", 4); videoStore.receiveRating("The Matrix", 5); videoStore.receiveRating("Godfather II", 5); videoStore.receiveRating("Godfather II", 4); videoStore.receiveRating("Star Wars Episode IV: A New Hope", 3); videoStore.checkOut("The Matrix"); videoStore.returnVideo("The Matrix"); videoStore.checkOut("Godfather II"); videoStore.returnVideo("Godfather II"); videoStore.checkOut("Star Wars Episode IV: A New Hope"); videoStore.returnVideo("Star Wars Episode IV: A New Hope"); videoStore.listInventory(); } } Output: