12

作成したクラスのタイプの複数のオブジェクトを作成しようとしています。次に、これらの値を配列リストに転送したいと思います。while ループを使用して異なる名前のオブジェクトを作成するにはどうすればよいですか。たとえば、ここに私のコードがありますが、1 つの名前のオブジェクトしか作成しません。

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

見たい場合は私のコンストラクター:

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

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

}

顧客クラス:

public class Customer {

private String name;
private double sale;

public Customer()
{

}

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

Store クラス(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++)
    {

    }
}
}
4

2 に答える 2

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

これが機能するには...コンストラクタを修正する必要があります...


Customerクラスに and という変数があると仮定するnamesale、コンストラクターは次のようになります。

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

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;
    }
}
于 2013-11-13T18:51:07.943 に答える
5

このコードを使用できます...

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++;
        }
    }
}
于 2013-11-13T18:50:40.063 に答える