2

我想从餐厅列表中创建搜索方法。用户有一个 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;
            }
        }
}
4

2 回答 2

1

首先为“meetsCriteria”方法编写单元测试,以便您可以轻松查看哪些条件通过,哪些条件失败。

例如:

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;
}
于 2012-06-13T16:27:52.913 回答
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.
}
于 2012-06-13T16:32:09.357 回答