Java In the exercises in Chapter 6 you created a class named

Java:

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, ask the user whether the Purchase objects should be sorted and displayed in invoice number order or sale amount order.

Save the file as SortPurchasesArray.java.

public 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 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));
}
}

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.

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

OUTPUT:

Enter invoice number and sale amount for purchase : 1
345
4599.45
Enter invoice number and sale amount for purchase : 2
235
904.2
Enter invoice number and sale amount for purchase : 3
23
6034
Enter invoice number and sale amount for purchase : 4
753
204.6
Enter invoice number and sale amount for purchase : 5
523
434.05
In which order do you want to sort the purchases:
1) Invoice number,
2)Sales Amount

1
The invoice number entered is: 23
The sales tax for the invoice number is: 301.7
The sales amount for the invoice number is: 6034.0
The total amount of the invoice is: 6335.7

The invoice number entered is: 235
The sales tax for the invoice number is: 45.21000000000001
The sales amount for the invoice number is: 904.2
The total amount of the invoice is: 949.4100000000001

The invoice number entered is: 345
The sales tax for the invoice number is: 229.9725
The sales amount for the invoice number is: 4599.45
The total amount of the invoice is: 4829.4225

The invoice number entered is: 523
The sales tax for the invoice number is: 21.7025
The sales amount for the invoice number is: 434.05
The total amount of the invoice is: 455.7525

The invoice number entered is: 753
The sales tax for the invoice number is: 10.23
The sales amount for the invoice number is: 204.6
The total amount of the invoice is: 214.82999999999998

Java: 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 seve
Java: 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 seve

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site