Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 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.      * Exercise 4/16
  45.      *
  46.      */
  47.     public void listFile(int index)
  48.     {
  49.         if(validIndex(index)) {
  50.             String filename = files.get(index);
  51.             System.out.println(filename);
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * Remove a file from the collection.
  57.      * @param index The index of the file to be removed.
  58.      */
  59.     public void removeFile(int index)
  60.     {
  61.         if(validIndex(index)) {
  62.             files.remove(index);
  63.         }
  64.     }
  65.    
  66.     /**
  67.      * Exercise 4.15
  68.      */
  69.     public boolean validIndex(int index)
  70.     {
  71.        if(index < 0 || index >= files.size()) {
  72.            return false;
  73.        }
  74.        else {
  75.            return true;
  76.        }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement