I need help with this question: A supermarket wants to reward its best customer
ID: 3880205 • Letter: I
Question
I need help with this 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 store keeps an ArrayList<Customer>. In the Store class, implement methods public void addSale(String customerName, double amount) public String nameOfBestCustomer() to record the sale and return 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 a Store object, and displays the best customer’s name. Use a price of 0 as a sentinel.
with the code as follow to start with:
import java.util.*;
/**
* Code for P7.13
* @author
*/
public class Store
{
public String nameOfBestCustomer(ArrayList<Double> sales,
ArrayList<String> customers)
{
String top = " ";
// task: store the name of the best customer into String variable "top"
// Your work starts here
// Your work ends here
return top;
}
public static void main(String[] args)
{
ArrayList<Double> price = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
Scanner in = new Scanner(System.in);
// task: use while loop to receive and store prices and last names from the console until the sentinel price 0 is recieved
// Your work starts here
// Your work ends here
Store top = new Store();
System.out.println("Best customer's name "
+ top.nameOfBestCustomer(price, names));
}
}
Explanation / Answer
Store.java
import java.util.*;
/**
* Code for P7.13
* @author
*/
public class Store
{
public String nameOfBestCustomer(ArrayList<Double> sales,
ArrayList<String> customers)
{
String top = " ";
// task: store the name of the best customer into String variable "top"
// Your work starts here
double max;
max=sales.get(0);
for(int i=0;i<sales.size();i++)
{
if(max<sales.get(i))
{
max=sales.get(i);
top=customers.get(i);
}
}
// Your work ends here
return top;
}
public static void main(String[] args)
{
ArrayList<Double> price = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
Scanner in = new Scanner(System.in);
int i=1;
double p;
String n;
final double SENTINEL=0;
System.out.print("Enter Customer#"+i+" purchased amount :");
p=in.nextDouble();
System.out.print("Enter name :");
n=in.next();
while(p!=SENTINEL)
{
names.add(n);
price.add(p);
i++;
System.out.print("Enter Customer "+i+" purchased amount :");
p=in.nextDouble();
if(p!=SENTINEL)
{
System.out.print("Enter name :");
n=in.next();
}
}
// task: use while loop to receive and store prices and last names from the console until the sentinel price 0 is recieved
// Your work starts here
// Your work ends here
Store top = new Store();
System.out.println("Best customer's name "
+ top.nameOfBestCustomer(price, names));
}
}
_________________
Output:
Enter Customer#1 purchased amount :450
Enter name :Ken
Enter Customer 2 purchased amount :500
Enter name :Williams
Enter Customer 3 purchased amount :300
Enter name :John
Enter Customer 4 purchased amount :600
Enter name :Mike
Enter Customer 5 purchased amount :200
Enter name :Johnson
Enter Customer 6 purchased amount :0
Best customer's name Mike
_______________Could you plz rate me well.Thank You