/*Program: Inventory Program
* File: Product.java
* Summary: Sets the product information, Also retrieves and displays the objects information
* Author: Joelene Wells
* Date: April 30, 2010
*/
import java.text.NumberFormat;
public class Product {
//begin product class
NumberFormat cf = NumberFormat.getCurrencyInstance();
//declare instance variables
String productName; //item's name
private int itemNumber;//item's number
private double itemPrice; //item's price
private int stockUnits; //number of units in stock of the item
private double productValue;//individual cd's Value
private double totalValue;//total value of all inventory
//creates product constructor
public Product(String setProductName, int setItemNumber, int setItemPrice, int setStockUnits){
productName = setProductName; //product name from the inventory class
itemNumber = setItemNumber; //sets product nunber from inventory class
itemPrice = setItemPrice; //sets product price from inventory class
stockUnits = setStockUnits; //sets stock units from inventory class
}
public void setProductName(String nameIn){
this.productName = nameIn;//set's item name and passes it to the method
}//end setProductName method
public String getProductName (){
return this.productName;//returns product name
}//end getProductName method
public void setItemNumber (int productNumber){
this.itemNumber = productNumber;//sets product number from method
}//end setItemNumber method
public int getItemNumber () {
return this.itemNumber; //returns product number
}//end getItemNumber method
public void setItemPrice (double productPrice){
this.itemPrice = productPrice;//sets product price from method
}//end setItemPrice method
public double getItemPrice (){
return this.itemPrice;//returns product price
}//end getItemPrice method
public void setStockUnits (int inStockUnits){
this.stockUnits = inStockUnits;// sets in stock units from method
}//end setStockUnits method
public int getStockUnits (){
return this.stockUnits;//returns in stock units
}//end getStockUnits method
public double getValue (){
return getStockUnits() * getItemPrice();
}
public void displayInventoryInfo(){
System.out.println();//blank line easier to read
System.out.println("Name: " + productName);//prints products name
System.out.println("Product Number: " + itemNumber);//prints item's number
System.out.printf("Item Price: " + cf.format(itemPrice));//prints the item price
System.out.println();//print blank line
System.out.println("Units In Stock: " + stockUnits);//prints the number of instock units
System.out.printf("Total Value: "+ cf.format(productValue));//prints the total value of stock
System.out.println();//print blank line
System.out.printf("Value of all Inventory: "+ cf.format(totalValue));//prints the value of all the inventory
System.out.println();//blank line for readability
}//end displayInventory Method
}//end product class
/* Program: Inventory Program
* File:Inventory.java
* Summary: This program is creating an array of objects and
* Author: Joelene Wells
* Date: April 30, 2010
*/
import java.util.Arrays; //imports the array package for usage
public class Inventory {
//start inventory class
public static void main(String[] args) {
Product cd[] = new Product[5];//creates the array
//instantiates the Product class
Product cd1 = new Product("Mariah Carey", 1205, 13, 130);//creats an instance of the product class and instantiates the class instance variables
Product cd2 = new Product("Ludacris", 1230, 10, 50);//creates an instance of the product class and instantiats the calss instance variables
Product cd3 = new Product("Justin Timberlake", 1250, 18, 100);//creates an instnce of the product class and instantiate the class instnace variables
Product cd4 = new Product("Jennifer Lopez", 1270, 14, 185); //creates an instance of the product class and instatiate the class instance variable
Product cd5 = new Product("DMX", 2000, 20, 150);//creates an instance of the product class and instantiates the class instance variables
// add the cd objects to the array
cd[0] = cd1; //adds Mariah Carey's cd to the array
cd[1] = cd2; //adds Ludacris's cd to the array
cd[2] = cd3; //adds Justin Timberlake's cd to the array
cd[3] = cd4; //adds Jennifer Lopez's cd to the array
cd[4] = cd5; //adds DMX's cd to the array
//sort the array and display the information on the console
Arrays.sort(cd, new CompareCDNames()); //sorts the cd Array
System.out.println("Displaying CD's In Order by Name: ");//tell the user that the items are in order
for (int i = 0; i < cd.length; i++){
cd[i].displayInventoryInfo();//displays the information in the Product class
}//end displaying of the inventory information
}//end main method
}//end inventory class
/*Program: Inventory Program
*File: CompareCDName.java
*Summary: Compare objects that sorts the CD's by the name
*Author: Joelene Wells
* Date: April 30, 2010
*/
import java.util.Comparator;//imports the java comparator to compare names
public class CompareCDNames implements Comparator {
//being CompareCDNames Class
public int compare (Object cd1, Object cd2)// creates two CD objects to compare the CD names
{ String name1 = ((Product)cd1).getProductName().toUpperCase();//gets the first name from the Product Class
String name2 = ((Product)cd2).getProductName().toUpperCase();//gets the second name from the Product Class
// compare name1 with name 2
if (!(name1.equals(name2)))
return name1.compareTo(name2);
else
return name2.compareTo(name1);
}//end compare method
}//end CompareCDNames class
Without trying to calculate the total inventory the files work. If there is a way someone can point me in the right direction I would greatly appreciate it.

New Topic/Question
Reply




MultiQuote







|