I'm trying to figure out why my removeProduct method won't actually remove the product from the list please give me some feedback.
InventoryApp.java class
package JayJuan;
import java.util.Scanner;
import java.math.BigDecimal;
import java.util.Scanner;
/* Main class used to add a product, remove a product, update a product,
or view the product list
*/
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"))
{
/* creates a new Product and adds it to the product list */
Product pro=new Product();
System.out.println("Enter the upc of the product");
String upc=sc.nextLine();
pro.setUpc(upc);
/* adds the details of the product and saves them to the product list
using the stored methods in the Product class
*/
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);
System.out.println("Product Added!");
/* calls the method in the inventoryManager class to add, update,
remove, or view a product based on the users input
*/
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(pro.toString());
}
else
{
System.out.println("No product exist with given upc");
}
}
else if(s.equalsIgnoreCase("remove"))
{
inventory.removeProduct(upc);
}
}
}
}
}
InventoryManager.java class
import edu.lcc.citp.utility.CollectionFileStorageUtility;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/* InventoryManager class containing the methods to add, remove, update, or view
the products stored in the list.
*/
public class InventoryManager {
/* Method used to get the current list of products the user has saved*/
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;
}
/*getProduct method used to return a specific product a user saved to the list
based off the upc
*/
public Product getProduct(String upc)
{
List<Product> products = getProductList();
for (Product pro : products) {
if (pro.getUpc().equals(upc)) {
return pro;
}
}
return null;
}
/*addProduct method used to add a product to the list and save it*/
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();
}
}
/*updateProduct method used to changed the stored information of any given
product a user has saved to the list*/
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");
}
}
/*removeProduct method used to delete a saved product from the list*/
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 class
import java.io.Serializable;
import java.math.BigDecimal;
/*Product class that declares the data and data type
that will be saved to the list, this class will also
return the details of any given product the user saves
to the list.
*/
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
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);
/**
* Saves some collection of serializable objects to a file in a directory in
* the user's home directory.
*
* The file will be named after the generic type of the collection (i.e. a
* {@literal Set<Circle>} object will produce the file "Circle.dat".
*
* @param <E> A serializable type.
* @param collection The collection of objects to save.
* @param clazz The type of the elements of the collection. This will
* determine the file name.
* @throws IOException If the necessary directory doesn't exist or cannot be
* created, or if the necessary file cannot be created or written to.
*/
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);
}
}
/**
* Loads and returns some collection of serializable objects from a file.
*
* The file must be found in a specific directory in the user's home
* directory and must be named after the specified class (representing the
* collection element type) with the extension ".dat".
*
* @param <E> A serializable type.
* @param clazz The type of the elements of the collection. This determines
* the file name to look for.
* @return The collection read from the file, or an empty immutable Set if a
* collection is not present to load.
* @throws IOException If the directory or file doesn't exist or cannot be
* read.
* @throws ClassNotFoundException If the type of the object read from the
* file does not match the requested 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();
}
}
}