📄 filecatalogloader.java
字号:
import java.util.*;
import java.io.*;
public class FileCatalogLoader implements CatalogLoader
{
private final static String PRODUCT__PREFIX = "Product";
private final static String COFFEE__PREFIX= "Coffee";
private final static String BREWER__PREFIX= "Brewer";
private final static String DELIM = "_";
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;
}
/**
* @param line
* @return
*/
/**
* Extracts the book data in the specified line and returns
* a {@link Book} object that encapsulates the book data.
*
* @param line a string that contains book data.
* @return a <code>Book</code> object that encapsulates the
* book 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 recording data in the specified line and returns
* a {@link Recording} object that encapsulates the book data.
*
* @param line a string that contains recording data.
* @return a <code>Recording</code> object that encapsulates the
* recording 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);
}
}
}
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);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -