@EDIT:我希望函数能够在没有我的情况下搜索它应该搜索的变量是什么。例如,我想要相同的函数来搜索minimumClients + Location并给我包含这些值的所有内容,但是如果用户只选择pricePerNight + roomsQuantity,那么它应该在同一列表中搜索不同的变量而不告诉它哪些变量到搜索范围。
我不想在函数“.getLocation()。equals(location)”中添加内容,我想这样做,它将作为参数传递的值比较为!=从NULL到列表中的对象
List<Property> properties = new ArrayList<>();
// Let's say I want to look for properties with the minimumClients = 4;
// I would do Property p = new Property();
p.setMinimumClients(4);
properties = getPropertyList(p);
// That would give me the Property 3
// But if I want to look for properties with pricePerNight = 30 and location = "Portugal" I would:
Property p = new Property();
p.setPricePerNight(20);
p.setLocation("Portugal);
properties = getPropertyList(p);
// This would give me the Property 1 using the same function above. I do not want to specify what I am looking for, I want to make it so it knows what I am looking for.
这就是我的意思:
我有以下课程
public class Property() {
private int minimumClients;
private int maximumClients;
private float pricePerNight;
private String location;
private int roomsQuantity;
}
我需要搜索属性,这意味着我可以搜索:
等
我将在另一个名为Repository
的类中拥有一个属性列表public class Repository() {
private List<Property> properties;
}
我想知道的是:由于属性上有大量的搜索可能性,有什么方法可以发送一个只包含我想要搜索的信息的对象,并且在迭代属性列表时只返回包含所有对象信息的属性?
示例:
public class Repository() {
private List<Property> properties; // Assume a constructor exists
// I'll be using a foreach here
// property contains location = "Spain" and roomQuantity = 2
// The rest is null, I want to add to the list properties that are located in spain and contain 2 rooms
public List getPropertiesList(Property property) {
List<Property> newList = new ArrayList<>();
for(Property p : this.properties)
{
// Is there any function I can use here to check if it contains the info I'm searching for, not caring if others are different?
if(p.containsEqualInfo(property))
{
newList.add(p);
}
}
return newList;
}
}
基本上:
我想搜索(例如)并返回位于西班牙的所有属性的列表,并且有多个房间等于2,而不关心其他变量值。
Property1
0
3
30
Portugal
2
Property2
0
3
30
Spain
2
Property3
4
7
120
Spain
2
Property4
0
10
300
Spain
3
如果我在存储库中有上面的列表,一旦我调用了函数(getPropertiesList(p),p =只包含西班牙的对象和2),返回的列表将包含:
Property2 + Property3