The aim is to be able to read data from a textfile containing information pertaining to stocklevels into an object and then manipulating that object in various ways
fields in the textfile are seperated by "#". The textfile either has 3 fields on a line or 5 fields on a line.
line with 3 fields go into the constructor of my superclass
line with 5 fields go read into the subclass
The superclass is called StockItem and the subclass is called StockItemFood, it extends StockItem(the superclass).
Below is my code, found in a seperate 'Manager' class which reads from the textfile into an array of StockItem objects. simple enough.
public void readFromText() throws IOException, FileNotFoundException{
String [] temp;
BufferedReader br = new BufferedReader (new FileReader("stocklist.txt"));
String line = br.readLine();
while(line!=null){
temp=line.split("#");
if (temp.length==3){
s[counter]=new StockItem(temp[0], (double) Integer.parseInt(temp[1]), temp[3]);
}else if (temp.length==5){
s[counter]=new StockItemFood(temp[0], (double) Integer.parseInt(temp[1]), temp[2], (double) Integer.parseInt(temp[3]), (double) Integer.parseInt(temp[4]));
}
}
counter++;
br.close();
}
In the below method i am trying to return a String that will be concatinated with the return of a method from the Superclass and a return from a method in the subclass. I cannot however see my subclass methods when i type s[y]. as seen below.
public String getOrderingList(){
String toOrder="";
for(int y = 0; y < s.length; y++){
toOrder+=s[y].getDescription() + s[y].//getOrderAmount() <-subclass method
}
}
return toOrderl
}
Below is the code for my subclass:
public class StockItemFood extends StockItem {
private double min,max; //3.2
public StockItemFood(String description, double quantity, String units,double min, double max) { //3.3
super(description, quantity, units);
this.min = min;
this.max=max;
}
public boolean mustOrder(){ //3.4
boolean b;
if(getQuantity()<min){
b=true;
} else {
b=false;
}
return b;
}
public double getOrderAmount(){ //3.5
double amount = max-getQuantity();
return amount;
}
}
I thought of maybe using instanceof but i am not entirely sure of the syntax required and i've also read several posts where it was adviced to avoid instanceof.
Any help would be much appreciated.
- Shaun