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
  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

  • JAVA Programming . Description: For an unknown number of employees: prompt and receive payroll data; calculate...

    JAVA Programming . Description: For an unknown number of employees: prompt and receive payroll data; calculate gross pay, taxes owed, and net pay; and display a pay stub. Input : Prompt and receive input from the user for the following:  /// Employee’s First and Last Name  /// Hours Worked  /// Pay Rate  /// Overtime Rate Multiplier ** For the Employee’s First and Last Name:  Both data items must be read in one prompting into one...

  • Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...

    Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due Friday, February 9th, 2017 @ 11:59PM EST Directions Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class...

  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • I have this problem now and I am not sure what we are exactly supposed to...

    I have this problem now and I am not sure what we are exactly supposed to be sorting in ascending order. I am in java programming two and this is my first time using Cengage (online program), so I am unaware of the problems leading up to this one. I have provided the question and the code my professor provided the class, but with COVID-19 pushing our class to online we are not getting any assistance from the professor now....

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • For Java Program In this lab you will gain experience using all the concepts we learned...

    For Java Program In this lab you will gain experience using all the concepts we learned to this point, which include classes, methods, collections, and file input/output. Also, you will gain experience in team programming. This assignment will be completed by teams of 2. Each member must complete an equal amount of the work in order to receive credit for this assignment. You must write the programmer’s name in a comment for each method you write. You need to create...

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