Results 1 to 4 of 4

Thread: Need an array

  1. #1
    Fired0g22 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Need an array

    Hey all. I am new to Java and trying to work on an assignment that will be due in a couple days. It says I need an array. I have read multiple pages as well bought a book. I understand how an array works but coding it is a little difficult. The assignment is as follows:

    Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information about one product at a time, including the item number, the name of the product, the department 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.

    Code::::

    /* A application that calculates the inventory of an item, the value, and how many of that item is in stock */

    import java.util.Scanner; // need to call the scanner before it can be used
    public class InventoryProgram
    {
    public static void main(String args[])
    {
    Scanner input = new Scanner(System.in);


    System.out.println("Inventory for cameras within the Electronics Department");
    System.out.print("Please enter the name of a camera: ");
    cam.setItem(input.nextLine());
    string[] cname = new string [5];
    for (string cname = ""; i < num.length; i++)


    System.out.print("Please enter the product number: ");
    cam.itemNumber(input.nextInt());

    System.out.print("Please enter the price per single unit: ");
    cam.price(input.nextDouble());

    System.out.print("Please enter the number of cameras that are currently in stock: ");
    cam.unitsAvail(input.nextInt());

    System.out.println("Item Name: " + cam.displayItem());
    System.out.println("Item Number: " + cam.itemNumber());
    System.out.println("There are " + cam.unitsAvail() + " unit(s) available at $" + cam.price() + " each.");
    System.out.println("The total value of this item in the " + cam.displayDept() + " department is $" + cam.inventValue());

    } // end method main
    } // end class PayrollProgram

    class Camera extends InventoryProgram
    {
    private int item_number;
    private int units_avail;
    private double unit_price;
    private double invent_value;
    private String item_name;
    public String dept_name;

    //default constructor
    public Camera()
    {
    item_number = 0;
    units_avail = 0;
    unit_price = 0;
    invent_value = 0;
    item_name = "";
    dept_name = "Electronics";
    } //end default constructor

    //Parameterized Constructor
    public Camera(int item_number, int units_avail, double unit_price, double invent_value, String item_name, String dept_name)
    {
    this.item_number = item_number;
    this.units_avail = units_avail;
    this.unit_price = unit_price;
    this.invent_value = invent_value;
    this.item_name = item_name;
    this.dept_name = dept_name;
    } //end constructor

    public int itemNumber(int item_number)
    {
    this.item_number = item_number;
    return item_number;
    }

    public int itemNumber()
    {
    return item_number;
    }

    public int unitsAvail(int units_avail)
    {
    this.units_avail = units_avail;
    return units_avail;
    }

    public int unitsAvail()
    {
    return units_avail;
    }

    public double price (double unit_price)
    {
    this.unit_price=unit_price;
    return unit_price;
    }

    public double price()
    {
    return unit_price;
    }

    public String setItem (String item_name)
    {
    this.item_name = item_name;
    return item_name;
    }

    public String displayItem()
    {
    return item_name;
    }

    public String setDept (String dept_name)
    {
    this.dept_name = dept_name;
    return dept_name;
    }

    public String displayDept()
    {
    return dept_name;
    }

    public double inventValue()
    {
    this.invent_value = (units_avail * unit_price);
    return invent_value;
    }

    } // end of camera class

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Need an array

    You could use the array to reference each item, ie. give each item a number and an idex in your array and then reference each object from your array.

  3. #3
    Opid is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: Need an array

    Create an array of cameras: Camera[] cam = new Camera[x]; I don't know how many cameras you want. I guess you could ask input from user like System.out.println("How Many Cameras? "); int x = input.nextLine(); Then just set up a for loop that goes for the duration which would be the length of the array

  4. #4
    Fired0g22 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Need an array

    I have updated my code. I think I am on the right track. I created an array class :

    Java Code:
    public class Arrays extends InventoryProgram {
    
    	Camera cam = new Camera();
              public Arrays(String[] args) {
              String[] product;              // declares an array of strings
    
              product = new String[6];      // allocates memory for 5 strings
                
              product[0] = cam.item_name; // initialize first element
              product[1] = cam.item_name; // initialize second element
              product[2] = cam.item_name; // etc.
              product[3] = cam.item_name;
              product[4] = cam.item_name;
              product[5] = cam.item_name;
    
    		  return product[0];
    		  
         }
    }
    if the array is correct, how do I get it to transfer the data to my main for output?
    Last edited by JosAH; 10-06-2011 at 08:50 AM. Reason: fixed the [code] ... [/code] tags

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •