两种不同的实现

时间:2018-04-23 19:41:54

标签: java class filter main implementation

我想做两个不同的过滤器实现,它只返回那些至少有50%积分的学生。我的问题是如何正确地实现这两种方法,并且在第一次我不明白为什么它不起作用,因此我需要你的帮助。

具有两种实现的过滤器类:

package u7a1;

import java.util.ArrayList;

class Filter implements IFilter {
    public ArrayList filterRaw(ArrayList groups)
    {
        ArrayList<Boolean> allstudents = new ArrayList();
        ArrayList<Student> students = new ArrayList();
        ArrayList pass = new ArrayList();
        for (int i = 0; i < groups.size(); i++)
        {
            if ((Integer)groups.get(i) >= 50) allstudents.add(true);
            else allstudents.add(false);
        }
        for (int i = 0; i < groups.size(); ++i)
        {
            if ((Boolean)allstudents.get(i) == true) pass.add(groups.get(i));
        }
        return pass;
    }

    public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups)
    {
        ArrayList<Boolean> allstudents = new ArrayList();
        ArrayList<Student> students = new ArrayList();
        ArrayList pass = new ArrayList();
        /*for (int i = 0; i < groups.size(); i++)
        {
            Integer mystudentpoints = new Integer(0);
            mystudentpoints = Student.getPoints();
            if (Student.getPoints() >= 50) allstudents.add(true);
            else allstudents.add(false);
        }*/
        for (int i = 0; i < groups.size(); ++i)
        {
            if ((Boolean)allstudents.get(i) == true) pass.add(groups.get(i));
        }
        return pass;
    }
}

过滤器工厂已经正确:

package u7a1;

/**
 * Factory for "Testat" filters 
 */
public class FilterFactory {
    /**
     * Create a "Testat" filter
     * @return a "Testat" filter
     */
    public static IFilter create()
    {
        // TODO
        return new Filter();
    }
}

以下是由方法无法正常工作的类实现的接口:

package u7a1;

import java.util.ArrayList;

/**
 * Filters for students who obtain the "Testat".
 * 
 * The requirements for the testat are to have at least {@link IFilter#criteria} percent 
 * of {@link IFilter#maxNumberofPoints} points.
 */
public interface IFilter {
    public final int maxNumberofPoints = 80;
    public final double criteria = 50;

    /**
     * Filter all students that achieved enough points for the "Testat".
     * 
     * @param groups an ArrayList of groups, where a group is an ArrayList of students
     * @return the ArrayList of all students who achieved enough points for the "Testat".
     */
    public ArrayList filterRaw(ArrayList groups);

    /**
     * Filter all students that achieved enough points for the "Testat".
     * 
     * @param groups an ArrayList of groups, where a group is an ArrayList of students
     * @return the ArrayList of all students who achieved enough points for the "Testat".
     */
    public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups);
}

我没有进一步编辑主类:

/**
 * Main class of the Java program. 
 * 
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("-- Array-Listen und Generics --");
        /* you can make function calls from here*/
    }
}

最后,我有一个不需要任何改变的学生课程:

package u7a1;

public class Student {
    private String name;
    private int legi;
    private int points;

    public Student(String name, int legi)
    {
        this.name = name;
        this.legi = legi;
        points = 0;
    }

    public int getLegi()
    {
        return this.legi;
    }

    public String getName()
    {
        return this.name;
    }

    public Student setPoints(int points)
    {
        this.points = points;
        return this;
    }

    public int getPoints()
    {
        return points;
    }

    public String toString()
    {
        return String.format("%s (%d)", name, legi);
    }
}

2 个答案:

答案 0 :(得分:0)

你的项目的核心是一个Main.class,有一切将被执行。其他类只是声明,但从不使用它们。运行程序时,它会在main方法中使用命令启动行。目前您唯一拥有的是打印到控制台字符串,带有文本:“ - Array-Listen und Generics - ”

现在您需要准备一些要过滤的数据,例如

Student student1 = new Student("John", 1);
student1.setPoints(51);
Student student2 = new Student("Alice", 2);
student1.setPoints(48);

List<Student> studentList = new ArrayList<>();
studentList.add(student1, student2);

假设您有测试数据,现在需要使用过滤器 要使用您的过滤器,我们需要使用它的实现

IFilter myFilter = FilterFactory.create();

现在myFilter是你的实现的一个实例,要使用它你需要调用一个声明数据的方法

List<Student> result = myFilter.filterRaw(studentList);

该方法的示例实现将是

public List<Student> filteredStudents(List<Student> input) {
    return input.stream().filter(s -> s.getPoints() > 50).collect(Collectors.toList());
}

但是您可能会注意到,接口声明中的更改也需要具有相同的类型。

答案 1 :(得分:0)

实现两种类型的过滤行,策略设计模式将有助于你这是一篇好文章http://www.oodesign.com/strategy-pattern.html 而且我认为你必须改变像

这样的填充物
  public ArrayList filterRaw(ArrayList groups)
{
    ArrayList pass = new ArrayList();
    for (int i = 0; i < groups.size(); i++)
    {
        if ((Integer)groups.get(i) >= 50) pass.add(groups.get(i));

    }
    return pass;
}