Post

Created by @nathanedwards
 at November 3rd 2023, 11:58:33 am.

Question:

You are tasked with creating a program that simulates a store inventory system. Declare and initialize an array named inventory that stores the details of each item in the store's inventory. The details for each item should include the item name, item code, quantity, and price. The inventory can store a maximum of 50 items.

Write the code to declare and initialize the inventory array.

Answer:

To declare and initialize the inventory array, we can use the following code:

public class InventorySystem {
    public static void main(String[] args) {
        // Declaring and initializing the inventory array
        String[][] inventory = new String[50][4];

        // Item 1 details
        inventory[0][0] = "Item 1";
        inventory[0][1] = "A001";
        inventory[0][2] = "10";
        inventory[0][3] = "29.99";

        // Item 2 details
        inventory[1][0] = "Item 2";
        inventory[1][1] = "B002";
        inventory[1][2] = "5";
        inventory[1][3] = "49.99";

        // Item 3 details
        inventory[2][0] = "Item 3";
        inventory[2][1] = "C003";
        inventory[2][2] = "20";
        inventory[2][3] = "9.99";

        // ... rest of the items in the inventory

        // Printing the inventory
        System.out.println("Item Name\tItem Code\tQuantity\tPrice");
        for (int i = 0; i < inventory.length && inventory[i][0] != null; i++) {
            for (int j = 0; j < inventory[i].length; j++) {
                System.out.print(inventory[i][j] + "\t\t");
            }
            System.out.println();
        }
    }
}

Explanation:

  • We declare a two-dimensional array named inventory with dimensions 50x4, where each row represents an item and each column represents a detail of that item (item name, item code, quantity, and price).
  • Using array index notation, we initialize the details of each item in the array by assigning values to the individual elements.
  • We then print the inventory by iterating over the array and displaying the details for each item on separate lines.

Note: This code only demonstrates the declaration and initialization of the inventory array. In a real-world scenario, additional functionality for managing the inventory system would be implemented.