A supermark wants to reward its best customer of each day sh
A supermark wants to reward its best customer of each day, showing the customer\'s name on a screen in the supermarket. For that purpose, the customer\'s purchase amount is stored in an ArrayList<Double> and the customer\'s name is tored in a corresponding ArrayList<String>. Implement a method public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers), that returns the name of the customer with the largest sale.
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.
This is for Java for Everyone Second Addition Business P6.30
Solution
Supermark.java
import java.util.ArrayList;
import java.util.Scanner;
public class Supermark {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> customers = new ArrayList<String>();
while(true){
System.out.println(\"Enter customer name: \");
String name =scan.nextLine();
System.out.println(\"Enter customer sales: \");
double price = scan.nextDouble();
scan.nextLine();
if(price == 0){
break;
}
sales.add(price);
customers.add(name);
}
String customerName = nameOfBestCustomer(sales, customers);
System.out.println(\"Best customer is \"+customerName);
}
public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers){
double max = 0;
int maxIndex = 0;
for(int i=0; i<sales.size(); i++){
if(max < sales.get(i)){
max = sales.get(i);
maxIndex = i;
}
}
return customers.get(maxIndex);
}
}
Output:
Enter customer name:
Suresh Murapaka
Enter customer sales:
100
Enter customer name:
Sekhar Murapaka
Enter customer sales:
120
Enter customer name:
Anshu Murapaka
Enter customer sales:
150
Enter customer name:
Revathi M
Enter customer sales:
130
Enter customer name:
AAAA
Enter customer sales:
0
Best customer is Anshu Murapaka

