001 package market; 002 003 import data.Catalog; 004 import data.CatalogItem; 005 import data.filters.CatalogFilter; 006 007 /** 008 * Filters CatalogItems by their category. 009 */ 010 public class CFilter extends CatalogFilter { 011 012 private Catalog source; 013 private int filter; 014 015 /** 016 * @param source the source catalog. 017 * @param filter the filter to be applied: 018 * <ul><li>0: no CatalogItems are filtered</li> 019 * <li>1: CatalogItems are filtered by their category</li></ul> 020 */ 021 public CFilter(Catalog source, int filter) { 022 super(source); 023 this.source = source; 024 this.filter = filter; 025 } 026 027 /** 028 * The actual filtering. 029 * @param ci the CatalogItem to be checked. 030 * @return <code>true</code> if CatalogItem passed the filter successfully, otherwise <code>false</code>. 031 */ 032 public boolean match(CatalogItem ci) { 033 String category = ((CIArticle)ci).getCategory(); 034 switch (filter) { 035 case 0: return true; 036 default: return category.equals(SMarket.getArticleCategories()[filter]); 037 } 038 } 039 }