I am a student working on an inventory app. Until this current assignment I felt I half way knew what I was doing. What is required is the following:
~Modify the program to handle multiple items: should work with this code added - it worked on another program

//Prompt user for number of employees
		System.out.print("Number of employees: ");
		int numEmployees = stdIn.nextInt();

		//Create employee array to store data
		Employee[] emp = new Employee[numEmployees];

~ Use an array to store the data - again should work with the addition of preceding code

~ Store and print out the input data for each item (name, product number, on-hand quantity, price, total price of that item type)one item at a time need help here

~Calculate the total vale of items on hand - (general concept is total values * numProducts) how to accomplish this I need help

~Sort the array by title - again big help needed

Here is the code I have to this point

// inventory app program
import java.util.Scanner;

public class InventoryApp {

	// main begin Java application
	public static void main (String[] args) 
	{
		//initialize keyboard
		Scanner input = new Scanner(System.in);

		String name;
		String item;
		int number;
		double price;


		System.out.println("Please enter the DVD Title:");
		dvd.setName(input.nextLine());
		
		
		System.out.print ("Enter DVD iten number"); //prompt
		dvd.setItem(input.next());
		System.out.println();


		System.out.print ("Enter number of DVD's on hand: ");
		dvd.setNumber(input.nextInt());
		System.out.println();

		System.out.print ("Enter the Retail Price for the DVD: ");
		dvd.setPrice(input.nextDouble());
		System.out.println();
		
		System.out.printf("%s's Total Retail Value is: $%.2f\n",
		dvd.getName(), dvd.getTotal ());
		System.out.println();
		System.out.println();


	} // end main

}//end class InventoryApp

// DvdCollection class

/* •	Create a product class that holds the item number, 
the name of the product, the number 
of units in stock, and the price of each unit.*/ 

// Class DvdCollection
public abstract class DvdCollection
{
private String name;
private String item;
private int number;
private double price;
public double total;
public double grandtotal;

public DvdCollection ()
	{
		//implicit call to object constructor occurs here
		name = "Title";
		item = "123456";
		number = 0;
		price = 2.99;
	}

// Four argument initialization constructor


public DvdCollection(String dvdnameIn, String itemnumberIn, int numberonhandIn,
					double retailpriceIn)
	{
		//implicit call to object constructor occurs here
		name = dvdnameIn;
		item = itemnumberIn;
		number = numberonhandIn;
		price = retailpriceIn;
	} // end four argument intialization constructor

public void setName (String name) 
	{
		this.name = name;
	}

public String getName ()
	{
		return name;
	}

public void setItem (String item) 
	{
		this.item = item;
	}

public double getItem () 
	{
		return item;
	}

public void setNumber (int number) 
	{
		this.number = number;
	}

public int getnumber () 
	{
		return number;
	}

public void setPrice(double price) 
	{
		this.price = price;
	}

public double getPrice()
	{
		return price;
	}

public double getTotal () 
	{
		return price * number;
	}
} // end class DvdCollection

Thanks

You have a class called DVDCollection but each instance actually represents a single DVD. This sounds a suble difference but it's very inportant.
When you say new DVDCollection =(...) you are NOT creating a new collection of DVDs, you are creating a single DVD. That's the right thing to do, but after that you need smething else to hold your collection of these new DVD objects.
So change your class to make it clear that it's a single DVD then create an array to hold a number of DVD instances.

Ok, here is what I have come up with so far.
my String toString is not working, it compiles but then creates a crash /sigh
and i have no idea how to add the all the getTotal's (one per array object) to create a total inventory value

// Inventory App part 2
//

import java.util.*;
import java.text.*;
import java.io.*;


public class InventoryApp2 {

public static void main(String args []) 
{
Scanner input = new Scanner(System.in);

		// Set the size of the array based on user input
		System.out.println( "Please enter the number of DVD's to record: " );
		int numDVD = input.nextInt();

		//Create employee array to store data
		DVD[] dvd = new DVD[numDVD];
		
			//Create a new employee for the number of employees entered by user
		for (int i=0; i<numDVD; i++)
		{
					DVD.load(dvd, i);
		}
		
		//Print employee data
		for (int i=0; i<numDVD; i++)
		{
			System.out.println(dvd[i].toString());
		}
} //end main

} // end class Inventory2

