Sunday 15 August 2010

Using an ArrayList to add new objects in java? -


I'm having trouble understanding an ArrayList I'm writing a program using 3 classes of client, video and invoice I am In the code given below, I am creating a new customer to add ArrayList to set up, but I think I am trying to treat it as it is an array, I want the user to have another customer object Wants to be able to add and run through a series of questions to use a counter "I" and add that client object. I know some of this is quite messy. Import java.util.Scanner; Import java.util.ArrayList; Public class Prog4 {public static zero main (string [] args) {scanner = new scanner (System.in); ArrayList & lt; Customers & gt; Customers = new ArrayList & lt; Customers & gt; (); Int i = 0; Cher Ens; Do {System.out.print ("Customer Name:"); String name = in.next (); Customer.add (i,) setName (name). System.out.print ("Street Address:"); String street address = in. Nxt (); D1.setStreetAddress (streetAddress); System.out.print ("City:"); String city = in.next (); D1.setCity (city); System.out.print ("state:"); String state = in.next (); D1.setState (state); System.out.print ("zipcode:"); String zipcode = in.next (); D1.setZipcode (zipcode); System.out.print ("Phone Number:"); String phone number = in.next (); D1.setPhoneNumber (PhoneNumber); Customer [i] = new customer (name, street address, city, state, zipcode, phone number); System.out.print ("Do you want to enter a new customer (Y / N)?"); String Answer = in.next (); Answer = answer.charAt (0); } While (ans == 'y'); First} I recommend setting the value for the customer object and then the customer. .add (customer object)

Something like this:

  import java.util.ArrayList; Import java.util.list; Import java.util.Scanner; Public class customer {personal string name; Private string street address; Private string city; Private string state; Private string zipcode; Private string phone number; Public string getName () {return name; } Public Zero Setname (string name) {this.name = name; } Public string getStreetAddress () {Return Street Gather; } Public Zero setStreetAddress (String Street audience) {this.streetAddress = streetAddress; } Public string getCity () {return city; } Public Zero Set City (String City) {this.city = city; } Public String Millstate () {Return State; } Public Zero SetState (String State) {this.state = state; } Public string getZipcode () {return pincode; } Public Zero Set zipcode (string zipcode) {this.zipcode = zipcode; } Public string received phone number () {back phone number; } Public Zero setphone number (string phone number) {this.phoneNumber = phoneNumber; } Public static zero main (string [] args) {scanner = new scanner (System.in); List & lt; Customers & gt; Customer List = New Arrestist & lt; Customers & gt; (); Cher Ens; Doing {customer customer = new customer (); System.out.print ("Customer Name:"); Customer.setName (in.next ()); System.out.print ("Street Address:"); Customer.setStreetAddress (in.next ()); System.out.print ("City:"); Customer.setCity (in.next ()); System.out.print ("state:"); Customer.setState (in.next ()); System.out.print ("zipcode:"); Customer.setZipcode (in.next ()); System.out.print ("Phone Number:"); Customer.setPhoneNumber (in.next ()); CustomerList.add (customer); System.out.print ("Do you want to enter a new customer (Y / N)?"); String Answer = in.next (); Answer = answer.charAt (0); } While (ans == 'y'); For (Customer C: Customer List) {System.out.print (c.getName ()); } (Int i = 0; i & lt; customerList.size (); i ++) {System.out.print (customerList.get (i) .getName ()); }}}    

No comments:

Post a Comment