I have the code for my InventoryManager class and my Product class as well as th
ID: 3663495 • Letter: I
Question
I have the code for my InventoryManager class and my Product class as well as the CollectionFileStorageUtility class. Based off this code, I need someone to help me write my InventoryApp Class which is a simple application which uses a menu , 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. Please also show the correct way of calling the methods from the InventoryManager class. ALL HELP is greatly appreciated! Thanks in advance.
InventoryManager.java class
import edu.lcc.citp.utility.CollectionFileStorageUtility;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class InventoryManager {
public static List getProductList() {
List products=null;
try {
products=(List) CollectionFileStorageUtility.load(Product.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return products;
}
/**
*
* @param upc
* @return
*/
public static Product getProduct(String upc)
{
List products = getProductList();
for (Product pro : products) {
if (pro.getUpc().equals(upc)) {
return pro;
}
}
return null;
}
/**
*
* @param p
*/
public static void addProduct(Product p)
{
List 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();
}
products.add(p);
try
{
CollectionFileStorageUtility.save(products, Product.class);
}
catch (IOException e)
{
e.printStackTrace();
}
}
/**
*
* @param p
*/
public static void updateProduct(Product p)
{
List 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 static void removeProduct(String upc)
{
List 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 class
import java.io.Serializable;
import java.math.BigDecimal;
public class Product implements Comparable,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;
}
}
CollectionFileStorageUtility.java class
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 void save(Collection collection, Class 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 Collection load(Class 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) ois.readObject();
}
} else {
return Collections.emptySet();
}
}
}
ALSO, Here are the requirements for this project just in case you need more claification. Thanks!
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
InventoryApp.java
import java.math.BigDecimal;
import java.util.Scanner;
public class InventoryApp {
public static void main(String[] args) {
while(true)
{
System.out.println("Enter add to add a product");
System.out.println("Enter remove to remove a product");
System.out.println("Enter update to update a product");
System.out.println("Enter view to view a product");
System.out.println("Enter quit to quit the menu");
Scanner sc = new Scanner(System.in);
String s="";
s=sc.nextLine();
if(s.equalsIgnoreCase("quit"))
{
break;
}
else if(s.equalsIgnoreCase("add")||s.equalsIgnoreCase("update"))
{
Product pro=new Product();
System.out.println("Enter the upc of the product");
String upc=sc.nextLine();
pro.setUpc(upc);
System.out.println("Enter the short detail of the product");
String sDetail=sc.nextLine();
pro.setShortDetails(sDetail);
System.out.println("Enter the long detail of the product");
String lDetail=sc.nextLine();
pro.setLongDetails(lDetail);
System.out.println("Enter the price of the product");
BigDecimal price=sc.nextBigDecimal();
pro.setPrice(price);
System.out.println("Enter the stock of the product");
int stock=sc.nextInt();
pro.setStock(stock);
InventoryManager inventory=new InventoryManager();
if(s.equalsIgnoreCase("add"))
{
inventory.addProduct(pro);
}
else if(s.equalsIgnoreCase("update"))
{
inventory.updateProduct(pro);
}
}
else if(s.equalsIgnoreCase("view")||s.equalsIgnoreCase("remove"))
{
System.out.println("Enter the upc of the product");
String upc=sc.nextLine();
InventoryManager inventory=new InventoryManager();
if(s.equalsIgnoreCase("view"))
{
Product pro=inventory.getProduct(upc);
if(pro!=null)
{
System.out.println("Product Details");
System.out.println("product upc "+pro.getUpc());
System.out.println("short details "+pro.getShortDetails());
System.out.println("long details "+pro.getLongDetails());
System.out.println("price "+pro.getPrice());
System.out.println("stock view"+pro.getStock());
}
else
{
System.out.println("No product exist with given upc");
}
}
else if(s.equalsIgnoreCase("remove"))
{
inventory.removeProduct(upc);
}
}
}
}
}
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 {
if(!CollectionFileStorageUtility.load(Product.class).isEmpty())
{
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());
}
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();
if(products==null)
{
return;
}
else
{
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");
}
}
}