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

I am trying to create mutliple objects of type class I. Then I want to pass these values ​​to a list of arrays. How to create objects using a while loop that have different names. For example, here is my code now, but it will only make an object with the same 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 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; } } 

Save class (there are methods for interacting 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++) { } } } 
+6
source share
2 answers
 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 will need to fix your constructor ...


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

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

Change the Store class to something 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; } } 
+11
source

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

Source: https://habr.com/ru/post/958021/


All Articles