📄 exercise 6.filecatalogloader.java
字号:
import java.io.*;
import java.util.*;
/**
* The class FileCatalogLoader implements interface CatalogLoader. It is used to
* obtain a product catalog from a file.
*
* @author Neil
* @version 1.1.0
* @see CatalogLoader
* @see Product
* @see Coffee
* @see CoffeeBrewer
*/
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 line of coffee-accessory data.
* @return a Product object that encapsulates the accessory data.
* @throws DataFormatException
* a DataFormatException that contains the line of malformed
* data.
*/
private Product readProduct(String line) throws DataFormatException {
StringTokenizer tknzr = new StringTokenizer(line, "_");
String code;
String description;
double price;
tknzr.nextToken();
code = tknzr.nextToken();
description = tknzr.nextToken();
price = Double.parseDouble(tknzr.nextToken());
return new Product(code, description, price);
}
/**
* 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 line
* a line of coffee data.
* @return a Coffee object that encapsulates the coffee data.
* @throws DataFormatException
* a DataFormatException that contains the line of malformed
* data.
*/
private Coffee readCoffee(String line) throws DataFormatException {
StringTokenizer tknzr = new StringTokenizer(line, "_");
String code;
String description;
double price;
String origin;
String roast;
String flavor;
String aroma;
String acidity;
String body;
tknzr.nextToken();
code = tknzr.nextToken();
description = tknzr.nextToken();
price = Double.parseDouble(tknzr.nextToken());
origin = tknzr.nextToken();
roast = tknzr.nextToken();
flavor = tknzr.nextToken();
aroma = tknzr.nextToken();
acidity = tknzr.nextToken();
body = tknzr.nextToken();
return new Coffee(code, description, price, origin, roast, flavor,
aroma, acidity, body);
}
/**
* 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
* encapsulates the brewer data. If the line has errors, that is, if it does
* not have the expected number of tokens or the tokens that should contain
* a number do not; this method throws a DataFormatException that contains
* the line of malformed data.
*
* @param line
* a line of coffee-brewer data.
* @return a CoffeeBrewer object that encapsulates the brewer data.
* @throws DataFormatException
* a DataFormatException that contains the line of malformed
* data.
*/
private CoffeeBrewer readCoffeeBrewer(String line)
throws DataFormatException {
StringTokenizer tknzr = new StringTokenizer(line, "_");
String code;
String description;
double price;
String model;
String waterSupply;
int numberOfCups;
tknzr.nextToken();
code = tknzr.nextToken();
description = tknzr.nextToken();
price = Double.parseDouble(tknzr.nextToken());
model = tknzr.nextToken();
waterSupply = tknzr.nextToken();
numberOfCups = Integer.parseInt(tknzr.nextToken());
return new CoffeeBrewer(code, description, price, model, waterSupply,
numberOfCups);
}
/**
* Loads the product catalog with the data in the specified file.
*
* @param filename
* The name of a file that contains catalog information.
* @return the product 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 badly-formed data.
*/
public Catalog loadCatalog(String fileName) throws FileNotFoundException,
IOException, DataFormatException {
Catalog result = new Catalog();
BufferedReader fileIn = new BufferedReader(new FileReader(fileName));
String line = fileIn.readLine();
while (line != null) {
StringTokenizer tknzr = new StringTokenizer(line, "_");
String type = tknzr.nextToken();
if (type.equals("Coffee"))
result.addProduct((readCoffee(line)));
if (type.equals("Brewer"))
result.addProduct(readCoffeeBrewer(line));
if (type.equals("Product"))
result.addProduct(readProduct(line));
line = fileIn.readLine();
}
fileIn.close();
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -