I keep getting this error:
" Exception in thread "main" java.lang.ClassCastException: java.util.Collections$EmptySet cannot be cast to java.util.List
at JayJuan.InventoryManager.getProductList(InventoryManager.java:15)
at JayJuan.InventoryManager.addProduct(InventoryManager.java:48)
at JayJuan.InventoryApp.main(InventoryApp.java:35)
C:\Users\JonesJ58\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 "
Based off this error and the code I provided please help me figure out how to get this program to run correctly. I am supposed to be using the InventoryManager and Product classes for methods and the InventoryApp class is the main class. InventoryApp class is supposed to be a simple application which uses a menu, a Scanner object, and an InventoryManager object to let users view, add, update, and remove product data. Please help me resolve this issue.
InventoryApp.java class
import java.util.Scanner;
public class InventoryApp
{
public static void main(String[] args)
{
int option,quit;
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("1.View 2. Add 3. Update 4. Remove and 5. Quit Product Menu:");
System.out.println("Enter your Selection from the Above Menu Options:");
option = sc.nextInt();
switch (option)
{
case 1:
System.out.println("View the Product");
{
String upc = null;
InventoryManager.getProduct(upc);
}
break;
case 2:
System.out.println("Add the Product");
{
Product p = null;
InventoryManager.addProduct(p);
}
break;
case 3:
System.out.println("Update the Product");
{
Product p = null;
InventoryManager.updateProduct(p);
}
break;
case 4:
System.out.println("Remove the Product");
{
String upc = null;
InventoryManager.removeProduct(upc);
}
break;
case 5:
System.out.println("Exit from the Menu");
System.exit(0);
}
}
}
}
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<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;
}
/**
*
* @param upc
* @return
*/
public static Product getProduct(String upc)
{
List<Product> products = getProductList();
for (Product pro : products) {
if (pro.getUpc().equals(upc)) {
return pro;
}
}
return null;
}
/**
*
* @param p
*/
public static 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();
}
}
/**
*
* @param p
*/
public static 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 static 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 class
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;
}
}
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 <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();
}
}
}