Hello Im fresh I need help with the following question Pleas
Hello, I\'m fresh. I need help with the following question. Please use basic Java Code.
Thanks!
A supermarket wants to reward its best customer of each day, showing the customer\'s name on ascreen in the supermarket. For that purpose, the customer\'s purchase amount is stored in anArrayList<Double> and the customer\'s name is stored 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.
====================================================================
Sample output:
Please enter the customer’s name (enter DONE to quit) : Joe
Please enter Joe’s spending : 200
Please enter the customer’s name (enter DONE to quit) : Harry
Please enter Harry spending : 300
.
.
Please enter the customer’s name : DONE
Eric has spent the most amount today with a sale of $300
Solution
SuperMarketTest.java
import java.util.ArrayList;
import java.util.Scanner;
public class SuperMarketTest {
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.print(\"Please enter the customer’s name (enter DONE to quit): \");
String name =scan.nextLine();
System.out.print(\"Please enter \"+name+\"’s spending: \");
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:
Please enter the customer’s name (enter DONE to quit): Joe
Please enter Joe’s spending: 200
Please enter the customer’s name (enter DONE to quit): Harry
Please enter Harry’s spending: 300
Please enter the customer’s name (enter DONE to quit): Suresh
Please enter Suresh’s spending: 500
Please enter the customer’s name (enter DONE to quit): Sekhar
Please enter Sekhar’s spending: 400
Please enter the customer’s name (enter DONE to quit): aaaa
Please enter aaaa’s spending: 0
Best customer is Suresh

