I have a bit of a problem with implementing the following method while handling 3 exceptions that I have to take care of. Should I include try / catch blocks as I do, or what should I leave this for the application instead of class design ?
The method says that I should implement this:
public Catalog loadCatalog(String filename)
throws FileNotFoundException, IOException, DataFormatException
This method loads information from the archive specified in the product catalog and returns the catalog.
It starts by opening the file for reading. Then he goes on to read and process each line of the file.
The method is String.startsWithused to determine the type of string:
- If the string type is Product, the readProduct method is called.
- If the string type is Coffee, the readCoffee method is called.
- If the string type is "Brewer", the readCoffeeBrewer method is called.
After processing the line, it loadCatalogadds the product (product, coffee or brewer) to the product catalog.
When all lines of the file have been processed, loadCatalogreturns the product catalog to the method that makes the call.
This method may raise the following exceptions:
FileNotFoundException - if the specified files do not exist.IOException - If the error is reading information about the specified file.DataFormatException - if the row has errors (the exception must contain a row with invalid data)
Here is what I still have:
public Catalog loadCatalog(String filename)
throws FileNotFoundException, IOException, DataFormatException{
String line = "";
try {
BufferedReader stdIn = new BufferedReader(new FileReader("catalog.dat"));
try {
BufferedReader input = new BufferedReader(
new FileReader(stdIn.readLine()));
while(! stdIn.ready()){
line = input.readLine();
if(line.startsWith("Product")){
try {
readProduct(line);
} catch(DataFormatException d){
d.getMessage();
}
} else if(line.startsWith("Coffee")){
try {
readCoffee(line);
} catch(DataFormatException d){
d.getMessage();
}
} else if(line.startsWith("Brewer")){
try {
readCoffeeBrewer(line);
} catch(DataFormatException d){
d.getMessage();
}
}
}
} catch (IOException io){
io.getMessage();
}
}catch (FileNotFoundException f) {
System.out.println(f.getMessage());
}
return null;
}
source
share