A supermarket wants to reward the top customers of the day, that is, the topN customers with the largest sales, where topN is a value that the user of the program supplies, showing the customer’s name on a screen in the supermarket. For that purpose, the customer’s purchase amount is stored in an ArrayList
Implement a method: public static ArrayList
Write a program that prompts the cashier to enter all prices and names, adds them to two array lists, calls the method that you implemented, and displays the result. Use a price of 0 as a sentinel.
My errors when compiling are:
------ Compile ----------
hw1num2.java:44: error: cannot find symbol
double check = iter.nextDouble();
^
symbol: method nextDouble()
location: variable iter of type Iterator
hw1num2.java:45: error: cannot find symbol
if (check>=sorted(topN-1))
^
symbol: method sorted(int)
location: class hw1num2
hw1num2.java:47: error: no suitable method found for add(double)
topCust.add(check);
^
method ArrayList.add(int,String) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(String) is not applicable
(actual argument double cannot be converted to String by method invocatio>n conversion)
3 errors
Output completed (1 sec consumed) - Normal Termination
Here's what I have:
import java.util.*;
public class hw1num2
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> customers = new ArrayList<String>();
boolean end = true;
while (end) //Continues to take input for customer names and purchases until done
{
System.out.println("Enter the first name of the customer.");
String cust = in.next();
System.out.println("Enter the purchase total of that customer.");
Double total = in.nextDouble();
sales.add(total);
customers.add(cust);
System.out.println("Enter 1 if you have another customer to add, or 2 if you are done.");
int choice = in.nextInt();
if (choice != 1)
{
end=false;
}
}
System.out.println("How many top customers would you like to display?");
int topN = in.nextInt();
System.out.println(nameOfBestCustomers(sales, customers, topN)); //calls method that computes top custs
}
public static ArrayList<String> nameOfBestCustomers(ArrayList<Double> sales, ArrayList<String> customers,
int topN) //Finds out who the topN customers were
{
ArrayList<Double> sorted = new ArrayList<Double>(sales); //create copy of sales ArrayList
ArrayList<String> topCust = new ArrayList<String>(); //create ArrayList to hold top cust names
Collections.sort(sorted); //sort the copied ArrayList.
Iterator iter = sales.iterator();
while (iter.hasNext()) //iterate through sales ArrayList to find indexes of top purchases
{
for (int i=0;i<sales.size();i++)
{
double check = (double)iter.next();
if (check>=sorted.get(topN-1)) //checks if each index is >= the top Nth customer
{
topCust.add(customers.get(i)); //if so, adds it to topCust list
}
}
}
return topCust; //returns the list with the top customer names
}
}