Eggnog
Eggnog

Reputation: 33

ArrayList giving error(plus method help)

Hi im doing an assignment with scanners and arraylist

Packet objects have unique ID number, weight in pounds with 2 decimals, and state abbreviations for their destination and origin.
Class Packet describes one packet and has variables idNumber, weight, and stateDest and stateOrig of type int, double, String and String respectively. In addition, it has the following methods • boolean isHeavy () that returns true when packet is above 10 pounds, and false otherwise. • boolean isInState() that returns true when packet is shipped to the same destination state as is the state of origin. • String toString() returns String which is one line string representation of Packet objects. • double getWeight() returns packet weight.

Create an input file called "packetData.txt" with the following 7 lines. Each line in the "packetData.txt" file has information about one packet object.

1001 8.37 CA MO
1002 2.17 CT CA
1003 11.35 NY NY
1004 3.77 MA MA
1005 9.99 FL CT
1006 14.91 VT MA 
1007 4.97 TX CA

Class Packages has shipmentList and totalWeight variables.

Variable called shipmentList contains the collection of all packets.

Variable shipmentList is of ArrayList type.

All objects in the ArrayList are of Packet type.

Totalweight variable specifies the total weight of all packets.

The constructor assigns variables shipmentList and totalWeight.

Variable totalWeight is initialized to 0.0 when declared in the class and updated when each packet is read from the file.

Variable shipmentList is assigned by reading the data from the input file specified above.

Each line in the file has data about one packet object.

As you read the data from one line in the input file a Packet object should be added to the shipmentList Class Packages has methods:

• String toString() which returns String representation about entire collection of packets with one packet object specified per line.

• void displayHeavyPackages() which displays all packets that are heavy .

• void displayInStatePackages() which displays all packets with the same origin and destination state. Your code must use method isInState().

• Packet displayMaxWeightPacket() returns heaviest packet object.

• void displayAverageWeight () displays average weight (with two decimals) of all packets. Use full sentence.

All of the methods are written by using variables shipmentList and totalWeight. Your application should also have class TestPackages with only main method in it, in addition to classes Packet and Packages.

Add method String destinationLocation() to Packet class. Method returns String "Local" if destination state is one of CT, MA, RI, or NY, and returns String "NonLocal" for all other states.

Add methods to Package class: • void displayLocalPackets() which displays one empty line, then title "LOCAL PACKETS", and next it displays all local packets that are shipped to any local state (Display one packet per line). Must use method destinationLocation in the code.

• void displayNonLocalPackets() which displays all packets that are not shipped to a local destination. Must use method destinationLocation to write the code.

I have implemented as much as possible but im getting incompatible types errors in two of my classes

import java.util.ArrayList;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;
public class Packages
{
    public ArrayList shipmentList;
    double totalWeight = 0.0;
    private int counter = 0;
    public Packages() throws IOException
    {
       ArrayList<Packet> shipmentList = new ArrayList<Packet>();
       String currentPacket;
       Scanner fileScan, packetScan;
       Scanner scan = new Scanner (System.in); //start scanner
       Scanner fileInput = new Scanner(new File("packetData.txt"));//file input
       while (fileInput.hasNextLine())
       {
           currentPacket = fileScan.nextLine();
           packetScan = new Scanner (currentPacket);
           int id = packetScan.nextInt();
           double w = packetScan.nextDouble();
           String s = packetScan.next();
           String d = packetScan.next();
           Packet temp = new Packet (id, w, s, d);
           shipmentList.add(temp);
       }
       scan.close();
    }
    public void displayHeavyPackages()
    {
       System.out.println("The heavy packages are : ");
       for(Packet mypacket : shipmentList)
       {
         if (mypacket.isHeavy())
         {
           System.out.println(mypacket);
         }
       }
    }
    public void displayInStatePackages()
    {
       System.out.println("These are the In-State Packages : ");
       for(Packet mypacket : shipmentList)
       {
         if (mypacket.isInState())
         {
           System.out.println(mypacket);
         }
       }
    }
    public void displayAverageWeight()
    {
       double hold;
       int count;
       for(Packet mypacket : shipmentList)
       {
           hold += mypacket.weight;
           count++; 
       }
       System.out.println("The average weight is " + totalWeight);
    }
    public String toString()
    {
       String result = "The shipment \n";    
       for(Packet mypacket : shipmentList)
       {
          result += mypacket;
        }
       return result;
    }
    public Packet maxWeightPacket()
    {

    }
    public void displayLocalPackets()
    {
       System.out.println("");
       System.out.println("Local Packages");
       for(Packet mypacket : shipmentList)
       {
           if (mypacket.destinationLocation())
           {
               System.out.println(mypacket);
           }
       }
    }
    public void displayNonLocalPackets()
    {
       System.out.println("");
       System.out.println("Non Local Packages");
       for(Packet mypacket : shipmentList)
       {
           if (mypacket.destinationLocation())
           {
               System.out.println(mypacket);
           }
       }
    }
}

Packet Class

public class Packet
{
    private int idNumber;
    public double weight;
    private String stateDest;
    private String stateOrig;
    public Packet(int id, double w, String sD, String sO)
    {
         idNumber = id;
         weight = w;
         stateDest = sD;
         stateOrig = sO;
    }
    public boolean isHeavy()
    {
        return (weight > 10 ); 
    }
    public boolean isInState()
    { 
        return (stateDest==stateOrig);
    }
    public String toString()
    {
         String result = "\nPacket id: " + idNumber + " State: ";
         result +=  "From " + stateOrig + " To " + stateDest + " Weight: " + weight;
         return result;
    }
    public double getWeight()
    {
        return weight;
    }
    public String destinationLocation() 
    {
        if (stateDest = "CT")
        {
            return "Local";
        } else if (stateDest = "MA")
        {
            return "Local";
        }else if (stateDest = "RI")
        {
            return "Local";
        }else if (stateDest = "NY")
        {
            return "Local";
        }else
        {
            return "Non Local";
        }
    }
}

Test Class

import java.io.*;
public class TestPackages 
{
    public static void main (String[] args) throws IOException 
    {
       Packages shipment = new Packages();
       System.out.println(shipment);
       shipment.displayHeavyPackages();
       shipment.displayInStatePackages();
       shipment.maxWeightPacket();
       shipment.displayAverageWeight() 
    } 
}

Upvotes: 0

Views: 264

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44854

You are shadowing the shipmentList variable, so change

public class Packages
{
    public ArrayList shipmentList;

to

public class Packages
{
    ArrayList<Packet> shipmentList = new ArrayList<Packet>();

delete from your constructor

  ArrayList<Packet> shipmentList = new ArrayList<Packet>();

Other errors

In displayLocalPackets

change to

if (mypacket.destinationLocation().equalsIgnoreCase ("local"))

make similar change to displayNonLocalPackets

More

Also learn how to compare Strings hint: return (stateDest==stateOrig); is wrong

Also

The method for averageWeight is wrong, you should be using the calculated variables

public void displayAverageWeight()
{
   double hold;
   int count;
   for(Packet mypacket : shipmentList)
   {
       hold += mypacket.weight;
       count++; 
   }
   System.out.println("The average weight is " + hold / count);
}

Upvotes: 1

Related Questions