Created
June 8, 2021 16:12
-
-
Save JakWai01/2bc9be1a1cf24bbc1c8945b5fdff8ee1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Main; | |
import java.io.IOException; | |
import java.util.Scanner; | |
/** | |
* Main class of this program | |
* @version 1.0.0 | |
* @author Jakob Waibel, Maximilian Dolbaum, Lukas Kleinau | |
*/ | |
public class Main | |
{ | |
/** | |
* This is a program to store and administrate Movie and Series database entries. | |
* In General, you can add new Media based on type, browse a sorted/filtered list and remove entries. | |
* | |
* @param args No relevant arguments to hand over at startup. | |
* @throws IOException Checking if input/output operations can be executed and everything is working as intended. | |
* Derives from clearScreen method in Helper class. | |
* @throws InterruptedException Allows putting main program on hold to execute clear command. | |
* Derives from clearScreen method in Helper class. | |
*/ | |
public static void main(String[]args) throws IOException, InterruptedException | |
{ | |
// Declaring variables | |
String title, genre, actor, releaseYear, rating, seasons, filterChoice = null; | |
// Declaring and initializing variables to store fileNames | |
String fileNameMedia, fileNameMovie = "Database_Movie.txt", fileNameSeries = "Database_Series.txt"; | |
/* | |
Movie is true if an operation concerning a movie was chosen. | |
Exit is true if "exit" was chosen in the main menu and the program will be terminated. | |
*/ | |
boolean movie, exit = false; | |
// Variable to store the entry which will be deleted in "remove" | |
int deleteChoice; | |
Helper help = new Helper(); | |
// Clears the screen | |
help.clearScreen(); | |
System.out.println("Welcome dear user!"); | |
//Program while loop. Gets executed unless "exit" is chosen | |
while(!exit) | |
{ | |
// Resets movie if it was set to "true" in previous operations. | |
movie = false; | |
// Resets fileName to Series if it was set to "fileNameMovie" in previous operations. | |
fileNameMedia = fileNameSeries; | |
// If a filter was applied the "Active Filter" will be displayed above the main menu. | |
if (filterChoice != null) | |
{ | |
System.out.println("\033[0;97m" + "\033[42m" + "Active Filter:" + "\u001B[0m" + " " + "\u001B[32m" + filterChoice + "\u001B[0m"); | |
} | |
// This is the main menu. | |
System.out.println("-----------------------------"); | |
System.out.println("What will you choose to do?"); | |
System.out.println(); | |
System.out.println("0: Browse"); | |
System.out.println("1: Toggle Title filter"); | |
System.out.println("2: Add"); | |
System.out.println("3: Remove"); | |
System.out.println("4: Exit"); | |
System.out.println(); | |
System.out.print("Your choice: "); | |
// Scanning "Your choice: " | |
Scanner scanner = new Scanner(System.in); | |
String choice = scanner.nextLine(); | |
// Switch to check which operation was chosen. | |
// Lowercase, uppercase, normal and number entries are allowed (e.g. : browse, BROWSE, Browse ,0) | |
switch (choice.toLowerCase()) | |
{ | |
case "0": | |
case "browse": | |
// Asking if the user wants to browse movies or series | |
System.out.print("Browse for Movie [1] or Series [2]: "); | |
String browseChoice = scanner.nextLine(); | |
help.clearScreen(); | |
switch (browseChoice.toLowerCase()) | |
{ | |
/* | |
If movie was chosen the variable will be adjusted for movies, | |
otherwise the variables stay as initialized above in favour of series. | |
Then it will fall through. If series was chosen, this case won't be reached. | |
*/ | |
case "1": | |
case "movie": fileNameMedia = fileNameMovie; movie = true; | |
case "2": | |
case "series": | |
// Checks if a dataBase exists so far. | |
if (!help.checkExistence(fileNameMedia)) | |
{ | |
break; | |
} | |
// Adjusts output | |
if(movie) | |
{ | |
System.out.println("Currently in Movie Database: "); | |
} | |
else | |
{ | |
System.out.println("Currently in Series Database: "); | |
} | |
System.out.println(); | |
// If no filter was applied so far the whole dataBase will be printed. | |
if (filterChoice == null) | |
{ | |
System.out.println(help.loadDatabase(fileNameMedia)); | |
} | |
// If a filter was applied only the matching results will be printed. | |
else | |
{ | |
Media filter = new Media(); | |
System.out.println(filter.filterMedia(filterChoice, fileNameMedia)); | |
} | |
System.out.println(); | |
break; | |
default: | |
help.clearScreen(); | |
System.out.println("\033[41m" + "Faulty input:" + "\033[0m" + " Please try again."); | |
} | |
break; | |
case "1": | |
case "filter": | |
help.clearScreen(); | |
// If a filter was applied reset it (toggle). | |
if (filterChoice != null) | |
{ | |
filterChoice = null; | |
break; | |
} | |
// Otherwise you can set a new filter option | |
System.out.println("What shall the entry contain?"); | |
System.out.print("Your choice: "); | |
filterChoice = scanner.nextLine(); | |
help.clearScreen(); | |
break; | |
case "2": | |
case "add": | |
// Asking if the user wants to add movies or series | |
System.out.print("Movie [1] or Series [2]: "); | |
String addChoice = scanner.nextLine(); | |
help.clearScreen(); | |
switch (addChoice.toLowerCase()) | |
{ | |
/* | |
If movie was chosen the variable will be adjusted for movies, | |
otherwise the variables stay as initialized above in favour of series. | |
Then it will fall through. If series was chosen, this case won't be reached. | |
*/ | |
case "1": | |
case "movie": movie = true; | |
case "2": | |
case "series": | |
// Asking for specific information about the movie/series | |
// Adjusts output | |
if(movie) | |
{ | |
System.out.print("Movie Title: "); | |
} | |
else | |
{ | |
System.out.print("Series Title: "); | |
} | |
title = scanner.nextLine(); | |
System.out.print("Release Year: "); | |
releaseYear = scanner.nextLine(); | |
System.out.print("Genre: "); | |
genre = scanner.nextLine(); | |
System.out.print("Main Actor: "); | |
actor = scanner.nextLine(); | |
System.out.print("Your Personal Rating (0-10): "); | |
rating = scanner.nextLine(); | |
// If a rating above 10 was entered the rating will be 10. | |
try | |
{ | |
if (Integer.parseInt(rating) >=10) | |
{ | |
rating = "10"; | |
} | |
} | |
// If the rating was no number the rating will be 0 | |
catch (NumberFormatException e) | |
{ | |
rating = String.valueOf(0); | |
} | |
// Adjusting | |
if(movie) | |
{ | |
// Creating object with constructor for movies | |
Media mediaMovie = new Media(title, releaseYear, genre, actor, rating); | |
// Adding the created Object to the dataBase | |
mediaMovie.addMedia(fileNameMovie); | |
} | |
else | |
{ | |
// If a series is being created currently the user will be asked for an additional information. | |
System.out.print("total Seasons: "); | |
seasons = scanner.nextLine(); | |
// Creating object with constructor for series | |
Media mediaSeries = new Media(title, releaseYear, genre, actor, rating, seasons); | |
// Adding the created Object to the dataBase | |
mediaSeries.addMedia(fileNameSeries); | |
} | |
help.clearScreen(); | |
break; | |
default: | |
System.out.println("\033[41m" + "Faulty input:" + "\033[0m" + " Please try again."); | |
} | |
break; | |
case "3": | |
case "remove": | |
// Asking if the user wants to remove movies or series | |
System.out.print("Remove Movie [1] or Series [2]: "); | |
String removeChoice = scanner.nextLine(); | |
help.clearScreen(); | |
switch (removeChoice.toLowerCase()) | |
{ | |
/* | |
If movie was chosen the variable will be adjusted for movies, | |
otherwise the variables stay as initialized above in favour of series. | |
Then it will fall through. If series was chosen, this case won't be reached. | |
*/ | |
case "1": | |
case "movie": fileNameMedia = fileNameMovie; movie = true; | |
case "2": | |
case "series": | |
// Checking if a dataBase exists so far. | |
if (!help.checkExistence(fileNameMedia)) | |
{ | |
break; | |
} | |
// Adjusting output | |
if(movie) | |
{ | |
System.out.println("Currently in Movie Database: "); | |
} | |
else | |
{ | |
System.out.println("Currently in Series Database: "); | |
} | |
System.out.println(); | |
// If no filter was applied so far the whole dataBase will be printed. | |
if (filterChoice == null) | |
{ | |
System.out.println(help.loadDatabase(fileNameMedia)); | |
} | |
// If a filter was applied only the matching results will be printed. | |
else | |
{ | |
Media filter = new Media(); | |
System.out.println(filter.filterMedia(filterChoice, fileNameMedia)); | |
} | |
System.out.println(); | |
// Asking for entry to delete (Index) | |
System.out.print("Index of entry to delete: "); | |
deleteChoice = scanner.nextInt(); | |
// If deleteChoice exceeds the number of lines the following message appears. | |
if (deleteChoice > help.countLines(fileNameMedia)) | |
{ | |
help.clearScreen(); | |
System.out.println("\033[41m" + "Exceeds line count in database" + "\033[0m"); | |
break; | |
} | |
// If deleteChoice is smaller than zero the following message appears. | |
else if (deleteChoice < 0) | |
{ | |
help.clearScreen(); | |
System.out.println("\033[41m" + "Negative index not assigned" + "\033[0m"); | |
break; | |
} | |
// Creating object | |
Media rmMovie = new Media(); | |
// Deleting line by Index and file (movie or series) | |
rmMovie.removeMedia(deleteChoice, fileNameMedia); | |
break; | |
default: | |
System.out.println("\033[41m" + "Faulty input:" + "\033[0m" + " Please try again."); | |
} | |
break; | |
case "4": | |
case "exit": | |
help.clearScreen(); | |
System.out.println("Good Bye! "); | |
// Program will be terminated | |
exit = true; | |
break; | |
// Easter egg | |
case "secret": | |
help.clearScreen(); | |
System.out.println(help.surprise()); | |
break; | |
default: | |
System.out.println("\033[41m" + "Faulty input:" + "\033[0m" + " Please try again."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment