Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 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.     // A player for the music files.
  14.     private MusicPlayer player;
  15.        
  16.     /**
  17.      * Create a MusicOrganizer
  18.      */
  19.     public MusicOrganizer()
  20.     {
  21.         files = new ArrayList<>();
  22.         player = new MusicPlayer();
  23.     }
  24.    
  25.     /**
  26.      * Add a file to the collection.
  27.      * @param filename The file to be added.
  28.      */
  29.     public void addFile(String filename)
  30.     {
  31.         files.add(filename);
  32.     }
  33.    
  34.     /**
  35.      * Return the number of files in the collection.
  36.      * @return The number of files in the collection.
  37.      */
  38.     public int getNumberOfFiles()
  39.     {
  40.         return files.size();
  41.     }
  42.    
  43.     /**
  44.      * List a file from the collection.
  45.      * @param index The index of the file to be listed.
  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.      * Show a list of all the files in the collection.
  57.      */
  58.     public void listAllFiles()
  59.     {
  60.         for(String filename : files) {
  61.             System.out.println(filename);
  62.         }
  63.     }
  64.    
  65.     /**
  66.      * Exercise 4.26
  67.      */
  68.     public void listMatching(String searchString)
  69.     {
  70.         boolean isMatch;
  71.         isMatch = true;
  72.         for(String filename : files) {
  73.             if(filename.contains(searchString)) {
  74.                 // A match.
  75.                 System.out.println(filename);
  76.             }
  77.         }
  78.         if (isMatch) {
  79.             System.out.println("Could not find the given file: "
  80.                                + searchString);
  81.         }
  82.     }
  83.    
  84.     /**
  85.      * Remove a file from the collection.
  86.      * @param index The index of the file to be removed.
  87.      */
  88.     public void removeFile(int index)
  89.     {
  90.         if(validIndex(index)) {
  91.             files.remove(index);
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Start playing a file in the collection.
  97.      * Use stopPlaying() to stop it playing.
  98.      * @param index The index of the file to be played.
  99.      */
  100.     public void startPlaying(int index)
  101.     {
  102.         if(validIndex(index)) {
  103.             String filename = files.get(index);
  104.             player.startPlaying(filename);
  105.         }
  106.     }
  107.  
  108.     /**
  109.      * Stop the player.
  110.      */
  111.     public void stopPlaying()
  112.     {
  113.         player.stop();
  114.     }
  115.  
  116.     /**
  117.      * Play a file in the collection. Only return once playing has finished.
  118.      * @param index The index of the file to be played.
  119.      */
  120.     public void playAndWait(int index)
  121.     {
  122.         if(validIndex(index)) {
  123.             String filename = files.get(index);
  124.             player.playSample(filename);
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Determine whether the given index is valid for the collection.
  130.      * Print an error message if it is not.
  131.      * @param index The index to be checked.
  132.      * @return true if the index is valid, false otherwise.
  133.      */
  134.     private boolean validIndex(int index)
  135.     {
  136.         // The return value.
  137.         // Set according to whether the index is valid or not.
  138.         boolean valid;
  139.        
  140.         if(index < 0) {
  141.             System.out.println("Index cannot be negative: " + index);
  142.             valid = false;
  143.         }
  144.         else if(index >= files.size()) {
  145.             System.out.println("Index is too large: " + index);
  146.             valid = false;
  147.         }
  148.         else {
  149.             valid = true;
  150.         }
  151.         return valid;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement