jeudi 16 novembre 2017

Why is my method looping without a loop?

I can't seem to figure out why my addRecord() method is looping. To be specific, it only loops at the "Enter Sales ID>>" at the first else of my if/else in the method. I have tried just invoking the programMenu() method at the end of the code, but then the compiler just keeps trying to run both. This method should only execute when "a" is received at the switch case statement, but it runs when both "a" AND "c" are entered, and then just keeps looping to the same else statement at "Enter Sales ID>>".

My method looks like this:

public static int addRecord(Salesperson[]salesPeopleArray, int 
numOfSalesPpl)
    {
        String name=null;
        String idNum;  
        double salesAmt = 0;
        String response;
        numOfSalesPpl = 0;
        final int MAX_LIMIT=20;
        final int ID_NUM_LIMIT = 8; 

        if(numOfSalesPpl == MAX_LIMIT)
        {
            System.out.print("Database has reached capacity.");
            System.out.print(" Please delete a record before ");
            System.out.println("adding to the database.");
        }
        else
        System.out.print("Enter Sales ID:>> ");
        idNum = userInput.nextLine();

        if(idNum.length() != ID_NUM_LIMIT)
        {
           System.out.println(">>>>>Sales ID must be 8 digits<<<<<<"); 
           programMenu();
        }
        else
        {
           System.out.print("Please enter name: >> ");
           name = userInput.nextLine();
           System.out.print("Sales amount : >> ");
           salesAmt = userInput.nextDouble();
           salesPeopleArray[numOfSalesPpl] = new Salesperson(name, 
idNum,salesAmt);    
           ++numOfSalesPpl; 
           userInput.nextLine();
           System.out.print("Do you want to display database> Y/N >>");
           response = userInput.nextLine();
           if(response.equals("y"))     

           displayDatabase(salesPeopleArray, numOfSalesPpl);
    }

    return numOfSalesPpl;
    }

My switch case looks like this:

String selection = programMenu();
   boolean loop = true;


     do
       {
          switch(selection)
          {
               case "A": 
               case "a":                     
                  addRecord(salesPeopleArray, numOfSalesPpl);
                  break;

                case "C": 
                case "c": 
                   changeRecord(numOfSalesPpl, salesPeopleArray);
                   break;

                case "Q": 
                case "q":
                System.out.print("You Are Leaving Database");
                System.out.print("   ");
                loop = false;
                break;
           }
       }
     while(loop); 

}

My programMenu() method looks like this:

public static String programMenu()
    {
        String selection;
    do

        {
         System.out.println("====================================");
         System.out.println("(A)dd a Record");
         System.out.println("(C)hange a Record");
         System.out.println("(Q)uit Database"); 
         System.out.print("Enter selection: >> ");
         selection = userInput.nextLine();
            if(!selection.equalsIgnoreCase("a") && 
!selection.equalsIgnoreCase("c") && !selection.equalsIgnoreCase("q"))
            System.out.println(">>>>Invalid Selection<<<<");           
         System.out.println("====================================");

        }
       while(!selection.equalsIgnoreCase("a") && 
!selection.equalsIgnoreCase("c") && !selection.equalsIgnoreCase("q"));

        return selection;
}

    } 

Aucun commentaire:

Enregistrer un commentaire