user1953907 user1953907 -4 years ago 34
Java Question

Creating multiple objects with different names in a loop to store in an array list

I am trying to create mutliple objects of a type of class I made. I then want to transfer these values into the array list. How can I create objects using a while loop that have different names. For example here is my code now, but it would only make an object of one name.

Customer cust = new Customer("bob", 20.0);


and my constructor if you want to see:

public Customer(String customerName, double amount)
{
String name=customerName;
double sale=amount;
}


StoreTest class (with main method):

import java.util.ArrayList;
import java.util.Scanner;

public class StoreTest {

ArrayList<Customer> store = new ArrayList<Customer>();

public static void main (String[] args)
{
double sale=1.0; //so the loop goes the first time
//switch to dowhile
Scanner input = new Scanner(System.in);

System.out.println("If at anytime you wish to exit" +
", please press 0 when asked to give " +
"sale amount.");
while(sale!=0)
{
System.out.println("Please enter the " +
"customer's name.");
String theirName = input.nextLine();

System.out.println("Please enter the " +
"the amount of the sale.");
double theirSale = input.nextDouble();

store.addSale(theirName, theirSale);
}
store.nameOfBestCustomer();
}

}


Customer class:

public class Customer {

private String name;
private double sale;

public Customer()
{

}

public Customer(String customerName, double amount)
{
name=customerName;
sale=amount;
}
}


Store class (has methods for messing with arraylist:

import java.util.ArrayList;

public class Store {

//creates Customer object and adds it to the array list
public void addSale(String customerName, double amount)
{
this.add(new Customer(customerName, amount));
}

//displays name of the customer with the highest sale
public String nameOfBestCustomer()
{
for(int i=0; i<this.size(); i++)
{

}
}
}

Answer Source
ArrayList<Customer> custArr = new ArrayList<Customer>();
while(youWantToContinue) {
    //get a customerName
    //get an amount
    custArr.add(new Customer(customerName, amount);
}

For this to work... you'll have to fix your constructor...


Assuming your Customer class has variables called name and sale, your constructor should look like this:

public Customer(String customerName, double amount) {
    name = customerName;
    sale = amount;
}

Change your Store class to something more like this:

public class Store {

    private ArrayList<Customer> custArr;

    public new Store() {
        custArr = new ArrayList<Customer>();
    }

    public void addSale(String customerName, double amount) {
        custArr.add(new Customer(customerName, amount));
    }

    public Customer getSaleAtIndex(int index) {
        return custArr.get(index);
    }

    //or if you want the entire ArrayList:
    public ArrayList getCustArr() {
        return custArr;
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download