mercredi 30 décembre 2020

If statement not working for all objects in an array

I'm very new to Java programming and have encountered a problem which I assume has a very simple solution. However I feel like I have tunnel vision right now and can't seem to figure out what the issue really is. So I'm making a program supposed to simulate a shoe warehouse, where one of the actions possible should be to add a new shoe - this part is working just fine. There's a catch to it tho - before being able to add a new shoe, the product ID of the shoe should be entered, and if the entered product ID is equal to the product ID of an already existing shoe, the user should be notified about this and not be able to add a shoe with this same product ID. I am using a if and if-else statement (nested in a for-loop, if that's how you say it) supposed to run through an array with Shoe class-objects in it, but it seems as if the if statement only runs for the first object in the array, where it works as expected and notifies the user that a shoe with that same product ID already exists, however if you enter the product ID of the 2nd or 3rd object in the array, the program will allow that to pass through without "blocking" the user from adding it, despite the fact that a shoe with that product ID already exists in the array. So my question is what's gone wrong here - is it something with the formatting of my code, typo, or something like that?

Code for the specific situation:

    System.out.println("Check to see if the product already exists "
        + "through entering product ID ");
    shoeExists = scanObj.next();
    
    //shoeArr is the array for Shoe object
    for (Shoe shoe:shoeArr) { 
        if (shoe != null && (shoe.getProdukt().equalsIgnoreCase(shoeExists))) {
            System.out.println("A shoe with product ID "+shoeExists+" already exists");
            System.out.println(" ");
            break;  
        }
                
        else if (shoe != null && !shoe.getProdukt().equalsIgnoreCase(shoeExists)){
            produktID = shoeExists;
            System.out.println("Product-ID saved. Enter other details below \n");
            System.out.println(" ");
            System.out.println("Product-ID: "+produktID);   
            System.out.println("Brand: ");
            brand = scanObj.next();
            System.out.println("Name: ");
            name = scanObj.next();
            System.out.println("Model: ");
            model = scanObj.next();
            System.out.println("Shoe type: ");
            shoeT = scanObj.next();
            System.out.println("Shoe size: ");
            shoeSize = scanObj.nextInt();
            System.out.println("Price: ");
            price = scanObj.nextInt();
            System.out.println("Stock: ");
            stock = scanObj.nextInt();
            
            Shoe b_obj = new Sko(produktID, brand, name, model,
                    shoeT, shoeSize, price, stock);
        
            if (counter < 20) {
                shoeArr[counter] = b_obj;
                counter++;
                
                System.out.println("Shoe with product ID "+produktID+ " added");
                
                break;
            }
            else {
                counter = skoArr.length-1;
                System.out.println("There is no room to add shoe.");
                
            }
        }
    }
        
    break;

I probably haven't used the correct terminology and language in some places, still new to this and learning by the day... so as I mentioned above this solution works for the first object in my array (that is pre-added to the array), but not for the other two objects that are pre-added to the array, and not for the ones added through this case either. Also I'm sure this is not the ideal or most efficient way of solving something like this by any means, but it is the solution I came to through the learning material and lectures from my teacher, and got closest to working.

If any more information is required to see what is causing the problem is needed or if something is poorly explained (which I'm sure is the case lol) just let me know and I will do my best to improve that. I suspect it's something very basic that's gone wrong though.

Thanks!

Aucun commentaire:

Enregistrer un commentaire