📄 filecatalogloader.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -