Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

•• Business P7.12 A supermarket wants to reward its best customer of each day, s

ID: 3875249 • Letter: #

Question

•• Business P7.12 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 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.

-----

-----

-----

import java.util.*;

/**

* Code for P7.13

* @author

*/

public class Store

{

public String nameOfBestCustomer(ArrayList sales,

ArrayList customers)

{

String top = " ";

// Your work starts here

// Your work ends here

return top;

}

public static void main(String[] args)

{

ArrayList price = new ArrayList();

ArrayList names = new ArrayList();

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));

}

}

Explanation / Answer

package chegg.september;

import java.util.*;

/**
* Code for P7.13
*
* @author Sam
*/
public class Store {

    public String nameOfBestCustomer(ArrayList sales, ArrayList customers) {
        String top = " ";
    // Your work starts here
        int topIndex = 0;
        for (int i = 1; i < sales.size(); i++)
            if ((Double)sales.get(topIndex) < (Double)sales.get(i))
                topIndex = i;
        top = (String) customers.get(topIndex);
    // Your work ends here
        return top;
    }

    public static void main(String[] args) {
        ArrayList price = new ArrayList();
        ArrayList names = new ArrayList();
        Scanner in = new Scanner(System.in);

    // Your work starts here
        while (true) {
            System.out.println("Enter price (enter 0 to quit):");
            double p = Double.parseDouble(in.nextLine());
            if (p == 0)
                break;
            System.out.println("Enter name:");
            String n = in.nextLine();
            price.add(p);
            names.add(n);
        }
        if (names.size() == 0) {
            System.out.println("No name entered. Exiting...");
            System.exit(0);
        }
    // Your work ends here
        Store top = new Store();
        System.out.println("Best customer's name " + top.nameOfBestCustomer(price, names));
    }
}