对歌曲集合进行排序和过滤

时间:2014-03-16 06:43:07

标签: arraylist

以下是作业的链接: pastelink.me/dl/bf6659#sthash.LtZIJpI7.dpuf

我需要完成SongCollection类才能到达第二个检查点。我对编码很陌生,而且我不是很擅长,所以我非常感谢你的所有答案!非常感谢。这是我到目前为止所做的工作:

主要课程:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class GazillionSongs {
public static void main(String[] args){
    try {
        System.out.println("Hello.  This program will sort and filter large databases of popular songs.  Please enter an input file.");
        Scanner file = new Scanner(System.in);
        String userSelection = file.nextLine();

        if (userSelection.toLowerCase().contains("gazillion")){
        Scanner input = new Scanner(new File("C:/Users/julie_000/workspace/juliet/bin/agazillionsongs.txt"));
        String user = input.nextLine();

        Song songObject = new Song(user);
        System.out.println(songObject.toString());

        }

    } catch (FileNotFoundException e) { 
        System.out.println("File not found"); 
    }
}
}

歌曲课程:

import java.util.StringTokenizer;

public class Song {
private static int year;
private static int rank;
private static String artist;
private static String title;

public Song(String s){
    StringTokenizer parseLine = new StringTokenizer(s, "\t");
    String strYear = parseLine.nextToken();
    year = Integer.parseInt(strYear);
    String strRank = parseLine.nextToken();
    rank = Integer.parseInt(strRank);
    artist = parseLine.nextToken();
    title = parseLine.nextToken();

}

public int getYear(){
    return year;
}

public int getRank(){
    return rank;
}

public String getArtist(){
    return artist;
}

public String getTitle(){
    return title;
}

public String toString(){
    return String.format(year + "\t" + rank + "\t" + artist + "\t" + title);

    }
}

SongCollection类:

import java.util.ArrayList;

public class SongCollection {

public SongCollection(ArrayList<Song> songs){
    SongCollection objects = new SongCollection(songs);
}

public void filterYear(Range r){

}

public void filterRank(Range r){

}

public void filterArtist(String s){

}

public void filterTitle(String s){

}

public void sortYear(){

}

public void sortRank(){

}

public void sortArtist(){

}

public void sortTitle(){

}

public String toString(){

    }
}

1 个答案:

答案 0 :(得分:0)

用于对条目进行排序使用比较器接口,为了过滤您的条目,我建议您编写自己的过滤器。 我为你做了一个小例子:

You Entityclass Song,我只添加了另一个构造函数:

import java.util.StringTokenizer;

public class Song implements Comparable<Song>{

    private int year;
    private int rank;
    private String artist;
    private String title;

    public Song(final int year, final int rank, final String title, final String artist) {
        this.year   = year;
        this.rank   = rank;
        this.title  = title;
        this.artist = artist;
    }

    public Song(String s){
        StringTokenizer stringTokenizer = new StringTokenizer(s, "\t");
        String strYear = stringTokenizer.nextToken();
        year = Integer.parseInt(strYear);
        String strRank = stringTokenizer.nextToken();
        rank = Integer.parseInt(strRank);
        title = stringTokenizer.nextToken();
        artist = stringTokenizer.nextToken();

    }

    public int getYear(){
        return year;
    }

    public int getRank(){
        return rank;
    }

    public String getArtist(){
        return artist;
    }

    public String getTitle(){
        return title;
    }

    public String toString(){
        return String.format(year + "\t" + rank + "\t" + artist + "\t" + title);

    }

    public int compareTo(Song o) {
        if(this.getYear() < o.getYear()) 
            return -1;
        if(this.getYear() > o.getYear()) 
            return 1;
        return 0;
    }
}

SongCollection课程:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.professional_webworkx.blog.cddb.comparators.ArtistComparator;
import de.professional_webworkx.blog.cddb.comparators.YearComparator;
import de.professional_webworkx.blog.cddb.model.Song;

public class SongCollection<T> extends ArrayList<T> {

    /**
     * 
     */
    private static final long serialVersionUID = -7754560588028902292L;

    public SongCollection() {

    }

    public void addSong(final T song) {
        this.add(song);
    }

    public T getSong(final int idx) {
        return this.get(idx);
    }

    public List<T> sortByArtist() {
        Collections.sort((List<Song>)this, new ArtistComparator());
        return this;
    }

    public List<T> sortByYear() {
        Collections.sort((List<Song>)this, new YearComparator());
        return this;
    }

}

正如您所看到的,我实现了两个方法,sortByArtist()和sortByYear以及相应的Artist-和YearComparator(),我在下面发布:

ArtistComparator

import java.util.Comparator;

import de.professional_webworkx.blog.cddb.model.Song;

public class ArtistComparator implements Comparator<Song> {

    public int compare(Song o1, Song o2) {

        int ret = o1.getArtist().compareTo(o2.getArtist());

        if(ret > 1) {
            return 1;
        }
        if(ret < 1) {
            return -1;
        }
        return 0;
    }

}

YearComparator

import java.util.Comparator;

import de.professional_webworkx.blog.cddb.model.Song;

public class YearComparator implements Comparator<Song> {

    public int compare(Song o1, Song o2) {
        if(o1.getYear() < o2.getYear()) 
            return -1;
        if(o1.getYear() > o2.getYear()) 
            return 1;

        return 0;
    }

}

现在,如果您想过滤条目,我建议使用IFilter接口:

的IFilter:

public interface IFilter<T> {

    public boolean accept(T filter);
}

实现IFilter IF的BetweenFilter:

public class BetweenFilter<T extends Object & Comparable<T>> implements IFilter<T> {

    private T lowBoundary;
    private T highBoundary;

    public BetweenFilter(final T lowBoundary, final T highBoundary) {
        this.lowBoundary    = lowBoundary;
        this.highBoundary   = highBoundary;
    }

    public boolean accept(T object) {
        return lowBoundary.compareTo(object) <= 0 &&
                highBoundary.compareTo(object) >= 0;
    }

}

现在我们需要一个不错的小型Utils类,我将它命名为FilterUtils,以应用不同的过滤器:

FilterUtils: 此类获取带有条目的列表和应该应用的过滤器并遍历列表,如果当前值匹配则将添加到结果列表中。

import java.util.ArrayList;
import java.util.List;

public class FilterUtils {

    public static <T> List<T> applyFilter(final List<T> list, final IFilter<T> filter) {

        final List<T> filteredList = new ArrayList<T>();
        for(final T val : list) {
            if(filter.accept(val)) {
                filteredList.add(val);
            }
        }

        return filteredList;

    }
}

现在,让我们尝试一下:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import de.professional_webworkx.blog.cddb.filter.BetweenFilter;
import de.professional_webworkx.blog.cddb.filter.BetweenYearFilter;
import de.professional_webworkx.blog.cddb.filter.FilterUtils;
import de.professional_webworkx.blog.cddb.filter.IFilter;
import de.professional_webworkx.blog.cddb.model.Song;

public class SongListManager {
    public static void main(String[] args){
        SongCollection<Song> collection = new SongCollection<Song>();
        try {


                Scanner input = new Scanner(new File("data/agazillionsongs.txt"));
                String line = "";
                while(input.hasNextLine()) {
                    String user = input.nextLine();

                    collection.add(new Song(user));
                }

            for(Song song : collection) {
                System.out.println(song);
            }

            System.out.println("Filtered by Artist");
            List<Song> filteredByArtist = collection.sortByArtist();
            for(Song song : filteredByArtist) {
                System.out.println(song);
            }
            IFilter<Song> betweenFilter = new BetweenFilter<Song>(new Song(1999, 1, "Hyper Hyper", "Scooter"), new Song(2001, 23, "Title", "DemoArtist"));
            List<Song> applyFilter = FilterUtils.applyFilter(filteredByArtist, betweenFilter);

            System.out.println("With applied Filter");
            for(Song val : applyFilter) 
                System.out.println(val);

            IFilter<Song> betweenYears  = new BetweenYearFilter<Song>(2010, 2013);
            List<Song> betweenYearsFiltered = FilterUtils.applyFilter(collection, betweenYears);

            System.out.println("With applied years filter");
            for(Song song : betweenYearsFiltered) {
                System.out.println(song);
            }

        } catch (FileNotFoundException e) { 
            System.out.println("File not found"); 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

您可以在this Repository中找到代码。

希望这个示例代码能为您提供帮助。