Results 1 to 4 of 4
  1. #1
    Indigrace is offline Member
    Join Date
    Feb 2013
    Location
    Scenic Flint, MI
    Posts
    6
    Rep Power
    0

    Default Update Function won't update!

    Hello!! I'm new to Java and the instructions for my assignment are to add an update function to the Product Maintenance application. I've done that and the application almost works, HOWEVER, I'm missing something because it won't update the product information. The basics work, I just need help on what I'm missing and our text isn't helping. Thank you!!
    Here is the code that I have so far

    Java Code:
    import java.util.Scanner;
    
    
    
    public class ProductMaintApp
    
    {
        // declare two class variables
        private static ProductDAO productDAO = null;
        private static Scanner sc = null;
    
        public static void main(String args[])
    
        {
    System.out.println("Welcome to the Product Maintenance application\n");
    
            // set the class variables
            productDAO = DAOFactory.getProductDAO();
          sc = new Scanner(System.in);
    
            // display the command menu
            displayMenu();
    
    
            // perform 1 or more actions
            String action = "";
            while (!action.equalsIgnoreCase("exit"))
            {
                // get the input from the user
                action = Validator.getString(sc,
                    "Enter a command: ");
                System.out.println();
    
                if (action.equalsIgnoreCase("list")) {
                    displayAllProducts();
                }
                else if (action.equalsIgnoreCase("add")) {
                    addProduct();
                }
                else if (action.equalsIgnoreCase("update")) {
                    updateProduct();
                }
                else if (action.equalsIgnoreCase("del") ||
                         action.equalsIgnoreCase("delete")) {
                    deleteProduct();
                }
                else if (action.equalsIgnoreCase("help") ||
                         action.equalsIgnoreCase("menu")) {
                    displayMenu();
                }
                else if (action.equalsIgnoreCase("exit")) {
                    System.out.println("Bye.\n");
                }
                else {
                    System.out.println("Error! Not a valid command.\n");
                }
            }
        }
    
        public static void displayMenu()
        {
            System.out.println("COMMAND MENU");
            System.out.println("list    - List all products");
            System.out.println("add     - Add a product");
            System.out.println("update  - Update a product");
            System.out.println("del     - Delete a product");
            System.out.println("help    - Show this menu");
            System.out.println("exit    - Exit this application\n");
        }
    
        public static void displayAllProducts()
        {
            System.out.println("PRODUCT LIST");
            System.out.println(productDAO.getProductsString());
        }
    
        public static void addProduct()
        {
            String code = Validator.getString(
                sc, "Enter product code: ");
            String description = Validator.getLine(
                sc, "Enter product description: ");
            double price = Validator.getDouble(
                sc, "Enter price: ");
    
            Product product = new Product();
            product.setCode(code);
            product.setDescription(description);
            product.setPrice(price);
            productDAO.addProduct(product);
    
            System.out.println();
            System.out.println(description
                + " has been added.\n");
        }
    
        public static void deleteProduct()
        {
            String code = Validator.getString(sc,
                "Enter product code to delete: ");
    
            Product p = productDAO.getProduct(code);
    
            System.out.println();
            if (p != null)
            {
                productDAO.deleteProduct(p);
                System.out.println(p.getDescription()
                    + " has been deleted.\n");
            }
            else
            {
                System.out.println("No product matches that product code.\n");
            }
    
        }
        public static void updateProduct() {
            {
       String code = Validator.getString(sc,
                "Enter product code to update: ");
       String description = Validator.getLine(
                sc, "Enter updated product description: ");
            double price = Validator.getDouble(
                sc, "Enter updated price: ");
           
            Product p = productDAO.getProduct(code);
            
            Product product = new Product();
            product.setDescription(description);
            product.setPrice(price);
            
            
             System.out.println();
            if (p != null)
           
                {
                    productDAO.updateProduct(p);
                    System.out.println("Update Complete. Here is your new product information: \n\n"
                        + "Product Code:   " + p.getCode() + "\n"
                        + "Description:    " + p.getDescription() + "\n"
                        + "Current Price:  " + p.getFormattedPrice() + "\n");       
                
            }
            else
            {
                System.out.println("No product matches that product code.\n");
            
      
    
        
    }
            }
        }
    
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Update Function won't update!

    You're setting the new information on product, but you update p:
    Java Code:
            Product p = productDAO.getProduct(code);
             
            Product product = new Product();
            product.setDescription(description);
            product.setPrice(price);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Indigrace is offline Member
    Join Date
    Feb 2013
    Location
    Scenic Flint, MI
    Posts
    6
    Rep Power
    0

    Default Re: Update Function won't update!

    THANK YOU!!! I just realized what my tired eyes were missing and it works great!! Thank you SO much!!

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: Update Function won't update!

    You're welcome.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Update
    By razmyasdfg in forum New To Java
    Replies: 1
    Last Post: 07-17-2011, 04:52 AM
  2. SQL Update function
    By liluma in forum New To Java
    Replies: 7
    Last Post: 07-15-2011, 05:54 PM
  3. Hibernate update function
    By ananddevaraj in forum Web Frameworks
    Replies: 1
    Last Post: 06-30-2010, 01:28 PM
  4. Auto Update Function
    By abiieez in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 04:17 PM
  5. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •