15

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++)
    {

    }
}
}
8
  • 2
    You constructor is not doing anything useful with the arguments passed. Commented Nov 13, 2013 at 18:43
  • 1
    Create a String array containing names, and when you loop to add your objects, pick a random value in this array. Commented Nov 13, 2013 at 18:44
  • What do you mean? Isn't it creating a Customer object? Commented Nov 13, 2013 at 18:44
  • 1
    Sure, but not setting any fields, only local variables. Commented Nov 13, 2013 at 18:45
  • I have other methods that can access the local variables. Commented Nov 13, 2013 at 18:47

2 Answers 2

16
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;
    }
}
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks! and so by doing this would the objects not have names? only the references from the array list?
Right... instead of myObject1, myObject2, myObject3, they'd be custArr.get(0), custArr.get(1), custArr.get(2), etc. But the name is just how you reference it in code... the data stored within the objects doesn't care how you call the object...
@nhgrif Apologies, there were no answers when I posted mine :)
@m59 Mmm, maybe they appeared between you starting your answer and posting your answer.
@nhgrif Now I am getting the error "The method add(Customer) is undefined for the type Store". I have 3 classes. Store, Customer, and StoreTest. The array of Customer type is declared in the StoreTest class. The the while loop code is contained in the Store class and invoked in the StoreTest class. So how do I get rid of this error? It comes at this line: this.add(new Customer(customerName, amount));
|
5

You can use this code...

public class Main {

    public static void main(String args[]) {
        String[] names = {"First", "Second", "Third"};//You Can Add More Names
        double[] amount = {20.0, 30.0, 40.0};//You Can Add More Amount
        List<Customer> customers = new ArrayList<Customer>();
        int i = 0;
        while (i < names.length) {
            customers.add(new Customer(names[i], amount[i]));
            i++;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.