I have written code for this but it doesn\'t quite work and I think I need to sc
ID: 3664629 • Letter: I
Question
I have written code for this but it doesn't quite work and I think I need to scratch it. Can you provide code for this with comments please.
Thanks
•• B usiness P 7.10 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.
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public class Customer {
String name;
@Override
public String toString() {
return "Customer [name=" + name + ", amount=" + amount + "]";
}
double amount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String nameOfBestCustomer(ArrayList<Customer> cust)
{
String winner=null;
double maxamount=cust.get(0).amount;
for (int i = 0; i < cust.size()-1; i++) {
if(cust.get(i).amount<cust.get(i+1).amount)
{
maxamount=cust.get(i+1).amount;
winner=cust.get(i+1).name;
}
}
return winner;
}
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
/* This function is been implemented here only.
public void addSale(String customerName, double amount)
*/
ArrayList<Customer> cust=new ArrayList<Customer>();
Customer c=new Customer();
for (int i = 0; i < 10; i++) {
System.out.println("Enter customer name");
c.name=obj.next();
System.out.println("Enter customers sale of the day");
c.amount=obj.nextDouble();
cust.add(c);
}
c.nameOfBestCustomer(cust);
}
}