Uploaded by Amaan Amaan

Java Lab: Sistem za kontrolu inventara

advertisement
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment-1.2
Student Name: Amaan
Branch: CSE
Semester: 6th
Subject Name: Java Lab
UID: 21BCS9523
Section/Group: 641 - A
Date of Performance: 19/01/2024
Subject Code: 21CSH-319
1. Aim: Design and implement a simple inventory control system for a small
video rental store.
2. Objective: The goal of this project is to design and implement a simple
inventory control system for a small video rental store. Define least two
classes: a class Video to model a video and a class VideoStore to model the
actual store. Finally, create a VideoStoreLauncher class with a main() method
which will test the functionality of your other two classes.
3. Algo. /Approach:
public class Video {
private String title;
private boolean checkedOut;
private double averageUserRating;
public Video(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public boolean isCheckedOut() {
return checkedOut;
}
public double getAverageUserRating() {
return averageUserRating;
public void checkOut() {
if (!checkedOut) {
checkedOut = true;
System.out.println("Video checked out: " + title);
} else {
System.out.println("Video is already checked out: " + title);
}
}
public void returnVideo() {
if (checkedOut) {
checkedOut = false;
System.out.println("Video returned: " + title);
} else {
System.out.println("Video is not checked out: " + title);
}
}
public void receiveRating(double userRating) {
if (userRating >= 0.0 && userRating <= 10.0) {
averageUserRating = (averageUserRating + userRating) / 2;
System.out.println("Rating received for " + title + ": " + userRating);
} else {
System.out.println("Invalid rating. Please provide a rating between
0.0 and 10.0 for " + title);
}
}
}
public 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 added to inventory: " + title);
return;
}
}
System.out.println("Inventory is full. Cannot add video: " + title);
public void checkOut(String title) {
Video video = findVideo(title);
if (video != null) {
video.checkOut();
}
}
public void returnVideo(String title) {
Video video = findVideo(title);
if (video != null) {
video.returnVideo();
}
}
public void receiveRating(String title, double userRating) {
Video video = findVideo(title);
if (video != null) {
video.receiveRating(userRating);
}
}
public void listInventory() {
System.out.println("Inventory:");
for (Video video : inventory) {
if (video != null) {
System.out.println("Title: " + video.getTitle() +
", Checked Out: " + video.isCheckedOut() +
", Average Rating: " + video.getAverageUserRating());
}
}
}
private Video findVideo(String title) {
for (Video video : inventory) {
if (video != null && video.getTitle().equals(title)) {
return video;
}
}
System.out.println("Video not found in inventory: " + title);
return null;
}
}
public class VideoStoreLauncher {
VideoStore videoStore = new VideoStore();
videoStore.addVideo("The Matrix");
videoStore.addVideo("Godfather III");
videoStore.addVideo("Star Wars Episode IV: A New Hope");
videoStore.receiveRating("The Matrix", 8.5);
videoStore.receiveRating("The Matrix", 9.0);
videoStore.receiveRating("Godfather III", 9.5);
videoStore.receiveRating("Godfather III", 8.0);
videoStore.receiveRating("Star Wars Episode IV: A New Hope", 7.5);
videoStore.receiveRating("Star Wars Episode IV: A New Hope", 8.5);
System.out.println("Inventory before renting out 'Godfather III':");
videoStore.listInventory();
videoStore.checkOut("Godfather III");
System.out.println("\nInventory after renting out 'Godfather III':");
videoStore.listInventory();
}
}
4. Output:
5.Leatning Outcomes:
I have learnt about :

how to use methods in java.
 Java classes.
 OOPs programming.
Download