Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Inventory program - using an array

807601May 5 2008 — edited May 6 2008
Help, I'm stuck... normally I try to write the code and run it first before I ask for help, but I don't even know where to begin with this one.

For our Java class we have to make an inventory program (I chose DVD's) in phases, the second phase requires it to handle multiple items. Specifically it says to use an array to store them, the output displaying product information one at a time (there's the title, item number, number of units in stock, and price per unit) and it should also display the value of the entire inventory by using a method to calculate it, and another method to sort the array items by name.

We have a textbook but it's not very helpful, especially for newbies. There is a chapter that covers arrays but I don't fully understand how to create one relevant to the project at hand, the examples don't make it clear. I don't know if I'm supposed to create a new class, or where to put it in my code, etc. Another issue I have is my program is supposed to prompt the user to enter the information it needs to work with and then it gives feedback, with all the examples I've seen in the textbook and elsewhere the information is already there, if that makes any sense. I think since the each items have different sets of information (name, price, etc) I need to have a two-dimensional array, I don't know how to isolate the "price" information and have a method calculate just that.

Anyway, this is what I already have, there is a main application and a separate product class:
// DVD class holds product information

class DVD
{

   // instance fields
   private String dvdTitle;
   private double dvdItem;
   private double dvdStock;
   private double dvdPrice;

   public DVD(String title, double stock, double price, double item)
   {
   
      dvdTitle = title;
      dvdItem = item;
      dvdStock = stock;
      dvdPrice = price;
   } // end class DVD constructor

   // set DVD name
   public void setDvdTitle(String title)
   {
      dvdTitle = title;
   } // end method setDvdTitle

   // return DVD name
   public String getDvdTitle()
   {
      return dvdTitle;
   } // end method getDvdTitle

   // set item number
   public void setDvdItem(double item)
   {
      dvdItem = item;
   } // end method setDvdItem

   // return item number
   public double getDvdItem()
   {
      return dvdItem;
   } // end method getDvdItem

   // set number of units in stock
   public void setDvdStock(double stock)
   {
      dvdStock = stock;
   } // end method setDvdStock

   // return number of units in stock
   public double getDvdStock()
   {
      return dvdStock;
   } // end method getDvdStock

   // set price of each unit
   public void setDvdPrice (double price)
   {
      dvdPrice = price;
   } // end method setDvdPrice

   // return price of each unit
   public double getDvdPrice()
   {
      return dvdPrice;
   } // end method getDvdPrice

   // calculate inventory value
   public double inventoryValue()
   {
      return dvdPrice * dvdStock;
   } // end method inventoryValue

} // end class DVD
// Inventory Program Part 1
// DVD object used in application
import java.util.Scanner; // program uses class Scanner

public class Inventory1
{

   // main method begins execution of Java application
   public static void main(String args [])
   {
      // create and initilize a DVD object

      String title = "";
      double item = 0.0;
      double stock = 0.0;
      double price = 0.0;
      DVD dvd = new DVD(title, item, stock, price); // invokes DVD constructor

      // create Scanner to obtain input from command window
      Scanner input = new Scanner( System.in );

      System.out.print(
         "Enter product name, item number, number of units in stock, and price of each unit, all separated by spaces: " ); //prompt for user input

      System.out.println(dvd);

      System.out.println("DVD Title is " + dvd.getDvdTitle());
      System.out.println("The item number is " + dvd.getDvdPrice());
      System.out.println("The number of units in stock is " + dvd.getDvdStock());
      System.out.println("The price of each unit is " + dvd.getDvdPrice());
      System.out.println("The inventory value is " + dvd.inventoryValue());

   } // end method main

} // end class Inventory1
I'm not even sure if what I have to start with is okay, any guidance would be greatly appreciated.

For the record I am NOT asking anyone to do my homework, I just don't even know where or how to begin this time. Even though I've done a few assignments I'm still at total newbie, so bear with me.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 3 2008
Added on May 5 2008
8 comments
1,250 views