Hi, I'm having a little bit of trouble implementing the following method while handling the 3 exceptions I'm supposed to take care of. Should I include the try/catch blocks like I'm doing or is that to be left for the application instead of the class design?
Uml diagram of the program: http://yfrog.com/9htuoj
The method says I'm supposed to implement this:
public Catalog loadCatalog(String filename)
throws FileNotFoundException, IOException, DataFormatException
This method loads the info from the archive specified in a catalog of products and returns the catalog.
It starts by opening the file for reading. Then it proceeds to read and process each line of the file.
The method String.startsWith
is used to determine the type of line:
- If the tipe of line is "Product" , the method readProduct is called.
- If the tipe of line is "Coffee" , the method readCoffee is called.
- If the tipe of line is "Brewer" , the method readCoffeeBrewer is called.
After the line is processed, loadCatalog
adds the product (product, coffee or brewer) to the catalog of products.
When all the lines of the file have been proccesed, loadCatalog
returns the Catalog of products to the method that makes the call.
This method can throw the following exceptions:
FileNotFoundException
— if the files specified does not exist.IOException
— If there is an error reading the info of the specified file.DataFormatException
— if a line has errors(the exception must include the line that has the wrong data)
Here is what I have so far:
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;
}