Welcome to Dream.In.Code
Become a Java Expert!

Join 149,527 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,470 people online right now. Registration is fast and FREE... Join Now!




Music Media Collection

 
Reply to this topicStart new topic

Music Media Collection

charcarty
4 Jul, 2007 - 02:57 PM
Post #1

New D.I.C Head
*

Joined: 1 May, 2007
Posts: 1


My Contributions
This (Java) is my first programming language, and therefore have nothing to compare it to.
Class project that has been building over the past weeks, consists of 7 files.
CommandLineInterfaceDriver, MusicMediaManager, MusicMedia (abstract), CD, DVD, Eight Track, and Track [] .

It has been extremely frustrating, when getting errors that I am not able to resolve. I would appreciate any assistance with these. The CD, DVD, and Eight Track should be identical except they have different max numbers of track, max length per track, and max length per media. I have been trying code and commenting it out, so there is probably some still hanging around.
CODE
import java.util.*;

public class CommandLineInterfaceDriver
{
     private MusicMediaManager m_manager;
     private Scanner m_input;

     /**************************************************************************
     *
     *    Basic constructor creates a data structure to hold all the music media.
     *
     **************************************************************************/
     public CommandLineInterfaceDriver()
     {
          m_manager = new MusicMediaManager();
          m_input = new Scanner(System.in);
     }

     /**************************************************************************
     *
     *    mainMenu
     *
     **************************************************************************/
     public void mainMenu()
     {
          String selection;

          do
          {

               System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
               System.out.println("****************************************************");
               System.out.println("*      Welcome to Music Media Manager      *");
               System.out.println("****************************************************");

               System.out.println("What would you like to do with the media today?\n");
               System.out.println("Add Media    [add]");
               System.out.println("Edit Media   [edit]");
               System.out.println("View Media   [view]");
               System.out.println("Remove Media [remove]");
               System.out.println("Quit         [quit]");
               System.out.println("Please type in the keywork of your media selection\n");
               System.out.print("\n> ");

               selection = m_input.nextLine();
               selection = selection.trim();
               selection = selection.toUpperCase();


               if( selection.equals("ADD") )
               {
                    addSubmenu();
                    m_input.nextLine();
               }
               else if( selection.equals("EDIT") )
               {
                    editSubmenu();
               }
               else if( selection.equals("VIEW") )
               {
                    viewSubmenu();
               }
               else if( selection.equals("REMOVE") )
               {
                    removeSubmenu();
               }

               else if( selection.equals("LOAD") )
               {
                    m_manager.addMedia(new MusicMedia("0001", "Mars", "Mars", 2005));
                    m_manager.addMedia(new MusicMedia("0010", "Reload", "Metallica", 1997));
                  
                    System.out.println("\nTest media is loaded...");
                    System.out.println("\nPress Enter key to continue...");

                    m_input.nextLine();
               }

               else if( selection.equals("SORT") )
               {
                    m_manager.sortCollection();

                    System.out.println("\nMedia collection sorted");
                    System.out.println("\nPress Enter key to continue...");

                    m_input.nextLine();
               }

               else if( selection.equals("") )
               {
                    // do nothing
               }
               else if( selection.equals("QUIT") || selection.equals("EXIT") )
               {
                    return;
               }
               else
               {
                    System.out.println("Invalid m_input, try again");

                    System.out.println("\nPress Enter key to continue");
                    m_input.nextLine();
               }
          }
          while(true);

     }

