Advertisement
Guest User

song collection class

a guest
Mar 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.*;
  4. public class SongCollection {
  5. //fields
  6. public static ArrayList<Song> songCollection = new ArrayList<Song>();
  7. public static ArrayList<Song> currentCollection = new ArrayList<Song>();
  8.  
  9. //main
  10. public static void main(String[] args) {
  11. //read in the file, iterate through it and add to the song collection ArrayList
  12. try {
  13. Scanner songsFile = new Scanner(new File("C:\\Users\\s-weidnerl\\eclipse-workspace\\Unit8Project\\src\\agazillionsongs.txt")); //read in the file
  14. while (songsFile.hasNextLine()) {
  15. songCollection.add(new Song(songsFile.nextLine()));
  16. }
  17. songsFile.close();
  18. } catch (FileNotFoundException e) {
  19. e.printStackTrace();
  20. }
  21. resetCollection();
  22. filterYear(new Range("1995-1995"));
  23. //printCollection();
  24. }
  25.  
  26. //filters
  27. public static void filterYear(Range yearRange) {
  28. sortYear();
  29. //if the first value is greater than the max or less than the min.
  30. System.out.println(currentCollection.get(0).getYear());
  31. System.out.println(yearRange.getMax());
  32. if (currentCollection.get(0).getYear() < yearRange.getMax()) { //if the fist item is greater than the greatest value
  33. int minIndex = -1;
  34. int maxIndex = -1;
  35. ArrayList<Song> newCollection = new ArrayList<Song>();
  36. for (int i = 0; i < currentCollection.size() || maxIndex >= 0; i++) {
  37. if (minIndex < 0 && currentCollection.get(i).getYear() >= yearRange.getMin()) { //look for first instance within range in sorted list
  38. minIndex = i;
  39. System.out.println("Min index set to " + minIndex);
  40. }
  41. if (maxIndex < 0 && currentCollection.get(i).getYear() < yearRange.getMax()) { //find last index within range in sorted list
  42. maxIndex = i-1;
  43. }
  44. }
  45. for (int i = minIndex; i == maxIndex; i++) { //construct ArrayList of only values between max and min
  46. newCollection.add(currentCollection.get(i));
  47. }
  48. currentCollection = newCollection;
  49. } else {
  50. System.out.println("That is not a valid range.");
  51. }
  52. }
  53.  
  54. public static void filterRank(Range rankRange) {
  55.  
  56. }
  57.  
  58. public static void filterArtist(String filter) {
  59.  
  60. }
  61.  
  62. public static void filterTitle(String filter) {
  63.  
  64. }
  65.  
  66. //sorts
  67. public static void sortYear() {
  68. for (int i = 1; i < currentCollection.size(); i++) { //for each value after the first
  69. int currentValue = currentCollection.get(i).getYear(); //set current value to the year of the current element
  70. int j = i; //set a temp variable for index swapping
  71. while(j > 0 && currentCollection.get(j).getYear() < currentCollection.get(j-1).getYear()) { //while j is above 0th index, and while it's less than the value before it
  72. Song temp = new Song(String.format("%d\t%d\t%s\t%s", currentCollection.get(j).getYear(),currentCollection.get(j).getRank(),currentCollection.get(j).getArtist(),currentCollection.get(j).getTitle())); //set a temp variable to j's index
  73. currentCollection.set(j, currentCollection.get(j-1)); //set j-1 to j's value
  74. currentCollection.set(j-1, temp); //set j to temp (j-1's) value
  75. j--; //reduce j by 1
  76. System.out.println(j);
  77. }
  78. }
  79. }
  80.  
  81. public static void sortRank() {
  82.  
  83. }
  84.  
  85. public static void sortArtist() {
  86.  
  87. }
  88.  
  89. public static void sortTitle() {
  90.  
  91. }
  92.  
  93. //Other Methods
  94. public static void resetCollection() {
  95. currentCollection = songCollection;
  96. }
  97.  
  98. public static void printCollection() {
  99. System.out.println("Rank\tYear\tSong");
  100. for (int song = 0; song < currentCollection.size(); song++) {
  101. System.out.println(currentCollection.get(song));
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement