Best of 2008: A developer's list

JW blogger Dustin Marx names his top 10 technology events of 2008. Highlights include updates to Java SE 6, runtime support in OpenLaszlo 4.2, and the clash of the titans that occurred early in the year, when Sun acquired MySQL on the same day that Oracle announced its acquisition of BEA. No two lists are alike and it's not too late: What were your top 10 for 2008?

Also see:

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

Need help sorting arrays

Tags:

I am new to java programming and totally lost. Will someone please help me in sorting the arrays in my product class of the following code. Thanks.

import java.util.Scanner;
import java.util.Arrays;
public class Inventory {

public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
boolean isValid = true;//**
String productName;// = "DVD"//++
int productNumber;//++
int onHand;//++
double price;//++
double totalInventory;//++
//int count;
double totInventory;//[] = new double[3];//++
double reStockFee = .05;
double reStockCost = 0;
totalInventory = 0;
int count = 0;

while (isValid)
{
System.out.println ("Enter Product Name. Enter exit to quit program");
productName = input.next();

if (!productName.equalsIgnoreCase ("exit"))//++
{
System.out.println ("Enter Item Number: ");
       productNumber = input.nextInt();
       System.out.println ("Enter number of items in stock: ");
       onHand = input.nextInt();
       System.out.println ("Enter item price: ");
       price = input.nextDouble();
       //compute student grade
       totInventory = (onHand * price);
       reStockCost = (onHand * price * reStockFee);
     //create a new class and pass the values to the method

    Product.getData(productName, productNumber, onHand, price, reStockCost, totInventory, count);//+++
    count++;  //increment the counter to use as the array position in second class//+++
}
else
{
isValid = false;//**

System.out.print ("Item Name" + "\t");

System.out.print ("Item Number" + "\t");

System.out.print ("Items on Hand" + "\t");

System.out.print ("Item Price" + "\t");

System.out.print ("Items Value" + "\t");

System.out.print ("Restock Fee" + "\n");

Product.printArray(count);



Product.modifyArray(pName);
System.out.println ("Thank you, come again.");

break;

}

}

}
}

//Product Class

public class Product {
static int maxlength = 100;
static String pName[] = new String[maxlength];
static int[] iNumber = new int [maxlength];
static int[] iStock = new int [maxlength];
static double[] uPrice = new double [maxlength];
static double[] tInventory = new double [maxlength];
static double [] rStockCost = new double [maxlength];
static double totalInventory = 0;
//static double reStockFee = .05;
//static double reStockCost = 0;
static double totreStock = 0;
//public Product(int productNumber, int onHand, double price,
// double totInventory, int count) {
// TODO Auto-generated constructor stub
//}

public static void getData(String productName, int productNumber, int onHand, double reStockCost, double price, double totInventory, int count)
{
  //check that count doesn't exceed maximum array length
  if (count > maxlength)
  {
   System.out.println("DATA OVERLOAD!!!!. YOU CRASHED THE SYSTEM!!!  Program Exiting!");
   System.exit(0);
  }
  else
  {
   //store values in the array
   pName[count] = productName;
   iNumber[count] = productNumber;
   iStock[count] = onHand;
   uPrice[count] = price;
   rStockCost[count] = reStockCost;
   tInventory[count] = totInventory;
   totalInventory += totInventory;
   totreStock += reStockCost;

   }

   }

//print the values in the array
//public static void printAll(int count)
public static void printArray(int count)
{
for (int i=0; i<count; i++ )
{
System.out.printf(pName[i]);
System.out.printf("%15d", iNumber[i]);
System.out.printf("%18d", iStock[i]);
System.out.printf("%11s$%.2f", "", uPrice[i]);
System.out.printf("%10s$%.2f", "", tInventory[i]);
System.out.printf("%10s$%.2f\n","", rStockCost[i]);
}
  System.out.printf("%s$%.2f\n", "Entire Inventory Value: ", totalInventory);
  System.out.printf("%s$%.2f\n", "Entire Inventory Restock Cost: ", totreStock);


}

}



public class modifyArray
{

public static void modifyArray(pName)
{
Arrays.sort(pName1);
System.out.println("Array contents sfter sort:");
for (int i = 0; i < pName.length; i++)
System.out.println(pName1);
}

}

Your rating: None

Make your objects "Comparable"

You need to implement the Comparable interface (java.util.Comparable) and implement its compareTo() method, this should do the job...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.