mercredi 13 décembre 2017

Java, problems with using multiple conditional statements, while loop, for loop, if statement

I am working on a program that helps with signing up guests to parties.

the party has to exist in the data

and ofcourse the amount of tickets wanted must be less than the tickets available

I am really struggling with adding one more condition - to check whether the input is higher than 0.

I have put the entire for loop into another if statement as you can see: if(ticketsWanted > 0)

and it prints "Please enter a positive number" if something below 1 has been entered

However, it also prints "This party does not exist or is fully booked." again..

Basically after the System.out.println("Please enter a positive number"); I want the program to provide an option to enter the number again until it's positive

So here are my questions:

How could I use the while loop so that it loops until the positive number has been entered?

Is there a more efficient/better way of doing what I intend to do ?

Thank you

 private static void signUp2(Guest validGuest){

              input.nextLine();
              System.out.println("Enter the name of the party");
              String partyName = input.nextLine();
              System.out.println("How many tickets the guest wishes to purchase?");
              int ticketsWanted = input.nextInt();
              boolean check = false;

              if(ticketsWanted > 0) {
               for(Party p: parties){
                 if(p.getPartyName().equals(partyName)){
                     if(ticketsWanted > e.getTickets()){
                 }
                   else{
                     check = true;  
                     validGuest.AddPartiesAndTickets(p, ticketsWanted);
                    }

                 }
              }
            }
             else{
                System.out.println("Please enter a positive number");

               }

            if(check == false){
            System.out.println("This party does not exist or is fully booked.");

Aucun commentaire:

Enregistrer un commentaire