1. CheckPoint: Inventory Program Part 2
• Resource: Java: How to Program
• Due Date: Day 4 [Individual] forum
• Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
• Create a method to calculate the value of the entire inventory.
• Create another method to sort the array items by the name of the product.
• Post as an attachment
Here is what I have so far. The second part is giving me problems as I am having alot of confusion in my understanding of java so far.
// Week 6 Checkpoint 1 - Inventory2
// Richard Fondy
// IT/215 - Java Programming
// Inventory2.java
// December 18th, 2008
import java.util.Arrays;
public class Inventory2 {
public static void main(String[] args) {
DVD dvd;
System.out.println("\nCheckPoint: Inventory Part 1\n");
dvd = new DVD(01, "No Country For Old Men", 1, 10.49f);
System.out.println(dvd);
dvd = new DVD(14, "Cloverfield", 2, 9.99f);
System.out.println(dvd);
dvd = new DVD(36, "Spiderman 3", 10, 6.89f);
System.out.println(dvd);
dvd = new DVD(04, "The Mist", 6, 5.75f);
System.out.println(dvd);
System.out.println("\nEnd of Inventory, have a good day!\n");
} //end main
} // end class Inventory2
class DVD {
private int dvdItem;
private String dvdTitle;
private int dvdStock;
private double dvdPrice;
public DVD(int item, String title, int stock, double price) {
dvdItem = item;
dvdTitle = title;
dvdStock = stock;
dvdPrice = price;
} //end four-argument constructor
// set DVD Item
public void setDvdItem(int item) {
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) {
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) {
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) {
dvdPrice = price;
} //end method setdvdPrice
//return DVD Price
public double getDvdPrice() {
return dvdPrice;
} //end method get Dvd Price
//calculate inventory value
public double value() {
return dvdPrice * dvdStock;
} //end method value
public String toString() {
return String.format("item=%03d title=%-23s units=%3d price= $%6.2f value= $%7.2f",
dvdItem, dvdTitle, dvdStock, dvdPrice, value());
}
} //end class DVD
class Inventory {
private DVD[] dvds; ///the DVD collection
private int numDVDs; ///the number of DVDs in the collection
Inventory() {
dvds = new DVD[100];
numDVDs = 0;
}
// Add DVD into Inventory end
public void addToInventory(DVD dvd)
{
// create a new array of DVDs that can hold one more than the current array of DVDs DVD[ ] newdvds = new DVD [numDVDs () + 1];
//Adds up the total value
public double value()
double total = 0;
for (int i = 0; i < numDVDs(); i++){
total = total + dvds[i].value();
return total;
}
// add the new Product to the end of the new array
new_dvds [numdvds ()] = new_DVD;
// replace the old inventory with the new one
dvds = newdvds;
}
// return the DVDs at the location index in the inventory
public DVD getDVD (int index)
{
return dvds [index];
}
// return the number of Products in the inventory
public int getSize()
{
return dvds.length;
}
// go through all the DVDs in the inventory, and add up their value.
public double getTotalValueOfAllInventory()
{
double tot = 0.00;
for(int i = 0; i < numDVDs(); i++)
{
tot += inventory [i].calculateValue();
}
return tot;
}
// sort the DVDs in the inventory by movie title
public void sortInventory()
{
Arrays.sort(inventory, 0, getSize());
}
} //end class Inventory

New Topic/Question
Reply




MultiQuote





|