I am trying to sort the 'stock' array (which is made up of Product objects) by the item name of each Product object. Before I start hacking at my code (which works well right now), I want to know if anyone can explain how I can sort this array by the String itemname of each Product object before output. Any help is much appreciated.
// Product.java // Product class for Inventory Program // Adam Gleason IT/215 2012 package inventory; //Declare package inventory public class Product { //Declare class Product public int itemnumber; //Declare integer itemnumber public String itemname; //Declare string itemname public int units; //Declare integer units public double price; //Declare double price public Product ( int newitemnumber, String newitemname, int newunits, double newprice) //constructor for class Product { itemnumber = newitemnumber; //Initialize integer newitemnumber itemname = newitemname; //Initialize string newitemname units = newunits; //Initialize integer newunits price = newprice; //Initialize integer newprice } public int getItemNumber() //Declare method getItemNumber { return itemnumber; //Return itemnumber } public String getItemName() //Declare method getItemName { return itemname; //Return itemname } public int getUnits() //Declare method getUnits { return units; //Return units } public double getPrice() //Declare method getPrice { return price; //Return price } public double calculateValue() //Declare method calculateValue { return units * price; //return value calculation } } //End class Product // Inventory.java // Exercise with input exception handling // Adam Gleason IT/215 2012 package inventory; //Declare package inventory import java.util.InputMismatchException; //Import definition for InputMismatchException import java.util.Scanner; //Import definition for Scanner import java.util.Arrays; public class Inventory { //Declare class Inventory public static void main(String[] args) { //Declare method main String stop; //Declare sentinal string stop int result; //declare sentinal int result Scanner input = new Scanner (System.in); //Declare new scanner object int itemnumber; //Delcare int itemnumber String itemname; //Delcare string itemname int units; //Delcare integer units double price; //Declare double price Product[] stock; //new stock = new Product[100]; int counter = 0; //new stop = "stop"; //Initialize sentinal string stop result = 1; //Initialize sentinal integer result System.out.print("Welcome to the Inventory Program"); //Welcome message while (result != 0) //Repetition statement for multiple product input { System.out.print( "\n\nEnter the item name (enter 'stop' to quit):" ); //Prompt for string itemname itemname = input.nextLine(); //Input string itemname result = itemname.compareTo(stop); //Compare sentinal string if (result !=0) //Test sential integer { boolean itemnumberloop = true; //Initialize intemnumberloop repetion statement itemnumber = 0; //Initialize integer itemnumber while (itemnumberloop) //Begin intemnumberloop repetition statement { try //Try statement for valid input { int test = 0; //Initialize test variable System.out.print("Enter the Item Number: "); //Prompt for integer itemnumber itemnumber = input.nextInt(); //Input integer itemnumber test = itemnumber / 1; //Test for valid input type while (itemnumber < 0) //test for valid integer input { System.out.println("That is an invalid item number, please try again"); //Display invalid integer input message System.out.print("Enter the Item Number: "); //Prompt for integer itemnumber itemnumber = input.nextInt(); //Input integer itemnumber } itemnumberloop = false; //End itemnumberloop repetition statement after valid input test = 0; //nullify value of test statement } catch (InputMismatchException inputMismatchException) //Exception statement for invalid input type { System.err.printf("\nError: %s\n", inputMismatchException); //Display error message input.nextLine(); //nullify invalid input System.out.println("You must input an integer, please try again\n"); //Display invalid input type message } } boolean unitsloop = true; //Initialize unitsloop repetition statement units = 0; //initialize integer units while (unitsloop) //Begin unitsloop repetition statement { try //Try statement for valid input { int test = 0; //Initialize test variable System.out.print("Enter the number of units: "); //Prompt for integer units units = input.nextInt(); //Input integer units test = units / 1; //Test for valid input type while (units < 0) //Repition statement for valid integer input { System.out.println("That is an invalid number of units, please try again"); //Display invalid integer input message System.out.print("Enter the number of units: ");//Prompt for integer units units = input.nextInt(); //Input integer units } unitsloop = false; //End unitsloop repetition statement after valid input test = 0; //nullify test variable } catch (InputMismatchException inputMismatchException) //Exception statement for invalid input type { System.err.printf("\nError: %s\n", inputMismatchException); //Display error message input.nextLine(); //nullify invalid input System.out.println("You must input an integer, please try again\n"); //Display invalid input message } } boolean priceloop = true; //Initialize pricelooop repetition statement price = 0; //Initialize double price while (priceloop) //Begin priceloop repettion statement { try //Try statement for valid input { double test = 0; //Initialize test variable System.out.print("Enter the price for each unit: ");//Prompt for double price price = input.nextDouble(); //Input double price test = price / 1; //Test for valid input type while (price < 0) //Repetition statement for valid double input { System.out.println("That is an invalid price, please try again"); //Display invalid double input message System.out.print("Enter the price for each unit: "); //Prompt for double price price = input.nextDouble(); //Input double price } priceloop = false; //End priceloop repetition statement after valid input test = 0; //nullify test variable } catch (InputMismatchException inputMismatchException) //Exception statement for invalid input type { System.err.printf("\nError: %s\n", inputMismatchException); //Display error message input.nextLine(); //nullify invalid input System.out.println("You must input a number, please try again\n"); //Display invalid input type message } } stock[ counter ] = new Product (itemnumber, itemname, units, price); counter++; //new //Display output using Product class methods itemname = input.nextLine(); //nullify string itemname for sentinal comparison } else { for (int count = 0; count < counter; count++) System.out.printf("\n\nItem Number: %d\nItem Name: %s\nUnits in stock: %d\nPrice per unit: $%.2f\nTotal value of inventory: $%.2f", stock[count].getItemNumber(), stock[count].getItemName(), stock[count].getUnits(), stock[count].getPrice(), stock[count].calculateValue()); double total = 0; for (int i = 0; i < counter; i++) total += stock[i].calculateValue(); System.out.printf("\n\nTotal value of all inventory items: $%.2f", total); System.out.println("\n\nThank you for using the Inventory Program"); //Display exit message after sentinal input } } //End repetition statement for multiple product input } //End method main } //End class Inventory