     /**************************************************************************
     *
     *    addSubmenu
     *
     **************************************************************************/
     public void addSubmenu()
     {
          String title, artist, barcode;
          int year;
          double value;

          System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          System.out.println("****************************************************");
          System.out.println("*      Welcome to Music Media Manager      *");
          System.out.println("****************************************************");

          System.out.println("What type media do you wish to access\n");
          System.out.println("Compact Disk        [cd]");
          System.out.println("DVD                 [dvd]");
          System.out.println("Eight Track         [et]");
          System.out.println("Quit                [quit]");
          System.out.println("Please type in the keywork of your media selection\n");
          System.out.print("\n> ");

          selection = m_input.nextLine();
          selection = selection.trim();
          selection = selection.toUpperCase();

          System.out.println("\n***Add a New Music Media***");
          System.out.print("Enter Title:         ");
          title = m_input.nextLine();

          System.out.print("Enter Artist:        ");
          artist = m_input.nextLine();

          System.out.print("Enter Barcode:       ");
          barcode = m_input.nextLine();

          System.out.print("Enter Year Released: ");
          year = m_input.nextInt();

          System.out.print("Enter Value:         ");
          value = m_input.nextInt();

          if(selection.equals("CD"))
          {
               m_manager.addMusicMedia(new CD(barcode, title, artist, year, value));
          }
          else if( selection.equals("DVD") )
          {
               m_manager.addMusicMedia(new DVD(barcode, title, artist, year, value));
          }
          else if( selection.equals("ET") )
          {
               m_manager.addMusicMedia(new ET(barcode, title, artist, year, value));
          }
     }
     /**************************************************************************
     *
     *    editSubmenu
     *
     **************************************************************************/
     public void editSubmenu()
     {
          MusicMedia media;
          String selection;

          System.out.println("\n***Edit an Existing Media***");
          System.out.print("Enter Title:      ");

          media = m_manager.searchCollection(m_input.nextLine());

          if( media == null )
          {
               System.out.println("\nMEdia by that title not found");
               return;
          }

          System.out.println("Media found!");
          System.out.println("Make change or accept default:");

          System.out.print("Media Title [" + media.getTitle() + "]: ");
          selection = m_input.nextLine();
          selection = selection.trim();

          if( selection.length() > 0 )
          {
               media.setTitle(selection);
          }

          System.out.print("Media Artist [" + media.getArtist() + "]: ");
          selection = m_input.nextLine();
          selection = selection.trim();

          if( selection.length() > 0 )
          {
               media.setArtist(selection);
          }

          System.out.print("Media Barcode [" + media.getBarcode() + "]: ");
          selection = m_input.nextLine();
          selection = selection.trim();

          if( selection.length() > 0 )
          {
               media.setBarcode(selection);
          }

          System.out.print("Media Year [" + media.getYear() + "]: ");
          selection = m_input.nextLine();
          selection = selection.trim();

          if( selection.length() > 0 )
          {
               try
               {
                    media.setYear(Integer.parseInt(selection));
               }
               catch(Exception ex)
               {
                    System.out.println(ex);
               }
          }

          System.out.print("Media Value [" + media.getValue() + "]: ");
          selection = m_input.nextLine();
          selection = selection.trim();

          if( selection.length() > 0 )
          {
               media.setYear(selection);
          }

          m_manager.setModified();
     }

     /**************************************************************************
     *
     *    viewSubmenu
     *
     **************************************************************************/
     public void viewSubmenu()
     {
          MusicMedia media;
          String selection;

          do
          {
               System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
               System.out.println("View Specific Media [media]");
               System.out.println("View All Media      [all]");
               System.out.println("Return to Main Menu [return]\n");
               System.out.print("> ");

               selection = m_input.nextLine();
               selection = selection.trim();
               selection = selection.toUpperCase();

               if( selection.equals("Media") )
               {
                    System.out.print("Enter Media Title: ");
                    media = m_manager.searchCollection(m_input.nextLine());

                    if( media != null )
                    {
                         media.printSingleEntry();
                    }
                    else
                    {
                         System.out.println("\nNo such Media found");
                    }

                    System.out.println("\nPress Enter key to continue");
                    m_input.nextLine();
               }
               else if( selection.equals("ALL") )
               {
                    System.out.println("\n***List of all Media's***");

                    m_manager.print();

                    System.out.println("\nPress Enter key to continue");
                    m_input.nextLine();
               }

               else if( selection.equals("") )
               {
                    // do nothing
               }
               else if( selection.equals("RETURN") )
               {
                    return;
               }
               else
               {
                    System.out.println("Invalid m_input, try again");

                    System.out.println("\nPress Enter key to continue");
                    m_input.nextLine();
               }
          }
          while(true);
     }

     /**************************************************************************
     *
     *    removeSubmenu
     *
     **************************************************************************/
     public void removeSubmenu()
     {
          MusicMedia media;
          String title;

          System.out.println("\n***Remove Media***");
          System.out.print("Enter Media Title:      ");

          title = m_input.nextLine();

          if( m_manager.removeMusicMedia(title) )
          {
               System.out.println("\nMedia \"" + title + "\" removed from collection");
          }
          else
          {
               System.out.println("\nMedia \"" + title + "\" was not found");
          }

          System.out.println("\nPress Enter key to continue");
          m_input.nextLine();
     }

     /**************************************************************************
     *
     *    main
     *
     **************************************************************************/
     public static void main(String[] args)
     {
          new CommandLineInterfaceDriver().mainMenu();
     }
}


CODE
import java.util.*;

public class MusicMediaManager
{
     private ArrayList<MusicMedia> m_Collection;
     private boolean m_isSorted;
     private Track[] m_tracks;

