package exercise6
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
/**
* The class FileCatalogLoader implements interface CatalogLoader. It is used to
* obtain a product catalog from a file. You should implement this class from
* scratch: FileCatalogLoader.java
*
* @author qiuyuan
* @version 1.6.0 see CatalogLoader
*
*/
public class FileCatalogLoader implements CatalogLoader {
/**
* This method reads a line of coffee-accessory data. It uses the class
* StringTokenizer to extract the accessory data in the specified line. If
* the line is error free, this method returns a Product object that
* encapsulates the accessory data. If the line has errors, that is, if it
* does not have the expected number of tokens or the token that should
* contain a double does not; this method throws a DataFormatException that
* contains the line of malformed data.
*
* @param line
* a string that contains recording data
* @return product
* @throws DataFormatException
*/
private Product readProduct(String line) throws DataFormatException {
Product product;
String code;
String description;
double price;
final String DEFUT = "_";
StringTokenizer tokenizer = new StringTokenizer(line, DEFUT);
if (tokenizer.countTokens() != 4)
throw new DataFormatException(line);
else {
try {
String def = tokenizer.nextToken();
code = tokenizer.nextToken();
description = tokenizer.nextToken();
price = Double.parseDouble(tokenizer.nextToken());
product = new Product(code, description, price);
return product;
} catch (NumberFormatException e) {
throw new DataFormatException(line);
}
}
}
/**
* This method reads a line of coffee data. It uses the class
* StringTokenizer to extract the coffee data in the specified line. If the
* line is error free, this method returns a Coffee object that encapsulates
* the coffee data. If the line has errors, that is, if it does not have the
* expected number of tokens or the token that should contain a double does
* not; this method throws a DataFormatException that contains the line of
* malformed data.
*
* @param a
* string that contains recording data
* @return coffee
* @throws DataFormatException
*/
private Coffee readCoffee(String line) throws DataFormatException {
Coffee coffee;
String code;
String description;
double price;
String origin;
String roast;
String flavor;
String aroma;
String acidity;
String body;
final String DEFUT = "_";
StringTokenizer tokenizer = new StringTokenizer(line, DEFUT);
if (tokenizer.countTokens() != 10) {
throw new DataFormatException(line);
} else {
try {
String dd = tokenizer.nextToken();
code = tokenizer.nextToken();
description = tokenizer.nextToken();
price = Double.parseDouble(tokenizer.nextToken());
origin = tokenizer.nextToken();
roast = tokenizer.nextToken();
flavor = tokenizer.nextToken();
aroma = tokenizer.nextToken();
acidity = tokenizer.nextToken();
body = tokenizer.nextToken();
coffee = new Coffee(code, description, price, origin, roast,
flavor, aroma, acidity, body);
return coffee;
} catch (NumberFormatException nfe) {
throw new DataFormatException(line);
}
}
}
/**
* This method reads a line of coffee-brewer data. It uses the class
* StringTokenizer to extract the brewer data in the specified line. If the
* line is error free, this method returns a CoffeeBrewer object that
*
* @param lia
* string that contains recording data
* @return
* @throws DataFormatException
*/
private CoffeeBrewer readCoffeeBrewer(String line)
throws DataFormatException {
final String DEFUT = "_";
StringTokenizer tokenizer = new StringTokenizer(line, DEFUT);
if (tokenizer.countTokens() != 7)
throw new DataFormatException(line);
else {
try {
String defsss = tokenizer.nextToken();
//System.out.println(defsss + "******************************");
String codes = tokenizer.nextToken();
String desCription = tokenizer.nextToken();
double price = Double.parseDouble(tokenizer.nextToken());
String model = tokenizer.nextToken();
String waterSupply = tokenizer.nextToken();
int numberOfCup = Integer.parseInt(tokenizer.nextToken());
CoffeeBrewer coffeebrewer = new CoffeeBrewer(codes,
desCription, price, model, waterSupply, numberOfCup);
return coffeebrewer;
} catch (NumberFormatException e) {
throw new DataFormatException(line);
}
}
}
/*
*
* This method loads the information in the specified file into a product
* catalog and returns the catalog. It begins by opening the file for
* reading. It then proceeds to read and process each line in the file. The
* method String.startsWith is used to determine the line type: If the line
* type is "Product", the method readProduct is invoked. If the line type is
* "Coffee", the method readCoffee is invoked. If the line type is "Brewer",
* the method readCoffeeBrewer is invoked. After the line is processed,
* loadCatalog adds the product (accessory, coffee, or brewer) to the
* product catalog. When all the lines in the file have been processed, load
* returns the product catalog to the calling method. This method can throw
* the following exceptions: FileNotFoundException¡ª if the specified file
* does not exist. IOException¡ª if there is an error reading the information
* in the specified file. DataFormatException¡ª if a line in the file has
* errors (the exception should contain the line of malformed data).
* (non-Javadoc)
*
* @param consturn the name
*
* @see CatalogLoader#loadCatalog(java.lang.String) return catalog
*/
public Catalog loadCatalog(String filename) throws FileNotFoundException,
IOException, DataFormatException {
Catalog catalog = new Catalog();
BufferedReader readFile = new BufferedReader(new FileReader(filename));
String readFileProductNames = readFile.readLine();
//System.out.println ("/////////////////"+readFileProductNames);
if (readFileProductNames == null) {
//System.out.println ("***************************");
//System.out.println ("/////////////////"+readFileProductNames);
//return null;
//throw new FileNotFoundException();
} else {
while (readFileProductNames != null) {
if (readFileProductNames.charAt(0) == 'P')
catalog.addProduct(readProduct(readFileProductNames));
else if (readFileProductNames.startsWith("Coffee")) {
catalog.addProduct(readCoffee(readFileProductNames));
} else if (readFileProductNames.startsWith("Brewer"))
catalog.addProduct(readCoffeeBrewer(readFileProductNames));
else
throw new DataFormatException(readFileProductNames);
readFileProductNames = readFile.readLine();
}
}
return catalog;
}
}