Here is my assignment:Choose a product that lends itself to an inventory (for example, products at your
workplace, office supplies, music CDs, DVD movies, or software).
• 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.
• Create a Java application that displays the product number, the name of the product, the
number of units in stock, the price of each unit, and the value of the inventory (the
number of units in stock multiplied by the price of each unit). Pay attention to the good
programming practices
Here is what i have so far:

package javaapplication24;

import java.util.Arrays;

class inventoryList
{
public static void main( String args[] )
{
final int arrayLength=5;
int dvdNumber[] = new int[arrayLength];
String dvdName[] = new String[] {"Sweet Home Alabama", "How to Loose a Guy in 10 Days", 
"Ps I love You", "Freedom Writters", "Hannah Montana"};
int dvdUnits[] = {5, 3, 2, 2, 3};
double dvdPrice[] = {19.95, 19.95, 15.95, 15.95, 19.95};
double dvdValue[] = new double[arrayLength];

sortcdName(dvdName);
for(int counter = 0; counter < dvdNumber.length; counter++)

System.out.println("\nDVD Item:"+(dvdNumber[counter]=1+counter)+
"\nDVD Name:"+dvdName[counter]+
"\nNumber of Units:"+dvdUnits[counter]+
"\nPrice per DVD:\u0024"+dvdPrice[counter]+
"\nCD Value:\u0024"+(dvdValue[counter]=dvdUnits[counter]*dvdPrice[counter]));

outputTotalValue(dvdValue);
}
public static void outputTotalValue(double cdValue[])
{
double totalDVDValue = 0;

for(int counter = 0; counter<dvdValue.length; counter++)
totalDVDValue += dvdValue[counter];

System.out.printf( "\nTotal Value of all Stock: \u0024"+totalDVDValue+"\n\n" );

}
public static void sortcdName( String dvdName[])
{
Arrays.sort(dvdName);

}

Recommended Answers

All 8 Replies

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.

int dvdNumber[] = new int[arrayLength];
String dvdName[] = new String[] {"Sweet Home Alabama", "How to Loose a Guy in 10 Days", 
"Ps I love You", "Freedom Writters", "Hannah Montana"};
int dvdUnits[] = {5, 3, 2, 2, 3};
double dvdPrice[] = {19.95, 19.95, 15.95, 15.95, 19.95};
double dvdValue[] = new double[arrayLength];

The idea of OOP is to use objects. You should create one class named DVD with attributes:
dvdNumber, dvdName, dvdUnits, dvdPrice, dvdValue. Then have methods inside the class that manipulate the above values.
ex: When you change the dvdUnits for one instance the dvdValue should change automatically:

public void setDvdUnits(int units) {
dvdUnits = units;
dvdValue = dvdUnits * dvdPrice;
}

Then have an array of DVDs:

DVD dvds = new DVD[5]; 
// this is an [B]array[/B]
//the dvds array is NOT null [B]but[/B]
//the dvds[0], dvds[1], ... , DVD objects ARE null

dvds[0] = new DVD(1, "Name of the dvd", 3, 25.00); // inside the constructor calculate the 
//dvdValue = dvdUnits * dvdPrice; (3*25.00)

for (int i=0;i<dvds.length;i++) {
System.out.println(dvds[i].getDvdNumber() +" "+dvds[i].getDvdName() +" "+ .......);
}

So my code is all wrong? I am a bit lost on what you are saying. I am not doing very well in this class, though i want to understand it it is not looking to promising.

It is not wrong, but instead of all those arrays use one that will contain objects. And each object will have the attributes I told you. Use the example I wrote.

Here is my second attempt:
and my current error message:
init:
deps-jar:
Compiling 1 source file to C:\NetBeansProjects\SCHOOL1\JavaApplication25\build\classes
C:\NetBeansProjects\SCHOOL1\JavaApplication25\src\javaapplication25\FGInventoryProgram1.java:142: reached end of file while parsing
{

package javaapplication25;



public class FGInventoryProgram1{
    



    public static void main(String args []) {
        
       DVD dvd;
        
                        
        dvd = new DVD("How to Loose a Guy in 10 days", 4, 19.99, 685);
        System.out.println(dvd);
        System.out.println("Product Title is " + dvd.getDvdTitle());
        System.out.println("The number of units in stock is" + dvd.getDvdStock());
        System.out.println("The price of each DVD is" + dvd.getDvdPrice());
        System.out.println("The item number is " + dvd.getDvdItem());
        System.out.println("The value of the inventory is" + dvd.value());
    
        
        dvd = new DVD ("Cyote Ugly", 8, 19.98, 565);
        System.out.println(dvd);
        System.out.println("Product Title is " + dvd.getDvdTitle());
        System.out.println("The number of units in stock is" + dvd.getDvdStock());
        System.out.println("The price of each DVD is" + dvd.getDvdPrice());
        System.out.println("The item number is " + dvd.getDvdItem());
        System.out.println("The value of the inventory is" + dvd.value());
    
        
        dvd = new DVD ("Legally Blonde", 10, 19.99,785);
        System.out.println(dvd);
        System.out.println("Product Title is " + dvd.getDvdTitle());
        System.out.println("The number of units in stock is" + dvd.getDvdStock());
        System.out.println("The price of each DVD is" + dvd.getDvdPrice());
        System.out.println("The item number is " + dvd.getDvdItem());
        System.out.println("The value of the inventory is" + dvd.value());
    
        
        dvd = new DVD ("Sweet Home Alabama",8,18.56,578);
        System.out.println(dvd);
        
        System.out.println("Product Title is " + dvd.getDvdTitle());
        System.out.println("The number of units in stock is" + dvd.getDvdStock());
        System.out.println("The price of each DVD is" + dvd.getDvdPrice());
        System.out.println("The item number is " + dvd.getDvdItem());
        System.out.println("The value of the inventory is" + dvd.value());
    
    }//end class main
        

} // end class FCG_Inventory1

class InventoryList{
    
   private String name;
    private int nbInStock;
    private double price;
    private int itemNumber;
   
  InventoryList(String pName, int nb, double priceEach, int itemNum){
      itemNumber = itemNum;
      name = pName;
      nbInStock = nb;
      price = priceEach;
   
  

class DVD {
    private String dvdTitle;
    private double dvdStock;
    private double dvdPrice;
    private double dvdItem;

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

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

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

    //set DVD Stock
    public void setDvdStock(double stock) {
        dvdStock = stock;
    } //end method setDvdStock

    //return DvdStock
    public double getDvdStock() {
        return dvdStock;
    } //end method get Dvdstock

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

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

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

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

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


class Product{
  
int dvdNumber[] = new int[arrayLength];
String dvdName[] = new String[] {"Sweet Home Alabama", "How to Loose a Guy in 10 Days",
"Ps I love You", "Freedom Writters", "Hannah Montana"};
int dvdUnits[] = {5, 3, 2, 2, 3};
double dvdPrice[] = {19.95, 19.95, 15.95, 15.95, 19.95};
double dvdValue[] = new double[arrayLength];
  }//end method
} //end class Product

Post only the code for the: FGInventoryProgram1.java and mark somehow line: 142 where you get the error. Don't expect us to count 142 lines to find where the errors.

And in general:
Keep only the DVD class and the FGInventoryProgram1 class. The other code is not necessary. The above classes should be in 2 different files.
If you have only those two classes your program should work. Try to make these two work before implementing the rest of my suggestions:

The code in FGInventoryProgram1 is correct from a certain point of view, but it is not what OOP is all about. Each time that you create a "new DVD(.....)" you "delete" the values of the previous. At the end of your program dvd has these values: dvd = new DVD ("Sweet Home Alabama",8,18.56,578)
Of course for what you are asked this is not wrong, but later you might you want to write a program where you want to access some of the previous created "dvds".

Again here is the same example:

DVD [] dvds = new DVD[5];
//dvds is an array, it is NOT null, but its elements: dvds[0], dvds[1], dvds[2], ... are null

//Its elements (dvds[0], dvds[1], dvds[2], ...) are DVD objects: 
dvds[0] = new DVD("How to Loose a Guy in 10 days", 4, 19.99, 685);
dvds[1] = new . . . . . . .
//you instantiated them 

for (int i=0;i<dvds.length;i++) {  //dvds is an array so you can use .length
  
        //dvds[i] is a DVD object so you can use: getDvdTitle(), getDvdStock(), ...
     
        System.out.println("Product Title is " + dvds[i].getDvdTitle());
        System.out.println("The number of units in stock is" + dvds[i].getDvdStock());
        System.out.println("The price of each DVD is" + dvds[i].getDvdPrice());
        System.out.println("The item number is " + dvds[i].getDvdItem());
        System.out.println("The value of the inventory is" + dvds[i].value());
}

have u made the project?
plz send me one for i m in urgent need.

commented: Do not ask for completed homework projects. -2

have u made the project?
plz send me one for i m in urgent need.

You are in urgent need of learning to do your own work. This forum is not for completing your homework for you.

I need a pseudo code using EOF FUNCTION for inventory and part number, part name, and quantity

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.