     /**************************************************************************
     *
     *    Basic constructor creates a data structure to hold all the MusicMedia.
     *
     **************************************************************************/
     public MusicMediaManager()
     {
          m_Collection = new ArrayList<MusicMedia>();
          m_isSorted = false;
     }

     /**************************************************************************
     *
     *    addMusicMedia
     *
     **************************************************************************/
     public void addMusicMedia(MusicMedia media)
     {
          Scanner m_input = new Scanner(System.in);
          m_Collection.add(media);
          m_isSorted = false;
     }

     /**************************************************************************
     *
     *    removeMusicMedia
     *
     **************************************************************************/
     public boolean removeMusicMedia(String title)
     {
          for(int index = 0; index < m_collection.size(); index++)
          {
               if( m_Collection.get(index).getTitle().equals(title) )
               {
                    m_Collection.remove(index);
                    return true;
               }
          }

          return false;
     }

     /**************************************************************************
     *
     *    size
     *
     **************************************************************************/
     public int size()
     {
          return m_Collection.size();
     }

     /**************************************************************************
     *
     *    setModified
     *
     **************************************************************************/
     public void setModified()
     {
          m_isSorted = false;
     }

     /**************************************************************************
     *
     *    sortCollection
     *
     **************************************************************************/
     public void sortCollection()
     {
          CompactDisk[] array;

          if( !m_isSorted )
          {
               array = m_Collection.toArray(new MusicMedia[0]);
               sortArray(array);

               m_Collection.clear();

               for(int index = 0; index < array.length; index++)
               {
                    m_Collection.add(array[index]);
               }

               m_isSorted = true;
          }
     }

     /**************************************************************************
     *
     *    sortArray
     *
     **************************************************************************/
     private void sortArray(MusicMedia[] list)
     {
          int j;

          for (int i=0; i<list.length-1; i++)
          {
               j = indexOfNextSmallest(list, i);
               swapArrayElements(list, i, j);
          }
     }

     /**************************************************************************
     *
     *    indexOfNextSmallest
     *
     **************************************************************************/
     private int indexOfNextSmallest(MusicMedia[] list, int startIndex)
     {
          String minMediaTitle = list[startIndex].getTitle();
          int minIndex = startIndex;

          for (int i=startIndex+1; i<list.length; i++)
          {
               if(list[i].getTitle().compareTo(minMediaTitle) < 0)
               {
                    minMediaTitle = list[i].getTitle();
                    minIndex = i;
               }
          }

          return minIndex;
     }

     /**************************************************************************
     *
     *    swapArrayElements
     *
     **************************************************************************/
     private void swapArrayElements(MusicMedia[] list, int i, int j)
     {
          MusicMedia temp;

          temp = list[i];
          list[i] = list[j];
          list[j] = temp;
     }

     /**************************************************************************
     *
     *    searchCollection
     *
     **************************************************************************/
     public MusicMedia searchCollection(String title)
     {
          int comparator;

          if( !m_isSorted )
          {
               sortCollection();
          }

          for(int index = 0; index < m_Collection.size(); index++)
          {
               comparator = m_Collection.get(index).getTitle().compareTo(title);

               // If match, return it
               if( comparator == 0 )
               {
                    return m_Collection.get(index);
               }

               // Finish early if we find a title that occurs alphabetically after
               // the title we're looking for
               if( comparator > 0 )
               {
                    return null;
               }
          }

          return null;
     }

     /**************************************************************************
     *
     *    print
     *
     **************************************************************************/
     public void print()
     {
          if( m_Collection.size() == 0 )
          {
               System.out.println("No Music Media");
               return;
          }

          for(int index = 0; index < m_Collection.size(); index++)
          {
               m_Collection.get(index).printLineItem();
          }
     }

}

CODE
import java.util.*;

public abstract class MusicMedia
{
     private String m_barcode;
     private String m_title;
     private String m_artist;
     private int m_year;
     private double m_value;
     //private Track[] m_tracks;
     private int m_numTracks;
     private ArrayList<MusicMedia>m_media;
     private String m_marker;

     /*********************************************************************
     *
     *    Only constructor allowed requires a barcode, title, artist,
     *    year released, and value.
     *
     *********************************************************************/
     public MusicMedia(String barcode, String title, String artist,
                         int year, double value, String m)
     {
          m_barcode = "00000000000";
          m_title = "No Title";
          m_artist = "No Artist";
          m_year = -1;
          m_marker = new String (m);
     }

     /*******************************************************************
     *
     *
     *
     *******************************************************************/

