Created
April 20, 2013 02:10
-
-
Save csabatini/5424409 to your computer and use it in GitHub Desktop.
CalebSabatiniP5.java
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
/** PROGRAM ASSIGNMENT 5 CalebSabatiniP5.java | |
* | |
* Program Description | |
* This is the client code for the Movie class and its subclasses, which | |
* allows the user to navigate a Warren movie theater menu. | |
* | |
*/ | |
import library.films.*; | |
import java.util.Scanner; | |
public class CalebSabatiniP5 { | |
public static void main(String[] args) { | |
/* Output name and class section*/ | |
System.out.println("Caleb Sabatini, online section\n"); | |
/* Create scanner object */ | |
Scanner input = new Scanner(System.in); | |
/* Variable to store user selection */ | |
int selection = 1; | |
/* Create Movie listay, with elements initialized using Movie subclasses */ | |
Movie[] movies = new Movie[6]; | |
movies[0] = new Animated("Bug's Life", "John Lasseter", 1998, 120, | |
0.60, 363.399); | |
movies[1] = new Animated("Toy Story 3", "Lee Unkrich", 2010, 200, | |
0.50, 1063.172); | |
movies[2] = new Drama("The Dark Knight", "Chrisopher Nolan", 2008, | |
185, 134, 7.50); | |
movies[3] = new Drama("Pulp Fiction", "Quentin Tarantino", 1994, 8, | |
33, 6.50); | |
movies[4] = new Documentary("Super Size Me", "Morgan Spurlock", 2004, | |
1.065, 2000, 0.015); | |
movies[5] = new Documentary("Fahrenheit 9/11", "Michael Moore", 2004, | |
6, 4000, 0.065); | |
do { | |
/* Display the movie menu */ | |
displayMovieMenu(); | |
/* Prompt user for menu selection */ | |
System.out.print("\nEnter a selection between 1 and 5: "); | |
selection = input.nextInt(); | |
/* Determine if the selection is valid; if not, restart loop */ | |
if (selection > 5 || selection < 1) { | |
continue; | |
} | |
/* Exit the loop if the user selected option 5 */ | |
else if (selection == 5) { | |
break; | |
} | |
/* Call the appropriate handling method for options 1-4 */ | |
else { | |
switch (selection) { | |
case 1: showMovieList(movies); | |
break; | |
case 2: showMovieTotals(movies); | |
break; | |
case 3: searchByTitle(movies); | |
break; | |
case 4: sortByProfit(movies); | |
break; | |
default: break; | |
} | |
} | |
} while (selection != 5); | |
} // end of main | |
/* Display movie menu for Warren theater */ | |
private static void displayMovieMenu() { | |
System.out.println("\n\n WARREN MOORE MOVIE MENU"); | |
System.out.println("1. Show the list of movies in the array"); | |
System.out.println("2. Display the total number of movies " + | |
"and the total revenues"); | |
System.out.println("3. Search movie by title"); | |
System.out.println("4. Display movies sorted by profit in " + | |
"decreasing order"); | |
System.out.println("5. Exit\n"); | |
} | |
/* Show the list of movies contained in the array */ | |
private static void showMovieList(Movie[] list) { | |
System.out.printf("%-16s %4s %8s %1s %8s %-11s\n", "Title", | |
"Year", "Revenue", "", "Profit", "Category"); | |
for (int i = 0; i < list.length; i++) { | |
System.out.printf("%-16s %4d %8.3f %1s %7.3f %-11s\n", | |
list[i].getTitle(), list[i].getYear(), list[i].calcRevenue(), "", | |
list[i].calcProfit(), list[i].getCategory()); | |
} | |
} | |
/* Display the total number of movies, and total revenue */ | |
private static void showMovieTotals(Movie[] list) { | |
double totalRevenue = 0; | |
for (int i = 0; i < list.length; i++) { | |
totalRevenue += list[i].calcRevenue(); | |
} | |
System.out.printf("The total number of movies is %d, and their total" + | |
" revenue is %7.3f million.", list[0].getTotalMovies(), | |
totalRevenue); | |
} | |
/* Search movies by title */ | |
private static void searchByTitle(Movie[] list) { | |
/* Call the bubble sort method */ | |
bubbleSort(list); | |
/* Retrieve key, and call the binary search method */ | |
Scanner searchScanner = new Scanner(System.in); | |
System.out.print("Enter the search key: "); | |
String key = searchScanner.nextLine(); | |
int index = binarySearch(key, list); | |
/* Output results of the search */ | |
if (index == -1) { | |
System.out.println("No match found for movie with title \"" + key + | |
"\""); | |
} | |
else { | |
System.out.println("\n" + list[index]); | |
} | |
} | |
/* Sort movies by profit */ | |
private static void sortByProfit(Movie[] list) { | |
/* Call the insertion sort method */ | |
insertionSort(list); | |
System.out.printf("%-16s %-17s %s %6s %7s", "Title", "Director", "", | |
"Cost", "Profit"); | |
for (int i = 0; i < list.length; i++) { | |
System.out.printf("\n%-16s %-17s %2s %6.3f %7.3f", | |
list[i].getTitle(), list[i].getDirector(), "", | |
list[i].getProductionCost(), list[i].calcProfit()); | |
} | |
} | |
/* Bubble sort method */ | |
private static void bubbleSort(Movie[] list) { | |
boolean needNextPass = true; | |
for (int k = 1; k < list.length && needNextPass; k++) { | |
needNextPass = false; | |
for (int i = 0; i < list.length - k; i++) { | |
if (list[i].getTitle().compareTo(list[i + 1].getTitle()) > 0) { | |
// Swap list[i] with list[i + 1] | |
Movie temp = list[i]; | |
list[i] = list[i + 1]; | |
list[i + 1] = temp; | |
needNextPass = true; // Next pass still needed | |
} | |
} | |
} | |
} | |
/* Binary search method */ | |
private static int binarySearch(String key, Movie[] list) { | |
int low = 0; | |
int high = list.length - 1; | |
while (high >= low) { | |
int mid = (low + high) / 2; | |
if (key.compareTo(list[mid].getTitle()) < 0) | |
high = mid - 1; | |
else if (key.equals(list[mid].getTitle())) | |
return mid; | |
else | |
low = mid + 1; | |
} | |
return -1; | |
} | |
/* Insertion sort method */ | |
private static void insertionSort(Movie[] list) { | |
for (int i = 1; i < list.length; i++) { | |
Movie currentElement = list[i]; | |
double currentProfit = currentElement.calcProfit(); | |
int k; | |
for (k = i - 1; k >= 0 && list[k].calcProfit() < currentProfit; | |
k--) { | |
list[k + 1] = list[k]; | |
} | |
list[k + 1] = currentElement; | |
} | |
} | |
} // end of class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment