Reading a file in
Hi
I am trying to read in a file to the Grocery store class as an array ( Item [] items = new Item[20];) then pass some of the generic attributes into the Item class and and then pass those attributes to the Fruit and Veg Class
Then in the same application after altering the data in the application i am going to write the altered data out to the same file replacing what was there previously.
The code i have is
Master Application class
class GroceryStore
{
privatestaticfinal Scanner console =new Scanner(System.in);
privatestaticfinal Item [] items =new Item[20];
privatestaticint numberOfRecords = 0;
publicstaticvoid main (String [] args)
{
publicstaticvoid loadData()
{
int index = 0;
Item temp =null;
try
{
BufferedReader br =new BufferedReader (new
FileReader ("grocery.dat"));
String inputLine;
inputLine = br.readLine();
while (inputLine !=null)
{
if (inputLine.equals("Fruit"))
{
temp =new Fruit();
}
elseif (inputLine.equals("Vegetable"))
{
temp =new Vegetable();
}
if (temp !=null)
{
temp.readAttributes(br);
items[index] = temp;
index ++;
inputLine = br.readLine();
temp =null;
}
br.close();
}
}
catch (FileNotFoundException e)
{
System.out.println("File grocery.dat was not found");
System.out.println("Error:");
System.out.println(" ");
System.err.print(e);
System.exit(1);
}
catch (IOException e)
{
System.out.println("there was an error reading the file");
}
}
Then in the Item Class i have:
import java.io.*;
publicabstractclass Item
{
public String itemCode;
public String type;
public String variety;
public Item()
{
itemCode ="Not Specified";
type="None";
variety ="None";
}
public Item(String itemCode, String type, String variety)
{
this.itemCode = itemCode;
this.type = type;
this.variety = variety;
}
public String getItemCode()
{
return itemCode;
}
public String getType()
{
return type;
}
public String getVariety()
{
return variety;
}
publicvoid readAttributes(BufferedReader br)throws IOException
{
this.itemCode = br.readLine();
this.type = br.readLine();
this.variety = br.readLine();
}
publicvoid writeAttributes(PrintWriter pw)
{
pw.println(itemCode);
pw.println(type);
pw.println(variety);
}
}
and in the fruit class (I also have a veg class but that is the same as the fruit):
import java.io.*;
publicclass Fruitextends Item
{
privatedouble stockLevel;
privatedouble pricePerKilo;
public Fruit()
{
super();
stockLevel = 0;
pricePerKilo = 0;
}
public Fruit(String code, String type, String variety,
double stockLevel,double pricePerKilo)
{
super(code, type, variety);
this.stockLevel = stockLevel;
this.pricePerKilo = pricePerKilo;
}
publicdouble getStockLevel()
{
return stockLevel;
}
publicvoid readAttributes(BufferedReader br)throws IOException
{
super.readAttributes(br);
this.stockLevel = Double.parseDouble(br.readLine());
this.pricePerKilo= Double.parseDouble(br.readLine());
}
publicvoid writeAttributes(PrintWriter pw)
{
super.writeAttributes(pw);
pw.println(stockLevel);
pw.println(pricePerKilo);
}
publicvoid writeObject(PrintWriter pw)
{
pw.println("Fruit");
writeAttributes(pw);
}
}
I think that some of my readAttributes() are dodgy. As i am ably to open the file with the program but that is as far as i can get with it.
Any help would be appreciated.
CC

