Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /**
  4.  * A class to hold details of audio files.
  5.  *
  6.  * @author David J. Barnes and Michael Kölling
  7.  * @version 2016.02.29
  8.  */
  9. public class MusicOrganizer
  10. {
  11.     // An ArrayList for storing the file names of music files.
  12.     private ArrayList<String> files;
  13.        
  14.     /**
  15.      * Create a MusicOrganizer
  16.      */
  17.     public MusicOrganizer()
  18.     {
  19.         files = new ArrayList<>();
  20.     }
  21.    
  22.     /**
  23.      * Add a file to the collection.
  24.      * @param filename The file to be added.
  25.      */
  26.     public void addFile(String filename)
  27.     {
  28.         files.add(filename);
  29.     }
  30.    
  31.     /**
  32.      * Return the number of files in the collection.
  33.      * @return The number of files in the collection.
  34.      */
  35.     public int getNumberOfFiles()
  36.     {
  37.         return files.size();
  38.     }
  39.    
  40.     /**
  41.      * List a file from the collection.
  42.      * @param index The index of the file to be listed.
  43.      */
  44.     public void listFile(int index)
  45.     {
  46.         if(index >= 0 && index < files.size()) {
  47.             String filename = files.get(index);
  48.             System.out.println(filename);
  49.         }
  50.     }
  51.    
  52.     /**
  53.      * Remove a file from the collection.
  54.      * @param index The index of the file to be removed.
  55.      */
  56.     public void removeFile(int index)
  57.     {
  58.         if(index >= 0 && index < files.size()) {
  59.             files.remove(index);
  60.         }
  61.     }
  62.    
  63.     /**
  64.      * Exercise 4.15
  65.      */
  66.     public boolean validIndex(int index)
  67.     {
  68.        if(index < 0 || index >= files.size()) {
  69.            return false;
  70.        }
  71.        else {
  72.            return true;
  73.        }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement