Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the exercises in Chapter 6, you created a class named Purchase. Each Purchase

ID: 3597241 • Letter: I

Question

In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains aninvoice number, amount ofsale, amount ofsales tax, and several methods. Add get methods for the invoice number and saleamount fields sotheir values can be used in comparisons. Next, write a program that declares anarray of five Purchase objects andprompt a user for their values. Then, in a loop that continues until a user inputs a sentinel value, ask the user whether the Purchase objectsshouldbesortedanddisplayedininvoicenumberorderorsaleamountorder. Save the file asSortPurchasesArray.java.

Enhance the program by displaying a menu that asks the user how they want to sort the services menu. 1) Sort by Invoice, 2) Sort by Amount of Sale, 3) Sort by Sales Tax, 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

Explanation / Answer

/Purchase.java
//Instance of Purchase object
public class Purchase
{

   //variable declarations
   private int invoiceNumber;
   private double sale;
   private double salesTax;

   //Construtor to set invoice number, sale and salestax
   public Purchase(int invoiceNumber,double sale,double salesTax) {
       setInvoice(invoiceNumber);
       setSale(sale);
       setSalesTax(salesTax);
   }


   //Setter methods
   public void setInvoice(int invoiceNumber) {    
       this.invoiceNumber=invoiceNumber;
   }

   public void setSale(double sale) {
       this.sale=sale;
   }
   public void setSalesTax(double salesTax) {
       this.salesTax=salesTax;
   }

   //Getter methods
   public int getInvoice() {    
       return invoiceNumber;
   }

   public double getSale() {
       return sale;
   }
   public double getSalesTax() {
       return salesTax;
    
   }
}


------------------------------------------------------------------------------------------------------------------------------------

/**
* The java program SortPurchasesArray that prompts user to
* enter invoice number, sales amount and sales tax and
* then prompts user to enter 1 to sort by invoice number.
* 2 to sort by sales amount and 999 to exit the program
* */
//SortPurchasesArray.java
import java.util.Scanner;
public class SortPurchasesArray
{
   public static void main(String[] args) {
    
       //Scanner class object
       Scanner scanner=new Scanner(System.in);
       int sortOrder;
       //Create five Purcahse type objects
       Purchase[] purchase=new Purchase[5];

    
       //Read five purcahse objects
       for (int i = 0; i < purchase.length; i++)
       {
           System.out.println("Purchse Item "+(i+1));
           System.out.println("Enter invoice number: ");
           int invoice=Integer.parseInt(scanner.nextLine());

           System.out.println("Enter sales amount: ");
           double sales=Double.parseDouble(scanner.nextLine());

           System.out.println("Enter sales tax: ");
           double salestax=Double.parseDouble(scanner.nextLine());

           //Create an instance of Purchase and set to purchase array at index i
           purchase[i]=new Purchase(invoice, sales, salestax);        
       }

       System.out.println("Enter 1. Sort by Invoice number");
       System.out.println("2. Sort by sale");
       System.out.println("999 to exit");
       //read sort order
       sortOrder=Integer.parseInt(scanner.nextLine());

       //Continue prompt until user enters 999 to exit from loop
       while(sortOrder!=999)
       {

           //call sortByInvoice for sortorder is 1
           if(sortOrder==1)
               sortByInvoice(purchase);
           //call sortByInvoice for sortorder is 2
           else if(sortOrder==2)
               sortBySales(purchase);

           //call print to display prucahse array
           print(purchase);

           System.out.println("Enter 1. Sort by Invoice number");
           System.out.println("2. Sort by sale");
           System.out.println("999 to exit");
           //read sort type
           sortOrder=Integer.parseInt(scanner.nextLine());
        
       }//end of while loop

   }


   //The helper method to print purchase objectss
   private static void print(Purchase[] purchase) {
       for (int i = 0; i < purchase.length; i++) {

           System.out.printf("%d%10.2f%10.2f ",purchase[i].getInvoice(),
                   purchase[i].getSale(),purchase[i].getSalesTax());
       }

   }

   /**
   * The method sortBySales takes Purcahse array as input
   * and sort the purchase objects by sale amount
   * */
   private static void sortBySales(Purchase[] purchase) {

       for (int i = 0; i < purchase.length; i++) {
           for (int j = 0; j < purchase.length-1; j++) {

               if(purchase[j].getSale()>purchase[j+1].getSale())
               {

                   double tempSales=purchase[j].getSale();
                   purchase[j].setSale(purchase[j+1].getSale());
                   purchase[j+1].setSale(tempSales);

                   int tempInvoice=purchase[j].getInvoice();
                   purchase[j].setInvoice(purchase[j+1].getInvoice());
                   purchase[j+1].setInvoice(tempInvoice);

                   double tempTax=purchase[j].getSalesTax();
                   purchase[j].setSalesTax(purchase[j+1].getSalesTax());
                   purchase[j+1].setSalesTax(tempTax);            
               }
           }
       }

   }//end of method sortBySales
   /**
   * The method sortByInvoice takes Purcahse array as input
   * and sort the purchase objects by invoice number
   * */
   private static void sortByInvoice(Purchase[] purchase) {

       for (int i = 0; i < purchase.length; i++) {
           for (int j = 0; j < purchase.length-1; j++) {

               if(purchase[j].getInvoice()>purchase[j+1].getInvoice())
               {
                   int tempInvoice=purchase[j].getInvoice();
                   purchase[j].setInvoice(purchase[j+1].getInvoice());
                   purchase[j+1].setInvoice(tempInvoice);

                   double tempSales=purchase[j].getSale();
                   purchase[j].setSale(purchase[j+1].getSale());
                   purchase[j+1].setSale(tempSales);


                   double tempTax=purchase[j].getSalesTax();
                   purchase[j].setSalesTax(purchase[j+1].getSalesTax());
                   purchase[j+1].setSalesTax(tempTax);            
               }
           }
       }

   }//end of method sortByInvoice

}//end of class

---------------------------------------------------------------------------------------------------------------------------

Sample output:

Purchse Item 1
Enter invoice number:
5
Enter sales amount:
1000
Enter sales tax:
0.25
Purchse Item 2
Enter invoice number:
4
Enter sales amount:
1500
Enter sales tax:
0.45
Purchse Item 3
Enter invoice number:
3
Enter sales amount:
8500
Enter sales tax:
0.74
Purchse Item 4
Enter invoice number:
2
Enter sales amount:
4100
Enter sales tax:
0.75
Purchse Item 5
Enter invoice number:
1
Enter sales amount:
7450
Enter sales tax:
0.85
Enter 1. Sort by Invoice number
2. Sort by sale
999 to exit
1
1   7450.00      0.85
2   4100.00      0.75
3   8500.00      0.74
4   1500.00      0.45
5   1000.00      0.25
Enter 1. Sort by Invoice number
2. Sort by sale
999 to exit
2
5   1000.00      0.25
4   1500.00      0.45
2   4100.00      0.75
1   7450.00      0.85
3   8500.00      0.74
Enter 1. Sort by Invoice number
2. Sort by sale
999 to exit
999
5   1000.00      0.25
4   1500.00      0.45
2   4100.00      0.75
1   7450.00      0.85
3   8500.00      0.74
Enter 1. Sort by Invoice number
2. Sort by sale
999 to exit