I am trying to get an array sorted but I can't figure out how to correctly write the "if" statement in the sort method. I have been given the task of sorting the arrays by product name. I am not done with the program, but what I do have so far is below. I need some input on the sort method only. The other required elements of this assignment I can manage on my own, but the sort thing has me puzzled.
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;
public class InventoryPart1// declares class for InventoryPart1
{
public static Printer[] sortArray (Printer[] printerProduct)
{
String[] names = new String [printerProduct.length];// step 1
Printer [] sorted = new Printer[printerProduct.length];// step 2
for (int i = 0; i < printerProduct.length; i++)// step 3
{
names[i] = printerProduct[i].getProductName();
}
for (int i = 0 ; i < printerProduct.length; i++)// step 4
{
for (int j = 0; j < names.length; j++)
{
if (names[i]= printerProduct.length)
{
}
}
return sorted;
}
}
public static void main(String args[])// main method
{
Printer printerProduct[] = new Printer[5];
printerProduct[0] = new Printer(1437, "InkSmart", 17, 29.99);// new instance of Printer class declared with variable values
printerProduct[1] = new Printer(2598, "All-In-One", 23, 34.89);
printerProduct[2] = new Printer(1145, "LaserJet", 14, 24.99);
printerProduct[3] = new Printer(9956, "PortaPrint", 21, 21.99);
printerProduct[4] = new Printer(1112, "PageSmart", 15, 59.99);
}
}
class Printer// class declaration
{
public int itemNumber;// variable declarations for class Printer
public String productName;
public int inStock;
public double unitPrice;
public Printer()// default constructor
{
itemNumber = 0;
productName = "";
inStock = 0;
unitPrice = 0;
}
public Printer (int itemNumber, String productName, int inStock, double unitPrice)// declares object's variavle types, and names
{
this.itemNumber = itemNumber;
this.productName = productName;
this.inStock = inStock;
this.unitPrice = unitPrice;
}
public void setItemNumber (int itemNumber)// method sets itemNumber variable
{
this.itemNumber = itemNumber;
}
public int getItemNumber ()// get method for itemNumber
{
return itemNumber;
}
public void setProductName (String productName)// set method for productName
{
this.productName = productName;
}
public String getProductName()// get method for productName
{
return productName;
}
public void setInStock(int inStock)// set method for inStock
{
this.inStock = inStock;
}
public int getInStock()// get method for inStock
{
return inStock;
}
public void setUnitPrice (double unitPrice)// set method for unitPrice
{
this.unitPrice = unitPrice;
}
public Double getUnitPrice ()// get method for unitPrice
{
return unitPrice;
}
public Double getInventoryValue ()// get method for inventoryValue. Calculates value of entire inventory
{
return unitPrice * inStock;
}
public String toString() {
return String.format("Item Number=%3d Product Name=%-20s Units In Stock=%d price=%.2f Inventory Value=%.2f",
itemNumber, productName, inStock, unitPrice, getInventoryValue());
}
}