Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RossMc

macrumors 65816
Original poster
Apr 30, 2010
1,220
70
Newcastle, UK
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.

136274703.jpg


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;  
    }  
  
}
 
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
 
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?
 
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
 
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.
 
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;
}
 
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;
}

Thanks alot for that. :)

I will give it a go tomorrow and post in this thread how I get on.
 
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?
 
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
 
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");
}
 
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?
 
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.
 
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?
 
Yeah that should all go in the test class, which will use the methods in the other three classes.
 
So I don't use the bookRoom(String) in room to set the customer name?

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
 
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);
	    
	    
	    
    }
    
}
 
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.
 
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.

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?
 
maybe try:

customerName = nameScanner.nextLine();

instead of:

customerName = nameScanner.next();
 
Off-topic, but in Java you should avoid using "null" wherever possible. It's better practice to throw an exception.
 
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.
 
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.
 
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
 
Thanks, I understand the second bit but don't really get what you mean on the first? which constructor is that is it toString?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.