user3380752
user3380752

Reputation: 1

Finding the max value in an arraylist

In this program I wrote I have to print out the name of the customer who spent the most in the store. I need help searching the array list for the customer who spent the most.

 package bestcustomer;
 import java.util.*;
/**
 *
 * @author muf15
 */
public class BestCustomer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<Double> sales = new ArrayList<Double>();
        ArrayList<String> names = new ArrayList<String>();
        double salesAmount;
        System.out.println("Enter the sales for first customer: ");
        salesAmount = in.nextDouble();
        while(salesAmount !=0)
        {
            sales.add(salesAmount);
            System.out.println("Enter customers name");
            names.add(in.next());
            System.out.println("Enter the next sales amount, 0 to exit: ");
            salesAmount = in.nextDouble();
        }
        String bestCustomer = nameOfBestCustomer(sales, names);

    }
    public static String nameOfBestCustomer(ArrayList<Double> sales,
            ArrayList<String> customers)
    {
        String name = "";
        double maxSales;



        return name;
    }


}

Upvotes: 0

Views: 1604

Answers (4)

lasclocker
lasclocker

Reputation: 311

In Java8, if defined Customer like mikey said

   customers.stream()
             .max(Comparator.comparing(Customer::getSales))
             .get()
             .getName()

Upvotes: 0

mikey
mikey

Reputation: 1460

I might be a bit late.. I think that if you create a Customer class with two fields, name and sale it would be better design as mentioned by other answers. Then in BestCustomer you could loop through the list of customer, find the highest sales and return the name. Something like this for BestCustomer

private ArrayList<Customer> customers = new ArrayList<Customer>();

public BestCustomer(){
    Scanner in = new Scanner(System.in);
    double salesAmount;
    System.out.println("Enter the sales for first customer: ");
    salesAmount = in.nextDouble();
    while(salesAmount !=0)
    {
        System.out.println("Enter customers name");
        String name = in.next();
        customers.add(new Customer(name, salesAmount));
        System.out.println("Enter the next sales amount, 0 to exit: ");
        salesAmount = in.nextDouble();
    }
    String bestCustomer = nameOfBestCustomer();
    System.out.print(bestCustomer);
}

private double highestSale(){
    double highestSale = 0;
    for(Customer c: customers)
        if (c.getSales() > highestSale)
            highestSale = c.getSales();

    return highestSale;
}

public String nameOfBestCustomer(){
    for (Customer c: customers)
        if(c.matchSale(highestSale()))
            return c.getName();
    return null;
}

}

and this is the Customer

public class Customer {
private String name;
private double sales;



public Customer(String name, double salesAmount) {
    this.name = name;
    sales = salesAmount;
}

public boolean matchSale(double sales){
    return this.sales == sales;
}

public double getSales(){
    return sales;
}

public String getName(){
    return name;
}

}

I am a beginner so I am pretty sure there is a more efficient way to do it. Also I am using two getters and as far as my understanding goes it is not the better design..

Upvotes: 0

androiddev19
androiddev19

Reputation: 306

You should consider making Customer a class, but this would find the name with your current data structures:

  public static String nameOfBestCustomer(ArrayList<Double> sales,
        ArrayList<String> customers)
{
    String name = "";
    double maxSales = 0;
    int index = -1;

    for(int i = 0; i < sales.size(); i++) {
       if(sales.get(i) > maxSales) {
         index = i;
         maxSales = sales.get(i);
       }
    }

    if(index == -1) {
       return null; //lists are empty
    }

    return customers.get(index);
}

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240900

You should wrap these two field in a class called probably Customer and then

Use Collections.max();

Collections.max(yourCollection, customComparator);

Upvotes: 1

Related Questions