Inventory program

need help with my code, please

Page 1 of 1

1 Replies - 2773 Views - Last Post: 01 March 2009 - 10:27 AM Rate Topic: -----

#1 kayc_1980   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 09

Inventory program

Posted 01 March 2009 - 07:33 AM

I know many already posted in here about the Inventory program but I just need someone to look over my code and see what I did wrong. I already put my code together for part 3 and 4 and the program compiles without any problems but it dont do what it should do, the main class doesnt call the other classes. here is what is needed for part 2
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.
Create a method to calculate the value of the entire inventory.
Create another method to sort the array items by the name of the product.

here is what i got so far for part 2

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inventoryprogrampart2;
import java.util.Scanner; // program uses class Scanner
/**
 *Checkpoint: Inventory Program Part 2
 * Inventory2.java
 * This program will stores DVD collection
 * @author Kay C
 */
public class Main{

	/**
	 * @param args the command line arguments
	 */
public static void main(String[] args) {
	   // create and initialize a DVD objeect
		
	  
	  
	  Scanner input = new Scanner( System.in );
 
	  System.out.println(); //A blank println command prints a simple blank line

	  //The following three lines display text and instructions for the user, just adds a little extra to the program
 
	  System.out.printf("Inventory Program Part 2\n");
 
	  System.out.printf("This program will inventory DVD collection.\n");
 
	  System.out.printf("Please enter the needed information.\n");
		  // prompt for user input
 
		 
	 float item; 
	  float price;  
	  float stock;
	  String title;
	  float value;
	  float sumValue;

	  System.out.println();
		  
		// prompt for and input DVD information  
		  System.out.println("Enter the DVD Titel or STOP to end the program:");
		  // user input DVD title
		  title = input.nextLine ();
		 
	   //Loops the program until stop is entered
	   while ( !title.equals( "STOP" )) //while ( !DVDTitle.equals("stop"))
	  { 
	 System.out.println( "Enter Item Number: "); // prompt
	 item = input.nextInt(); // read entered hourly pay rate entered by user
	 
		  System.out.println("Enter Units in Stock:");
		  // user input Quantity in Stock
		  stock = input.nextInt ();
		  
		 System.out.println("Enter Price per Unit:");
		  // user input Unit Price
		  price = input.nextInt();
		  
				 
	//Calculation to get Inventory Value   
		   value = price * stock;
		 
		 
		  DVD myDVD = new DVD(title,item,stock,price);
		  
		  
		  System.out.println();
 
	  System.out.println();
System.out.println("____________________________________________________"); 

System.out.printf("DVD Title:  %s\n", myDVD.getDvdTitle());
System.out.printf("Item Number: %s\n", myDVD.getDvdItem());
System.out.printf("Quantity:  %s\n", myDVD.getDvdStock());
System.out.printf("Unit Price:  $%.2f\n", myDVD.getDvdPrice());
System.out.printf("Inventory  Value: $%.2f", myDVD.getValue());

 
	  System.out.println("");
 
	  System.out.println();
 
	  // prompt for and input Employee's name
	  System.out.println( "Enter the DVD Titel or STOP to end the program: " );
		title = input.next(); //read line of text
	 
			}//end main
   }//end method main
}//end class
	

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inventoryprogrampart2;
import java.text.DecimalFormat;
import java.io.*;
/**
 *ckpoint: Inventory Program Part 2
 * Inventory2.java
 * This program will stores DVD collection
 * @author Kay C
 */
public class Inventory {
 // Setup an array of Products that holds 25 items
	int inventorySize = 25;
	String printInventory = "default";



	private DVD items[] = new DVD[inventorySize];

	// format values into currency form.
	DecimalFormat formatter = new DecimalFormat("$##,###.00");
	private int InventorySize;


	// Adds a product to the array of products
	public void addDVD(DVD item) {
		for (int i = 0; i < inventorySize; i++)
			if (items[i] == null) {
				items[i] = item;
				return;
			}
	}

	public double getTotalValue() // calculation for total inventory value
   {
	   double totalValue = 0.0;
	   for (DVD item : items)
	   {
		   if (item != null){
			   totalValue += item.getValue();
		   }
		  }
	return totalValue;

	}
		public void printInventory() { // Prints Inventory list

	   System.out.println("Printing items in inventory...\n");

		boolean hasItems = false;

		for (DVD item : items) {
			if (item!= null) {
				hasItems = true;
			}
				System.out.println("Inventory is currently empty.\n");

		System.out.println( "Thank you for using this Inventory Program" );
		}
	}
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inventoryprogrampart2;
/**
 *Checkpoint: Inventory Program Part 2
 * Inventory2.java
 * This program will stores DVD collection
 * @author Kay C
 */
public class DVD {

	//DVD myDVD = new DVD(title,item,stock,price);
	 public DVD(){
		 DVD myDVD1 = new DVD("Cars", 1, 5, 12.99);
		 DVD myDVD2 = new DVD("Jeff Dunham - Spark of Insanity", 2, 8, 15.99);
		 DVD myDVD3 = new DVD("Jeff Dunham - Arguing with Myself", 3, 4, 16.99);
		 DVD myDVD4 = new DVD("The Lake House", 4, 9, 19.99);
		 DVD myDVD5 = new DVD("Honey", 5, 2, 10.99);
	 }

	private String dvdTitle;
	private double dvdItem;
	private double dvdStock;
	private double dvdPrice;
	private double getValue;


	// Default constructor uses other constructor to set the
	// default state of the object.
	 String getDvdTitle = "default";
	 double item = 0.0;
	 double stock = 0.0;
	 double price = 0.0;
	 double value = 0.0;



	// Constructor that user can specify a name, quantity, and price for for items.
	 public DVD(String title, double item, double stock, double price) {
	  dvdTitle = title;
	  dvdItem = item;
	  dvdStock = stock;
	  dvdPrice = price;
	}   // end class DVD constructor

	  public void dvdStock(int Stock) {
		if (Stock > 0) {
			dvdStock = Stock;
		}
		else { dvdStock = 0; }
	}

	// Set price of a product and defaults it to zero if negative.
	public void setPrice(double itemPrice) {
		if (itemPrice > 0.00) {
			price = itemPrice;
		}
		else { price = 0.00; }
	}

	// return DVD name
   public String getDvdTitle(){
	 return dvdTitle;
   }
	 // set DVD name
	public void setDvdTitle(String title){
	 this.dvdTitle = title;
   }
	// return item number
	public double getDvdItem(){
	return dvdItem;

   }
   // set item number
   public void setDvdItem(double item){
	this.dvdItem = item;
   }
   // return number of units in stock
	public double getDvdStock(){
	 return dvdStock;
	 }

   // set number of units in stock
   public void setDvdStock(double stock){
	 this.dvdStock = stock;
   }
	// return price of each unit
   public double getDvdPrice(){
	 return dvdPrice;

   }

   // set price of each unit
   public void setDvdPrice (double price){
	 this.dvdPrice = price;
   }
	  // calculate inventory value
   public double getValue()
   {
	  double Value = dvdPrice * (double)dvdStock;
	   return Value;

   } // end method inventoryValue

}  // end class DVD





I would appreciate if someone could tell me what I did wrong here

here is my code for part 3 and this is whats needed
Modify the Inventory Program by creating a subclass of the product class that uses one
additional unique feature of the product you chose (for the DVDs subclass, you could use
movie title, for example). In the subclass, create a method to calculate the value of the
inventory of a product with the same name as the method previously created for the
product class. The subclass method should also add a 5% restocking fee to the value of
the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking
fee.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inventoryprogrampart3;
import java.util.Scanner; // program uses class Scanner

/**
 *Checkpoint: Inventory Program Part 3
 * Inventory3.java
 * This program will stores DVD collection
 * @author Kay C
 */
public class Main{

	/**
	 * @param args the command line arguments
	 */
public static void main(String[] args) {
	   // create and initialize a DVD objeect



	  Scanner input = new Scanner( System.in );

	  System.out.println(); //A blank println command prints a simple blank line

	  //The following three lines display text and instructions for the user, just adds a little extra to the program

	  System.out.printf("Inventory Program Part 3\n");

	  System.out.printf("This program will inventory DVD collection.\n");

	  System.out.printf("Please enter the needed information.\n");
		  // prompt for user input


	 float item; // item number
	  float price; // item price
	  float stock; // quantity
	  String title; // item name
	 float year; // item year
	  float value; // total for item



	  System.out.println();

		// prompt for and input DVD information
		  System.out.println("Enter the DVD Titel or STOP to end the program:");
		  // user input DVD title
		  title = input.nextLine ();

	   //Loops the program until stop is entered
	   while ( !title.equals( "STOP" )) //while ( !DVDTitle.equals("stop"))
	  {
	 System.out.println( "Enter Item Number: "); // prompt
	 item = input.nextInt(); // read entered hourly pay rate entered by user

		  System.out.println("Enter Units in Stock:");
		  // user input Quantity in Stock
		  stock = input.nextInt ();

		  System.out.println("Enter the Year per Unit:");
		  // user input Unit Price
		  year = input.nextInt();

		 System.out.println("Enter Price per Unit:");
		  // user input Unit Price
		  price = input.nextInt();




	//Calculation to get Inventory Value
		   value = price * stock;

		  DVD myDVD = new DVD(title,item,stock,year,price);


		  System.out.println();

	  System.out.println();
System.out.println("____________________________________________________");

System.out.printf("DVD Title:  %s\n", myDVD.getDvdTitle());
System.out.printf("Item Number: %s\n", myDVD.getDvdItem());
System.out.printf("Quantity:  %s\n", myDVD.getDvdStock());
System.out.printf("Item Year: %s\n", myDVD.getDvdYear());
System.out.printf("Unit Price:  $%.2f\n", myDVD.getDvdPrice());
System.out.printf("Inventory  Value: $%.2f", myDVD.getValue());


	  System.out.println("");

	  System.out.println();

	  // prompt for and input Employee's name
	  System.out.println( "Enter the DVD Titel or STOP to end the program: " );
		title = input.next(); //read line of text


			}//end main
   }//end method main
}//end class


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inventoryprogrampart3;
import java.text.DecimalFormat;
import java.io.*;
/**
 *ckpoint: Inventory Program Part 3
 * Inventory3.java
 * This program will stores DVD collection
 * @author Kay C
 */
public class Inventory {
 // Setup an array of Products that holds 25 items
	int inventorySize = 25;
	String printInventory = "default";



	private DVD items[] = new DVD[inventorySize];

	// format values into currency form.
	DecimalFormat formatter = new DecimalFormat("$##,###.00");
	private int InventorySize;


	// Adds a product to the array of products
	public void addDVD(DVD item) {
		for (int i = 0; i < inventorySize; i++)
			if (items[i] == null) {
				items[i] = item;
				return;
			}
	}

	public double getTotalValue() // calculation for total inventory value
   {
	   double totalValue = 0.0;
	   for (DVD item : items)
	   {
		   if (item != null){
			   totalValue += item.getValue();
		   }
		  }
	return totalValue;

	}
		public void printInventory() { // Prints Inventory list

	   System.out.println("Printing items in inventory...\n");

		boolean hasItems = false;

		for (DVD item : items) {
			if (item!= null) {
				hasItems = true;
			}
				System.out.println("Inventory is currently empty.\n");

		System.out.println( "Thank you for using this Inventory Program" );
		}
	}
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package inventoryprogrampart3;
/**
 *Checkpoint: Inventory Program Part 3
 * Inventory3.java
 * This program will stores DVD collection
 * @author Kay C
 */
public class DVD {

	//DVD myDVD = new DVD(title,item,stock,price);
	 public DVD(){
		 DVD myDVD1 = new DVD("Cars", 1, 5, 12.99, 2006);
		 DVD myDVD2 = new DVD("Jeff Dunham - Spark of Insanity", 2, 8, 15.99, 2007);
		 DVD myDVD3 = new DVD("Jeff Dunham - Arguing with Myself", 3, 4, 16.99, 2007);
		 DVD myDVD4 = new DVD("The Lake House", 4, 9, 19.99, 2006);
		 DVD myDVD5 = new DVD("Honey", 5, 2, 10.99, 2003);
	 }

