Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.*;
- public class SongCollection {
- //fields
- public static ArrayList<Song> songCollection = new ArrayList<Song>();
- public static ArrayList<Song> currentCollection = new ArrayList<Song>();
- //main
- public static void main(String[] args) {
- //read in the file, iterate through it and add to the song collection ArrayList
- try {
- Scanner songsFile = new Scanner(new File("C:\\Users\\s-weidnerl\\eclipse-workspace\\Unit8Project\\src\\agazillionsongs.txt")); //read in the file
- while (songsFile.hasNextLine()) {
- songCollection.add(new Song(songsFile.nextLine()));
- }
- songsFile.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- resetCollection();
- filterYear(new Range("1995-1995"));
- //printCollection();
- }
- //filters
- public static void filterYear(Range yearRange) {
- sortYear();
- //if the first value is greater than the max or less than the min.
- System.out.println(currentCollection.get(0).getYear());
- System.out.println(yearRange.getMax());
- if (currentCollection.get(0).getYear() < yearRange.getMax()) { //if the fist item is greater than the greatest value
- int minIndex = -1;
- int maxIndex = -1;
- ArrayList<Song> newCollection = new ArrayList<Song>();
- for (int i = 0; i < currentCollection.size() || maxIndex >= 0; i++) {
- if (minIndex < 0 && currentCollection.get(i).getYear() >= yearRange.getMin()) { //look for first instance within range in sorted list
- minIndex = i;
- System.out.println("Min index set to " + minIndex);
- }
- if (maxIndex < 0 && currentCollection.get(i).getYear() < yearRange.getMax()) { //find last index within range in sorted list
- maxIndex = i-1;
- }
- }
- for (int i = minIndex; i == maxIndex; i++) { //construct ArrayList of only values between max and min
- newCollection.add(currentCollection.get(i));
- }
- currentCollection = newCollection;
- } else {
- System.out.println("That is not a valid range.");
- }
- }
- public static void filterRank(Range rankRange) {
- }
- public static void filterArtist(String filter) {
- }
- public static void filterTitle(String filter) {
- }
- //sorts
- public static void sortYear() {
- for (int i = 1; i < currentCollection.size(); i++) { //for each value after the first
- int currentValue = currentCollection.get(i).getYear(); //set current value to the year of the current element
- int j = i; //set a temp variable for index swapping
- 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
- 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
- currentCollection.set(j, currentCollection.get(j-1)); //set j-1 to j's value
- currentCollection.set(j-1, temp); //set j to temp (j-1's) value
- j--; //reduce j by 1
- System.out.println(j);
- }
- }
- }
- public static void sortRank() {
- }
- public static void sortArtist() {
- }
- public static void sortTitle() {
- }
- //Other Methods
- public static void resetCollection() {
- currentCollection = songCollection;
- }
- public static void printCollection() {
- System.out.println("Rank\tYear\tSong");
- for (int song = 0; song < currentCollection.size(); song++) {
- System.out.println(currentCollection.get(song));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement