Writing an update method

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 3141 Views - Last Post: 25 June 2009 - 04:42 PM Rate Topic: -----

#1 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Writing an update method

Post icon  Posted 24 June 2009 - 08:03 AM

Is this how you write a method for "updateProduct"?
private static void updateProduct() 
{
	  ProductMaintApp.updateProduct();		
}


Do I need to add get and set methods as well?

Thanks

This post has been edited by rollsroyce: 24 June 2009 - 08:05 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Writing an update method

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing an update method

Posted 24 June 2009 - 08:06 AM

What are you attempting to do exactly? What you have there wont do a lot at the moment.
Was This Post Helpful? 0
  • +
  • -

#3 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Writing an update method

Posted 24 June 2009 - 08:09 AM

View Postnick2price, on 24 Jun, 2009 - 07:06 AM, said:

What are you attempting to do exactly? What you have there wont do a lot at the moment.


I am trying to add an "updateProduct" function so that the user can update a product from the menu.
Was This Post Helpful? 0
  • +
  • -

#4 kmangold  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Writing an update method

Posted 24 June 2009 - 08:13 AM

In Java, they are called methods. And yes, you successfully created a method called updateProduct.
Was This Post Helpful? 1
  • +
  • -

#5 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing an update method

Posted 24 June 2009 - 08:25 AM

View Postkmangold, on 24 Jun, 2009 - 07:13 AM, said:

In Java, they are called methods. And yes, you successfully created a method called updateProduct.


If i am right in thinking however that this method is in ProductMaintApp class, although i could be wrong, the method is just going to call itself up and do nothing. It might help to see more code. Does your Product have its own class? If so, you can just update it through the constructor. If your product is just a single variable, say a String, you might just do
public void updateProduct(String pro){
this.pro=pro;
}


It tottally depends on what exactly you are trying to do. More code might help.
Was This Post Helpful? 1
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Writing an update method

Posted 24 June 2009 - 08:29 AM

View Postkmangold, on 24 Jun, 2009 - 07:13 AM, said:

In Java, they are called methods. And yes, you successfully created a method called updateProduct.

He may have created the method but logically there are many errors. First starting off with it's going to be recursive. There is no code to the method. He has given no information to what exactly it is updating/ params if any.

int ohYeah = "owned";

Yay I created an int! Doesn't make a correct :)
Was This Post Helpful? 0
  • +
  • -

#7 kmangold  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Writing an update method

Posted 24 June 2009 - 08:57 AM

View PostFuzzyness, on 24 Jun, 2009 - 09:29 AM, said:

He may have created the method but logically there are many errors. First starting off with it's going to be recursive. There is no code to the method. He has given no information to what exactly it is updating/ params if any.


Maybe he has another updateProduct() method in a different class; this would not necessarily make it recursive.

Quote

Yay I created an int! Doesn't make a correct :)


That is true. But based off what the user gave us, that is the best short answer.
Was This Post Helpful? 0
  • +
  • -

#8 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Writing an update method

Posted 24 June 2009 - 09:39 AM

View Postnick2price, on 24 Jun, 2009 - 07:25 AM, said:

View Postkmangold, on 24 Jun, 2009 - 07:13 AM, said:

In Java, they are called methods. And yes, you successfully created a method called updateProduct.


If i am right in thinking however that this method is in ProductMaintApp class, although i could be wrong, the method is just going to call itself up and do nothing. It might help to see more code. Does your Product have its own class? If so, you can just update it through the constructor. If your product is just a single variable, say a String, you might just do
public void updateProduct(String pro){
this.pro=pro;
}


It tottally depends on what exactly you are trying to do. More code might help.


Here is my code for the ProductMaintApp:
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");
		}
   
	}
	ProductMaintApp updateProduct = new ProductMaintApp();
	  
	
	
	private static void updateProduct() 
	{
		ProductMaintApp.updateProduct();
	}
	

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

Was This Post Helpful? 0
  • +
  • -

#9 kmangold  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 167
  • Joined: 24-June 09

Re: Writing an update method

Posted 24 June 2009 - 09:42 AM

Yes, that would be an infinite recursive loop.
Was This Post Helpful? 0
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing an update method

Posted 24 June 2009 - 09:43 AM

Ok, so at the moment your update method is going to do nothing, just become recursive. I cant see how your storing your products in ProductDAO, are you sending them to a database because thats the sort of class name I would give to an database query class. If not, are you storing them in a Collection of some sort, e.g. Array, ArrayList...
Was This Post Helpful? 1
  • +
  • -

#11 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Writing an update method

Posted 24 June 2009 - 03:14 PM

View Postnick2price, on 24 Jun, 2009 - 08:43 AM, said:

Ok, so at the moment your update method is going to do nothing, just become recursive. I cant see how your storing your products in ProductDAO, are you sending them to a database because thats the sort of class name I would give to an database query class. If not, are you storing them in a Collection of some sort, e.g. Array, ArrayList...


After the description or price of the Product object is updated, the updateProduct method should call the updateProduct method of the ProductDAO object to update the product. This is what I have so far:
import java.util.Scanner;


public class ProductMaintApp
{
	// declare two class variables
	private static ProductDAO productDAO = null;
	private static Scanner sc = null;
	private static String code;
	private static String description;
	private static double price;

	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");
		}
	}
						  
	private static void updateProduct() 
	{
				   		
	}
	

	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 setDescription(String description) {
		ProductMaintApp.description = description;
	}

	public static String getDescription() {
		return description;
	}

	public static void setCode(String code) {
		ProductMaintApp.code = code;
	}

	public static String getCode() {
		return code;
	}

	public static void setPrice(double price) {
		ProductMaintApp.price = price;
	}

	public static double getPrice() {
		return price;
	}
}


Was This Post Helpful? 0
  • +
  • -

#12 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing an update method

Posted 24 June 2009 - 03:22 PM

Can u shouw your ProductDAO class
Was This Post Helpful? 0
  • +
  • -

#13 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Writing an update method

Posted 24 June 2009 - 03:43 PM

View Postnick2price, on 24 Jun, 2009 - 02:22 PM, said:

Can u shouw your ProductDAO class


There are 9 files to my ProductMaintApp. All I need to be able to do is to modify the ProductMaintApp class and call the updateProduct method of the ProductDAO object to update the product.
Was This Post Helpful? 0
  • +
  • -

#14 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Writing an update method

Posted 24 June 2009 - 03:54 PM

But surely you have a ProductDAO class and DAOFactory class? I dont think these are java classes. I just wanted to see them so i can see what your adding your product too, because surely you are saving it into some sort of Collection. But the basic function would be to instantiate the Obect class, and call the updateMethod
 productDAO.updateProduct(); //might need to pass it somthing but i cant tell unless i see the class


Can you just say whether you have these classes or if this is the only class you have?
Was This Post Helpful? 0
  • +
  • -

#15 rollsroyce  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Writing an update method

Posted 24 June 2009 - 06:16 PM

View Postnick2price, on 24 Jun, 2009 - 02:54 PM, said:

But surely you have a ProductDAO class and DAOFactory class? I dont think these are java classes. I just wanted to see them so i can see what your adding your product too, because surely you are saving it into some sort of Collection. But the basic function would be to instantiate the Obect class, and call the updateMethod
 productDAO.updateProduct(); //might need to pass it somthing but i cant tell unless i see the class


Can you just say whether you have these classes or if this is the only class you have?


Yes. I have 9 classes that are packaged together. DAOFactory, Product, ProductConstants, ProductDAO, ProductMaintApp (which I have displayed), ProductReader, ProductTextFile, ProductWriter, and Validator class. The ProductDAO:

 public interface ProductDAO extends ProductReader, ProductWriter, ProductConstants
{
	// all methods from the ProductReader and ProductWriter interfaces
	// all static constants from the DAOConstants interface
}


As per out instructor, all we need to do is to modify the ProductMaintApp to add an "updateProduct" method. I'm not sure if what I have so far is correct. Can you help me?

This post has been edited by rollsroyce: 24 June 2009 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3