Of course I have to make things difficult. If I just put 5 cd names in to an array it would not be that hard to set up a sort for them, and get them in alphabetical order, but I have to have the user enter the names in, so we do not know the names to be used in the sort until after the loop is entered.
Either I do not understand the sorting inside Java, or am just too inexperienced to code it correctly.
Here are my two classes:
import java.util.Scanner; //uses class Scanner
public class Inventory
{// begin class Inventory
public static void main(String[] args)
{//begin method main
// create cd Array
Compactdisk[] cds = new Compactdisk[5];
[B]Arrays.sort(cds); [/B]
cds[0] = new Compactdisk(); // adds to array
cds[0].getName();
cds[0].getPrice();
cds[0].getItemno();
cds[0].getNstock();
int cdCount = 0;
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 5 CD Names or STOP to Exit: ");
String nameInput = input.next(); //read cd name
while (!nameInput.equalsIgnoreCase("STOP"))
{// begin main While
cds[cdCount] = new Compactdisk();
cds[cdCount].setName(nameInput);
System.out.print("Enter Price of this CD: "); // prompt for price
cds[cdCount].setPrice(input.nextFloat()); // price input from user.
while (cds[cdCount].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[cdCount].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[cdCount].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
cds[cdCount].setNstock(input.nextInt()); // cds in stock input from user
System.out.print("CD "+cds[cdCount].getName()+", Item Number "+cds[cdCount].getItemno()+","); // display name
System.out.printf(" is worth %c%.2f.\n", '$', cds[cdCount].getPrice()); // display individual price
System.out.printf("We have %d copies in stock, making our inventory for this cd worth %c%.2f\n", cds[cdCount].getNstock(), '$', cds[cdCount].getValue()); //inventory value
try
{
totalValue = totalValue + cds[cdCount].getValue();
}
catch(NullPointerException npe)
{
} // handles exception
cdCount++;
System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n", '$', totalValue);
System.out.print("Enter up to 5 CD Names or STOP to Exit: "); // internal loop prompt
nameInput = input.next(); //name input from user
} // End main While
System.out.print("Ending Program.");
}// end method main
} // end class Payroll
I am getting a "cannot find symbol" error on the arrays.sort line bolded above. I did not expect errors until I started trying to sort on the actual name inside the array. Now I am really in over my head. I have never done sorting before, and wish I had done a much simpler array for my first time. This simple weekend project has turned in to a real bear.
Here is my other class just for clarity.
public class Compactdisk
{// begin class
//InventoryCD class has 5 fields
String Name; // Name of cd
float price; // price of cd
int itemno; // item number of cd
int nstock; // how many units in stock
int cdCount; // cd counter for array
float value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
Name = "";
price = 0;
itemno = 0;
nstock = 0;
cdCount = 0;
value = 0;
}
// set values
public void setName(String diskName)
{
Name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void setcdCount(int Count)
{
cdCount = Count;
}
// return values
public String getName()
{
return (Name);
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
}
// returns indivudual inventory value for a disk
public float getValue()
{
return(price * nstock);
}
}// end class
Any help on setting up a sort this way would be so much appreciated. I am going to feel like I have lost a weekend for nothing if I cannot get this set up by the time I get back to work in the morning.
Thanks guys.