Question
Please improve the following code according to the question This code prints the best customer, please improve it so it prints the top customers according
Please improve the following code according to the question
This code prints the best customer, please improve it so it prints the top customers according to the question : (JAVA)
public static void main(String[] args) { //The input varuables: String CustomerName; double CustomerSales; ArrayList
Scanner in = new Scanner(System.in); //A scanner object
// this will be repeated until the user enters zero while (true) {
//prompt user to enter sales System.out.print("Enter Customer's sales: "); //save user's input into this variable CustomerSales = Float.parseFloat(in.nextLine());
if (CustomerSales == 0) { break; } //add users input to the array list sales.add(CustomerSales);
//prompt user to enter customer's name System.out.print("Enter customer's name: "); //Store the input CustomerName = in.nextLine(); //Add the input to the array list customers.add(CustomerName); } String Answer = nameOfBestCustomer(sales, customers); //ensure that the information exist or print a message telling that there is an error if(Answer.isEmpty()) { System.out.println("No information found"); }else { System.out.println("Best customer is: "+Answer); } }
public static String nameOfBestCustomer(ArrayList
double largest = sales.get(0); FinalResult = customers.get(0);
//get the largest sale value and match it with the name for (int i = 1; i
if (sales.get(i) > largest) { largest = sales.get(i); //result store best customer name FinalResult = customers.get(i); } } } return FinalResult; //return best customer }
}
Improve the program from Part 1 so that it displays the top customers, that is, the top customers with the largest sales, where topN is a value that the user of the program supplies. Implement a method public static ArrayListStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started