3 Replies - 1219 Views - Last Post: 18 March 2012 - 09:14 PM Rate Topic: ***** 1 Votes

#1 MasterZeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 39
  • Joined: 30-January 12

An array list of items whilst allowing the user to define the names?

Posted 18 March 2012 - 07:00 PM

Having a bit of trouble with the following code. Basically I need to allow a user to be prompted to create a bunch of items, however they need to be able to search the array list afterwards by the name of the item and change quantities if it pleases them. Thus they need to be able to control the name of the object so they can search by it later. My biggest problem is with the code here:

	ItemOrder item = new ItemOrder(inputName, price, quantity);
			
			cart.add(item);
			


How can I make it so that instead of creating an object called item, I can create an object with the same name as inputName (see complete code bellow)? Thus far the only logical path I could think of was prompting for the name first, then creating an empty item object with solely the name in it and setting the values afterwards, but it still has the same inherent problem of creating an object with a user defined name so it can be searched in an Array list. Otherwise my arrayList will try to add a new object item continuously and the ArrayList.contains(object) method won't work for searching.

Items are objects that have a name, and a value. I created a subclass called ItemOrder which defines the quantity. The classes for Item and ItemOrder are not included as they are simple constructors.

As usual any and all help is much appreciated. :)

Cheers
Master Zeddicus

import java.util.*; 
public class ShoppingCart {

	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		boolean keepGoing = true;
		String inputName, input; 
		int quantity; 
		double price; 
		ArrayList cart = new ArrayList();
		
		
		
		while(keepGoing)
		{
			System.out.println("What name would you like to give your item?");
			inputName = keyboard.nextLine();
			System.out.println("What is the market price of a " + inputName + "?");
			price = keyboard.nextDouble();
			
			while (price <= 0)
			{
				System.out.println("You cannot have a negative or 0 value for an article");
				System.out.println("What is the market price of a " + inputName + "?");
				price = keyboard.nextDouble();
			}
				
			System.out.println("How many " + inputName + " would you like to order?" );
			quantity = keyboard.nextInt();
			
			while (quantity < 0)
			{
				System.out.println("You cannot have a negative quanitity in your cart");
				System.out.println("How many " + inputName + " would you like to order?" );
				quantity = keyboard.nextInt();
			}
			
			ItemOrder item = new ItemOrder(inputName, price, quantity);
			
			cart.add(item);
			
			System.out.println("Would you like to create another item? (Y/N)");
			input = keyboard.nextLine();
			
			while(!input.equalsIgnoreCase("N") && !input.equalsIgnoreCase("Y"))
			{
				System.out.println("Error: That is not a valid response, please enter Y or N");
				System.out.println("Would you like to create another item? (Y/N)");
				input = keyboard.nextLine();
			}
			
			if (input.equalsIgnoreCase("Y"))
				keepGoing = true; 
			else
				keepGoing = false; 
			
		}		

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: An array list of items whilst allowing the user to define the names?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: An array list of items whilst allowing the user to define the names?

Posted 18 March 2012 - 07:51 PM

Depends what you consider duplicate or not and that should be implement in your equals() method

class ItemOrder {

   String name;
   double price;
   double quantity;

   // two ItemOrder are the same if they have the same name
   public boolean equals(Object o) {
      if(!o instanceof ItemOrder) return false;
      ItemOrder other = (ItemOrder) o;
      return name.equals(other.name);
   }

or

   // two ItemOrder are the same if they have the same name and the same price
   public boolean equals(Object o) {
      if(!o instanceof ItemOrder) return false;
      ItemOrder other = (ItemOrder) o;
      if(!name.equals(other.name)) retun false;
      return price == other.price;
   }



based on that you can:
- create your ItemOrder object
- check if it is the ArrayList using its contains() method
- reject the transaction (do not add it to he ArrayList) if it is already there

Happy coding
Was This Post Helpful? 0
  • +
  • -

#3 MasterZeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 39
  • Joined: 30-January 12

Re: An array list of items whilst allowing the user to define the names?

Posted 18 March 2012 - 09:07 PM

Sorry Pbl, but that post didn't help at all. I'm not trying to check if they're equal when someone enters a new object, though it is a valid point as far as error checking goes it's not really what I was asking.

What I'm asking is how to allow the user to define the name of the new object being created. I know generally you don't allow a user to give the name of variables and all variables are supposed to be made prior to someone loading the program but in that sense I would have to change the entire logic of the algorithm. Before I do that I need to know if it is possible or not.

Again I need to know how to allow the object name that appears in the cart array list the same as the inputName variable so it can be searched using the ArrayList.contains(object name) method.

Thanks for trying though :).
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: An array list of items whilst allowing the user to define the names?

Posted 18 March 2012 - 09:14 PM

View PostMasterZeddicus, on 19 March 2012 - 12:07 AM, said:

What I'm asking is how to allow the user to define the name of the new object being created.

You mean the actual variable name in the program ?
Snobol4 is the only programming language that I know that allows that, and DCL if you consider that as a language.

When a program is compiled, the name of the variables just vanished, they are replaced by their address in memory so you are a step ahead with the actual variable names.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1