I am new to programming and have a question regarding a homework assignment. First off this is for an Inventory Program and it is part 2. The assignment is as follows:
Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
I have the code written with the exception of creating a method to calculate the total inventory. My issue is that I do not completely understand how to sort the array by name. I have inserted the method and it compiles but it does not sort. I know that I must be missing a line of code somewhere but I am unsure where. Any guidance would be greatly appreciated.
import java.util.Arrays;
public class Inventory1 //declares public class inventory
{
public static void main(String args []) //starts the program
{
DVD[] dvd = new DVD[3];
dvd[0] = new DVD( 1, "Knocked Up", 2, 17.99 );//declares a new item number, dvd title, quantity, and price
dvd[1] = new DVD( 2, "Napolean Dynamite", 4, 14.99 );
dvd[2] = new DVD( 3, "Happy Gilmore", 6, 9.99 );
for(int i = 0; i < 3; i++)
{
System.out.println(dvd[i]);
System.out.println();
System.out.println("The item number is " + dvd[i].getItemNumber());
System.out.println("Product Title is " + dvd[i].getDvdTitle());
System.out.println("The number of units in stock is " + dvd[i].getOnHand());
System.out.println("The price of each DVD is " + dvd[i].getDvdCost());
System.out.println("The value of the inventory is " + dvd[i].value());
}
}//end main
} // end class Inventory1
class DVD //declares class inventory
{
private int itemNumber;// declares item number as in
private String dvdTitle;// declares dvd title as string
private int onHand; // declares on hand as int
private double dvdCost;// declares cost as double
public DVD(int stockNumber, String title, int inStock, double price) //constructor
{
itemNumber = stockNumber;//intitiates stock number, title, instock, and price
dvdTitle = title;
onHand = inStock;
dvdCost = price;
} //
// set DVD StockNumber //sets stock number
public void setItemNumber(int stockNumber)
{
itemNumber = stockNumber;
}
public int getItemNumber() // class item number
{
return itemNumber; // returns item number
} //end method get Dvd Item
public void setDvdTitle(String title) //set DVD Title
{
dvdTitle = title;
}
public String getDvdTitle() //gets and returns DVD Title
{
return dvdTitle;
}
public void setOnHand(int inStock) //Set on hand
{
onHand = inStock;
}
public int getOnHand() //gets and returns on hand
{
return onHand;
} //
public void setDvdCost(double price) // sets dvd cost
{
dvdCost = price;
}
public double getDvdCost() //gets and returns DVD cost
{
return dvdCost;
}
public double value() //calculates the value of stock
{
return dvdCost * onHand;
}
public DVD[] SortDVD (DVD[] theDVD)
{
DVD tmp;
for (int index = 0; index < theDVD.length; index++)
{
for (int anotherindex = index + 1; anotherindex< theDVD.length; anotherindex++)
{
String s1 = theDVD[index].getDvdTitle();
String s2 = theDVD[anotherindex].getDvdTitle();
if( s1.compareTo(s2) > 0)
{
tmp = theDVD[index];
theDVD[index] = theDVD[anotherindex];
theDVD [anotherindex] = tmp;
}
}
}
return theDVD;
}
} //end class DVD