Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /**
- * A class to hold details of audio files.
- *
- * @author David J. Barnes and Michael Kölling
- * @version 2016.02.29
- */
- 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<>();
- }
- /**
- * 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();
- }
- /**
- * List a file from the collection.
- * @param index The index of the file to be listed.
- *
- * Exercise 4/16
- *
- */
- public void listFile(int index)
- {
- if(validIndex(index)) {
- String filename = files.get(index);
- System.out.println(filename);
- }
- }
- /**
- * Remove a file from the collection.
- * @param index The index of the file to be removed.
- */
- public void removeFile(int index)
- {
- if(validIndex(index)) {
- files.remove(index);
- }
- }
- /**
- * Exercise 4.15
- */
- public boolean validIndex(int index)
- {
- if(index < 0 || index >= files.size()) {
- return false;
- }
- else {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement