⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catalog.java

📁 ssd2 exercise4 的题目和答案
💻 JAVA
字号:
import java.util.*;

/**
 * Maintains the information of a product catalog. Contains a
 * collection of {@link Product} objects.
 *
 * @author Libo Du
 * @version  1.0.0
 * @see Product
 */
public class Catalog implements Iterable<Product>{
	
	/* Collection of <code>Product</code> objects.*/
	private ArrayList<Product> products;
	
	/**
     * Constructs an collection <code>products</code>, which is initially empty. 
     */
    public Catalog(){
        
    	products = new ArrayList<Product>();
    }

    /**
     * Add the specified {@link Product} to the collection <code>products</code>. 
     * 
     * @param product  the specified {@link Product} object.
     */
    public void addProduct(Product product){
       
    	products.add(product);
    }

    /**
     * Returns an iterator over the instances in the collection <code>products</code>.
     * 
     * @return  an {@link Iterator} of {@link Product}
     */
    public Iterator<Product> iterator(){
        
    	return products.iterator();
    }

    /**
     * Returns a reference to the {@link Product} instance with the 
     * specified <code>code</code>.
     * Returns <code>null</code> if there are no {@link Product} in the catalog with 
     * the specified <code>code</code>.
     * 
     * @param code  the code of an {@link Product}.
     * @return  The {@link Product} object with the specifed
     *          <code>code</code>. Returns <code>null</code> if the object with
     *          the <code>code</code> is not found.
     */
    public Product getProduct(String code){
        
    	for(Iterator<Product> itr = products.iterator(); itr.hasNext();){
            
    		Product product = itr.next();
            
    		if(product.getCode().equals(code))
                
            	return product;
        }

        return null;
    }

    /**
     * Returns the number of instances in the collection <code>products</code>.
     *
     * @return the number of {@link Product} objects in this catalog.
     */
    public int getNumberOfProducts(){
        
    	return products.size();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -