Question: 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

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.

Step by Step Solution

3.23 Rating (167 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javautilScanner public class SortPurchasesArray public static void mainString args Purchase purchases new Purchase6 int i String message char choice final char QUIT Z int number double price Scanner keyboard new ScannerSystemin fori 0 i purchaseslength i SystemoutprintEnter invoice number number keyboardnextInt SystemoutprintEnter sale amount price keyboardnextDouble purchasesi new Purchase purchasesisetInvoiceNumbernumber purchasesisetSaleAmountprice keyboardnextLine Systemoutprint Sort Purchases by Invoice number or Sale amount choice keyboardnextLinecharAt0 whilechoice QUIT ifchoice I sortByInvoicepurchases message Sorted by invoice number displaypurchases message else ifchoice S sortBySaleAmountpurchases message Sorted by sale amount displaypurchases message else SystemoutprintlnInvalid choice Systemoutprintln Sort Purchases by Invoice number or Sale amount Systemoutprint or enter QUIT to quit choice keyboardnextLinecharAt0 public static void sortBySaleAmountPurchase array int a b Purchase temp int highSub arraylength 1 for a 0 a highSub a for b 0 b highSub b ifarraybgetSaleAmount arrayb 1getSaleAmount temp arrayb arrayb arrayb 1 arrayb 1 temp public static void sortByInvoicePurchase array int a b Purchase temp int highSub arraylength 1 for a 0 a highSub a for b 0 b highSub b if arraybgetInvoiceNumber arrayb 1getInvoiceNumber temp arrayb arrayb arrayb 1 arrayb 1 temp public static void displayPurchase p ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!