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 in the text to ensure your source code is readable and well
documented.
I'm having trouble with the code to print the value of the inventory. Please assist.
///MICHAEL -- See below...
// Author Michael Anderson
// IT 215 Java Programming April 27, 2008
// Inventory1.java
// Inventory program that displays the value of inventory and number of units in stock.
///MICHAEL - This is public class Inventory1. It MUST be in a file named Inventory1.java
/// NOTE the capitalization...
public class Inventory1 {
///MICHAEL - I have straightened out your indentation
/// I also corrected some typos re DVD, dvd below... System
public static void main(String[] args) {
DVD dvd;
dvd = new DVD(11, "American Gangster", 1, 19.99);
System.out.println(dvd);
dvd = new DVD(21, "Scarface", 5, 14.95);
System.out.println(dvd);
dvd = new DVD(13, "Donnie Brasco", 3, 13.50);
System.out.println(dvd);
dvd = new DVD(41, "The Godfather", 10, 29.99);
System.out.println(dvd);
} // end main
} // end class Inventory1
class DVD {
private int itemNum;
private String dvdTitle;
private int numInStock;
private double dvdPrice;
///MICHAEL - This is the DEFAULT constructor...
public DVD () {
itemNum = 0;
dvdTitle = "";
numInStock = 0;
dvdPrice = 0;
}// end default constructor
// 4-argument constructor
DVD(int itemNum, String dvdTitle, int numInStock, double dvdPrice) {
this.itemNum = itemNum;
this.dvdTitle = dvdTitle;
this.numInStock = numInStock;
this.dvdPrice = dvdPrice;
}//end constructor
public int getItemNum() {
return itemNum;
}
public String dvdTitle() {
return dvdTitle;
}
public int numInStock() {
return numInStock;
}
public double dvdPrice() {
return dvdPrice;
}
public double valueOfInventory() {
return numInStock * dvdPrice;
}
///MICHAEL - I added this for you...
/// It's incomplete... YOU need to complete it...
public String toString() {
return String.format("Num=%3d Title=%-20s Value=$%7.2f",
itemNum, dvdTitle, valueOfInventory());
}
} // end class DVD