Question

I'm 5 weeks into my first intro to java course, and I'm stuck. I am supposed to make an inventory program. It's supposed to let the user input several things, including name, product ID, units, and price. It then is supposed to output that information including a total value, i.e units * price.

I created a class, built a constructor, and created a toString method, but it seems that these things aren't being called into the main method, and for the life of me I can't figure out what I'm missing.

I have no idea how to get these things to actually work. I've been searching for hours looking for what I'm missing, and I think I need a fresh perspective.

public class Inventoryprogram {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    boolean finish = false;
    String dvdName;
    int itemNum;
    int quantity;
    double price;

    DVD dvd;

    while (!finish){
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter name of DVD: ");
        dvdName = input.nextLine();

        if (dvdName.equals("stop")) {
            System.out.println("Exiting Program");
            break;
        }
        else {
            System.out.print("Please enter Product Number: ");
            itemNum = input.nextInt();
            System.out.print("Please enter units: ");
            quantity = input.nextInt();
            System.out.print("Please enter price of DVD: ");
            price = input.nextDouble();

            System.out.println("DVD: " + dvdName);
            System.out.println("ID: " + itemNum);
            System.out.println("Units: " + quantity);
        }
    }
}

public class DVD {

private String name;
private int id;
private int items;
private double cost;

//default constructor
public DVD() {
    name = "";
    id = 0;
    items = 0;
    cost = 0.0;
}//end default constructor

//constructor to initialize object
public DVD(String dvdName, int itemNum, int quantity, double price) {
    dvdName = name;
    itemNum = id;
    quantity = items;
    price = cost;
}//end constructor


//method to calculate value
public double getInventoryValue() {
       return items * cost;
}

//method to set name
public void setdvdName(String dvdName){
    this.name = dvdName;
}

//method to get name
public String getName(){
    return name;
}

//method to set id
public void setitemNum( int itemNum){
    this.id = itemNum;
}

//method to get id
public int getId(){
    return id;
}

//method to set items
public void setquantity(int quantity){
    this.items = quantity;   
}

//method to get items
public int getItems(){
    return items;
}

//method to set cost
public void setprice( double price){
    this.cost = price;
}

//method to get cost
public double getCost(){
    return cost;
}



public String toString(String getName, int getID, int getItems, double getCost, double getInventoryValue) {

    return "DVD Name: " + getName +
           "ID: " + getID +
           "Items: " + getItems +
           "Cost: " + getCost + 
           "Total Value: " +getInventoryValue;
    }
}

}
Was it helpful?

Solution

To create a new instance using your class above, you would write the following:

DVD dvd = new DVD(dvdName, itemNum, quantity, price);

Any time you want to work with an instance of an object as opposed to static methods (which is most of the time) that's what you'd do. It's just like calling a method in a class, except that you're calling the constructor via the 'new keyword to create a new instance.


EDIT to address comment If you're getting errors stating that your values aren't initialized, and declaring your inner class as static resolves it, it means you're referencing the inner class without a creating an instance of the outer class.

For syntax of how to instantiate an inner class using an instance of your outer class, you want:

OuterClass.InnerClass innr = new OuterClass().new InnerClass();

That said - in most cases (not all, but really almost all cases) if you're referencing an inner class from another class, you would be better off making the inner class into it's own separate full-fledged class in it's own .java file.

OTHER TIPS

In Java, constructors are called exactly and only when the new keyword is used. If you want to make a DVD object, you'll need to have new DVD somewhere in your code, followed by a parameter list that will comprise the constructor's arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top