Skip to content

Commit 4c90807

Browse files
committed
[Manan] ADD:Movie Management System using a Doubly Linked List
1 parent faca77e commit 4c90807

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

MovieManagementSystem.java

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
// Class representing a Movie node in the doubly linked list
2+
class Movie {
3+
String title;
4+
String director;
5+
int releaseYear;
6+
double rating;
7+
Movie next; // Pointer to the next movie
8+
Movie prev; // Pointer to the previous movie
9+
10+
// Constructor to initialize movie details
11+
public Movie(String title, String director, int releaseYear, double rating) {
12+
this.title = title;
13+
this.director = director;
14+
this.releaseYear = releaseYear;
15+
this.rating = rating;
16+
this.next = null;
17+
this.prev = null;
18+
}
19+
}
20+
21+
// Class to manage the Movie list using a doubly linked list
22+
class MovieManager {
23+
private Movie head; // Points to the first movie
24+
private Movie tail; // Points to the last movie
25+
26+
// Method to add a movie at the beginning
27+
public void addMovieAtBeginning(String title, String director, int releaseYear, double rating) {
28+
Movie newMovie = new Movie(title, director, releaseYear, rating);
29+
if (head == null) {
30+
head = tail = newMovie;
31+
} else {
32+
newMovie.next = head;
33+
head.prev = newMovie;
34+
head = newMovie;
35+
}
36+
}
37+
38+
// Method to add a movie at the end
39+
public void addMovieAtEnd(String title, String director, int releaseYear, double rating) {
40+
Movie newMovie = new Movie(title, director, releaseYear, rating);
41+
if (tail == null) {
42+
head = tail = newMovie;
43+
} else {
44+
tail.next = newMovie;
45+
newMovie.prev = tail;
46+
tail = newMovie;
47+
}
48+
}
49+
50+
// Method to add a movie at a specific position
51+
public void addMovieAtPosition(String title, String director, int releaseYear, double rating, int position) {
52+
if (position <= 0) {
53+
System.out.println("Invalid position.");
54+
return;
55+
}
56+
57+
Movie newMovie = new Movie(title, director, releaseYear, rating);
58+
59+
if (position == 1) {
60+
addMovieAtBeginning(title, director, releaseYear, rating);
61+
return;
62+
}
63+
64+
Movie temp = head;
65+
for (int i = 1; i < position - 1 && temp != null; i++) {
66+
temp = temp.next;
67+
}
68+
69+
if (temp == null || temp.next == null) {
70+
addMovieAtEnd(title, director, releaseYear, rating);
71+
} else {
72+
newMovie.next = temp.next;
73+
newMovie.prev = temp;
74+
temp.next.prev = newMovie;
75+
temp.next = newMovie;
76+
}
77+
}
78+
79+
// Method to remove a movie by title
80+
public void removeMovie(String title) {
81+
if (head == null) {
82+
System.out.println("No movies in the list.");
83+
return;
84+
}
85+
86+
Movie temp = head;
87+
while (temp != null && !temp.title.equalsIgnoreCase(title)) {
88+
temp = temp.next;
89+
}
90+
91+
if (temp == null) {
92+
System.out.println("Movie with title '" + title + "' not found.");
93+
return;
94+
}
95+
96+
if (temp == head) {
97+
head = temp.next;
98+
if (head != null) {
99+
head.prev = null;
100+
} else {
101+
tail = null;
102+
}
103+
} else if (temp == tail) {
104+
tail = temp.prev;
105+
tail.next = null;
106+
} else {
107+
temp.prev.next = temp.next;
108+
temp.next.prev = temp.prev;
109+
}
110+
111+
System.out.println("Movie '" + title + "' removed.");
112+
}
113+
114+
// Method to search for a movie by Director
115+
public void searchMovieByDirector(String director) {
116+
Movie temp = head;
117+
boolean found = false;
118+
while (temp != null) {
119+
if (temp.director.equalsIgnoreCase(director)) {
120+
System.out.println("Movie Found: " + temp.title + " | Directed by " + temp.director);
121+
found = true;
122+
}
123+
temp = temp.next;
124+
}
125+
if (!found) {
126+
System.out.println("No movies found directed by '" + director + "'.");
127+
}
128+
}
129+
130+
// Method to search for a movie by Rating
131+
public void searchMovieByRating(double rating) {
132+
Movie temp = head;
133+
boolean found = false;
134+
while (temp != null) {
135+
if (temp.rating == rating) {
136+
System.out.println("Movie Found: " + temp.title + " | Rating: " + temp.rating);
137+
found = true;
138+
}
139+
temp = temp.next;
140+
}
141+
if (!found) {
142+
System.out.println("No movies found with rating " + rating + ".");
143+
}
144+
}
145+
146+
// Method to update a movie's rating by title
147+
public void updateMovieRating(String title, double newRating) {
148+
Movie temp = head;
149+
while (temp != null) {
150+
if (temp.title.equalsIgnoreCase(title)) {
151+
temp.rating = newRating;
152+
System.out.println("Updated rating of '" + title + "' to " + newRating);
153+
return;
154+
}
155+
temp = temp.next;
156+
}
157+
System.out.println("Movie with title '" + title + "' not found.");
158+
}
159+
160+
// Method to display all movies in forward order
161+
public void displayMoviesForward() {
162+
if (head == null) {
163+
System.out.println("No movies in the list.");
164+
return;
165+
}
166+
167+
System.out.println("Movies (Forward Order):");
168+
Movie temp = head;
169+
while (temp != null) {
170+
System.out.println(temp.title + " | Director: " + temp.director + " | Year: " + temp.releaseYear + " | Rating: " + temp.rating);
171+
temp = temp.next;
172+
}
173+
}
174+
175+
// Method to display all movies in reverse order
176+
public void displayMoviesReverse() {
177+
if (tail == null) {
178+
System.out.println("No movies in the list.");
179+
return;
180+
}
181+
182+
System.out.println("Movies (Reverse Order):");
183+
Movie temp = tail;
184+
while (temp != null) {
185+
System.out.println(temp.title + " | Director: " + temp.director + " | Year: " + temp.releaseYear + " | Rating: " + temp.rating);
186+
temp = temp.prev;
187+
}
188+
}
189+
}
190+
191+
// Main class to run the program
192+
public class MovieManagementSystem {
193+
public static void main(String[] args) {
194+
MovieManager movieList = new MovieManager();
195+
196+
// Adding movies
197+
movieList.addMovieAtEnd("PK", "RajKumar", 2016, 8.8);
198+
movieList.addMovieAtEnd("3 Idiots", "Rajkumar", 2012, 8.6);
199+
movieList.addMovieAtBeginning("83", "Kabir Khan", 2021, 7.8);
200+
movieList.addMovieAtPosition("Tiger Zinda Hai", "Ali Khan", 2016, 9.2, 2);
201+
202+
// Display movies in forward order
203+
movieList.displayMoviesForward();
204+
205+
// Display movies in reverse order
206+
movieList.displayMoviesReverse();
207+
208+
// Searching for a movie by Director
209+
movieList.searchMovieByDirector("Rajkumar");
210+
211+
// Searching for a movie by Rating
212+
movieList.searchMovieByRating(9.2);
213+
214+
// Updating a movie's rating
215+
movieList.updateMovieRating("PK", 8.0);
216+
217+
// Removing a movie
218+
movieList.removeMovie("PK");
219+
220+
// Display movies after update
221+
movieList.displayMoviesForward();
222+
}
223+
}
224+
225+
226+
//SampleOutput
227+
//Movies (Forward Order):
228+
// 83 | Director: Kabir Khan | Year: 2021 | Rating: 7.8
229+
//Tiger Zinda Hai | Director: Ali Khan | Year: 2016 | Rating: 9.2
230+
//PK | Director: RajKumar | Year: 2016 | Rating: 8.8
231+
// 3 Idiots | Director: Rajkumar | Year: 2012 | Rating: 8.6
232+
//Movies (Reverse Order):
233+
// 3 Idiots | Director: Rajkumar | Year: 2012 | Rating: 8.6
234+
//PK | Director: RajKumar | Year: 2016 | Rating: 8.8
235+
//Tiger Zinda Hai | Director: Ali Khan | Year: 2016 | Rating: 9.2
236+
// 83 | Director: Kabir Khan | Year: 2021 | Rating: 7.8
237+
//Movie Found: PK | Directed by RajKumar
238+
//Movie Found: 3 Idiots | Directed by Rajkumar
239+
//Movie Found: Tiger Zinda Hai | Rating: 9.2
240+
//Updated rating of 'PK' to 8.0
241+
//Movie 'PK' removed.
242+
//Movies (Forward Order):
243+
// 83 | Director: Kabir Khan | Year: 2021 | Rating: 7.8
244+
//Tiger Zinda Hai | Director: Ali Khan | Year: 2016 | Rating: 9.2
245+
// 3 Idiots | Director: Rajkumar | Year: 2012 | Rating: 8.6

0 commit comments

Comments
 (0)