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

📄 catalog.java

📁 ssd3练习4的答案
💻 JAVA
字号:
import java.util.*;

/**
 * Maintains the information of a catalog. Contains a collection of
 * {@link Product} objects.
 * 
 * @author Wang Yunsheng
 * @version 1.0.0
 * @see Product
 */

public class Catalog implements Iterable<Product> {
	
        /* Collection of <code>Product</code> objects. */
        private ArrayList<Product> products;

	/**
	 * Constructs an empty catalog.
	 */
        public Catalog() {

		this.products = new ArrayList<Product>();
	}

	/**
	 * Adds a {@link Product} object to this catalog.
	 * 
	 * @param product
	 *            the {@link Product} object.
	 */
        public void addProduct(Product product) {

		this.products.add(product);
	}

	/**
	 * Returns an iterator over the products in this catalog.
	 * 
	 * return an {@link Iterator}
	 */
	public Iterator<Product> iterator() {

		return this.products.iterator();
	}

	/**
	 * Returns the {@link Product} object with the specified <code>code</code>.
	 * 
	 * @param code
	 *            the code of an product.
	 * @return The {@link Product} object with the specifed code. Returns
	 *         <code>null</code> if the object with the code is not found.
	 */
	public Product getProduct(String code) {

		for (Iterator i = this.iterator(); i.hasNext();) {
			Product product = (Product) i.next();

			if (product.getCode().equals(code)) {

				return product;
			}

		}

		return null;
	}

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

}

⌨️ 快捷键说明

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