A supermarket wants to reward its best customer of each day, showing the custome
ID: 3882910 • 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 store keeps an ArrayList. In the Store class, implement the methods.
public coid 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 chasier to enter all prices and names, adds them to a Store obect, and displays the best customer's name. Use the price of a 0 as a sentinel.
***Please help finish this code in Eclipse Java Neon and include the EXACT following code within it***
***Please fill in the spots where it says "your work starts here"***
import java.util.*;
/**
* Code for P7.13
* @author
*/
public class Store
{
public String nameOfBestCustomer(ArrayList<Double> sales,
ArrayList<String> customers)
{
String 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);
// Your work starts here
// Your work ends here
Store top = new Store();
System.out.println("Best customer's name "
+ top.nameOfBestCustomer(price, names));
}
}
Thank you.
Explanation / Answer
import java.util.*;
public class Store
{
public static String nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers)
{
int max = 0;
for(int i = 1; i < sales.size(); i++)
{
if(sales.get(max) < sales.get(i))
{
max = i;
}
}
return customers.get(max);
}
public static ArrayList<String> namesOfBestCustomers(ArrayList<Double> sales, ArrayList<String> customers, int topN)
{
ArrayList<String> names = new ArrayList<String>();
if(topN >= sales.size())
{
names = new ArrayList<String>(customers);
}
else
{
for(int i = 0; i < topN; i++)
{
int max = 0;
for(int j = 1; j < sales.size() - 1; j++)
{
if(sales.get(max) < sales.get(j))
{
max = j;
}
}
sales.remove(max);
names.add(customers.get(max));
customers.remove(max);
}
}
return names;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList<Double> price = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
do
{
System.out.print("Customer`s purchase amount: ");
price.add(in.nextDouble());
if(price.get(price.size() - 1) != 0)
{
System.out.println(" Customer`s name: ");
names.add(in.next());
}
}
while(price.get(price.size() - 1) != 0);
price.remove(price.size() - 1);
System.out.println("Sales: " + price.toString());
System.out.println("Names: " + names.toString());
System.out.println("Best customer of the day is " + nameOfBestCustomer(price, names));
System.out.println("Enter the number of top customers to diplay: ");
int number = in.nextInt();
in.close();
System.out.println("The top " + number + " customers are: " + namesOfBestCustomers(price, names, number).toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.