Tarinkot
Tarinkot

Reputation: 43

ArrayList of Objects from a different class

New to Java.

I have a txt file:

In one class called Goods I have been able to read it, split it using delimiter, add it to an ArrayList.

I have another class called GoodsList where I will be creating an ArrayList which should have the above as an object that I can use if the user requests it. Thanks

Upvotes: 0

Views: 453

Answers (2)

KgaboL
KgaboL

Reputation: 120

Are you looking for something like:

public class FileReaderExample
{


  public class Goods
  {

    private String goodName = null;
    private String price = null;
    private String quantity = null;

    public Goods(String goodName ,String price,String quantity)
    {
      this.goodName = goodName;
      this.price = price;
      this.quantity = quantity;
    }

    public String getGoodName()
    {
      return goodName;
    }

    public void setGoodName(String goodName)
    {
      this.goodName = goodName;
    }

    public String getPrice()
    {
      return price;
    }

    public void setPrice(String price)
    {
      this.price = price;
    }

    public String getQuantity()
    {
      return quantity;
    }

    public void setQuantity(String quantity)
    {
      this.quantity = quantity;
    }
  }

  private ArrayList<Goods> populateGoods()
  {
    ArrayList<Goods> goodsList = new ArrayList<Goods>();

    File file = new File("d:\\text.txt");
    try
    {
      BufferedReader br = new BufferedReader(new FileReader(file));
      String line;
      while ((line = br.readLine()) != null)
      {

          String[] itemsOnLine = line.trim().split(",");
          goodsList.add(new Goods(itemsOnLine[0],itemsOnLine[1],itemsOnLine[2]));
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    return goodsList;
  }


  public static void main(String[] args)
  {
    FileReaderExample fileReaderExample = new FileReaderExample();
    ArrayList<Goods> goodsList  =  fileReaderExample.populateGoods();

    for (Goods goods : goodsList)
    {
      System.out.println(goods.getGoodName());
    }
 }
}

Upvotes: 0

Nomesh DeSilva
Nomesh DeSilva

Reputation: 1649

I think you are asking about java generics. If so, create your Goods class since you need to access it as an object via the ArrayList.

  public Class Goods implements Serializable {
  private String goodName;
  private double price;
  private int quantity;

  public String getGoodName() {
    return goodName;
  }

  public void setGoodName(String goodName) {
    this.goodName = goodName;
  }

  public double getPrice() {
    return price;
  }

  public void setPrice(double price) {
    this.price = price;
  }

  public int getQuantity() {
    return quantity;
  }

  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

}

and then Write your GoodsList class to create the list with Goods object you set:

public class GoodsList {

  public static void main(String args[]) {

    Goods g = new Goods();
    Goods g2 = new Goods();
    Goods g3 = new Goods();

    g.setGoodName("hat");
    g.setQuantity(50);
    g.setPrice(100.00);

    g2.setGoodName("pants");
    g2.setQuantity(50);
    g2.setPrice(100.00);

    g3.setGoodName("shoes");
    g3.setQuantity(50);
    g3.setPrice(100.00);

    List < Goods > goodsList = new ArrayList < Goods > ();
    goodsList.add(g);
    goodsList.add(g2);
    goodsList.add(g3);


    //printing goods:

    for (Goods g: goodsList) {
      System.out.println(g.getGoodName() + "," + g.getQuantity() + "," + g.getPrice());
    }

  }

}

is that what you're looking for?

Upvotes: 1

Related Questions