	private String dvdTitle;
	private double dvdItem;
	private double dvdStock;
	private double dvdPrice;
	private int dvdYear;
	private double getValue;
	private double ReStockingFee;

	// Default constructor uses other constructor to set the
	// default state of the object.
	 String getDvdTitle = "default";
	 double item = 0.0;
	 double stock = 0.0;
	 double price = 0.0;
	 double year = 0.0;
	double value = 0.0;
	double Restockingfee = 0.5;



	// Constructor that user can specify a name, quantity, and price for for items.
	 public DVD(String title, double item, double stock, double year, double price) {
	  dvdTitle = title;
	  dvdItem = item;
	  dvdStock = stock;
	  dvdYear = (int) year;
	  dvdPrice = price;

	}   // end class DVD constructor

	  public void dvdStock(int Stock) {
		if (Stock > 0) {
			dvdStock = Stock;
		}
		else { dvdStock = 0; }
	}

	// Set price of a product and defaults it to zero if negative.
	public void setPrice(double itemPrice) {
		if (itemPrice > 0.00) {
			price = itemPrice;
		}
		else { price = 0.00; }
	}

	// return DVD name
   public String getDvdTitle(){
	 return dvdTitle;
   }
	 // set DVD name
	public void setDvdTitle(String title){
	 this.dvdTitle = title;
   }
	// return item number
	public double getDvdItem(){
	return dvdItem;

   }
   // set item number
   public void setDvdItem(double item){
	this.dvdItem = item;
   }
   // return number of units in stock
	public double getDvdStock(){
	 return dvdStock;
	 }

   // set number of units in stock
   public void setDvdStock(double stock){
	 this.dvdStock = stock;
   }
   // return number of units in stock
	public double getDvdYear(){
	 return dvdYear;
	 }

   // set number of units in stock
   public void setDvdYear(int year){
	 this.dvdYear = year;
   }
	// return price of each unit
   public double getDvdPrice(){
	 return dvdPrice;

   }

   // set price of each unit
   public void setDvdPrice (double price){
	 this.dvdPrice = price;
   }
	  // calculate inventory value
   public double getValue()
   {
	  double Value = dvdPrice * (double)dvdStock;
	   return Value;
		}

	   public double getReStockingFee(){
	   double RestockingFee = value * .05;
	   return RestockingFee;

   } // end method inventoryValue

}  // end class DVD




I appreciate everones help. Im getting pretty frustrated because my teacher is not really helping me with this. already contacted him twice and still no answer.
thanks

This post has been edited by kayc_1980: 01 March 2009 - 07:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Inventory program

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5294
  • View blog
  • Posts: 14,132
  • Joined: 18-April 07

Re: Inventory program

Posted 01 March 2009 - 10:27 AM

Well the reason it doesn't call the other classes is because you never define an instance of those classes. Your main class should have a member variable like...

// Create an instance of Inventory called myInventory
Inventory myInventory = new Inventory();

// Now that I have an inventory, lets create some products and put them
// into the inventory...

DVD goldeneye = new DVD("GoldenEye", 1.0, 23.0, 19.95);
DVD fargo = new DVD("Fargo", 1.2, 29.0, 16.95);
DVD nemo = new DVD("Finding Nemo", 3.0, 3.0, 29.95);

// Now add them
myInventory.addDVD(goldeneye);
myInventory.addDVD(fargo);
myInventory.addDVD(nemo);

// Get total inventory value
double total = myInventory.getTotalValue();

// Print it...
System.out.println("Total inventory value is: " + total);



So make sure that you declare an instance of the inventory class, that you use that instance to then call functions of that class and you should be well on your way to getting this up and running.

Be sure to check out my blog where I run through the first three parts of this project and provide all the code...

Martyr2's Programming Underground - Parts 1-3 of Product/Inventory Project

Enjoy!

"At DIC we be inventory loving code ninjas... just look at our inventory of hearts, valentines day candy, flowers. Can't you feel how much we love our inventory?" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1