12 Replies Latest reply: Dec 7, 2009 6:46 AM by 796262 RSS

    Checking an array of objects

    801439
      Hi, I'm trying to create some code which will check whether a room is available or not. I know how to use a for loop to run through the array but I don't how to 'check' each object to see whether it is available or not. What I've got so far...
      I keep getting a nullpointer exception though so that's the first problem.

      Main
      package Assignment2;
      
      import java.util.Scanner;
      
      public class Front {
      
          public static void main(String[] args) {
              Scanner scan = new Scanner(System.in);
              int roomChoice;
      
              SingleRoom singleRoom[] = new SingleRoom[4];
      
              System.out.println("We have the following types of room:");
              System.out.print("(1)Single\n(2)Double\n(3)Family\n");
              System.out.print("Please select a room type: ");
              roomChoice = scan.nextInt();
      
              switch (roomChoice) {
                  case 1:
                      System.out.println("in the loop");
      
                      for (int i = 0; i < singleRoom.length; i++) {
      
                              singleRoom.bookRoom(); // Book room

      break;
      }
      }
      }
      }
      Room class...
      package Assignment2;

      public class Room {

      protected boolean available;

      public Room() {
      }
      }
      and the child of that, SingleRoom...
      package Assignment2;

      public class SingleRoom extends Room {

      public SingleRoom() {
      super();
      available = true;
      }

      public boolean checkRoom() {
      return available;
      }

      public boolean bookRoom() {
      this.available = false;
      return available;
      }
      }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
        • 1. Re: Checking an array of objects
          stdunbar
          Where do you allocate singleRoom?
          • 2. Re: Checking an array of objects
            796262
            aaron101 wrote:
            I keep getting a nullpointer exception though so that's the first problem.
            You never initialize the SingleRoom Objects in your array. Think about it this way:
            Object obj;
            System.out.println(obj.toString());
            That will throw a NullPointerException, because you really wanted to do this:
            Object obj = new Object();
            System.out.println(obj.toString());
            With arrays, the line SingleRoom singleRoom[] = new SingleRoom[4] is simply creating the variables, not instantiating anything. That's like the first example. What you need to do is instantiate each room before you try to call bookRoom( ) on it, like the second example. You could do that in a separate loop.
            • 3. Re: Checking an array of objects
              801439
              Ok, took a couple of tries but I've managed to get past the Nullpointerexception (yay!) I did this instead
                      SingleRoom singleRoom[] = {
                          new SingleRoom(),
                          new SingleRoom(),
                          new SingleRoom(),
                          new SingleRoom()};
              So now I need to be able to check if the rooms are available and if one is then book it else if there are none available then exit. How can I book just one at a time without running through the array and booking all the empty rooms, that's what I can't figure out. Thanks for the help so far! :]
              • 4. Re: Checking an array of objects
                796262
                aaron101 wrote:
                Ok, took a couple of tries but I've managed to get past the Nullpointerexception (yay!) I did this instead
                SingleRoom singleRoom[] = {
                new SingleRoom(),
                new SingleRoom(),
                new SingleRoom(),
                new SingleRoom()};
                For the record, you could also have done that this way:
                for (int i = 0; i < singleRoom.length; i++) {
                   singleRoom[i] = new SingleRoom();               
                }
                >
                So now I need to be able to check if the rooms are available and if one is then book it else if there are none available then exit. How can I book just one at a time without running through the array and booking all the empty rooms, that's what I can't figure out. Thanks for the help so far! :]
                What have you tried? What didn't work? Why didn't it work? What output were you expecting, and what output did you get?

                Hint: What are you trying to do here:
                switch (roomChoice) {
                            case 1:
                                System.out.println("in the loop");
                 
                                for (int i = 0; i < singleRoom.length; i++) {
                 
                                        singleRoom.bookRoom(); // Book room

                break;
                }
                }
                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                • 5. Re: Checking an array of objects
                  801439
                  Thanks Kevin for your help, managed to get a few ideas working and I have somehow managed to figure it out :O Actually can't believe it! Probably very trivial and easy but I'm amazed, haha.
                  • 6. Re: Checking an array of objects
                    796262
                    aaron101 wrote:
                    Thanks Kevin for your help, managed to get a few ideas working and I have somehow managed to figure it out :O Actually can't believe it! Probably very trivial and easy but I'm amazed, haha.
                    Feel free to post your solution, we can take a look at it if you want.
                    • 7. Re: Checking an array of objects
                      801439
                      Ok, so I used this loop which checks the availablity, displays the unavailable rooms and books the next one.
                      switch (roomChoice) {
                                      case 1:
                                          System.out.println("in the loop");
                      
                                          for (int i = 1; i < singleRoom.length; i++) {
                      
                                              if (singleRoom.checkRoom() == false) {
                      System.out.println("Room " + i + " unavailable");
                      } else {
                      singleRoom[i].bookRoom(); // Book room
                      System.out.println("room" + i + " booked");
                      break;
                      }
                      }
                      }
                      That's it!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                      • 8. Re: Checking an array of objects
                        796262
                        aaron101 wrote:
                        Ok, so I used this loop which checks the availablity, displays the unavailable rooms and books the next one.
                        switch (roomChoice) {
                        case 1:
                        System.out.println("in the loop");
                        
                        for (int i = 1; i < singleRoom.length; i++) {
                        
                        if (singleRoom.checkRoom() == false) {
                        System.out.println("Room " + i + " unavailable");
                        } else {
                        singleRoom[i].bookRoom(); // Book room
                        System.out.println("room" + i + " booked");
                        break;
                        }
                        }
                        }
                        That's it!
                        Okay, just a couple questions:

                        What happens when you try to book too many rooms? How many rooms can you book?

                        Hint: What's the first index of an array?
                        • 9. Re: Checking an array of objects
                          801439
                          I created 5 objects, but only want 4 rooms. The reason for this is that for the 'i' I didn't want it to display 'Room 0 is unavailable etc, so I started the for loop at 1. Not sure what to do when all rooms are booked. I imagine it'll be something to do with comparing the array length with something...
                          • 10. Re: Checking an array of objects
                            796262
                            aaron101 wrote:
                            I created 5 objects, but only want 4 rooms. The reason for this is that for the 'i' I didn't want it to display 'Room 0 is unavailable etc, so I started the for loop at 1. Not sure what to do when all rooms are booked. I imagine it'll be something to do with comparing the array length with something...
                            Ohh okay, I see. You could just add one to the index before you print it out, like so:
                            for (int i = 0; i < singleRoom.length; i++) {
                               if (singleRoom.checkRoom() == false) {
                            System.out.println("Room " + (i + 1) + " unavailable");
                            } else {
                            singleRoom[i].bookRoom(); // Book room
                            System.out.println("room" + (i + 1) + " booked");
                            break;
                            }
                            }
                            It probably doesn't matter for this small number of small Objects, but it's generally not a good idea to create instances you're never going to use. But it's up to you!
                            
                            Edit- Another idea is to add a room number int (or String) variable to your Room class. Then call the singleRoom.getRoomNumber( ) method when you print it out. That way you can use a numbering system that's independent of iterating through your array.

                            Edited by: kevinaworkman on Dec 4, 2009 11:41 AM
                            • 11. Re: Checking an array of objects
                              801439
                              Hmm, back again. As you can see earlier, I got the for loop working ok. The thing is I can't seem to use methods like checkRoom and roomCost outside the for loop. toString works, but that's it. The only way I can get them to work is by having everything in the for loop but then the only way to exit is by filling all the bookings. Ideally I wanted to ask the user if they wanted to book another room (a double room or a family or even another single) but it just cycles through the same room type until full. Very annoying.

                              Main
                              package Assignment2;
                              
                              import java.util.Scanner;
                              
                              public class Front {
                              
                                  public static void main(String[] args) {
                                      Scanner scan = new Scanner(System.in);
                                      int viewRoom, roomChoice, date, nights, cardNumber,
                                              issueDate, expiryDate;
                                      int run = 1, runAgain = 0, total = 0, costOption = 0;
                                      String firstName, secondName, email;
                              
                                      SingleRoom singleRoom[] = {
                                          new SingleRoom(),
                                          new SingleRoom(),
                                          new SingleRoom(),
                                          new SingleRoom(),
                                          new SingleRoom()};
                                      DoubleRoom doubleRoom[] = {
                                          new DoubleRoom(),
                                          new DoubleRoom(),
                                          new DoubleRoom()
                                      };
                                      FamilyRoom familyRoom[] = {
                                          new FamilyRoom(),
                                          new FamilyRoom(),
                                          new FamilyRoom()
                                      };
                              
                              
                                      while (run == 1) {
                                          System.out.println("Welcome to the Old Frenchay Inn!");
                                          System.out.println("Would you like to view room types?");
                                          System.out.println("(1)Yes\n(2)No");
                                          System.out.print("Please select an option: ");
                                          viewRoom = scan.nextInt();
                                          if (viewRoom != 1) {
                                              System.out.println("Goodbye!");
                                              System.exit(0);
                                              break;
                                          }
                                          System.out.println("We have the following types of room:");
                                          System.out.print("(1)Single\n(2)Double\n(3)Family\n");
                                          System.out.print("Please select a room type: ");
                                          roomChoice = scan.nextInt();
                              
                                          switch (roomChoice) {
                                              case '1':
                                                  for (int i = 1; i < singleRoom.length; i++) {
                                                      if (singleRoom.checkRoom() == false) {
                              System.out.println("Room " + i + " unavailable.");
                              } else {
                              }

                              System.out.println(singleRoom[i].toString());
                              System.out.println("We currently only have rooms available in April.");
                              System.out.print("Please enter the date you would like to arrive: ");
                              date = scan.nextInt();
                              System.out.print("Please enter how many nights would you like to stay for: ");
                              nights = scan.nextInt();
                              while (date + nights > 31) {
                              System.out.println("We don't have any rooms available in May.");
                              System.out.print("Please re-enter how many nights you would like to stay for: ");
                              nights = scan.nextInt();
                              }
                              total = singleRoom[i].roomCost(total, nights);
                              System.out.print("Total: " + total + "\n");
                              System.out.println("Is this ok?");
                              System.out.println("(1)Yes\n(2)No");
                              System.out.print("Please select an option: ");
                              costOption = scan.nextInt();
                              if (costOption != 1) {
                              System.out.println("Goodbye!");
                              break;
                              }
                              System.out.print("First Name: ");
                              firstName = scan.next();
                              System.out.print("Last Name: ");
                              secondName = scan.next();
                              System.out.print("Email Address: ");
                              email = scan.next();
                              // reservation.makeReservation();
                              singleRoom[i].bookRoom(); // Book room
                              System.out.println("Room " + i + " booked.");
                              // System.out.println("Your reservation number: " + reservation.checkReservation());
                              System.out.print("Please enter your payment details.\n");
                              System.out.print("Card Number: ");
                              cardNumber = scan.nextInt();
                              System.out.print("Issue Date (MMYY): ");
                              issueDate = scan.nextInt();
                              System.out.print("Expiry Date (MMYY): ");
                              expiryDate = scan.nextInt();
                              Customer customer = new Customer(firstName, secondName, email);
                              System.out.println(customer.toString());
                              System.out.println("Would you like to book another room?");
                              System.out.println("(1)Yes\n(2)No");
                              System.out.print("Please select an option: ");
                              runAgain = scan.nextInt();
                              if (runAgain != 1) {
                              System.out.println("Goodbye!");
                              break;
                              }
                              Room
                              package Assignment2;

                              public class Room {

                              protected boolean available;
                              protected int roomNo;
                              protected int cost, total;
                              protected String facilityOne, facilityTwo;

                              public Room() {

                              }
                              }
                              SingleRoom
                              package Assignment2;

                              public class Room {

                              protected boolean available;
                              protected int roomNo;
                              protected int cost, total;
                              protected String facilityOne, facilityTwo;

                              public Room() {

                              }
                              }
                              Thanks in advance!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                              • 12. Re: Checking an array of objects
                                796262
                                aaron101 wrote:
                                Hmm, back again. As you can see earlier, I got the for loop working ok. The thing is I can't seem to use methods like checkRoom and roomCost outside the for loop. toString works, but that's it. The only way I can get them to work is by having everything in the for loop but then the only way to exit is by filling all the bookings. Ideally I wanted to ask the user if they wanted to book another room (a double room or a family or even another single) but it just cycles through the same room type until full. Very annoying.
                                I'm not sure I understand your question. Why can't you use those methods?

                                By the way, instead of using ints for something that could only be two things, use a boolean. They represent true and false, and can be used in if statements directly.