Question

ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes,...

ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes, where the Media superclass has the artistName and the mediaName as the common attributes. Create a subclass called CDMedia that has the additional arrayList of String objects containing the songs per album. Create another subclass called DVDMedia that has the additional year the movie came out, and an arrayList of co-stars for the movie. 1.) The superclass Media will implement Comparable, and will define a compareTo() method based on mediaName. 2.) Create another class, ComparatorByArtistName, which will implement the Comparator interface, and will define the compare() method based on artistName. Driver Class: 3.) Using the attached catalog2.txt file, create a driver class that creates an arrayList of type Media, which contains individual objects of CDMedia and DVDMedia. Print the entire arrayList of Media once after sorting them by mediaName. Display the entire content of the Media. Then, print a dividing line of asterisks, and print the entire arrayList of Media again after sorting them by artistName. 4.) Display a menu for the user to select from the following options: Search by Media Title (movie name or album name) Search by Artist (singer or actor) Add media to catalog Quit In order to do a binarySearch method, you must create a search object with the value in the field of what you are searching for. Thus, if you are searching for a media name, create a Media object with only the value of the media name in it. If you are searching for an artist, then create a Media object with only the value of the artist in it. See the Java API for the Collections class, to look up the format of the binarySearch method. 5.) For the media search, first sort the ArrayList of Media by media name, using the Comparable interface and compareTo method in the Media class. Then, use the binary search method for Collections, Collections.binarySearch(), to find the media requested by the user, for a specific media name. Display the entire Media object that matches the media name supplied. 6.) For the artist search, sort the ArrayList of Media by artist name, using your comparator class. Once sorted by artist, use the binary search method for Collections, Collections.binarySearch() to find all the media for a particular artist requested by the user. Display all the Media objects that match the artist name supplied. ***Hint: Since there are multiple media objects with the same artist name, the value returned by the binarySearch() method is an arbitrary index in the arrayList of Media objects with the same artist. You will have to keep getting the rest of the Media objects by searching above and below that arbitrary index, until the artist is different. Use the ArrayList.get()method (see page 349 of Big Java, 5th Edition, for an example of a loop to sequentially go through an arrayList.) 7.) Implement the Scanner and File classes to read the catalog2.txt file. 8.) Implement the FileWriter and PrintWriter classes to write to the Media file. 9.) Thus, prompt the user for the name of the input/output file. Catch exceptions such as IOException and FileNotFound. Give the user a message that says "Enter catalog2.txt for the file input." 10.) If the user selects the "add media to catalog" option, then prompt the user for all the information that makes up a CDMedia object or a DVDMedia object, and once created, add the new object to the ArrayList of Media. Don't forget to permanently add the media record to the external file, using the FileWriter and PrintWriter classes. It may be easier to create a .toString() method in the specific media subclass, and then use it to write the new Media record to the file. (Ex.: output.println(objectName) will invoke the .toString()) behind the scenes.) Remember to add the C or D as the first field for a CDMedia or a DVDMedia record. 11.) When adding a new record to the catalog2.txt file, if the user enters a non-numeric field wherever a number is required (i.e. year), then catch that numberFormatException, and give the user a chance to enter that field again. 12.) If the user is adding a DVDMedia record to the catalog2.txt file, make sure to loop for all the co-stars of that movie. If the user is adding a CDMedia record to the catalog2.txt file, make sure to loop for all the songs in that album. In both of these records, you will have an arrayList of co-stars, or an arrayList of songs, as part of the object.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implemented the problem solution in java

Note: I wrote code for all methods with comments use the methods you want it includes the driver class

