Skip to content

Instantly share code, notes, and snippets.

@thecodeduchess
Last active January 11, 2016 02:47
Show Gist options
  • Select an option

  • Save thecodeduchess/b927732572bd925369a5 to your computer and use it in GitHub Desktop.

Select an option

Save thecodeduchess/b927732572bd925369a5 to your computer and use it in GitHub Desktop.
Step 1 of the music player, Make a music organizer class
import java.util.ArrayList;
/**
* A class to hold details of audio files.
*/
public class MusicOrganizer
{
// An ArrayList for storing the file names of music files.
private ArrayList<String> files;
/**
* Create a MusicOrganizer
*/
public MusicOrganizer()
{
files = new ArrayList<String>();
}
/**
* Add a file to the collection.
* @param filename The file to be added.
*/
public void addFile(String filename)
{
files.add(filename);
}
/**
* Return the number of files in the collection.
* @return The number of files in the collection.
*/
public int getNumberOfFiles()
{
return files.size();
}
/**
* Exercise 4.14 ASSIGNMENT
*/
public void checkIndex(int num)
{
if (num < 0 || num > files.size()-1) {
System.out.println("Not a valid range. A valid range is between 0 and the number of files in the collection.");
}
}
/**
* Exercise 4.15 ASSIGNMENT
*/
public boolean validIndex(int num)
{
if (num < 0 || num > files.size()-1) {
return false;
}
else{
return true;
}
}
/**
* List a file from the collection.
* @param index The index of the file to be listed.
* Modify method, exercise 4.16 (Not implemented) Add my code
*/
public void listFile(int index)
{
if(index >= 0 && index < files.size()) {
String filename = files.get(index);
System.out.println(filename);
}
else {
System.out.println("No file added");
}
}
/**
* Remove a file from the collection.
* @param index The index of the file to be removed.
*/
public void removeFile(int index)
{
if(index >= 0 && index < files.size()) {
files.remove(index);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment