Please help me with the JAVA code for this program with in depth explanations. C
ID: 3661775 • Letter: P
Question
Please help me with the JAVA code for this program with in depth explanations.
Create the structure for the "Product" and "InventoryManager" classes from the following class diagram. Note that "Comparable" and "Serializable" are standard Java interfaces and must be imported, not created!
For the Product class...
All "get" and "set" methods should be standard field accessors and mutators. You can use NetBeans to generate these for you!
The "compareTo" method should sort by the upc field. This means that it should return the result of comparing the current Product's upc ("this") with the parameter Product's upc ("p").
For the InventoryManager class..."getProductList" should...
Use the CollectionStorageFileUtility's "load" method to load the Product class collection...
Add the collection to a new ArrayList object...
Return the new ArrayList.
"getProduct" should...
Call "getProductList" to get the list of Products...
Find a product with the matching UPC...
Return the matching product, or null if no such product exists.
"addProduct" should...
Call "getProductList" to get the list of Products...
Check to see if a product with the same UPC already exists...
If a product with the same UPC already exists then print an error message and return...
Otherwise, add the new product to the list...
Use the CollectionStorageFileUtility's "save" method to save the updated list.
"updateProduct" should...
Call "getProductList" to get the list of Products...
Check to see if a product with the same UPC exists...
If a product with the same UPC does not exist then print an error message and return...
Otherwise, for each non-null field in the parameter product other than the UPC, update that field for the product in the list...
Use the CollectionStorageFileUtility's "save" method to save the updated list.
"removeProduct" should...
Call "getProductList" to get the list of Products...
Check to see if a product with the same UPC exists...
If a product with the same UPC does not exist then print an error message and return...
Otherwise, remove that matching product from the list...
Use the CollectionStorageFileUtility's "save" method to save the updated list.
In your main method (inside InventoryApp.java) create a simple application which uses a menu (similar to the Module 1 Milestone), a Scanner object, and an InventoryManager object to let users view, add, update, and remove product data. Be sure to use a loop so that the user can perform as many menu choices as they like before they choose to quit.
Explanation / Answer
I have tried to implement things with given information although CollectionStorageFileUtility code is not there so there may be some improper implementations.Post comments with information about CollectionStorageFileUtility so that I can help you further.
Product.java
import java.io.Serializable;
import java.math.BigDecimal;
//By implementing interface Comparable we need to implement compareTo function which is used for any //comparisons required for Product class while sorting or comparison.
public class Product implements Comparable<Product>,Serializable {
private String upc;
private String shortDetails;
private String longDetails;private BigDecimal price;
private int stock;
public String getUpc() {
return upc;
}
public void setUpc(String upc) {
this.upc = upc;
}
public String getShortDetails() {
return shortDetails;
}
public void setShortDetails(String shortDetails) {
this.shortDetails = shortDetails;
}
public String getLongDetails() {
return longDetails;
}
public void setLongDetails(String longDetails) {
this.longDetails = longDetails;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public int compareTo(Product p) {
if(this.getUpc().equals(p.getUpc()))
{
return 1;
}
return 0;
}
}
InventoryManager.java
//CollectionStorageFileUtility is not provided so no idea how it will work so I am implementing rest of the methods //except getProductList in which using CollectionStorageFileUtility's load method we need to get Product Collection
//In all other methods also we need to call save method of CollectionStorageFileUtility but not sure how to do that //without the code,so I have written a comment at places where we need to call the save method so that If you //have the code for CollectionStorageFileUtility you can call save method
import java.util.List;
public class InventoryManager {
public List<Product> getProductList() {
List<Product> products=null;
try {
products=(List<Product>) CollectionFileStorageUtility.load(Product.class);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return products;
}
public Product getProduct(String upc) {
List<Product> products = getProductList();
for (Product pro : products) {
if (pro.getUpc().equals(upc)) {
return pro;
}
}
return null;
}
public void addProduct(Product p)
{
List<Product> products = getProductList();
for (Product pro : products)
{
if (pro.compareTo(p))
{
System.out.println("A product with given upc alredy exist,cannot duplicate upc");
return;
}
}
products.add(p);
try
{
CollectionFileStorageUtility.save(products, Product.class);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void updateProduct(Product p)
{
List<Product> products = getProductList();
for (Product pro : products)
{
if (pro.compareTo(p))
{
if (!p.getLongDetails().isEmpty())
{
pro.setLongDetails(p.getLongDetails());
}
if (p.getPrice()!=null)
{
pro.setPrice(p.getPrice());
}
if (!p.getShortDetails().isEmpty())
{
pro.setShortDetails(p.getShortDetails());
}
if (pro.getStock()!=0)
{
pro.setStock(p.getStock());
}
try
{
CollectionFileStorageUtility.save(products, Product.class);
}
catch (IOException e)
{
e.printStackTrace();
}
return;
}
}
System.out.println("A product with given upc does not exist");
}
public void removeProduct(String upc) {
List<Product> products = getProductList();
for (Product pro : products)
{
if (pro.getUpc().equals(upc))
{
products.remove(pro);
try
{
CollectionFileStorageUtility.save(products, Product.class);
}
catch (IOException e)
{
e.printStackTrace();
}
return;
}
}
System.out.println("A product with given upc does not exist");
}
}