Program in Java Program 1: We Reward Our Top Customer! This is based on Exercise
ID: 3706323 • Letter: P
Question
Program in Java
Program 1: We Reward Our Top Customer! This is based on Exercise P6.11 on page 326 of BJLO 2nd Ed. 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 and the customer's name is stored in a corresponding ArrayList. Implement a method public static String nameOfBestCustomer (ArrayList sales, ArrayList customers) that returns the name of the customer with the largest sale. Write a program that prompts the cashier to enter all prices and names, adding each pair to two array lists. Use a price of 0 as a sentinel (i.e., the value that ends the user input loop). The program then calls the nameOfBestCustomer method that you implemented Note: Consider implementing and then calling the following helper method from the nameOfBestCustomer method: public static int findLargestSales(ArrayList sales) that returns the index of the largest sales amount in the given array list of sales amounts. Program 2: We Reward Our Top N Customers! This is based on Exercise P6.11 on page 326 of BJLO 2nd Ed. Make a copy of Program 1, and then improve that copy 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 public static ArrayList namesOfBestCustomers(ArrayList sales, ArrayList customers, int topll) If there were fewer than topN customers, include all of them. Note: Consider sorting a copy of the sales list (you want to keep the original in order in order to match sales with customer names). The object class java.util.Collections has static methods that can be used with array lists of comparable object classes (like Double). Remember that sorting usually means non-decreasing (increasing) order, so the largest value will end up at the end of the list. In addition, consider implementing and then calling the following helper method from the names OfBestCustomers method: public static int findSales(ArrayList sales, double sale) that returns the first (lowest) index of given sale in the given sales list (-1 if the sale is not in the list).Explanation / Answer
SuperMarketTest.java
import java.util.ArrayList;
import java.util.Scanner;
public class SuperMarketTest {
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.print("Please enter the customer’s name (price 0 to quit): ");
String name =scan.nextLine();
System.out.print("Please enter "+name+"’s spending: ");
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);
System.out.println("Largest sale index: "+findLargestSales(sales));
}
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);
}
public static int findLargestSales(ArrayList<Double> sales) {
double max = sales.get(0);
int maxIndex = 0;
for(int i=0;i<sales.size();i++) {
if(max<sales.get(i)) {
max = sales.get(i);
maxIndex=i;
}
}
return maxIndex;
}
}
Output:
Please enter the customer’s name (price 0 to quit): Suresh
Please enter Suresh’s spending: 1000
Please enter the customer’s name (price 0 to quit): Sekhar
Please enter Sekhar’s spending: 12000
Please enter the customer’s name (price 0 to quit): Anshi
Please enter Anshi’s spending: 323
Please enter the customer’s name (price 0 to quit): aaa
Please enter aaa’s spending: 0
Best customer is Sekhar
Largest sale index: 1
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);
System.out.println("Enter the sale to be searched: ");
double searched = scan.nextDouble();
System.out.println("Fild sale "+searched+": "+findSales(sales, searched));
}
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);
}
public static int findSales(ArrayList<Double> sales, double sale) {
for(int i=0;i<sales.size();i++) {
if(sales.get(i) == sale) {
return i;
}
}
return -1;
}
}
Output:
Enter customer name:
Suresh
Enter customer sales:
1000
Enter customer name:
Sekhar
Enter customer sales:
20000
Enter customer name:
Anshu
Enter customer sales:
2333
Enter customer name:
aaaa
Enter customer sales:
0
Best customer is Sekhar
Enter the sale to be searched:
20000
Fild sale 20000.0: 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.