Question

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

Était-ce utile?

La solution

abstract class StockItem{

     protected abstract double getOrderAmount();


     public String getOrderingList(){
          String result = method(); //can invoke it
     }
}

class StockItemFood extends StockItem{
   @Override
   protected double getOrderAmount(){
      //return the value
   }
}

Abstract keyword in Java

I don't know if I have completely understood your question. But I will try to answer it. Create method getOrderAmount() as abstract in your base class.

If every StockItem in your design is going to have a order amount then you should define it as abstract method in your class StockItem.

i've also read several posts where it was adviced to avoid instanceof.

Yes that is true, you should avoid using instanceof as it means you don't have proper design for your code.

Autres conseils

still unclear about your explanation, the solution given is based on a feeble understanding, Explain further by editing your question and maybe a better solution will be provided then.

assuming s is an array of type SuperClass

String toOrder = "";
for(int y=0;y<s.length;y++){
  toOrder+=s[y].superClassMethod();

  if(s[y] instanceof subclass) {
    toOrder+= ( (SubClassName)s[y]).subClassMethod();
  }
}

will work for you

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top