Help please. Big Java Edition P6.31 Improve the program of Exercise P6.30(WHICH
ID: 3666235 • Letter: H
Question
Help please.
Big Java Edition P6.31
Improve the program of Exercise P6.30(WHICH IS BELOW and works), 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<String> nameOfBestCustomer(ArrayList<Double> sales, ArrayList<String> customers, int topN). If there were fewer than topN customers, include them all.
import java.util.ArrayList;
import java.util.Scanner;
public class ProbSixThirty {
public static void main(String[] args) {
/*customer's name is stored in a corresponding ArrayList<String>*/
ArrayList<String> Customers=new ArrayList<>();
/*the customer's purchase amount is stored in an ArrayList<Double>*/
ArrayList<Double> Sales=new ArrayList<>();
/*to read input*/
Scanner keyboard=new Scanner(System.in);
/*Just to start the while loop*/
double price=999;
String name;
int n=1;
/* Reading customer details , till cashier entering price as 0 */
/* since price 0 is used as sentinel */
while(price>0)
{
System.out.println("Enter details for customer "+n);
System.out.println("Name: ");
name=keyboard.nextLine();
/*Add to Customers list */
Customers.add(name);
System.out.println("Purchase Amount: ");
price=keyboard.nextDouble();
keyboard.nextLine();
/*if price is 0, stop the loop*/
if(price==0)
break;
//else
/*Add to Customers list */
Sales.add(price);
n++;
}
/*Display the name of the best customer*/
System.out.println(" Result: ");
/*call the function to find best customer*/
String bestCustomer=nameOfBestCustomer(Customers,Sales);
System.out.println("Best Customer is: "+bestCustomer);
}
/*---------------------------------------------------------------------------*/
/*This function will find and return the customer with lasrgest sale */
private static String nameOfBestCustomer(ArrayList<String> Customers, ArrayList<Double> Sales) {
/*Assume starting value as largest sale*/
double largest=Sales.get(0);
/*store its position*/
int pos=0;
/*Search the Sales arraylist find the largest sale, by comparision
and replacement. Store the position correspondingly */
for(int i=1;i<Sales.size();i++)
{
if(Sales.get(i)>largest)
{
largest=Sales.get(i);
pos=i;
}
}
/* obtain the name using pos in Customer list and return */
return Customers.get(pos);
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class ProbSixThirty {
public static void main(String[] args) {
/* customer's name is stored in a corresponding ArrayList<String> */
ArrayList<String> Customers = new ArrayList<String>();
/* the customer's purchase amount is stored in an ArrayList<Double> */
ArrayList<Double> Sales = new ArrayList<Double>();
/* to read input */
Scanner keyboard = new Scanner(System.in);
/* Just to start the while loop */
double price = 999;
String name;
int n = 1;
/* Reading customer details , till cashier entering price as 0 */
/* since price 0 is used as sentinel */
while (price > 0) {
System.out.println("Enter details for customer " + n + " :");
System.out.print("Name: ");
name = keyboard.nextLine();
/* Add to Customers list */
Customers.add(name);
System.out.print("Purchase Amount: ");
price = keyboard.nextDouble();
keyboard.nextLine();
/* if price is 0, stop the loop */
if (price == 0)
break;
// else
/* Add to Customers list */
Sales.add(price);
n++;
}
/* Display the name of the best customer */
System.out.println(" Result: ");
/* call the function to find best customer */
String bestCustomer = nameOfBestCustomer(Customers, Sales);
System.out.println("Best Customer is: " + bestCustomer);
System.out.println("Top 3 customers are:"
+ nameOfBestCustomer(Sales, Customers, 3));
}
/*---------------------------------------------------------------------------*/
/* This function will find and return the customer with lasrgest sale */
private static String nameOfBestCustomer(ArrayList<String> Customers,
ArrayList<Double> Sales) {
/* Assume starting value as largest sale */
double largest = Sales.get(0);
/* store its position */
int pos = 0;
/*
* Search the Sales arraylist find the largest sale, by comparision and
* replacement. Store the position correspondingly
*/
for (int i = 1; i < Sales.size(); i++) {
if (Sales.get(i) > largest) {
largest = Sales.get(i);
pos = i;
}
}
/* obtain the name using pos in Customer list and return */
return Customers.get(pos);
}
/**
* method to return top n customers according to sales
*
* @param sales
* @param customers
* @param topN
* @return
*/
public static ArrayList<String> nameOfBestCustomer(ArrayList<Double> sales,
ArrayList<String> customers, int topN) {
ArrayList<String> topNcustomers = new ArrayList<String>();
for (int i = 0; i < sales.size() - 1; i++) {
for (int j = i + 1; j < sales.size(); j++) {
if (sales.get(i).compareTo(sales.get(j)) < 0) {
double tempSale = sales.get(i);
sales.set(i, sales.get(j));
sales.set(j, tempSale);
String tempName = customers.get(i);
customers.set(i, customers.get(j));
customers.set(j, tempName);
}
}
}
// there were fewer than topN customers, include them all.
if (topN > customers.size()) {
topN = customers.size();
}
for (int i = 0; i < topN; i++) {
topNcustomers.add(customers.get(i));
}
return topNcustomers;
}
}
OUTPUT:
Enter details for customer 1 :
Name: Srinivas
Purchase Amount: 132
Enter details for customer 2 :
Name: Pavan
Purchase Amount: 142
Enter details for customer 3 :
Name: Rajesh
Purchase Amount: 26
Enter details for customer 4 :
Name: Chakri
Purchase Amount: 432
Enter details for customer 5 :
Name: Krishna
Purchase Amount: 987
Enter details for customer 6 :
Name: Kumar
Purchase Amount: 234
Enter details for customer 7 :
Name: Suresh
Purchase Amount: 213
Enter details for customer 8 :
Name: No
Purchase Amount: 0
Result:
Best Customer is: Krishna
Top 3 customers are:[Krishna, Chakri, Kumar]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.