title: JAVA Implementing Interfaces
body: <p>I am working on an assignment in my JAVA class I'm stuck on. Basically I have a Product class as shown below. Then, I have a ProductDBImpl class that is to implement a ProductDB interface which is also shown below. The ProductDB is supposed to be a database of different products. Finally, the ProductDB interface is also shown below. </p>
  
  <pre><code>public class Product {
      private Integer id;
      private String name;
      private double price;
      private DeptCode dept; 
  
      public Product(String name, double price, DeptCode code){...}
      public Product(Integer id, String name, double price, DeptCode code) {...}
      public String getName() {...}
      public double getPrice() {...}
      public Integer getId() {...}
      public void setId(Integer id) {...}
      public DeptCode getDept() {...}
      public void setDept(DeptCode dept) {...}
      public void setName(String name) {...}
      public void setPrice(double price) {...}
      public String toString() {...}
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import java.util.List;
  public class ProductDBImpl implements ProductDB {
  
      public Product getProduct(int productId) {...}
      /**
       * Retrieve product by primary key
       * @param productId
       * @return null if not found
       */
      @Override
      public List&lt;Product&gt; getProductsByDept(DeptCode code) {...}
      /**
       * Retrieve all products in database
       * @return empty list if no products in database
       */
      @Override
      public void addProduct(Product product)
      /**
       * Update product in database with given information
       * @param p
       * @throws ProductNotFoundException if can't find given product by id
       */
      @Override
      public void updateProduct(Product product) throws ProductNotFoundException {...}
      /**
       * Remove product from database by product id
       * @param productId
       * @throws ProductNotFoundException if can't find given product by id
       */
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import java.util.List;
  
  public interface ProductDB {
  
  Product getProduct(int productId);
  List&lt;Product&gt; getProductsByDept(DeptCode code);
  List&lt;Product&gt; getAllProducts();
  void addProduct(Product product) throws ProductAlreadyExistsException;
  void updateProduct(Product product) throws ProductNotFoundException;
  void deleteProduct(int productId) throws ProductNotFoundException;
  }
  </code></pre>
  
  <p>I kind of understand that interfaces are sort of like a rule book that any class that tries using must follow. However, I am having a difficult time implementing the methods in the ProductDBImpl class. For example, when I try to implement the 'getProduct' method, I try the following but get errors:</p>
  
  <pre><code>    public Product getProduct(int productId) {
      // TODO Auto-generated method stub
      ProductDB someProduct = new ProductDBImpl();
      someProduct.getProduct();
  }
  </code></pre>
  
  <p>I am using the getProduct() method because it is the method in the Product class that returns the Product ID. </p>
  
  <p>Then, for the getProductsByDept() method I am not sure how to implement that because the Product class does not contain any of those methods, however there is a DeptCode class as the following:</p>
  
  <pre><code>public enum DeptCode {
       BOOK, COMPUTER, ELECTRONICS, DVD, SHOE
  }
  </code></pre>
  
  <p>Am I supposed to implement it similar to the getProduct() method as follows:</p>
  
  <pre><code>    public List&lt;Product&gt; getProductsByDept(DeptCode code) {
      // TODO Auto-generated method stub
      ProductDB someProduct = new ProductDBImpl();
      return someProduct.getProductsByDept(code);
  }
  </code></pre>
  
  <p>I guess I'm pretty confused on how to approach this whole assignment. Any help would be appreciated. Thanks!</p>
  
  <p>After tombrown52's post things started making more sense. I started by adding the ArrayList for Products and implementing the getProduct method in ProductDBImpl class. However I am getting an error. Here is my code:</p>
  
  <pre><code>public List&lt;Product&gt; Products;
  
      @Override
  public Product getProduct(int productId) {
      // TODO Auto-generated method stub
      for (int i = 0; i &lt; Products.size(); i++)
      {
          if (Products.get(i).getId() == productId )
          {
              return Products.get(i);
          }
          else
          {
              return null;
          }
      }
  </code></pre>
  
  <p>The error I'm getting is that "This method must return a result of type Product'. I thought Products.get(i) was a Product? </p>
  
  <p>Latest Edit:
  I am totally stumped. I have even tried the following code and still no luck:</p>
  
  <pre><code>// field declarations
      public ArrayList products = new ArrayList();
  
  
  
      @Override
      public Product getProduct(int productId)
      {
          // TODO Auto-generated method stub
  
          for (int i = 0; i &lt; products.size(); i++)
          {
              Product p = (Product)products.get(i);
              if (p.getId() == productId )
              {
                  return p;
              }
              else
              {
                  return null;
              }
          }
      }
  </code></pre>