如何在arraylist中使用以前定义的类

时间:2014-02-16 15:23:18

标签: java arraylist inventory

我正在编写一个java库存问题的补充,该问题使用arraylist编写方法来添加,搜索等库存中的项目。

我正在使用另一个名为InventoryItem的类,它包含sku,itemName,price和stock的变量。

我想知道在编写添加项目的方法时,我是否正在使用一个过去定义的类。

import java.util.*;

public class Warehouse {

      private ArrayList<InventoryItem> inventory = new ArrayList<InventoryItem>();

      public static void addItem(InventoryItem i ) 

          inventory.add(i);

      }

      public static void main(String [] args) {

           addItem();    

      }
}

这是InventoryItem类;

public class InventoryItem {

   private int sku;
   private String itemName;
   private double price;
   private int stock;



   public InventoryItem (int sku, String itemName, double price, int stock) {

   this.sku = sku;      
   this.itemName = itemName;
   this.price = price; 
   this.stock = stock;

}

   public int getSku() {

      return sku;

   }


   public String getitemName () {

      return itemName;

   }

   public void setPrice (double price) {

      this.price = price;

   }   

   public double getPrice () {

      return price;

   }

   public void setStock(int stock) {

      this.stock = stock;

   }


   public int getStock() {

      return stock;

   }
    @Override
   public String toString() {

      return String.format("[%d ,%s ,%1.2f ,%d]", sku, itemName, price, stock);

   }

   public static void main(String[] args) {
      InventoryItem itemName = new InventoryItem(1, "asdf", 2.4, 5);
      System.out.println(itemName);

   }

}

2 个答案:

答案 0 :(得分:2)

addItem()方法存在的主要问题是static,因此无法访问warehouse列表,这是一个实例只能从实例访问的变量。

要解决此(及其他)问题,请尝试以下操作:

public class Warehouse {
    private List<InventoryItem> inventory = new ArrayList<InventoryItem>();

    public void addItem(InventoryItem i) 
        inventory.add(i);
    }

    public static void main(String [] args) {
        // create a Warehouse instance
        Warehouse warehouse = new Warehouse();
        // create an InventoryItem instance 
        InventoryItem i = new InventoryItem(sku, itemName, price, stock);
        // add the InventoryItem to the Warehouse
        warehouse.addItem(i);    
    }
}

答案 1 :(得分:1)

你是对的。调用addItem()方法时,需要传入InventoryItem对象。所以也许更像是:

double sku = 111;
String itemName = "someItem";
double price = 2.99;
int stock = 1;

InventoryItem inventoryItem = new InventoryItem(sku, itemName, price, stock);
addItem(inventoryItem);

您当然需要确保在InventoryItem类中有一个构造函数,它接受您需要的参数。

所以,基于你的InventoryItem类,以及下面的Bohemian的额外提示,它将如下所示:

     import java.util.ArrayList;
     import java.util.List;

    public class Warehouse {
        private List<InventoryItem> inventory = new ArrayList<InventoryItem>();

        public void addItem(InventoryItem i) {
        inventory.add(i);
    }

    public static void main(String [] args) {
        // create a Warehouse instance
        Warehouse warehouse = new Warehouse();
        // create an InventoryItem instance 

        int sku = 111;
        String itemName = "someItem";
        double price = 2.99;
        int stock = 1;

        InventoryItem i = new InventoryItem(sku, itemName, price, stock);
        // add the InventoryItem to the Warehouse
        warehouse.addItem(i);    
    }
}