0

I have the code for my InventoryManager class, my Product class, and my InventoryApp class . Based off this code, I need someone help me correctly call the methods I already have in place. There is a section where the method is supposed to be called and I have made a comment in that place because when I call the method as InventoryManager.getProduct(upc) or similar to that way I get errors. So I changed the methods in the InventoryManager class to static to make the errors go away but that just created more problems. Please help me call these methods correctly and finish this program. Any help is greatly appreciate. Thanks in advance.

InventoryManager.java class

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 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;
   }


}

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");
                   // Add method here (InventoryManager.getProduct(upc);) ----maybe I'm calling the methods wrong but this is what I did and it won't work. 
                   break;
               case 2:
                   System.out.println("Add the Product");
                   // Add method Here ( InventoryManager.addProduct(p);)
                   break;
               case 3:
                   System.out.println("Update the Product");
                   // Add method Here (InventoryManager.updateProduct(p);)
                   break;
               case 4:
                   System.out.println("Remove the Product");
                   // Add method Here ( InventoryManager.removeProduct(upc);)
                   break;
               case 5:
                   System.out.println("Exit from the Menu");                  
                   System.exit(0);                  
           }
       }

   }
}
2
Contributors
5
Replies
14
Views
1 Year
Discussion Span
Last Post by jjones0150
0

You first need to create an instance of InventoryManager - once you have an actual instance then you can do all the following operations using that instance. eg

InventoryManager myManager = new InventoryManager();
...
myManager.addProduct(...
myManager.getProduct(...

Why does it work this way? - one example would be if you had two warehouses (Boston and Denver), each with its own inventory of products. You can now create two instances of InventoryManager, one for each location, and manage them both at the same time.

0

So would I create that new instance under the inventory manager class or the inventory app main class? Also would I just be adding the myManager.addProduct to the product class or the inventory manager as well?

0

It's the inventory application class that needs an instance of InventoryManager to work with.
I don't understand your second question.

0

I see from your new thread that you made all your members static. Just be aware that although this fixes this particular problem, it's completely the wrong way to do things in Java, so don't make a habit of it!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.