Program 2 Make a copy of Program 1, and then improve that copy so that it displa
ID: 3882261 • Letter: P
Question
Program 2
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<String> namesOfBestCustomers(ArrayList<Double> sales, ArrayList<String> customers, int topN)
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.Collectionshas 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 namesOfBestCustomers method:
public static int findSales(ArrayList<Double> 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).
Program 1:
import java.util.ArrayList;
import java.util.Scanner;
public class Prog1 {
public static void main(String[] args) {
//array of type Double to store sales of each customers
ArrayList<Double> sales = new ArrayList();
//array of type tring to store names of the customers
ArrayList<String> customers = new ArrayList();
//number of customers
int numOfitems=0;
double price=0;
//read inputs
Scanner in = new Scanner(System.in);
//flag when list is over
boolean entryComplete= false;
//use do while loop to continue read value from user
//till he has not entered 0 as sale.
do {
//enter sales of each customer
System.out.println("Enter the price (0 to end)");
price = in.nextDouble();
sales.add(price);
if(price!=0) {
//enter name of each customer
System.out.println("Customer's Last name");
customers.add(in.next());
//increment for next pointer
numOfitems++;}}
while(price!=0&&numOfitems<100);
//call method to get Best Customer
String bestCustomer= nameOfBestCustomer(sales,customers);
System.out.println("Best customer's name " + bestCustomer);
}
/*
* Returns name of the customer who has maximum purchase
*/
public static String nameOfBestCustomer(ArrayList sales, ArrayList customers)
{
//index of the largest sale
int largestIndex=0;
//assume first element as largest
double largest= (double) sales.get(0);
//for interate over all list
int i=0;
//use while loop to traverse sale array to find out largest sale.
while (i<sales.size()) {
//if current index element is greater than value of largest
if((double)sales.get(i)>largest) {
//update variable
largest=(double) sales.get(i);
largestIndex=i;
}
//increment to next element
i++;
}
return (String) customers.get(largestIndex);
}
}
Sample run for program 2:
Enter the price (0 to end):
55
Customer's last name
Jones
Enter the price (0 to end):
40
Customer's last name
Smith
Enter the price (0 to end):
60
Customer's last name
Davis
Enter the price (0 to end):
0
How many top customers should display?
2
Best customer's names:
Jones
Smith
Explanation / Answer
Hi,
Please find the Answer for your Question.
Program :
Prog2.java
package program;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
/**
*
* @author hanumanthareddy.k
*/
public class Prog2 {
public static void main(String[] args) {
//array of type Double to store sales of each customers
ArrayList<Double> sales = new ArrayList();
//array of type tring to store names of the customers
ArrayList<String> customers = new ArrayList();
//number of customers
int numOfitems=0;
double price=0;
//read inputs
Scanner in = new Scanner(System.in);
//flag when list is over
boolean entryComplete= false;
//use do while loop to continue read value from user
//till he has not entered 0 as sale.
do {
//enter sales of each customer
System.out.println("Enter the price (0 to end)");
price = in.nextDouble();
if(price!=0)
sales.add(price);
if(price!=0) {
//enter name of each customer
System.out.println("Customer's Last name");
customers.add(in.next());
//increment for next pointer
numOfitems++;
}
}while(price !=0 && numOfitems < 100);
//call method to get Best Customer
System.out.println("Please enter Nth highest value to list the names of customers : ");
int Nth = in.nextInt();
nameOfBestCustomer(sales,customers,Nth);
System.out.println("Please enter to find sale in exists list : ");
double finDouble = in.nextDouble();
int index = findSales(sales,finDouble);
System.out.println(index);
}//method main
//Implemented the method to get the sales and customers list along with top nth sales and customer names
public static void nameOfBestCustomer(ArrayList sales, ArrayList customers,int topN)
{
System.out.println("sales "+sales);
System.out.println("customers "+customers);
//Map object to hold key vale pair list
Map aMap = new HashMap();
//two sales and customers arraylists making as hashmap.
for (int i = 0; i < sales.size(); i++) {
aMap.put(sales.get(i),customers.get(i));
}
Collections.sort(sales,Collections.reverseOrder());
//itterating the given topN customers and sales
for(int kk=0;kk<topN;kk++) {
System.out.println(" ####### Nth Highest Sales ########### ");
System.out.println(" Sale : "+sales.get(kk)+" Best Customer Name : "+aMap.get(sales.get(kk)));
}
}//method nameOfBestCustomer
//This method returns 1 if the sale exists in the list else return -1.
public static int findSales(ArrayList<Double> sales, double sale){
if(sales.indexOf(sale) != -1)
return 1;
return sales.indexOf(sale);
}//method findSales
}//class Prog2
OUT PUT 1:
run:
Enter the price (0 to end)
100
Customer's Last name
HH
Enter the price (0 to end)
700
Customer's Last name
TT
Enter the price (0 to end)
400
Customer's Last name
UU
Enter the price (0 to end)
300
Customer's Last name
KK
Enter the price (0 to end)
234
Customer's Last name
NN
Enter the price (0 to end)
876
Customer's Last name
VV
Enter the price (0 to end)
0
Please enter Nth highest value to list the names of customers :
5
sales [100.0, 700.0, 400.0, 300.0, 234.0, 876.0]
customers [HH, TT, UU, KK, NN, VV]
####### Nth Highest Sales ###########
Sale : 876.0 Best Customer Name : VV
####### Nth Highest Sales ###########
Sale : 700.0 Best Customer Name : TT
####### Nth Highest Sales ###########
Sale : 400.0 Best Customer Name : UU
####### Nth Highest Sales ###########
Sale : 300.0 Best Customer Name : KK
####### Nth Highest Sales ###########
Sale : 234.0 Best Customer Name : NN
Please enter to find sale in exists list :
700
1
BUILD SUCCESSFUL (total time: 52 seconds)
OUT PUT 2:
run:
Enter the price (0 to end)
30
Customer's Last name
JJ
Enter the price (0 to end)
20
Customer's Last name
KK
Enter the price (0 to end)
50
Customer's Last name
YY
Enter the price (0 to end)
10
Customer's Last name
VV
Enter the price (0 to end)
30
Customer's Last name
NN
Enter the price (0 to end)
100
Customer's Last name
BB
Enter the price (0 to end)
80
Customer's Last name
ZZ
Enter the price (0 to end)
70
Customer's Last name
XX
Enter the price (0 to end)
0
Please enter Nth highest value to list the names of customers :
6
sales [30.0, 20.0, 50.0, 10.0, 30.0, 100.0, 80.0, 70.0]
customers [JJ, KK, YY, VV, NN, BB, ZZ, XX]
####### Nth Highest Sales ###########
Sale : 100.0 Best Customer Name : BB
####### Nth Highest Sales ###########
Sale : 80.0 Best Customer Name : ZZ
####### Nth Highest Sales ###########
Sale : 70.0 Best Customer Name : XX
####### Nth Highest Sales ###########
Sale : 50.0 Best Customer Name : YY
####### Nth Highest Sales ###########
Sale : 30.0 Best Customer Name : NN
####### Nth Highest Sales ###########
Sale : 30.0 Best Customer Name : NN
Please enter to find sale in exists list :
76
-1
BUILD SUCCESSFUL (total time: 59 seconds)
Thank You.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.