and

// Inventory App DVD Class

import java.util.*;
import java.text.*;
import java.io.*;

public abstract class DVD 
{
	private int dvdItem;
	private String dvdTitle;
	private int dvdStock;
	private double dvdPrice;
	private double dvdValue;

	public DVD(int item, String title, int stock, double price, double value) 
	{
	dvdItem = item;
	dvdTitle = title;
	dvdStock = stock;
	dvdPrice = price;
	dvdValue = value;
	
	} //end four-argument constructor

	public static void load(DVD[] dvd, int i)
	{
		Scanner input = new Scanner(System.in);
		
		// input the DVD Title
		System.out.println( "Please enter the Title for DVD # " + (i + 1) +": " );
		String dvdTitle = input.nextLine();
		
		// Input the DVD's item number
		System.out.println( "Please enter the item number for \nDVD # " + (i + 1) +": " );
		int dvdItem = input.nextInt();
		
		// Input the DVD's quantity on hand
		System.out.println( "Please enter the quantity on hand for DVD # " + (i + 1) +": " );
		int dvdStock = input.nextInt();
		
		// Input the Price for the DVD
		System.out.println( "Please enter the price for DVD # " + (i + 1) +": " );
		double dvdPrice = input.nextDouble();
	}

	// set DVD Item
	public void setDvdItem(int item) {
	this.dvdItem = item;
	} //end method set Dvd Item

	//return DVD Item
	public int getDvdItem() {
	return dvdItem;
	} //end method get Dvd Item

	//set DVD Title
	public void setDvdTitle(String title) {
	this.dvdTitle = title;
	} //end method set Dvd Title

	//return Dvd Title
	public String getDvdTitle() {
	return dvdTitle;
	} //end method get Dvd Title

	public void setDvdStock(int stock) {
	this.dvdStock = stock;
	} //end method set Dvd Stock

	//return dvd Stock
	public int getDvdStock() {
	return dvdStock;
	} //end method get Dvd Stock

	public void setDvdPrice(double price) {
	this.dvdPrice = price;
	} //end method setdvdPrice

	//return DVD Price
	public double getDvdPrice() {
	return dvdPrice;
	} //end method get Dvd Price
	
	public double getTotal(){
	return dvdPrice*dvdStock;
	}

	/* set value
	public void setDvdValue(double value)
	{
		dvdValue = value;
	}
	
	//calculate inventory value
	public double getDvdValue() {
	return value;
	} //end method value */

	public String toString() 
	{

		return String.format("item=%3d title=%-20s units=%d price=%.2f value=%.2f",
		dvdItem, dvdTitle, dvdStock, dvdPrice, dvdValue);
	}

} //end class DVD

Here are the related sections of code and the compile error(s)

public double getTotal(){
	return dvdPrice*dvdStock;
	}

	/* set value
	public void setDvdValue(double value)
	{
		dvdValue = value;
	}
	
	//calculate inventory value
	public double getDvdValue() {
	return value;
	} //end method value */

	public String toString() 
	{

		return String.format("item=%3d title=%-20s units=%d price=%.2f value=%.2f",
		dvdItem, dvdTitle, dvdStock, dvdPrice, getTotal);
	}

} //end class DVD

and

//Print employee data
		for (int i=0; i<numDVD; i++)
		{
			System.out.println(dvd[i].toString());
		}
} //end main

} // end class Inventory2

The red is error line

the compile error is :

InventoryApp2.java:31: cannot find symbol
symbol : variable DVD
location: class InventoryApp2
System.out.println(DVD.toString());
1 error

It seems that by changing DVD to dvd allows for it to compile correctly
When the toString method is called for instead of producing a nice block of output I get
Exception in thread "main" java.lang.NullPointerException at InventoryApp2.main(InventoryApp2.java:31: )

