3 Replies - 3654 Views - Last Post: 12 December 2010 - 07:03 AM Rate Topic: -----

#1 Tomfarm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 10

Creating an instance of an object via User Input

Posted 11 December 2010 - 09:41 AM

My current program contains four classes; Hotel, Room, Customer and Menu. The menu class acts as a driver for the other 3 classes. I am attempting to create a method in the "menu" class that would create an instance of "Customer", using input from the user. However, I don't know how to name the instance via user input; Here is my current code-

// add new Customer
      System.out.println("Enter Customer ID");
      int tempID= UserInput.readInt();
      System.out.println("Enter Customer's Name");
      String tempName= UserInput.readString();
      UserInput.prompt("Enter Customer's Phone Number");
      String tempPhone = UserInput.readString();
      Customer tempNew= new Customer(tempID, tempName, tempPhone);


This won't work as it will constantly create the customer object "tempNew". How can I use user input to name it differently each time? Is this even possible, or I am simply going about the whole process incorrectly?

Thank you for your time,

Tomfarm

Is This A Good Question/Topic? 0
  • +

Replies To: Creating an instance of an object via User Input

#2 wreckmyhead  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 21-October 10

Re: Creating an instance of an object via User Input

Posted 11 December 2010 - 03:43 PM

You could try it this way.


public Customer createCustomer()
   {
     //obtain args
      return new Customer(//add args);
   }
}

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,758
  • Joined: 27-December 08

Re: Creating an instance of an object via User Input

Posted 11 December 2010 - 03:48 PM

You should use an array or Collection of some sort, like an ArrayList. Since arrays and ArrayLists are indexed numerically, you can simply add the new Customer to the end of the data structure.
Was This Post Helpful? 2
  • +
  • -

#4 Tomfarm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 10

Re: Creating an instance of an object via User Input

Posted 12 December 2010 - 07:03 AM

Thank you Macosxnerd101, I think I shall use an ArrayList.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1