filecatalogloader.java

来自「SSD3 icarnegie FileCatalogLoader」· Java 代码 · 共 63 行

JAVA
63
字号
import java.io.*;
import java.util.*;

/**
 * Crea un cat醠ogo y lo carga con los datos almacenados en un archivo.
 * 

 * @version 1.0
 * @see CatalogLoader
 * @see Catalog
 * @see Product
 * @see Coffee
 * @see CoffeeBrewer
 * @see GourmetCoffee
 */
public class FileCatalogLoader implements CatalogLoader {

	public static final String PRODUCT_PREFIX = "Product";

	public static final String COFFEE_PREFIX = "Coffee";

	public static final String BREWER_PREFIX = "Brewer";
	
	private final static String DELIM = "_";

	/*
	 * Este m閠odo carga la informaci髇 del archivo especificado en un cat醠ogo
	 * de productos y regresa el cat醠ogo. Comienza por abrir el archivo para su
	 * lectura.Despu閟 procede a leer y procesar cada l韓ea del archivo. El
	 * m閠odo String.startsWith es utilizado para determinar el tipo de l韓ea.
	 * 
	 * @param filename The name of a file that contains catalog information.
	 * @return un {@link Catalog} con los datos indicados en el archivo.
	 * @throws FileNotFoundException si el archivo especificado no existe. 
	 * @throws IOException si hay un error al leer la informaci髇 del archivo en 
	 * 					   el archivo especificado. 
	 * @throws DataFormatException si el archivo contiene datos mal formados.
	 */
	public Catalog loadCatalog(String fileName) throws FileNotFoundException,
			IOException, DataFormatException {

		String line;
		Catalog catalog = new Catalog();

		BufferedReader fileIn = new BufferedReader(new FileReader(fileName));

		while ((line = fileIn.readLine()) != null) {
			if (line.startsWith(PRODUCT_PREFIX)) {
				catalog.addProduct(readProduct(line));
			} else if (line.startsWith(COFFEE_PREFIX)) {
				catalog.addProduct(readCoffee(line));
			} else if (line.startsWith(BREWER_PREFIX)) {
				catalog.addProduct(readCoffeeBrewer(line));
			}
		}

		fileIn.close();

		return catalog;
	}

	/*
	 * Este m閠odo lee una l韓ea de datos sobre un accesorio de caf

⌨️ 快捷键说明

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