Edited 6 Years Ago by Guest51: n/a

NullPointerException at InventoryApp2.main(InventoryApp2.java:31: )

What object is null at line 31 in InventoryApp2?

I do not have a a clue, sorry
line 31 reads
System.out.println( dvd.toString());

Here is my method ~ toString that resides in Class DVD

public String toString( String title, int item, int stock, double price ) 
	{

		return ("\n" + "Title: " + dvdTitle + "\n"
			+ "Stock Number: " + dvdItem + "\n"
			+ "Quantity on hand: " + dvdStock +"\n" 
			+ "Price per unit: " + dvdPrice + "\n\n");
	}

and here is my method call for that method that lies in Class InventoryApp2

for (int i=0; i<numDVD; i++)
		{
		
			System.out.println(dvd.toString);
		}

My newest error is that the compiler cannot find the varriable toString, so this same agonizing line of code has a new glitch

the line called is : System.out.println(dvd.toString());
or : System.out.println(dvd.toString());
both varriations generate the same error message, cannot fing the varriable toString

OK well I got it to something
the method written like this

public String toString( String title, int item, int stock, double price ) 
	{

		return ("\n" + "Title: " + title + "\n"
			+ "Stock Number: " + item + "\n"
			+ "Quantity on hand: " + stock +"\n" 
			+ "Price per unit: " + price + "\n\n");
	}

and the call when written like this

for (int i=0; i<numDVD; i++)
		{
		
			System.out.println(dvd.toString());
		}

produced this result:
[LDVD;@14318bb

when this input was supplied
number of DVD's to input = 1
Title = "Rest" (w/o the "")
Item number = 123
quantity on hand = 1
price = 9.99

Depending on my input values I have received different variations of these results
all remaining consistent with the [LDVD;@

Edited 6 Years Ago by Guest51: n/a

You just overload your new toString() method, not override. The toString() method does NOT take any argument. That's why you get the default value of toString(), the method you did not override.

Also, any variable used inside toString() should comes from your own class variables. I see that you have private variables - dvdItem, dvdTitle, dvdStock, dvdPrice, and dvdValue - use them.

// sample for toString()
public String toString() {
  return ("\n" + "Title: " + this.dvdTitle + "\n"
          + "Stock Number: " + this.dvdItem + "\n"
          + "Quantity on hand: " + this.dvdStock +"\n" 
          + "Price per unit: " + this.dvdPrice + "\n\n");
}

Another problem is that your first index of 'dvd' will be null because your load start from 1! Why's that? And you should not implement load() inside DVD because it has nothing to do with your class. You could, instead, implement constructor that takes these value to create a DVD object. Anyway, I should not comment any further because it is your style. Just my 2 cents...

You bring many good points to my attention, Thank You,
First - is load a special word ~ I simply used it to name my method where I "loaded" the values from the user input, I can change it to something else if it should be.
Second ~ I finally figured out what you were talking about with the "null" thing, I have set my default values at 0(zero) and have filled my array starting at the second slot (1) by using the (i+1) statement in the data gathering process..... How can I change that to work?
Third ~ in one of my earlier versions I did open with a default value array and use a constructor with it, ( at least that is what I think I was doing /shrug) the code for which is :

public DvdCollection ()
	{
		//implicit call to object constructor occurs here
		name = "Title";
		item = "123456";
		number = 0;
		price = 2.99;
	}

	// Four argument initialization constructor


	public DvdCollection(String dvdnameIn, String itemnumberIn, int numberonhandIn, 
	double retailpriceIn) 
	{ //implicit call to object constructor occurs here
		name = dvdnameIn;
		item = itemnumberIn;
		number = numberonhandIn;
		price = retailpriceIn;
	} // end four argument intialization constructor

Would this be a better way to set things up, if so how would I integrated it into what I already have?
Thanks in advance

I think you are going the right way, don't go back. Doing the load like that is not "wrong", but in a real-life system you probably wouldn't do it like that - you would separate the user I/O into another class, but for your current project I wouldn't worry about that.

This article has been dead for over six months. Start a new discussion instead.