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.
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() {...}
}
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<Product> 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
*/
}
}
import java.util.List;
public interface ProductDB {
Product getProduct(int productId);
List<Product> getProductsByDept(DeptCode code);
List<Product> getAllProducts();
void addProduct(Product product) throws ProductAlreadyExistsException;
void updateProduct(Product product) throws ProductNotFoundException;
void deleteProduct(int productId) throws ProductNotFoundException;
}
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:
public Product getProduct(int productId) {
// TODO Auto-generated method stub
ProductDB someProduct = new ProductDBImpl();
someProduct.getProduct();
}
I am using the getProduct() method because it is the method in the Product class that returns the Product ID.
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:
public enum DeptCode {
BOOK, COMPUTER, ELECTRONICS, DVD, SHOE
}
Am I supposed to implement it similar to the getProduct() method as follows:
public List<Product> getProductsByDept(DeptCode code) {
// TODO Auto-generated method stub
ProductDB someProduct = new ProductDBImpl();
return someProduct.getProductsByDept(code);
}
I guess I'm pretty confused on how to approach this whole assignment. Any help would be appreciated. Thanks!
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:
public List<Product> Products;
@Override
public Product getProduct(int productId) {
// TODO Auto-generated method stub
for (int i = 0; i < Products.size(); i++)
{
if (Products.get(i).getId() == productId )
{
return Products.get(i);
}
else
{
return null;
}
}
The error I'm getting is that "This method must return a result of type Product'. I thought Products.get(i) was a Product?
Latest Edit: I am totally stumped. I have even tried the following code and still no luck:
// field declarations
public ArrayList products = new ArrayList();
@Override
public Product getProduct(int productId)
{
// TODO Auto-generated method stub
for (int i = 0; i < products.size(); i++)
{
Product p = (Product)products.get(i);
if (p.getId() == productId )
{
return p;
}
else
{
return null;
}
}
}