Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the Product class... All \"get\" and \"set\" methods should be standard fiel

ID: 3663496 • Letter: F

Question

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, 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.

CODE FOR CollectionFileStorageUtility:

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;

/**
* Provides utility methods to save and load a collection of objects to and from
* a directory in the user's home directory.
*
* The name of the directory is "{@value #FILE_DIR_NAME}", and the name of the
* file in which the is saved is that collection's generic type with the ".dat"
* extension. This means that if you have some {@literal List} object
* and save it then it will be saved to "Person.dat". If you then later also
* save a {@literal Set} then the original Person.dat will be
* overwritten with the new collection.
*
*/
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} object will produce the file "Circle.dat".
*
* @param 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 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);
}

}

/**
* 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 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 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();
}
}
}

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());
                   }
                   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");
       }
      
   }
}