A supermarket wants to reward its best customer of each day, showing the custome
ID: 3857713 • Letter: A
Question
A supermarket 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 stored in a corresponding ArrayList<String>.
Implement a method
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.
Improve the program so that it displays the top customers, that is, the topN customers with the largest sales, where topN is a value that the user of the program supplies.
Implement a method
If there were fewer than topN customers, include all of them.
Also,Read the customer information for an input text file name customer.txt.
Here is the customer.txt information
Joy Bean 49.96
Tonya Bordeaux 65.00
Caleb Autry 14.00
Delores Melvin 125.00
Adria Bryant 23.00
public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers)
Explanation / Answer
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.