?? filecatalogloader.java
字號(hào):
import java.util.*;
import java.io.*;
/**
* Creates a coffee catalog and loads it with data stored in
* a file.
*
* @author 張維
* @version 1.0.0
* @see CatalogLoader
* @see Catalog
* @see CatalogItem
* @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 brewer 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 IOException,
FileNotFoundException, 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
* coffee 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 brewer data in the specified line and returns
* a {@link Coffeebrewer} object that encapsulates the brewer data.
*
* @param line a string that contains brewer data.
* @return a <code>Coffeebrewer</code> object that encapsulates the
* brewer 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);
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -