Please help me with the JAVA code for this program with in depth explanations. C
ID: 3661987 • 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.
here is the code for the CollectionStorageFileUtility:
package edu.lcc.citp.utility;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
public class CollectionFileStorageUtility { private static final String FILE_DIR_NAME = ".citp290files";
private static final Path FILE_DIR = paths.get(System.getProperty("user.home")).resolve(FILE_DIR_NAME); public static <E extends Serializable> void save(Collection<E> collection, Class<E> clazz) throws IOException {final Path file = FILE_DIR.resolve(clazz.getName() + ".dat"); if (!Files.exists(FILE_DIR)) { Files.createDirectory(FILE_DIR); } try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file))) { oos.writeObject(collection); } }
public static <E extends Serializable> Collection<E> load(Class<E> clazz) throws IOException, ClassNotFoundException { final Path file = FILE_DIR.resolve(clazz.getName() + ".dat");
if (Files.exists(file)) { try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(file))) { return (Collection<E>) ois.readObject(); } } else { return Collections.emptySet(); } } }
Product upc: String shortDetails: String -longDetails: String price: Big Decimal stock: Integer +getUpc():String +setU pc(String) +getShortDetails(): String +setShortDetails(String) +getLongDetails(): String +setLongDetails(String) +getPrice(): BigDecima +setPrice (BigDecimal) +getStock(): Integer +setStock (Integer) Comparable implements +compareTo(Product): int compare TolProduct) int -1 mplements Serializable dependson InventoryManager +getProduct(String upc): Product +getProductList(): List +addProduct(Product p) +updateProduct(Product p) +removeProduct(String upc)Explanation / Answer
InventoryManager.java
import java.io.IOException;
import java.util.ArrayList;
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) {
e.printStackTrace();
} catch (IOException e) {
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();
if(products!=null)
{
for (Product pro : products)
{
if (pro.getUpc().equals(p.getUpc()))
{
System.out.println("A product with given upc alredy exist,cannot duplicate upc");
return;
}
}
}
else
{
products=new ArrayList<Product>();
}
products.add(p);
try
{
CollectionFileStorageUtility.save(products, Product.class);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void updateProduct(Product p)
{
List<Product> products = getProductList();
if(products==null)
{
return;
}
else
{
for (Product pro : products)
{
if (pro.getUpc().equals(p.getUpc()))
{
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());
}
return;
}
}
System.out.println("A product with given upc does not exist");
}
}
public void removeProduct(String upc)
{
List<Product> products = getProductList();
if(products==null)
{
return;
}
else
{
for (Product pro : products)
{
if (pro.getUpc().equals(upc))
{
products.remove(pro);
return;
}
}
System.out.println("A product with given upc does not exist");
}
}
}
Product.java
import java.io.Serializable;
import java.math.BigDecimal;
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;
}
}