The folloing to java files input 5 objects into an array and can then be sorted
ID: 3859443 • Letter: T
Question
The folloing to java files input 5 objects into an array and can then be sorted by invoice number or price. Make the following two modifications:
1. Enhance the program by displaying a menu that asks the user how they want to sort the services menu. 1) Sort by Service Description, 2) Sort by Price, 3) Sort by Time (Minutes), or 0) to Exit.
2. Add a do...while() loop that keeps prompting the user for the next preferred sort order until the user finally chooses “0” to exit
import java.util.Scanner;
public class SortPurchasesArray
{
public static void main(String[] args)
{
final int size = 5;
int i;
Purchase[] purchase = new Purchase[size];
Scanner input = new Scanner(System.in);
for(i = 0; i <purchase.length;i++)
{
System.out.println("Invoice number: ");
int invoiceNumber = input.nextInt();
System.out.println("Sale price: ");
double sales = input.nextDouble();
purchase[i]=new Purchase();
purchase[i].setInvoice(invoiceNumber);
purchase[i].setAmount(sales);
}
print(purchase);
System.out.println("Select sort: 1 by Invoice Number 2 by Price ");
int choice = input.nextInt();
switch(choice)
{
case 1:
sortByInvoice(purchase);
print(purchase);
break;
case 2:
sortBySales(purchase);
print(purchase);
break;
default:
System.out.println("Invalid sort selection");
}
}
private static void sortByInvoice(Purchase[] purchase)
{
Purchase tempPurchase;
for(int j = 0;j<purchase.length-1;j++)
{
for(int k = 0;k<purchase.length-1;k++)
{
if(purchase[k].getInvoice()>purchase[k+1].getInvoice())
{
tempPurchase = purchase[k];
purchase[k]=purchase[k+1];
purchase[k+1]=tempPurchase;
}
}
}
}
private static void sortBySales(Purchase[] purchase)
{
Purchase temp;
for(int i = 0; i<purchase.length-1;i++)
{
for(int j = 0;j<purchase.length-1;j++)
{
if(purchase[j].getAmount()>purchase[j+1].getAmount())
{
temp=purchase[j];
purchase[j]=purchase[j+1];
purchase[j+1]=temp;
}
}
}
}
private static void print(Purchase[] purchase)
{
System.out.printf("%-15s%-10s%-10s ","Invoice","Sales","Tax");
for(int i = 0;i<purchase.length;i++)
{
System.out.printf("%-10d%-10.2f%-10.2f ",purchase[i].getInvoice(),purchase[i].getAmount(),purchase[i].getTax());
}
}
}
public class Purchase
{
private final double tax = .05;
private int invoiceNumber;
private double saleAmount;
private double salesTax;
public void setInvoice(int invoiceNumber)
{
this.invoiceNumber=invoiceNumber;
}
public void setAmount(double saleAmount)
{
this.saleAmount=saleAmount;
salesTax = saleAmount*tax;
}
public int getInvoice()
{
return invoiceNumber;
}
public double getAmount()
{
return saleAmount;
}
public double getTax()
{
return salesTax;
}
}
Explanation / Answer
As per your question you wanted menu like this : -
1) Sort by Service Description, 2) Sort by Price, 3) Sort by Time (Minutes), or 0) to
but thee is no service description, Price and Time in Purchase class, So I am assuming this you mentioned just as a sample of what I need to build.
following is your code. I have not changed the Purchase class So, I am not pasting it here.
SortPurchasesArray.java
import java.util.Scanner;
public class SortPurchasesArray {
public static void main(String[] args) {
final int size = 5;
int i;
Purchase[] purchase = new Purchase[size];
Scanner input = new Scanner(System.in);
for (i = 0; i < purchase.length; i++) {
System.out.println("Invoice number: ");
int invoiceNumber = input.nextInt();
System.out.println("Sale price: ");
double sales = input.nextDouble();
purchase[i] = new Purchase();
purchase[i].setInvoice(invoiceNumber);
purchase[i].setAmount(sales);
}
print(purchase);
boolean done = false;
while (!done) {
System.out.println(" Select sort: 1 by Invoice Number 2 by Price 0 Exit");
int choice = input.nextInt();
switch (choice) {
case 1:
sortByInvoice(purchase);
print(purchase);
break;
case 2:
sortBySales(purchase);
print(purchase);
break;
case 0:
done = true;
break;
default:
System.out.println("Invalid sort selection");
}
}
}
private static void sortByInvoice(Purchase[] purchase) {
Purchase tempPurchase;
for (int j = 0; j < purchase.length - 1; j++) {
for (int k = 0; k < purchase.length - 1; k++) {
if (purchase[k].getInvoice() > purchase[k + 1].getInvoice()) {
tempPurchase = purchase[k];
purchase[k] = purchase[k + 1];
purchase[k + 1] = tempPurchase;
}
}
}
}
private static void sortBySales(Purchase[] purchase) {
Purchase temp;
for (int i = 0; i < purchase.length - 1; i++) {
for (int j = 0; j < purchase.length - 1; j++) {
if (purchase[j].getAmount() > purchase[j + 1].getAmount()) {
temp = purchase[j];
purchase[j] = purchase[j + 1];
purchase[j + 1] = temp;
}
}
}
}
private static void print(Purchase[] purchase) {
System.out.printf("%-15s%-10s%-10s ", "Invoice", "Sales", "Tax");
for (int i = 0; i < purchase.length; i++) {
System.out.printf("%-10d%-10.2f%-10.2f ", purchase[i].getInvoice(), purchase[i].getAmount(),
purchase[i].getTax());
}
}
}
Sample Run: -
Invoice number:
12
Sale price:
121
Invoice number:
5
Sale price:
21231
Invoice number:
22
Sale price:
2341
Invoice number:
64
Sale price:
24242
Invoice number:
2
Sale price:
434232
Invoice Sales Tax
12 121.00 6.05
5 21231.00 1061.55
22 2341.00 117.05
64 24242.00 1212.10
2 434232.00 21711.60
Select sort:
1 by Invoice Number
2 by Price
0 Exit
1
Invoice Sales Tax
2 434232.00 21711.60
5 21231.00 1061.55
12 121.00 6.05
22 2341.00 117.05
64 24242.00 1212.10
Select sort:
1 by Invoice Number
2 by Price
0 Exit
1
Invoice Sales Tax
2 434232.00 21711.60
5 21231.00 1061.55
12 121.00 6.05
22 2341.00 117.05
64 24242.00 1212.10
Select sort:
1 by Invoice Number
2 by Price
0 Exit
2
Invoice Sales Tax
12 121.00 6.05
22 2341.00 117.05
5 21231.00 1061.55
64 24242.00 1212.10
2 434232.00 21711.60
Select sort:
1 by Invoice Number
2 by Price
0 Exit
2
Invoice Sales Tax
12 121.00 6.05
22 2341.00 117.05
5 21231.00 1061.55
64 24242.00 1212.10
2 434232.00 21711.60
Select sort:
1 by Invoice Number
2 by Price
0 Exit
1
Invoice Sales Tax
2 434232.00 21711.60
5 21231.00 1061.55
12 121.00 6.05
22 2341.00 117.05
64 24242.00 1212.10
Select sort:
1 by Invoice Number
2 by Price
0 Exit
0