public class Packages
{
private ArrayList shipmentList = new ArrayList();
private double totalWeight;
public Packages(String shiplist) throws IOException
{
Scanner scanFile = new Scanner(new File(shiplist));
scanFile.useDelimiter("\n");
while(scanFile.hasNext() == true){
String pack = scanFile.next();
Scanner splitter = new Scanner(pack);
splitter.useDelimiter(" ");
int id = splitter.nextInt();
double weight = splitter.nextDouble();
String state = splitter.next();
shipmentList.add(new Packet(id, weight, state));
}
this.totalWeight = 0;
}
public String toString()
{String allShipments ="";
for(int i =0;i<shipmentList.size();i++)
allShipments += shipmentList.get(i) + "\n";
return allShipments;
}
//needs work. Cannot find method isLight() from class packet, yet toString() fires fine.
public void displayLightPackages()
{
for (int i=0;i<shipmentList.size();i++){
Packet thisShipment = shipmentList.get(i);
if(thisShipment.isLight() == true){
System.out.println("Package: "+ shipmentList.get(i).toString()+" Is light.");
}else{
System.out.print("");
}
}
}
Method displayLightPackages() is giving me errors. here is the packet class:
public class Packet
{
// instance variables - replace the example below with your own
private int idNumber;
private double weight;
private String state;
public Packet(int idNumber, double weight, String state)
{
this.idNumber= idNumber;
this.weight = weight;
this.state = state;
}
public boolean isHeavy()
{
return (weight>10);
}
public boolean isLight()
{
return (weight<10);
}
public String toString()
{
return "ID: "+idNumber+" WEIGHT: "+weight+" STATE: "+state;
}
}
Method toString fires fine on a call from the main method, however, I cannot access any of the other methods from the Packet class. The constructor works when adding packets to the Arraylist containing all packets. I tried defining a packet in the displayLightPackages with
Packet thisShipment = shipmentList.get(i);
but I get an incompatible type error. When just using the code
shipmentList.get(i).isLight()
in the displayLightPackages method, I get an error where the compiler cannot find the symbol isLight. I suspect the error has something to do with the dataType in the arraylist, but other than that, i'm at a loss at this point.
Thank you