     public void print()
     {
          System.out.println(m_barcode + ", " + m_title + ", " + m_artist + ", " +
               m_year + m_value + ",");
     }
}
     // Sub classes need to implement these methods
     //public abstract double calculateArea();
     //public abstract String getType();


CODE
import java.util.*;

public class CD extends MusicMedia
{
     private static final int MAX_NUM_TRACKS = 15;
     private static final int MAX_LEN_TRACK = 8;
     private static final int MAX_LEN_MEDIA = 100;


     /**************************************************************************
     *
     *    Only constructor allowed requires a barcode, title, artist and year released
     *
     **************************************************************************/
     public CD(String barcode, String title, String artist, int year, double value,
          String m)
     {
          super(barcode, title, artist, year, value);

     }

     /**************************************************************************
     *
     *    Set the barcode to any sequence of 0 or 1
     *
     **************************************************************************/
     public void setBarcode(String barcode)
     {
          if( barcode.matches("[01]*") )
          {
               m_barcode = new String(barcode);
          }
     }

     /**************************************************************************
     *
     *    Get the barcode
     *
     **************************************************************************/
     public String getBarcode()
     {
          return new String(m_barcode);
     }

     /**************************************************************************
     *
     *    Sets the title only if it starts with a capital letter
     *
     **************************************************************************/
     public void setTitle(String title)
     {
          if( title.matches("[A-Z][A-Za-z0-9 ]*") )
          {
               m_title = new String(title);
          }
     }

     /**************************************************************************
     *
     *    Get the title
     *
     **************************************************************************/
     public String getTitle()
     {
          return new String(m_title);
     }

     /**************************************************************************
     *
     *    Sets the artist only if it starts with a capital letter
     *
     **************************************************************************/
     public void setArtist(String artist)
     {
          if( artist.matches("[A-Za-z0-9 ]*") )
          {
               m_artist = new String(artist);
          }
     }

     /**************************************************************************
     *
     *    Gets the artist name
     *
     **************************************************************************/
     public String getArtist()
     {
          return new String(m_artist);
     }

     /**************************************************************************
     *
     *    Sets the year only if between 1980-2100
     *
     **************************************************************************/
     public void setYear(int year)
     {
          if( year >= 1980 && year <= 2100 )
          {
               m_year = year;
          }
     }

     /**************************************************************************
     *
     *    Gets the year
     *
     **************************************************************************/
     public int getYear()
     {
          return m_year;
     }

     /**************************************************************************
     *
     *    Adds up to a certain number of tracks (specified by MAX_NUM_TRACKS).
     *    Any additional tracks are not recorded.
     *
     **************************************************************************/
     public void addTrack(String title, int duration)
     {
          if(m_tracks != null && m_numTracks < m_tracks.length)
          {
               m_tracks[m_numTracks] = new Track(title, duration);
               m_numTracks++;
          }
     }

     /**************************************************************************
     *
     *    Return the track specified by the track title
     *
     **************************************************************************/
     public Track getTrackByTitle(String title)
     {
          if( m_tracks != null )
          {
               for(int index = 0; index < m_numTracks && index < m_tracks.length; index++)
               {
                    // Return first match found
                    if( m_tracks[index].getTitle().equals(title) )
                    {
                         return m_tracks[index];
                    }
               }
          }

          return null;
     }

     /**************************************************************************
     *
     *    Print the CD data and track data
     *
     **************************************************************************/
     public void printSingleEntry()
     {
          System.out.println("\nBarcode:  " + m_barcode);
          System.out.println("Title:    " + m_title);
          System.out.println("Artist:   " + m_artist);
          System.out.println("Released: " + m_year);
          System.out.println("Value:    " + m_value);
          System.out.println("Tracks: ");

          for(int index = 0; index < m_numTracks && index < m_tracks.length; index++)
          {
               System.out.print("[" + index + "] " + m_tracks[index].getTitle() + " : ");
               System.out.println(m_tracks[index].getDurationSeconds() + " sec");
          }
     }

     /**************************************************************************
     *
     *    Print the CD data and track data
     *
     **************************************************************************/
     public void printLineItem()
     {
          System.out.println(m_barcode + ", " + m_title + ", " + m_artist + ", "
                              + m_year + m_value + ",");
     }

     /**************************************************************************
     *
     *    main - for diagnostic purposes
     *
     **************************************************************************/
     /*public static void main(String[] args)
     {
          media media = new MusicMedia("0110", "The Kill", "Mars", 2006);
          media.addTrack("The Kill (Bury
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month