两个不同的实现



我想做两个不同的过滤器实现,这仅返回那些至少有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);
    }
}

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

那样更改Fillerrow
  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;
}

您的项目的核心是主要的。其他类仅被声明,但从未使用过。运行程序时,它以主方法的命令启动行。当前,您唯一拥有的东西是用文本打印到控制台字符串:" - 阵列上列和仿制药 - "

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

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());
}

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

相关内容

  • 没有找到相关文章

最新更新