📄 filecatalogloader.java
字号:
import java.io.*;
import java.util.*;
/**
* This class is used to obtain a product catalog from a file.
*
* @author LT
* @version 1.0.0
* @see CatalogLoader
* created on 2006-11-25
*/
public class FileCatalogLoader
implements CatalogLoader {
/*Prefix of a line with coffee data*/
private final static String COFFEE_PREFIX = "Coffee";
/*Prefix of a line with coffee brewer data*/
private final static String BREWER_PREFIX = "Brewer";
/*Prefix of a line with product data*/
private final static String PRODUCT_PREFIX= "Product";
/*The string tokenizer*/
private static StringTokenizer tokenizer;
/*The specified tokenizer*/
private static String TOKENIZER = "_";
/**
* Returns a {@link @Product} object that encapsulates the
* accessory data in the specified line.
*
* @param line The line with the accessory data of Product.
* @return a Product object that encapsulates the
* accessory data in the specified line.
* @throws DataFormatException
* if it does not have the expected number of tokens
* or the token that should contain a double does not.
*/
private Product readProduct(String line)
throws DataFormatException {
String code = "",
description = "";
double price = 0.0;
if( tokenizer.countTokens() != 3 ) {
throw new DataFormatException( line );
} else {
try {
code = tokenizer.nextToken();
description = tokenizer.nextToken();
price = Double.parseDouble( tokenizer.nextToken() );
Product product = new Product( code, description, price );
return product;
} catch( NumberFormatException dfe ) {
throw new DataFormatException( line );
}
}
}
/**
* Returns a {@link @Coffee} object that encapsulates the
* coffee data in the specified line.
*
* @param line The line with the data of Coffee.
* @return a {@link Coffee} object that encapsulates the
* coffee data in the specified line.
* @throws DataFormatException
* if it does not have the expected number of tokens
* or the token that should contain a double does not.
*/
private Coffee readCoffee(String line)
throws DataFormatException {
String code = "",
description = "",
origin = "",
roast = "",
flavor = "",
aroma = "",
acidity= "",
body = "";
double price = 0.0;
if( tokenizer.countTokens() != 8 ) {
throw new DataFormatException( line );
} else {
try {
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 coffee = new Coffee(code,description,price,
origin,roast, flavor,
aroma, acidity, body);
return coffee;
} catch(NumberFormatException nfe) {
throw new DataFormatException( line );
}
}
}
/**
* Returns a {@link @CoffeeBrewer} object that encapsulates the
* brewer data
*
* @param line The line with the data of Coffee Brewer.
* @return a {@link CoffeeBrewer} object that encapsulates the
* brewer data
* @throws DataFormatException
* if it does not have the expected number of tokens
* or the token that should contain a double does not.
*/
private CoffeeBrewer readCoffeeBrewer(String line)
throws DataFormatException {
String code = "",
description = "",
model = "",
waterSupply ="";
double price = 0.0;
int numberOfCups = 0;
if( tokenizer.countTokens() != 4 ) {
throw new DataFormatException( line );
} else {
try {
code = tokenizer.nextToken();
description = tokenizer.nextToken();
price = Double.parseDouble( tokenizer.nextToken() );
model = tokenizer.nextToken();
waterSupply = tokenizer.nextToken();
numberOfCups = Integer.parseInt( tokenizer.nextToken() );
CoffeeBrewer coffeeBrewer =
new CoffeeBrewer( code, description, price,
model,waterSupply,numberOfCups );
return coffeeBrewer;
} catch(NumberFormatException nfe) {
throw new DataFormatException( line );
}
}
}
/**
* Returns A {@link @Catalog} object that contains the information
* in the specified file.
*
* @param filename The file that contains the information.
* @return A {@link @Catalog} object that contains the information
* in the specified file.
* @throws FileNotFoundException
* if the file doesn't exist or can't be found
* @throws IOException
* @throws DataFormatException
* if it does not have the expected tokens
*/
public Catalog loadCatalog(String filename)
throws FileNotFoundException,
IOException,
DataFormatException {
BufferedReader fileIn =
new BufferedReader( new FileReader(filename) );
Catalog catalog = new Catalog();
String line = fileIn.readLine();
while( line != null ) {
tokenizer = new StringTokenizer(line, TOKENIZER);
tokenizer.nextToken();
if( line.startsWith(COFFEE_PREFIX) )
catalog.addProduct( readCoffee(line) );
else if( line.startsWith(BREWER_PREFIX))
catalog.addProduct( readCoffeeBrewer(line) );
else if( line.startsWith(PRODUCT_PREFIX))
catalog.addProduct( readProduct(line) );
else {
throw new DataFormatException( line );
}
line = fileIn.readLine();
}
fileIn.close();
return catalog;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -