Results 1 to 4 of 4
Thread: Update Function won't update!
- 02-05-2013, 12:13 PM #1
Member
- Join Date
- Feb 2013
- Location
- Scenic Flint, MI
- Posts
- 6
- Rep Power
- 0
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"); } } } }
- 02-05-2013, 12:21 PM #2
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
- 02-05-2013, 12:35 PM #3
Member
- Join Date
- Feb 2013
- Location
- Scenic Flint, MI
- Posts
- 6
- Rep Power
- 0
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!!
- 02-05-2013, 01:49 PM #4
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
-
Update
By razmyasdfg in forum New To JavaReplies: 1Last Post: 07-17-2011, 04:52 AM -
SQL Update function
By liluma in forum New To JavaReplies: 7Last Post: 07-15-2011, 05:54 PM -
Hibernate update function
By ananddevaraj in forum Web FrameworksReplies: 1Last Post: 06-30-2010, 01:28 PM -
Auto Update Function
By abiieez in forum New To JavaReplies: 4Last Post: 04-16-2008, 04:17 PM -
Using sql:update tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks