////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
package Project2;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
public class MovieQueueMain {
/* You may create additional private static
variables or methods as needed */
public static void main(String args[]) {
int toAdd;
int toRemove;
String temp;
LinkedList<String> movieQueue = new LinkedList<String>();
LinkedList<String> comedy = new LinkedList<String>("comedy");
LinkedList<String> drama = new LinkedList<String>("drama");
LinkedList<String> action = new LinkedList<String>("action");
LinkedList<String> horror = new LinkedList<String>("horror");
LinkedList<String> family = new LinkedList<String>("family");
//** You may also add additional variables as needed **//
Scanner stdin = new Scanner(System.in); // for reading console input
boolean done = false;
while (!done) {
System.out.print("Enter option - a, c, l, m, p, r, s, w or x: ");
String input = stdin.nextLine();
if (input.length() > 0) {
char choice = input.charAt(0); // strip off option character
String remainder; // used to hold the remainder of input
// trim off any leading or trailing spaces
remainder = input.substring(1).trim();
switch (choice) {
case 'a' :
//** Add your code here for option a **//
//add movie to queue
//if statement to select genre
LinkedList<String> tempList = new LinkedList();
if (remainder.equals("comedy")){
tempList = comedy;
}
if (remainder.equals("drama")){
tempList = drama;
}
if (remainder.equals("action")){
tempList = action;
}
if (remainder.equals("horror")){
tempList = horror;
}
if (remainder.equals("family")){
tempList = family;
}
//genre.print();
System.out.println(tempList.print(true));
toAdd = 0;
boolean goodInput = false;
while (goodInput == false){
//prompt user for selection
System.out.println("Please enter a number between 1 and "
+ tempList.size());
//add check to make sure input is a valid character
toAdd = Integer.parseInt(stdin.nextLine());
if (0 < toAdd && toAdd < tempList.size() + 1) goodInput = true;
else System.out.println("Invalid input. Please try again.");
}
//String tempString = String.valueOf(tempList.get(toAdd));
//movieQueue.add(tempString);
System.out.println("Added " + tempList.get(toAdd) + " to queue.");
movieQueue.add(tempList.get(toAdd));
/**Add a movie to the movie queue from a database of movies of the given genre.
* The genre will be one of the following: comedy, drama, action, horror, family.
* Your program will then open and read from the file with the name genre.txt (e.g.,
* horror.txt or action.txt), which will be a list of movie titles in that genre, one
* per line. Each movie title should be printed, one per line, in the following format:
1 MovieTitle1
2 MovieTitle2
...
N MovieTitleN
After the movie titles have been printed in order, print "Please enter a number between
1 and N", where N is the number of movies in the genre. Read the number entered by the user.
If it is an invalid number (not an integer, or an integer less than 1 or greater than N),
repeat the prompt until a valid number is received. The movie corresponding to this number
should then be added to the end of the movie queue. Once added, print: "Added title to queue."
where title is the title of the movie added to the queue.
If the file specified by genre.txt does not exist or is not readable, print "Cannot find the specified file."
**/
break;
case 'c' :
//** Add your code here for option c **//
//copy to end of queue
//prompt user for selection
toAdd = Integer.parseInt(stdin.nextLine());
//movieQueue.add(temp)
movieQueue.add(movieQueue.get(toAdd));
/**Copies the line at line# to the end of the movie queue, and print "Copied title to end of queue.",
* where title is the title of the copied movie. This does not remove the line at line#. If an
* InvalidListPositionException is thrown, or if the argument is not an int, print "Invalid line number."
**/
break;
case 'l' :
//** Add your code here for option l **//
//load new queue from text file
//prompt user for file name to load
String toLoad = stdin.nextLine();
//load list
movieQueue = new LinkedList<String>(toLoad);
/**Load the movie queue from the given file. First empty your old movie queue (or create a new one).
* Then read in from the file line by line until the end of the file is reached. Each of these lines
* should be stored as one movie in your queue. If the file cannot be found, display "Cannot find the
* specified file." Once the file is successfully stored in the queue, print the queue with line numbers.
*
*/
break;
case 'm' :
//** Add your code here for option m **//
//prompt user for selection
toAdd = Integer.parseInt(stdin.nextLine());
//movieQueue.add(temp)
movieQueue.add(movieQueue.size(), movieQueue.get(toAdd));
/** Move the movie at position line# to the front of the movie queue, and print "Moved title to front
* of queue.", where title is the title of the movie that was moved. This does remove the movie from
* its original position. If an InvalidListPositionException is thrown, or if the argument is not an int,
* print "Invalid line number."
*
*/
break;
case 'p' :
//** Add your code here for option p *//
if (movieQueue.size() == 0){
System.out.println("Empty.");
}
else{
System.out.print(movieQueue.print(true));
}
/**Print the movie queue in the format specified by the LinkedList's print() method. You should print
* line numbers (set the lineNumbers flag to true). If the queue is empty, print "Empty."
*
*/
break;
case 'r' :
//** Add your code here for option r **//
toRemove = Integer.parseInt(remainder);
temp = movieQueue.get(toRemove);
movieQueue.remove(toRemove);
System.out.println("Removed " + temp + " from queue.");
/**Remove the specified movie from the list, and print "Removed title from queue.", where title is the
* title of the movie that was removed. If an InvalidListPositionException is thrown, or if the argument
* is not an int, print "Invalid line number."
*
*/
break;
case 's' :
//** Add your code here for option s **//
// FileUtils.writeStringToFile(new File("queue.txt"), movieQueue.print(false));
/**Save the movie queue to the specified fileName. This file should be saved in the format returned by
* the print method in the LinkedList class. You should not save line numbers (set the lineNumbers flag
* to false). If the specified file cannot be written to, print, "Cannot write to the specified file."
* If the movie queue is currently empty, print, "Cannot write to file, movie queue is empty."
*
*/
break;
case 'w' :
//** Add your code here for option w **//
toRemove = Integer.parseInt(remainder);
if (toRemove > 0){
for (int i = 0; i < toRemove; i++){
movieQueue.remove(1);
}
System.out.println("Removed first " + toRemove +
" from queue.");
}
else {
System.out.println("Invalid number of movies.");
}
/**Mark the first amount movies in the list as watched (i.e., remove them from the queue). If amount is
* greater than the number of movies currently in the queue, remove all movies in the queue. Print "Removed
* first numMovies from queue.", where numMovies is the number of movies that were actually removed from
* the queue. If amount is not an integer or is less than or equal to zero, print "Invalid number of movies."
*
*/
break;
case 'x' :
//Exit the program. This command is already implemented.
done = true;
System.out.println("exit");
break;
default :
System.out.println("Unknown Command");
break;
}
}
}
}
//static void writeStringToFile(File file, String data) {
// FileUtils.writeStringToFile(new File("queue.txt"), movieQueue.print(false));
//}
}