admin menu
unknown
java
2 years ago
6.3 kB
9
Indexable
import java.util.Scanner;
import java.util.ArrayList;
public class AdminMenu {
private static final int ADMIN_PASS = 123;
public static void displayMenu(Scanner keyboard, ArrayList<Movie> action, ArrayList<Movie> comedy,
ArrayList<Movie> romance, ArrayList<Movie> horror, ArrayList<Movie> kids) {
boolean continueMenu = true;
while (continueMenu) {
System.out.println("Please Enter the Admin Password:");
int enteredPass = keyboard.nextInt();
if (enteredPass == ADMIN_PASS) {
System.out.println("Welcome Back! What Would You Like To Do?");
System.out.println("1. Add New Movie");
System.out.println("2. Remove Movie");
System.out.println("3. Display Movies by Genre");
System.out.println("4. Exit Admin Menu");
int adminChoice = keyboard.nextInt();
switch (adminChoice) {
case 1:
addNewMovie(keyboard, action, comedy, romance, horror, kids);
break;
case 2:
removeMovie(keyboard, action, comedy, romance, horror, kids);
break;
case 3:
displayMoviesByGenre(keyboard, action, comedy, romance, horror, kids);
break;
case 4:
continueMenu = false;
break;
default:
System.out.println("Invalid choice. Please select a number from 1-4.");
}
} else {
System.out.println("Incorrect Admin Password. Access Denied.");
break; // Exit the loop if the password is incorrect
}
}
}
private static void addNewMovie(Scanner keyboard, ArrayList<Movie> action, ArrayList<Movie> comedy,
ArrayList<Movie> romance, ArrayList<Movie> horror, ArrayList<Movie> kids) {
char contAdd = 'Y';
while (Character.toUpperCase(contAdd) == 'Y') {
System.out.println("Enter Movie Name:");
keyboard.nextLine(); // Consume the leftover newline if any
String name = keyboard.nextLine();
keyboard.nextLine();
System.out.println("Enter Genre:");
String genre = keyboard.next();
System.out.println("Enter Rating (1-10):");
int rating = keyboard.nextInt();
keyboard.nextLine();
Movie newMovie = new Movie(name, genre, rating);
switch (genre.toLowerCase()) {
case "action":
action.add(newMovie);
break;
case "comedy":
comedy.add(newMovie);
break;
case "romance":
romance.add(newMovie);
break;
case "horror":
horror.add(newMovie);
break;
case "kids":
kids.add(newMovie);
break;
default:
System.out.println("Unknown genre: " + genre + ". Movie not added.");
}
System.out.println("Movie added successfully! Would you like to add another? (Y/N)");
contAdd = keyboard.next().charAt(0);
}
System.out.println("Okay, goodbye!");
}
private static void removeMovie(Scanner keyboard, ArrayList<Movie> action, ArrayList<Movie> comedy,
ArrayList<Movie> romance, ArrayList<Movie> horror, ArrayList<Movie> kids) {
System.out.println("Please select a genre to remove a movie:");
System.out.println("1. Action");
System.out.println("2. Comedy");
System.out.println("3. Romance");
System.out.println("4. Horror");
System.out.println("5. Kids");
int genreChoice = keyboard.nextInt();
ArrayList<Movie> selectedGenreList;
switch (genreChoice) {
case 1:
selectedGenreList = action;
break;
case 2:
selectedGenreList = comedy;
break;
case 3:
selectedGenreList = romance;
break;
case 4:
selectedGenreList = horror;
break;
case 5:
selectedGenreList = kids;
break;
default:
System.out.println("Invalid choice. Please select a number from 1-5.");
return;
}
System.out.println("Select the index of the movie you want to remove:");
for (int i = 0; i < selectedGenreList.size(); i++) {
Movie movie = selectedGenreList.get(i);
System.out.println(i + 1 + ". " + movie.getName());
}
int movieIndex = keyboard.nextInt() - 1; // Adjusting for zero-based indexing
if (movieIndex >= 0 && movieIndex < selectedGenreList.size()) {
selectedGenreList.remove(movieIndex);
System.out.println("Movie removed successfully.");
} else {
System.out.println("Invalid movie index.");
}
}
private static void displayMoviesByGenre(Scanner keyboard, ArrayList<Movie> action, ArrayList<Movie> comedy,
ArrayList<Movie> romance, ArrayList<Movie> horror, ArrayList<Movie> kids) {
System.out.println("Please select a genre:");
System.out.println("1. Action");
System.out.println("2. Comedy");
System.out.println("3. Romance");
System.out.println("4. Horror");
System.out.println("5. Kids");
int genreChoice = keyboard.nextInt();
switch (genreChoice) {
case 1:
displayMovies(action, "Action");
break;
case 2:
displayMovies(comedy, "Comedy");
break;
case 3:
displayMovies(romance, "Romance");
break;
case 4:
displayMovies(horror, "Horror");
break;
case 5:
displayMovies(kids, "Kids");
break;
default:
System.out.println("Invalid choice. Please select a number from 1-5.");
}
}
private static void displayMovies(ArrayList<Movie> movies, String genre) {
if (movies.isEmpty()) {
System.out.println("There are no movies in the " + genre + " genre.");
} else {
System.out.println("Displaying movies in the " + genre + " genre:");
for (Movie movie : movies) {
movie.print();
}
}
}
}
Editor is loading...
Leave a Comment