Java Help

Discussion in 'Mac Programming' started by RossMc, Aug 7, 2010.

  1. RossMc macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #1
    I am writing a Hotel Booking Application.

    On the sheet I have it says.

    "If a room is not booked, the customer is null"

    I guess I have to write an if statement somewhere to check if the room has been booked or not and then set the customer to null?

    Does anyone know where I would write this code in here? I have wrote the classes like specified in this UML Diagram.

    [​IMG]

    Here is the code I have for Room which I am guessing is where this if statement would be? Can anyone help me out?

    Code:
    import javax.swing.*;  
       
    public class Room  
    {  
        int roomNumber;  
        int n = roomNumber;  
          
        double rate;  
        double t = rate;  
          
        String customer;  
          
          
        Room()  
        {  
        }  
      
        Room(int n, double t)  
        {  
            if( n >= 201 && roomNumber <= 203)  
            {  
                t = 35;  
                customer = null;  
               
            }     
            else   
            {  
                System.out.println("Please enter a correct room number");  
            }  
        }  
          
        void bookRoom(String customer)  
        {  
              
              
        }  
          
        void clearBooking()  
        {  
              
        }  
      
        String getCustomer()  
        {  
            return customer;  
        }  
      
        double getRate()  
        {  
            return rate;  
        }  
      
        int getRoomNumber()  
        {  
            return roomNumber;  
        }  
      
        public String toString()  
        {  
            return customer;  
        }  
      
    }   
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Both constructors, bookRoom, and clearBooking are the places that customer will change. Which of them should result in an empty room? Those are the places to null customer.

    -Lee
     
  3. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #3
    Thanks for the reply.

    So in the bookRoom constructor I should have an if statement checking if the Room is occupied? and then in the clearBooking I should have it set the customer to null?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I would say bookRoom needs to either return an error or throw an exception if the room is already booked (customer is not null). Maybe a doubleBookingException? Then, if the customer is vacating a room in clearBooking, then there is no more customer.

    I don't remember java's initialization rules, but I think an Object ivar will be null on instantiation. Even if that's true, I'd set customer to null if every new room is empty,to demonstrate you know what's going on.

    -Lee
     
  5. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #5
    Thanks for that I will have a go and see what I can get done.

    I am new to Java and this is a re-sit Java Assignment I am doing for University.

    I have till the 20th of August to get it done. This first part is only worth 10% but I have found it the hardest part LOL :(.

    I moved on and got the GUI done for it which is worth 25% but then there is mouse events which I think I need this first part done before I can move on to the rest so I can get 40% to pass the Assignment.
     
  6. Alfihar macrumors member

    Joined:
    Aug 7, 2010
    Location:
    UK
    #6
    Depending on how you want to do things you could just have the if statement in whatever part of the program that calls the object.

    Code:
    String customer;  
         
    public void bookRoom(String customer) {  
        this.customer = customer;
    }  
          
    public void clearBooking() {  
        customer = null;
    }
    

    Code:
    String customerName = blahblah;
    Room testRoom = new Room();
    
    if (testRoom.getCustomer == null){
        testRoom.bookRoom(customerName)
    } else {
        // Display message about room already being booked
        // or however you want to handle it.
    }
    

    Or you could do something like this, and use the returned boolean to decide what to do. Though as lee1210 mentioned, throwing an exception would be another way.

    Code:
    String customer;
    
    public Boolean bookRoom(String customer) {
        Boolean roomBookedSuccessfully = true;
    
        if (customer == null){
            this.customer = customer;
        } else {
            roomBookedSuccessfully = false;
        }
    
        return roomBookedSuccessfully;
    }
    
    public void clearBooking() {  
        customer = null;
    }
    
     
  7. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #7
    Thanks alot for that. :)

    I will give it a go tomorrow and post in this thread how I get on.
     
  8. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #8
    Just an update.

    I used the second way and it compiled. I have not tested if it worked yet. I need to do some more stuff then write a test class.

    I have another question if anyone can help. It also says "The Method bookRoom(String) will set the customer name.

    I guess that I will have to have an input string with a scanner for the customers name?

    Is this right? and do I have to write that code in the bookRoom(String) bit?
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    I don't think you should do any I/O in the classes you're writing now. There is a single parameter to bookRoom. It is the same type as customer. There are no other ivars of this type in your room class.

    -Lee
     
  10. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #10
    So I don't use the bookRoom(String) in room to set the customer name?
     
  11. Alfihar macrumors member

    Joined:
    Aug 7, 2010
    Location:
    UK
    #11
    The room object class shouldn't really have anything like that in it.

    So if you went with the second way that I posted, in another class you could have something like this:

    Code:
    // Some code to read in the customer name...
    // and set a string called customerName to equal the customers name
    
    
    Boolean success = false;
    // Assume you've created an instance of the Room somewhere else called testRoom
    success = testRoom.bookRoom(customerName);
    
    // Check to see what happened
    if (success == true){
        system.out.println("yay it worked");
    } else {
        system.out.println("uh oh, the room was already booked");
    }
    
    
     
  12. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #12
    Oh I think I get it now.

    So that second way I went with should stay in the Room class but to read the customers name and to test if the bookRoom works I should write a test class?
     
  13. Alfihar macrumors member

    Joined:
    Aug 7, 2010
    Location:
    UK
    #13
    Yeah write a test class.

    Should only need a few lines of code, probably a mixture of the second block of code I posted in my first post, and the code I just posted a few minutes ago.
     
  14. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #14
    On my assignment sheet it says.

    Write a seperate class with a main method that will test the three subclasses. The test class you will create objects of each subclass. Using the classes constructors, and output the results of calling the objects methods. A command line output using MSDOS will be sufficient for this.

    Then underneath it has the things I have been working on.

    All rooms have a room number and room rate.

    All rooms may have a customer. Initially the customer will be null.

    If a room is not booked, the customer is null.

    The method to bookRoom(String) will set the customer name.

    The method clearBooking() will set the customer to null

    Bedrooms have an additional description giving the size of the room: either double or single.

    MeetingRooms have a maximum capacity of the room.

    The Room.toString() method should provide a text description of the room, including the name of the customer, if any

    The toString() method the subclasses must override Room.toString() , providing additional information on the room(capacity or type of room).

    You will be asked to save details of rooms. Remember to add the appropriate interface in the Room class declaration.


    Does all of that have to go in the test class?
     
  15. Alfihar macrumors member

    Joined:
    Aug 7, 2010
    Location:
    UK
    #15
    Yeah that should all go in the test class, which will use the methods in the other three classes.
     
  16. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #16
    You do set the customer ivar in bookRoom, but you don't need to read the name from somewhere in that method (that's what it sounded like you wanted to do), the name is passed in.

    -Lee
     
  17. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #17
    Edit :

    I have got it to work but when I enter the customers name it does nothing. What should I be doing with it?

    Code:
    import java.util.Scanner;
    import java.awt.*;
    import javax.swing.*;
    
    public class testRoom
    {
        public static void main(String[] args)
        {
    	   String customerName;
    	   
    	   do
    	   {
    		 String inputString = JOptionPane.showInputDialog(null,"Please Enter The Customers Name");
         	 Scanner nameScanner = new Scanner(inputString);               
         	 customerName = nameScanner.next();									
         	 String outputString = "The Customer Name Is "+ customerName;
         	 
     	 	}
     	 	while (customerName.length() < 3 || customerName.length() > 40);
    	    
    	    
    	    
        }
        
    }
     
  18. Alfihar macrumors member

    Joined:
    Aug 7, 2010
    Location:
    UK
    #18
    If you add a:

    System.out.println(customerName);

    at the end of the main method you'll see that it's set customerName to be whatever test you entered into the dialogue box. So you have the name ready to do whatever you want with it.
     
  19. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #19
    Thanks.

    If I type the name say SteveBruce it will show the full name but if I put the name with a space like Steve Bruce it will only show the Steve bit.

    How would I get it to show the name with a space included?
     
  20. Alfihar macrumors member

    Joined:
    Aug 7, 2010
    Location:
    UK
    #20
    maybe try:

    customerName = nameScanner.nextLine();

    instead of:

    customerName = nameScanner.next();
     
  21. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
    #21
    Off-topic, but in Java you should avoid using "null" wherever possible. It's better practice to throw an exception.
     
  22. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #22
    I have added the code to test the booking room and the output I get is null.

    That is not right is it?

    Here is the testRoom code.

    Code:
    import java.util.Scanner;
    import java.awt.*;
    import javax.swing.*;
    
    public class testRoom
    {
        public static void main(String[] args)
        {
    	   String customerName;
    	   Room broom = new Room();
    	   
    	   do
    		{
    		String inputString = JOptionPane.showInputDialog(null,"Please Enter The Customers Name");
    		Scanner nameScanner = new Scanner(inputString); 
    		customerName = nameScanner.nextLine();	
    		broom.bookRoom(customerName);
    		}
    		while (customerName.length() < 3 || customerName.length() > 40);
    		
    		System.out.println(broom.getCustomer());
    		
         }
    	 
    }
    Edit :

    I have just changed the bookRoom to

    Code:
      public void bookRoom(String customer)
        {
    	    this.customer = customer;
    	    
    	}
    It seems easier option I am going to try do the testRoom again.
     
  23. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #23
    So I have got the Test class done for that bit and a box comes up to enter customers name and the output I get is the customers name plus the rate and the room number

    So at the minute I get Steve Bruce 0.00 0

    Code:
    import java.util.Scanner;
    import java.awt.*;
    import javax.swing.*;
    
    public class testRoom
    {
        public static void main(String[] args)
        {
    	   String customerName;
    	   Room broom = new Room();
    	   
    	   do
    		{
    		String inputString = JOptionPane.showInputDialog(null,"Please Enter The Customers Name");
    		Scanner nameScanner = new Scanner(inputString); 
    		customerName = nameScanner.nextLine();	
    		broom.bookRoom(customerName);
    		}
    		while (customerName.length() < 3 || customerName.length() > 40);
    		
    		System.out.println(broom.getCustomer() + " " + broom.getRate() + " " + broom.getRoomNumber());
    		
         }
    	 
    }
    
    
    So in the test class now do I have to write code to put a customer in a certain room? and how would I go about doing that lol.
     
  24. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #24
    You're setting who's in the room, but not its number or rate. There is a constructor that should set both. Use that instead of the default constructor.

    Also, in your test, book the room outside of your while. Otherwise if a bad customer name is entered, it will get the room, and the good name entered next won't.

    -Lee
     
  25. RossMc thread starter macrumors 65816

    RossMc

    Joined:
    Apr 30, 2010
    Location:
    Newcastle, UK
    #25
    Thanks, I understand the second bit but don't really get what you mean on the first? which constructor is that is it toString?
     

Share This Page