Question

Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase....

Java Programming Exercise 9-7 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. Do not use numbers for accepting options from user. Use below characters Mandatory Sorting Oder Options: I - Invoice Sorting S - Sale Amount Z - Exit

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

Purchase.java

package classes1;

public class Purchase {
   private int invoiceNumber;
   private double amountOfSale;
   private double amountOfSaleTax;
  
   public Purchase() {
       super();
       this.invoiceNumber = 0;
       this.amountOfSale = 0;
       this.amountOfSaleTax = 0;
   }
  
   public Purchase(int invoiceNumber, double amountOfSale, double amountOfSaleTax) {
       super();
       this.invoiceNumber = invoiceNumber;
       this.amountOfSale = amountOfSale;
       this.amountOfSaleTax = amountOfSaleTax;
   }
   public int getInvoiceNumber() {
       return invoiceNumber;
   }
   public void setInvoiceNumber(int invoiceNumber) {
       this.invoiceNumber = invoiceNumber;
   }
   public double getAmountOfSale() {
       return amountOfSale;
   }
   public void setAmountOfSale(double amountOfSale) {
       this.amountOfSale = amountOfSale;
   }
   public double getAmountOfSaleTax() {
       return amountOfSaleTax;
   }
   public void setAmountOfSaleTax(double amountOfSaleTax) {
       this.amountOfSaleTax = amountOfSaleTax;
   }
  
  

}

PurchaseTest.java

package classes1;

import java.util.Scanner;

public class PurchaseTest {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       Scanner scChoice = new Scanner(System.in);
       String choice = "";
       System.out.println("Enter details of five purchase product");
       Purchase items[] = new Purchase[5];
       for(int i=0;i<5;i++){
           System.out.println("Enter details for item # "+(i+1));
           System.out.print("\tEnter invoice number : ");
           items[i] = new Purchase();
           items[i].setInvoiceNumber(sc.nextInt());
           System.out.print("\tEnter amount of sale : ");
           items[i].setAmountOfSale(sc.nextDouble());
           System.out.print("\tEnter amount of sale tax : ");
           items[i].setAmountOfSaleTax(sc.nextDouble());
       }

       while(true){
           System.out.println("Select an option");
           System.out.println("I - Invoice Sorting");
           System.out.println("S - Sale Amount ");
           System.out.println("Z - Exit");
           System.out.print("Enter your choice : ");
           choice = scChoice.nextLine();
           if(choice.equalsIgnoreCase("I")){
               for(int i=0;i<5;i++){
                   for(int j=i+1;j<5;j++){
                       if(items[i].getInvoiceNumber()>items[j].getInvoiceNumber()){
                           Purchase temp = items[i];
                           items[i] = items[j];
                           items[j] = temp;
                       }
                   }  
               }
               System.out.printf("%-20s%-20s%-20s\n","Invoice Number","Sale Tax Amount","Sale Tax Amount Tax");
               for(int i=0;i<5;i++){
                   System.out.printf("%-20s%-20s%-20s\n",items[i].getInvoiceNumber(),items[i].getAmountOfSale(),items[i].getAmountOfSaleTax());  
               }
           }else if(choice.equalsIgnoreCase("S")){
               for(int i=0;i<5;i++){
                   for(int j=i+1;j<5;j++){
                       if(items[i].getAmountOfSale()>items[j].getAmountOfSale()){
                           Purchase temp = items[i];
                           items[i] = items[j];
                           items[j] = temp;
                       }
                   }  
               }
               System.out.printf("%-20s%-20s%-20s\n","Invoice Number","Sale Tax Amount","Sale Tax Amount Tax");
               for(int i=0;i<5;i++){
                   System.out.printf("%-20s%-20s%-20s\n",items[i].getInvoiceNumber(),items[i].getAmountOfSale(),items[i].getAmountOfSaleTax());  
               }
           }else if(choice.equalsIgnoreCase("Z")){
               System.out.println("Bye Bye....");
               break;
           }else{
               System.out.println("Please select an valid option...");
           }
       }
   }
}

output:

Add a comment
Know the answer?
Add Answer to:
Java Programming Exercise 9-7 In the exercises in Chapter 6, you created a class named Purchase....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT