简体   繁体   English

在循环中创建具有不同名称的多个对象以存储在数组列表中

[英]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. 如何使用具有不同名称的while循环创建对象。 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): StoreTest类 (使用main方法):

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: 商店类 (有解决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++)
    {

    }
}
}
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: 假设您的Customer类具有名为namesale变量,您的构造函数应如下所示:

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

Change your Store class to something more like this: Store类更改为更像这样的内容:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM