Question: I have the current code for this problem but I am having issues with how to exactly modify this code with these requirements. Enhance the

I have the current code for this problem but I am having issues with how to exactly modify this code with these requirements. "Enhance the program by displaying a menu that asks the user how they want to sort the services menu. 1) Sort by Service Description, 2) Sort by Price, 3) Sort by Time (Minutes), or 0) to Exit. Add a do...while() loop that keeps prompting the user for the next preferred sort order until the user finally chooses 0 to exit." If I could have help on how to add these into the code that would be amazing.

import java.util.*;

class SortPurchasesArray {

public static void main(String[] args) {

Purchase[] purchases = new Purchase[5];

Scanner scanner = new Scanner(System.in);

for(int i=0; i<5; i++){

System.out.println("Enter invoice number and sale amount for purchase : "+(i+1));

int invNum = scanner.nextInt();

Double sAmt = scanner.nextDouble();

Purchase purchase = new Purchase();

purchase.setInvoiceNumber(invNum);

purchase.setSaleAmount(sAmt);

purchases[i] = purchase;

}

System.out.println("In which order do you want to sort the purchases: 1) Invoice number, 2)Sales Amount ");

int option = scanner.nextInt();

if(option==1){

Arrays.sort(purchases, new PurchaseInvoiceNumberSorter());

printArray(purchases);

}else if(option==2){

Arrays.sort(purchases, new PurchaseSalesAmountSorter());

printArray(purchases);

}else {

System.out.println("Invalid option");

}

}

public static void printArray(Purchase[] purchases){

for(Purchase p : purchases)

p.displayInfo();

System.out.println(" ");

}

}

}

class PurchaseInvoiceNumberSorter implements Comparator {

@Override

public int compare(Purchase o1, Purchase o2) {

return Integer.valueOf(o1.getInvoiceNumber()).compareTo(Integer.valueOf(o2.getInvoiceNumber()));

}

}

class PurchaseSalesAmountSorter implements Comparator {

@Override

public int compare(Purchase o1, Purchase o2) {

return Double.valueOf(o1.getSaleAmount()).compareTo(Double.valueOf(o2.getSaleAmount()));

}

}

class Purchase {

private int invoiceNumber;

private double saleAmount;

private double salesTax;

public Purchase() {

}

public void setInvoiceNumber(int anyInvoiceNumber) {

invoiceNumber = anyInvoiceNumber;

}

public void setSaleAmount(double anySaleAmount) {

saleAmount = anySaleAmount;

salesTax = saleAmount * 0.05;

}

public int getInvoiceNumber() {

return invoiceNumber;

}

public double getSaleAmount() {

return saleAmount;

}

public double getSalesTax() {

return salesTax;

}

public void displayInfo() {

System.out.println("The invoice number entered is: " + invoiceNumber);

System.out.println("The sales tax for the invoice number is: " + salesTax);

System.out.println("The sales amount for the invoice number is: " + saleAmount);

System.out.println("The total amount of the invoice is: " + (saleAmount + salesTax));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!