0

I currently have a programming assignment. In the assignment, I have to have two array that I need to store the data of a store's stock with. I use the first array for the Manager to access. I use the second array for the Cashier to access.

The manager may add the code, name, price, and quantity of up to 15 items. The cashier may add the code and quantity of any item. If the code of the does not exist in the manager's array, the program will automatically exit.

The elements of the arrays are not in the same order for both array lists. So how do I make sure that the item code that the cashier has entered, actually exists in the Manager's array?

This is how I have declared the arrays:

        String[] stockItemCode = new String[15];
        String[] stockItemName = new String[stockItemCode.length];
        int[] stockItemQuantity = new int[stockItemCode.length];
        double[] stockItemPrice = new double[stockItemCode.length];

        String[] saleItemCode = new String[stockItemCode.length];
        String[] saleItemName = new String[stockItemCode.length];
        int[] saleItemQuantity = new int[stockItemCode.length];
        double[] saleItemPrice = new double[stockItemCode.length];

This is how the manager enters items:

// 3- The manager:
// 3.1- The manager has to login to add the items to the inventory list. The username and password are "Manager" and "Man_2020" respectively.
            if (username.equals(managerUsername) && password.equals(managerPassword))
            {
// 3.2- Once the manager is logged in, he will enter the item's code, name, quantity, and price. 
                do
                {
                    System.out.println("Please enter item code: ");
                    stockItemCode[stockItemLimit] = sc.next();

                    System.out.println("Please enter item name: ");
                    stockItemName[stockItemLimit] = sc.next();

                    System.out.println("Please enter item quantity (numbers only): ");
                    stockItemQuantity[stockItemLimit] = sc.nextInt();

                    System.out.println("Please enter item price (numbers only): ");
                    stockItemPrice[stockItemLimit] = sc.nextDouble();

                    stockItemLimit++;
// 3.3- After entering the above information for each item, the program prompts the manager to continue or logout of his account. He has to use 'L' or 'S' to sign-out.
                    System.out.println("Would you like to stop the program? Entering S or L will stop the program");
                    logoutPrompt = sc.next().charAt(0);
                }
                while(!(logoutPrompt == 'L' || logoutPrompt == 'S'));
            }

This is how I have tried to compare the elements of the arrays (I know it's wrong, but I don't know why. I'm very new to programming).

// 4- The sales employee:
// 4.1- The sales employee logs in to scan the sold items and print the receipt. The username and password are "Sales" and "Sale_2020" respectively.
            else if (username.equals(salesUsername) && password.equals(salesPassword))
            {
                i = 0;
// 4.2- The sales employee, enters the *code* and the *quantity* of the sold item.
                do
                {
                    System.out.println("Please enter item code: ");
                    saleItemCode[i] = sc.next();

                    System.out.println("Please enter item quantity (numbers only): ");
                    saleItemQuantity[i] = sc.nextInt();

                    saleItemName[i] = stockItemName[i]; //This is the problem
                    saleItemPrice[i] = stockItemPrice[i]; //This is the problem

// 4.3- The program calculates the total price of the transaction, by using the quantity and the price of the sold items.
                    if(saleItemCode[i] == stockItemCode[i])
                    {
                        saleItemPrice[i] = stockItemPrice[i] * saleItemQuantity[i];
// 4.4- The program has to deduct the sold quantity of the items from the stock list.
                        stockItemQuantity[i] = stockItemQuantity[i] - saleItemQuantity[i];
// 4.5- After entering each item, the employee is prompted to continue adding the item to the receipt, or print it (by using 'P').
                        System.out.println("Would you like to print the receipt by entering P? Enter anything else to continue adding items.");
                        logoutPrompt = sc.next().charAt(0);
                    }

                    else
                    {
// If the code is entered incorrectly, the program skips to 4.5.
                        System.out.println("Would you like to print the receipt by entering P? Enter anything else to continue adding items.");
                        logoutPrompt = sc.next().charAt(0);
                    }

                    i++;

                }
                while(logoutPrompt != 'P');

Also, whenever I try to print the information of the stock's items while in the Cashier's loop, the arrayElements return as null as if they do not exist even though they clearly do.

for(i = 0; i <= stockItemLimit; i++) //COME HERE
{
int indexNumb = i+1;
System.out.println(indexNumb + "\t" + saleItemCode[i] + "\t" + saleItemName[i] + "\t" + "$" + saleItemPrice[i] + "\t" + saleItemQuantity[i]);
}

All help would be appreciated.

tkruse
  • 10,222
  • 7
  • 53
  • 80

1 Answers1

0

You find an element in an unsorted array by looping over all elements and comparing each.

 int stockitemindex = -1;
 for(int searchindex = 0; searchindex <= stockItemLimit; searchindex++) {
     if (stockItemCode[searchindex].equals(saleItemCode[i])) {
          stockitemindex = searchindex;
          break;
     }
 }

It's not clear from your description why the arrays of stockitem and saleitem have the same size. It's not even clear if you need an array for saleitem. Just a String code and int quantity would seem enough.

Also in your loop at the end of your question, you access saleitem array, not stockitem array, that's why you get null contents.

tkruse
  • 10,222
  • 7
  • 53
  • 80
  • Thanks for the tip on the first part, I seem to have fixed that issue thanks to your help. However, regarding this one: "Also in your loop at the end of your question, you access saleitem array, not stockitem array, that's why you get null contents." , I am currently trying to access the stockItem array, and I get the same error, again. Why may that be? – Ballen Abdullah May 12 '20 at 13:35