Please help me..I need to fix my array. This is from my instructor..Your program compiled and ran well. It implemented all of the requirements for the assignment. You have a separate class that stores your Product, and your products are stored in arrays. You also implemented methods to sort the array and to calculate the entire inventory value. However, upon completion of the program, it does not display the product information sorted by the product name or the inventory value. The reason is that your array is initialized to a size of 0. An array cannot be dynamically resized. You must either prompt the user to enter the number of Cameras to be entered, or you must initialize the array to a rather large size. It does not need to be completely filled in.
//Inventory.java
//Program that displays the value of cameras.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Locale;
// class that represents a camera in inventory
class Camera implements Comparable<Object> {
private double itemnum;
private double cameraprice;
private double numinstock;
static String cameraname;
private String deptname;
public Camera(int theItemNumber, double theCameraPrice,
int theNumberInStock, String theCameraName) // constructor
{
itemnum = 0.0;
cameraprice = 0.0;
numinstock = 0.0;
}
public void setItemNum(double num) // set item number
{
itemnum = num;
}
public double getitemnumber() // get item number
{
return itemnum;
}
public void setcameraprice(double camera) // set price
{
cameraprice = camera;
}
public double getcameraprice() // get price
{
return cameraprice;
}
public void setnuminstock(double numstock) // set number in stock
{
numinstock = numstock;
}
public double getnuminstock() // get number in stock
{
return numinstock;
}
public void setdptname(String department) // set department name
{
deptname = department;
}
public String getdeptname() // get department name
{
return deptname;
}
public void setcameraname(String name) // set camera name
{
setCameraname(name);
}
public String getcameraname() // get camera name
{
return getCameraname();
}
public double calculateValue() // calculate the value
{
return cameraprice * numinstock;
}
@Override
public int compareTo(Object o) {
Camera p = (Camera) o;
return getCameraname().compareTo(p.getcameraname());
}
// returns a string representation of the product
@Override
public String toString() {
return "Department Name : "
+ getdeptname()
+ "\n"
+ "Camera Name : "
+ getcameraname()
+ "\n"
+ "Camera Number : "
+ getitemnumber()
+ "\n"
+ "Camera Price : "
+ NumberFormat.getCurrencyInstance(Locale.US).format(
getcameraprice())
+ "\n"
+ "Number in Stock : "
+ getnuminstock()
+ "\n"
+ "Value : "
+ NumberFormat.getCurrencyInstance(Locale.US).format(
calculateValue());
}
public void setCameraname(String cameraname) {
Camera.cameraname = cameraname;
}
public static String getCameraname() {
return cameraname;
}
}// end class Camera
class DigitalCamera extends Camera {
// image resolution of the digital camera added
public static String imageResolution;
// restocking fee
private static double restockingFee;
public DigitalCamera() {
super(0, restockingFee, 0, imageResolution);
// the default restocking fee is 5%
restockingFee = 0.05;
}
// initialize an OfficeSupply object with the given information
public DigitalCamera(int theItemNumber, double theCameraPrice,
int theNumberInStock, String theCameraName,
String theImageResolution) {
// call the Product constructor
super(theItemNumber, theCameraPrice, theNumberInStock, theCameraName);
// and fill in the extra details about an OfficeSupply
imageResolution = theImageResolution;
// the default restocking fee is 5%
restockingFee = 0.05;
}
public static void setResolution(String theImageResolution) {
imageResolution = theImageResolution;
}
public String getResolution() { // get resolution
return imageResolution;
}
public double getRestockingFee() // get restocking fee
{
return super.calculateValue() * restockingFee;
}
// include a restocking fee in the inventory's value
@Override
public double calculateValue() // calculates total inventory value including
// restocking fee
{
return super.calculateValue() + getRestockingFee();
}
@Override
public String toString() {
return "Department Name : "
+ getdeptname()
+ "\n"
+ "Camera Name : "
+ getcameraname()
+ "\n"
+ "Image Resolution : "
+ getResolution()
+ "\n"
+ "Camera Number : "
+ getitemnumber()
+ "\n"
+ "Camera Price : "
+ NumberFormat.getCurrencyInstance(Locale.US).format(
getcameraprice())
+ "\n"
+ "Number in Stock : "
+ getnuminstock()
+ "\n"
+ "Restocking Fee : "
+ NumberFormat.getCurrencyInstance(Locale.US).format(
getRestockingFee())
+ "\n"
+ "Value : "
+ NumberFormat.getCurrencyInstance(Locale.US).format(
calculateValue());
}
} // end class digital camera
// class inventory
class Inventory {
static Camera[] inventory;
public Inventory() {
inventory = new Camera[0];
}
public static void addCamera(Camera new_Camera) {
Camera[] new_inventory = new Camera[getSize() + 1];
// old array to the front of the new array
for (int i = 0; i < getSize(); i++) {
new_inventory[i] = inventory[i];
}
// new camera to the end of the new array
new_inventory[getSize()] = new_Camera;
// replace the old inventory with the new one
inventory = new_inventory;
}
// return the cameras at the index in the inventory
public static Camera getCamera(int index) {
return inventory[index];
}
// return the number of cameras in the inventory
public static int getSize() {
return inventory.length;
}
// add up inventory value.
public static double getTotalValueOfAllInventory() {
double tot = 0.00;
for (int i = 0; i < getSize(); i++) {
tot += inventory[i].calculateValue();
}
return tot;
}
// sort the cameras
public static void sortInventory() {
Arrays.sort(inventory, 0, getSize());
}
}
// program that displays the value of a camera in inventory.
public class Inventory2
{
public static void main(String args[]) throws IOException {
boolean stop = false;
while (!stop) {
// create scanner
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
new Inventory();
Camera cameraItem = new DigitalCamera();
// welcome screen
System.out.println("\nWelcome to the Inventory Program\n");
System.out
.print("Please enter the name of the Camera or the word Stop:"); // prompt
// for
// user
// input
cameraItem.setcameraname(input.readLine());// read camera item name
// or stop
// if stop is entered for the item name, then terminate program
if (Camera.getCameraname().compareTo("stop") == 0) {
System.out
.println("Thank you, the Inventory Program will now terminate");
stop = true;
} else if ("name" != Camera.cameraname) {
System.out.print("Please enter the Department Name:"); // prompt
// for
// user
// input
cameraItem.setdptname(input.readLine()); // for
System.out.print("Please enter the Camera Item Number:"); // prompt
// for
// user
// input
double d = Double.valueOf(input.readLine()).doubleValue();
cameraItem.setItemNum(d);// read item number
while (cameraItem.getitemnumber() <= 0.0) {
System.out.println("Invalid Entry");
System.out
.println("Please enter a positive Camera Item Number:");
cameraItem.setItemNum(Double.valueOf(input.readLine())
.doubleValue()); // read Item number
}
System.out.print("Please enter the Camera's Image Resolution:");
DigitalCamera.setResolution(input.readLine());
System.out.print("Please enter number of Cameras in Stock:");
cameraItem.setnuminstock(Double.valueOf(input.readLine())
.doubleValue()); // read
// number of camera items in stock
while (cameraItem.getnuminstock() <= 0.0) {
System.out.println("Invalid Entry");
System.out
.println("Please enter a positive number for Cameras in Stock:");
cameraItem.setnuminstock(Double.valueOf(input.readLine())
.doubleValue()); // read Item number
}
System.out.print("Please enter the price of the Camera: "); // prompt
// for
// input
cameraItem.setcameraprice(Double.valueOf(input.readLine())
.doubleValue()); // read camera price
while (cameraItem.getcameraprice() <= 0.0) {
System.out.println("Invalid Entry");
System.out.println("Please enter a positive Camera price:");
cameraItem.setcameraprice(Double.valueOf(input.readLine())
.doubleValue()); // read camera Price
}
// add the newly created object to inventory
Inventory.addCamera(cameraItem);
} // end while
Inventory.sortInventory();
// display the inventory of cameras on the screen, one camera at a
// time
for (int i = 0; i < Inventory.getSize(); i++) {
System.out.println(Inventory.getCamera(i)); // returns the
// product at location i
System.out.println();
}
// display the total value of the inventory on the screen
System.out.println("The Total Value of the inventory is: "
+ NumberFormat.getCurrencyInstance(Locale.US).format(
Inventory.getTotalValueOfAllInventory()));
} // end method main
}
} // end class Inventory2