Filename: Program.java

Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Program {
  
Scanner kb = new Scanner(System.in);
ArrayList<Media> myMedia = new ArrayList<Media>();
String fileName;

/**
* Main method calls the methods to read the file, display the media an display the user menu. Depending on the option selected, it calls
* the methods to search the media or the artist or to add media to the file. It continues looping till the option exit is selected
*/
public static void main(String[] args) {
  
int userChoice;
MidtermProject myMidtermProject = new MidtermProject();
myMidtermProject.readMedia();
myMidtermProject.displayMedia();
do
{
userChoice = myMidtermProject.displayMenu();

switch(userChoice)
{
case 1:
myMidtermProject.mediaSearch();
break;
case 2:
myMidtermProject.artistSearch();
break;
case 3:
myMidtermProject.addMedia();
break;
case 4:
System.out.println("Thanks for using the program!");
break;
}
}
while(userChoice != 4);

}
/**
* This readMedia method opens a file entered by the user if it is the right file. Then, it loops throughout the file, and reads each of the media elements,
* which will be stored in their corresponding objects (CD or DVD), and added to the global ArrayList of media objects.
*/
public void readMedia() {
  
boolean rightFile = false;
while(!rightFile)
{
try
{
System.out.println("Enter catalog2.txt for the name of the file.");
fileName = kb.nextLine();
File aFile = new File(fileName);
Scanner myFile = new Scanner(aFile);
rightFile = true;
String aRecord, mediaType, artistName, mediaName, year;

while(myFile.hasNext())
{
aRecord = myFile.nextLine();
String aRecArray[] = aRecord.split(" ");
mediaType = aRecArray[0];
artistName = aRecArray[1];
mediaName = aRecArray[2];
ArrayList<String> songs = new ArrayList<String>();
if (mediaType.equals("C"))
{
for(int i = 3; i < aRecArray.length; i++)
{
songs.add(aRecArray[i]);
}
Media aCDMedia = new CDMedia(songs, artistName, mediaName);
myMedia.add(aCDMedia);
}
else if (mediaType.equals("D"))
{
year = aRecArray[3];
Media aDVDMedia = new DVDMedia(year, artistName, mediaName);
myMedia.add(aDVDMedia);
}
}   
}
catch(FileNotFoundException e)
{
System.out.println("Wrong file!!");
}
  

}
}
/**
* This displayMedia method sorts the global ArrayList by mediaName and displays each of its elements using a for loop.
* Then it sorts it again using the ComparatorByArtistName and prints each element in the same way.
*/
public void displayMedia() {
  
System.out.println("**********Media by Media Name**********");
Collections.sort(myMedia);
for(int i = 0; i < myMedia.size(); i++)
{
System.out.println(myMedia.get(i));
}
System.out.println("\n***********Media by Artist Name**********");
Collections.sort(myMedia, new ComparatorByArtistName());
for(int i = 0; i < myMedia.size(); i++)
{
System.out.println(myMedia.get(i));
}
  
}
/**
* This displayMenu method shows to the user the options to interact with the catalog of media. It returns an int, which is the user choice and validates it.
*/
public int displayMenu() {
  
int userChoice = 4;
boolean correctChoice = false;

while(!correctChoice)
{
try
{
System.out.println("\nPlease, select one of the options:"
+ "\n1. Search by Media Title (movie name or album name)"
+ "\n2. Search by Artist (singer or actor)"
+ "\n3. Add media to catalog"
+ "\n4. Quit");

userChoice = kb.nextInt();
kb.nextLine();
if(userChoice >= 1 && userChoice <= 4)
{
correctChoice = true;
}
else
{
System.out.println("Wrong Input. Try again!");
}

}
catch(InputMismatchException e)
{
System.out.println("Wrong Input. Try again!");
kb.nextLine();
}
}


return userChoice;

  
}
/**
* This mediaSearch method sorts the global ArrayList by mediaName first. Then, it prompts the user for the name of the media, and uses binarySearch to look for a media match
* in the ArrayList. Then it prints the object found using the index returned by the binarySearch method.
*/
public void mediaSearch() {
  
try
{
Collections.sort(myMedia);
String userMedia;
int index;
System.out.println("What media are you searching for? (Fill all spaces with underscores if necessary)");
userMedia = kb.next();
Media searchMedia = new Media("Artist", userMedia);
index = Collections.binarySearch(myMedia, searchMedia);
System.out.println(myMedia.get(index));
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Media not found!");
}

}
/**
* This artistSearch method sorts the global ArrayList by artistName first. Then, it prompts the user for the name of the artist, and uses binarySearch to look for an artist match
* in the ArrayList. Since there is more than one object with the same artist name in most of the cases, it will loop backwards and forwards the index till it finds a different artist name.
* This way it prints all the objects with the same artistName.
*/
public void artistSearch() {
  
try
{
Collections.sort(myMedia, new ComparatorByArtistName());
String userArtist;
int index;
System.out.println("What artist are you searching for? (Fill all spaces with underscores if necessary)");
userArtist = kb.nextLine();
Media searchArtist = new Media(userArtist, "media");
index = Collections.binarySearch(myMedia, searchArtist, new ComparatorByArtistName());
System.out.println(myMedia.get(index));
int i = index - 1;
while( i >= 0 && myMedia.get(i).getArtistName().equals(myMedia.get(index).getArtistName()))
{
System.out.println(myMedia.get(i));
i--;
}
i = index + 1;
while(i < myMedia.size() && myMedia.get(i).getArtistName().equals(myMedia.get(index).getArtistName()))
{
System.out.println(myMedia.get(i));
i++;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Artist not found!");
}
  
}
/**
* This addMedia method opens the file again. Depending on the type of media the user selects, it will prompt the user for the corresponding info.
* It will create the corresponding object and will use the toString method of each of them to append the info to the file. Then it closes the file.
*/
public void addMedia(){
  
String mediaType, artistName, mediaName, year, song;
ArrayList<String> songs = new ArrayList<String>();
boolean moreSongs = true;
boolean correctInput;
try
{
FileWriter writeFile = new FileWriter(fileName,true);
PrintWriter printFile = new PrintWriter(writeFile);
do
{
System.out.println("Do you want to add a CD Media or a DVD media? (Enter CD or DVD)");
mediaType = kb.nextLine();
if(!mediaType.equalsIgnoreCase("CD")&&!mediaType.equalsIgnoreCase("DVD")){
System.out.println("Incorrect input. Try again!");
}
}
while(!mediaType.equalsIgnoreCase("CD")&&!mediaType.equalsIgnoreCase("DVD"));
System.out.println("What is the name of the artist? (Fill all spaces with underscores if necessary)");
artistName = kb.nextLine();
System.out.println("What is the name of the media (album or movie)(Fill all spaces with underscores if necessary)");
mediaName = kb.nextLine();
if(mediaType.equalsIgnoreCase("CD"))
{
do
{
System.out.println("What is the name of the song? (Fill all spaces with underscores if necessary)");
songs.add(kb.nextLine());
correctInput = false;
while(!correctInput)
{
try
{
System.out.println("More songs in the album?(true or false)");
moreSongs = kb.nextBoolean();
kb.nextLine();
correctInput = true;
}
catch(InputMismatchException e)
{
System.out.println("Incorrect Input!");
kb.nextLine();
  
}
}
}
while(moreSongs);
CDMedia newCD = new CDMedia(songs, artistName, mediaName);
myMedia.add(newCD);
printFile.println(newCD);
}
else if(mediaType.equalsIgnoreCase("DVD"))
{
System.out.println("What is the year of the movie?");
year = kb.nextLine();
DVDMedia newDVD = new DVDMedia(year, artistName, mediaName);
myMedia.add(newDVD);
printFile.println(newDVD);
}
printFile.close();
}
catch (IOException e)
{
e.getMessage();
}
  
}
  
}
Code Screenshots:

Working Code Output Screenshot:

If this answer helps you, please hit thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 2 Using the Album and Music StoreDemo from problem 1, write a method to find...

    Problem 2 Using the Album and Music StoreDemo from problem 1, write a method to find all of the albums that are cheaper than a specific price In the MusicStoreDemo, add a new method called getAlbumsCheaperThan. The method passes in the ArrayList of albums and the price. The method will return an ArrayList of all albums cheaper than the price passed in. Update the main method in the Music Store Demo and have it find all of the albums that...

  • Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object con...

    Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object consists of the following fields: int rollno String name String address Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method and place your code in a separate Java source file. Do not use a sort method from the Java collections library.

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees....

    JAVA Problem:  Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • 1. Do the following a. Write a class Student that has the following attributes: - name:...

    1. Do the following a. Write a class Student that has the following attributes: - name: String, the student's name ("Last, First" format) - enrollment date (a Date object) The Student class provides a constructor that saves the student's name and enrollment date. Student(String name, Date whenEnrolled) The Student class provides accessors for the name and enrollment date. Make sure the class is immutable. Be careful with that Date field -- remember what to do when sharing mutable instance variables...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • In this next example, we'll build two classes that implement the Cloneable Interface. The QuizTracker class...

    In this next example, we'll build two classes that implement the Cloneable Interface. The QuizTracker class contains an ArrayList of QuizScore objects, and it's up to us to build these two classes so that we can make deep copies of QuizTrackers (and share no internal aliases). The key idea here is that to make deep copies of compound objects (or lists of objects), we need to copy (using clone) every object in every list, and even make copies of the...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT