我想从餐馆列表中创建搜索方法。用户具有GUI表单并仅完成他想要的字段。 我做了一个方法meetCriteria来检查这个餐厅是否存在。它起作用但并非在所有情况下都适用。
public class RestaurantList {
private ArrayList<Restaurant> _restaurants = new ArrayList<Restaurant>();
RestaurantList selRest;
RestaurantList searchRestaurant(String name, String area, String phone, String category)
{
selRest = new RestaurantList();
for (int i=0; i< _restaurants.size(); i++)
{
if(_restaurants.get(i).meetsCriteria(name, area, phone, category))
{
selRest.addRestaurant(_restaurants.get(i));
}
}
return this.selRest;
}
public class Controller {
//this is list with all Restaurants
static RestaurantList restList = new RestaurantList();
//this is list with the result of the search.
public static RestaurantList selList; // selectedList
public void addRestaurant (Restaurant rest)
{
restList.addRestaurant(rest);
}
public void searchCriteria(String name, String area, String phone, String category)
{
int size = restList.getRestaurants().size();
for(int i =0; i<size; i++)
selList = restList.searchRestaurant(name, area, phone, category);
}
}
public class Restaurant {
private String _name;
private String _address;
private String _phoneNum;
private Area _area;
public boolean meetsCriteria(String name, String area, String phone, String category)
{
if( this._name.equals(name) && this._area.getArea().equals(area) && this._phoneNum.equals(phone) && this._category.equals(category) )
{
return true;
}
if ( name.equals("") && area.equals(this._area.getArea()) && phone.equals("") && category.equals("") )
{
return true;
}
if ( name.equals("") && area.equals(this._area.getArea()) && phone.equals(this._phoneNum) && category.equals("") )
{
return true;
}
if (name.equals("") && area.equals(this._area.getArea()) && phone.equals("") && category.equals(this._category) )
{
return true;
}
if ( area.equals(this._area.getArea()) && phone.equals("") && category.equals("") )
{
int index = this._name.indexOf(name);
if (index != -1)
{
return true;
}else return false;
}
if (area.equals(this._area) && category.equals(this._category) )
{
return true;
}
else
{
return false;
}
}
}
答案 0 :(得分:1)
首先为“meetCriteria”方法编写单元测试,以便您可以轻松查看哪些条件通过,哪些条件失败。
例如:
assertTrue(myRestaurant.meetsCriteria("nandos", "new york", "0124536", "tasty"));
Ps。我可能会将你的“searchRestaurant”变成这样的东西:
List<Restaurant> searchRestaurant(List<Restaurant> allRestaurants, String name, String area, String phone, String category) {
List<Restaurant> matches = new ArrayList<Restaurant>();
for (Restaurant restaurant : allRestaurants) {
if (restaurant.meetsCriteria(name, area, phone, category)) {
matches.addRestaurant(allRestaurants.get(i));
}
}
return matches;
}
答案 1 :(得分:1)
您的meetCriteria()方法看起来有点复杂,并且错过了指定或未指定的许多标准组合。由于您有4个参数,并且每个参数都可以指定,因此您可能有16(2 ^ 4)种不同的情况。显然,你的代码并不包含那么多代码。对于您添加到此Restaurant类的每个字段,可能的案例数量会翻倍。你必须找到一种方法来避免这种组合爆炸,确保你只需要为每个字段添加一个测试。
我可以建议您尝试这种方式:
public class Restaurant {
private String _name;
private String _address;
private String _phoneNum;
private Area _area;
public boolean meetsCriteria(String name, String area, String phone, String category)
{
if ( !name.equals("") && !name.equals(this._name) && this._name.indexOf(name) < 0){
// name has been specified and does not match
return false;
}
if(!areas.equals("") && this._aread.getArea().equals(area)){
// area has been specified and does not match
return false;
}
// ... snip ...
// do the same kind of thing for phone and category
// ... snip ...
// At this point, all the criteria that have been specified by the user match,
// and all the criteria that haven't been specified have been ignored.
return true;
}
}
您可以在一个巨大的if()语句中完成所有这些操作,但这可能不太可读。
甚至更好,您可以将每个字段的匹配逻辑提取到自己的方法,如下所示:
public class Restaurant {
private String _name;
private String _address;
private String _phoneNum;
private Area _area;
public boolean meetsCriteria(String name, String area, String phone, String category)
{
return matchesName(name) && matchesArea(area) && matchedPhone(phone) && matchesCategory(category);
}
private boolean matchesName(String name){
return name.equals("") || name.equals(this._name) || this._name.indexOf(name) >= 0;
}
private boolean matchesArea(String area) {
return areas.equals("") || this._aread.getArea().equals(area);
}
// create the missing methods yourself.
}