Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 namesOfBestCustomers(ArrayList sales, ArrayList 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 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 sales = new ArrayList();

//array of type tring to store names of the customers

ArrayList 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

//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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions