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

📄 filecatalogloader.java

📁 卡耐基教程SSD3中所有exercise和quiz的全部答案
💻 JAVA
字号:
import java.util.*;
import java.io.*;

/**
 * Implements interface CatalogLoader and obtain a product catalog from a file.
 * 
 * @author chengxiangsheng053690
 * @version 1.0.0
 * @see CatalogLoader
 * @see Catalog
 * @see Product
 * @see Coffee
 * @see CoffeeBrewer
 */

public class FileCatalogLoader implements CatalogLoader {
	/* Prefix of a line with product data */
	private final static String PRODUCT_PREFIX = "Product";

	/* Prefix of a line with coffee data */
	private final static String COFFEE_PREFIX = "Coffee";

	/* Prefix of a line with coffeeBrewer data */
	private final static String BREWER_PREFIX = "Brewer";

	/* Delimiter */
	private final static String DELIM = "_";

	/**
	 * Loads the information in the specified file into a catalog and returns
	 * the catalog.
	 * 
	 * @param filename
	 *            The name of a file that contains catalog information.
	 * @return a catalog.
	 * @throws FileNotFoundException
	 *             if the specified file does not exist.
	 * @throws IOException
	 *             if there is an error reading the information in the specified
	 *             file.
	 * @throws DataFormatException
	 *             if the file contains malformed data.
	 */

	public Catalog loadCatalog(String fileName) throws FileNotFoundException,
			IOException, DataFormatException {
		Catalog catalog = new Catalog();
		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		String line = reader.readLine();
		while (line != null) {
			Product product = null;
			if (line.startsWith(PRODUCT_PREFIX)) {
				product = readProduct(line);
			} else if (line.startsWith(COFFEE_PREFIX)) {
				product = readCoffee(line);
			} else if (line.startsWith(BREWER_PREFIX)) {
				product = readCoffeeBrewer(line);
			} else {
				throw new DataFormatException(line);
			}
			catalog.addProduct(product);
			line = reader.readLine();
		}
		return catalog;
	}

	/**
	 * Extracts the product data in the specified line and returns a
	 * {@link Product} object that encapsulates the product data.
	 * 
	 * @param line
	 *            a string that contains product data.
	 * @return a <code>Product</code> object that encapsulates the product
	 *         data in the specified line.
	 * @throws DataFormatException
	 *             if the line contains errors.
	 */
	private Product readProduct(String line) throws DataFormatException {
		StringTokenizer tokenizer = new StringTokenizer(line, DELIM);
		if (tokenizer.countTokens() != 4) {
			throw new DataFormatException(line);
		} else {
			try {
				String prefix = tokenizer.nextToken();
				return new Product(tokenizer.nextToken(),
						tokenizer.nextToken(), Double.parseDouble(tokenizer
								.nextToken()));
			} catch (NumberFormatException nfe) {
				throw new DataFormatException(line);
			}
		}
	}

	/**
	 * Extracts the coffee data in the specified line and returns a
	 * {@link Coffee} object that encapsulates the coffee data.
	 * 
	 * @param line
	 *            a string that contains coffee data.
	 * @return a <code>Coffee</code> object that encapsulates the product data
	 *         in the specified line.
	 * @throws DataFormatException
	 *             if the line contains errors.
	 */
	private Coffee readCoffee(String line) throws DataFormatException {
		StringTokenizer tokenizer = new StringTokenizer(line, DELIM);
		if (tokenizer.countTokens() != 10) {
			throw new DataFormatException(line);
		} else {
			try {
				String prefix = tokenizer.nextToken();
				return new Coffee(tokenizer.nextToken(), tokenizer.nextToken(),
						Double.parseDouble(tokenizer.nextToken()), tokenizer
								.nextToken(), tokenizer.nextToken(), tokenizer
								.nextToken(), tokenizer.nextToken(), tokenizer
								.nextToken(), tokenizer.nextToken());
			} catch (NumberFormatException nfe) {
				throw new DataFormatException(line);
			}
		}
	}

	/**
	 * Extracts the product data in the specified line and returns a
	 * {@link Product} object that encapsulates the product data.
	 * 
	 * @param line
	 *            a string that contains product data.
	 * @return a <code>Product</code> object that encapsulates the product
	 *         data in the specified line.
	 * @throws DataFormatException
	 *             if the line contains errors.
	 */
	private CoffeeBrewer readCoffeeBrewer(String line)
			throws DataFormatException {
		StringTokenizer tokenizer = new StringTokenizer(line, DELIM);
		if (tokenizer.countTokens() != 7) {
			throw new DataFormatException(line);
		} else {
			try {
				String prefix = tokenizer.nextToken();
				return new CoffeeBrewer(tokenizer.nextToken(), tokenizer
						.nextToken(),
						Double.parseDouble(tokenizer.nextToken()), tokenizer
								.nextToken(), tokenizer.nextToken(), Integer
								.parseInt(tokenizer.nextToken()));
			} catch (NumberFormatException nfe) {
				throw new DataFormatException(line);
			}
		}

	}
}

⌨️ 快捷键说明

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