JAVA 1.2 suman
JAVA 1.2 suman
Experiment 1.2
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. CODE:
import java.util.HashSet;
import java.util.Scanner;
class Video {
public String title;
public boolean isChecked;
public double rating;
public int numRated;
Video(String title) {
this.title = title;
this.numRated = 0;
this.rating = 0;
this.isChecked = false;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
};
class VideoStore {
Scanner sc = new Scanner(System.in);
public HashSet<Video> inventry = new HashSet<Video>();
public void addVideo(String s) {
Video newVideo = new Video(s);
inventry.add(newVideo);
}
public void addRating(String title) {
System.out.print("Enter a rating between 1 to 10: ");
double rating = sc.nextDouble();
for (Video v : inventry) {
if (v.title.equals(title)) {
double oldrating = v.rating * v.numRated;
v.numRated++;
v.rating = (oldrating + rating) / v.numRated;
break;
}
}
}
public void checkout(String title){
for (Video v : inventry) {
if (v.title.equals(title)) {
v.isChecked = true;
System.out.println(v.isChecked);
break;
}
}
}
public void returnVideo(String title){
for (Video v : inventry) {
if (v.title.equals(title)) {
v.isChecked = false;
break;
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
vidStore.addVideo(title);
System.out.println();
}
if (key == 2) {
System.out.print("Enter title of video: ");
String title = sc.next();
vidStore.addRating(title);
System.out.println();
}
if (key == 3) {
System.out.print("Enter title of the video: ");
String title = sc.next();
vidStore.checkout(title);
System.out.println();
}
if (key == 4) {
System.out.print("Enter title of the video: ");
String title = sc.next();
vidStore.returnVideo(title);
System.out.println();
}
if(key==5){
vidStore.showInventry();
System.out.println();
}
}
sc.close();
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING