[java] 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++)
    {

    }
}
}

This question is related to java object arraylist while-loop

The answer is


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++;
        }
    }
}

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to object

How to update an "array of objects" with Firestore? how to remove json object key and value.? Cast object to interface in TypeScript Angular 4 default radio button checked by default How to use Object.values with typescript? How to map an array of objects in React How to group an array of objects by key push object into array Add property to an array of objects access key and value of object using *ngFor

Examples related to arraylist

Adding null values to arraylist How to iterate through an ArrayList of Objects of ArrayList of Objects? Dynamically adding elements to ArrayList in Groovy How to replace existing value of ArrayList element in Java How to remove the last element added into the List? How to append elements at the end of ArrayList in Java? Removing Duplicate Values from ArrayList How to declare an ArrayList with values? In Java, can you modify a List while iterating through it? Load arrayList data into JTable

Examples related to while-loop

While, Do While, For loops in Assembly Language (emu8086) MySQL Insert with While Loop Python loop to run for certain amount of seconds How to break a while loop from an if condition inside the while loop? How to find sum of several integers input by user using do/while, While statement or For statement Python: How to keep repeating a program until a specific input is obtained? Creating multiple objects with different names in a loop to store in an array list ORA-06502: PL/SQL: numeric or value error: character string buffer too small How to break out of a loop in Bash? for or while loop to do something n times