in a loop that continues until a user inputs a sentinel value of Z, ask the user whether the Purchase objects should be sorted and displayed in invoice number order or sale amount order.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 7PE
icon
Related questions
Question

This is the question -

In the exercises in Chapter 6, you created a class named Purchase. Each Purchase contains an invoice number, amount of sale, amount of sales tax, and several methods. Add get methods for the invoice number and sale amount fields so their values can be used in comparisons.

Next, write a program that declares an array of five Purchase objects and prompt a user for their values. Then, in a loop that continues until a user inputs a sentinel value of Z, ask the user whether the Purchase objects should be sorted and displayed in invoice number order or sale amount order.

This is the code I have. The programming isn't likeing what I have at all for the area where it says to write code - 

public class Purchase
{
   private int invoiceNumber;
   private double saleAmount;
   private double tax;
   private static final double RATE = 0.05;
   public void setInvoiceNumber(int num)
   {
      invoiceNumber = num;
   }
   public void setSaleAmount(double amt)
   {
      saleAmount = amt;
      tax = saleAmount * RATE;
   }
   public double getSaleAmount()
   {
      return saleAmount;
   }
   public int getInvoiceNumber()
   {
      return invoiceNumber;
   }
   public void display()
   {
      System.out.println("Invoice #" + invoiceNumber +
         "  Amount of sale: $" + saleAmount + "  Tax: $" + tax);
   }
}
------------------------------------------------------------------
import java.util.Scanner;
public class SortPurchasesArray {
    public static void main(String[] args) {
        // Write your code here
         //create a Scanner class object
  Scanner scan=new Scanner(System.in);
  //create of 5 Purchase objects
  Purchase[] p=new Purchase[5];
  //declare a string variables 
  String choice;
  //read sales amount and invoice for five Purchase objects 
  for (int i = 0; i < p.length; i++) 
  {
   p[i]=new Purchase();
   System.out.printf("Enter sale amount,$: ");
   double sales=Double.parseDouble(scan.nextLine());
   System.out.printf("Enter invoice number#: ");
   int invoice=Integer.parseInt(scan.nextLine());
   p[i].setInvoiceNumber(invoice);
   p[i].setSaleAmount(sales);
  }
  
  //do while continues until user enters 'Z' to stop loop
  do
  {
   System.out.println("***Menu***");
   System.out.println("a. sort by sales");
   System.out.println("b. sort by invoice");
   System.out.println("Enter your choice or Z to exit: ");
   choice=scan.nextLine();
   //check if choice is "a"
   if(choice.equals("a"))
   {
    sortBySaleAmount(p);
    display(p, "Sort By Sales");
   }
   //check if choice is "b"
   else if(choice.equals("b"))
   {
    sortByInvoice(p);
    display(p, "Sort By Invoice");
   }
      
  }while(!choice.equals("Z"));  
 }
 
 /*static method , sortBySaleAmount that takes Purchase array
  * and then sort the data by sales amount. */
    }
    public static void sortBySaleAmount(Purchase[] array) {
        // Write your code here
          int n = array.length;  
  Purchase temp = null;  
  for(int i=0; i < n; i++)
  {  
   for(int j=1; j < (n-i); j++)
   {                 
    if(array[j-1].getSaleAmount()> array[j].getSaleAmount())
    {  
     //swap elements  
     temp = array[j-1];  
     array[j-1] = array[j];  
     array[j] = temp;  
    }                       
   }  
  }  
 }
 /*static method , sortByInvoice that takes Purchase array
  * and then sort the data by invoice account number. */
    }
    public static void sortByInvoice(Purchase[] array) {
        // Write your code here
        int n = array.length;  
  Purchase temp = null;  
  for(int i=0; i < n; i++)
  {  
   for(int j=1; j < (n-i); j++)
   {                 
    if(array[j-1].getInvoiceNumber()> array[j].getInvoiceNumber())
    {  
     //swap elements  
     temp = array[j-1];  
     array[j-1] = array[j];  
     array[j] = temp;  
    }                       
   }  
  } 
 }
 /*static display method that takes a Purchase array and message
  * as input arguments and then display the Purchase and message*/
    }
    public static void display(Purchase[] p, String msg) {
        // Write your code here
         // Write code here
  System.out.println(msg);
  for (int index = 0; index < p.length; index++) 
  {
   p[index].display();
  }
 }//end of the display method
}
    }
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage