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

Code:
Room()

and

Code:
Room(int n, double t)

So instead of:

Code:
Room broom = new Room();

you could use the other constructor and do something like:

Code:
int roomNumber = 12;
double rate = 9.2;

Room broom = new Room(roomNumber, rate);
 
Oh I get it now so in the test will I only need to do one room

Code:
int roomNumber = 201;
double rate = 35;


Room broom = new Room(roomNumber, rate);

As room 201 to 204 has a rate of 35 or will I have to it again for every room as the bedroom's have a different rate and so do the meeting rooms
 
A main thrust for OOP is modeling the real world. A class is the idea associated with a thing/object, an instance is a concrete realization. You can have one plan for a house, but that's not a house. You can build 100 actual houses from the plan.

If you need to model 20 rooms, you would instantiate 20 room objects. Even if every room had the same rate and number (because the innkeep was a comedian or crazy), you'd need one object to represent each room.

-Lee
 
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 shouldn't use user supplied values to test the code, unless I've misunderstood your assignment completely.

When doing actual testing, one writes test cases with unchanging input. That is, you could express

"The method to bookRoom(String) will set the customer name." and "The method clearBooking() will set the customer to null"

as

Code:
Room room = new Room(1,2);
room.bookRoom("foo");
if (!room.getCustomer().equals("foo")) {
  throw new RuntimeException("bookRoom did not set customer name");
}
room.clearBooking();
if (room.getCustomer() != null) {
  throw new RuntimeException("clearBooking did not set customer name to null");
}

Note that you can write these test cases without knowing how Room is implemented. That means that you could write the test class against a Room class with empty methods and use this to test your class as you implement it.
 
Hi,

I added that code underneath the first part on my Test class.

When I compile and run it and enter the customers name I get the output

Steve Bruce 0.0 0
Please enter a correct room number.

Is that right?

Do I need to get a pop up box to enter a room number in the test class?

I know later on I will have to link it to the buttons from the GUI so when the user presses one of the labels it will book that room or something.

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());
		
		Room room = new Room(1,2);
		room.bookRoom("foo");
		if (!room.getCustomer().equals("foo")) 
		{
  		throw new RuntimeException("bookRoom did not set customer name");
		}
		
		room.clearBooking();
		if (room.getCustomer() != null) 
		{
 		throw new RuntimeException("clearBooking did not set customer name to null");
		}
		
     }
	 
}
 
Hi,

I added that code underneath the first part on my Test class.

When I compile and run it and enter the customers name I get the output

Steve Bruce 0.0 0
Please enter a correct room number.

Is that right?

First of all, none of the code outputs the string "Please enter a correct room number".

Secondly, you're still using a popup box to enter values, which is a bad way to test code. Your test code need to be separate from the application. Validating that the input is sound is not the same as actually testing the Room classes.

Thirdly, forgive me for being blunt, but you're not going to pass the course by stringing stuff together found on the internet. You need to understand what you're doing. At the moment it seems you're quite fuzzy on the concepts the assignment is trying to check if you've learned. If you have any kind of course material, it might be a good idea to look it over carefully.
 
Yeah I know what you mean about the understanding of it.

I passed my first two assignments and I haven't done Java for a while and I was just beginning it when I done those two assignments and I have forgot a lot about it.

This assignment I am doing now is a re-sit assignment that has to be done by the 20th of this month so I am trying to get as much of it done as I can however I can as I don't have time to learn it all again for the time is has to be in.

Plus I just need to pass this assignment as on the second year of my course we do not do anything to do with programming.
 
This isn't a matter of being "correct". You're writing tests, you should determine what the result should be. In your Room constructor that takes two arguments, if the room isn't between certain values you print the message you're getting. Is that correct behavior? Is the value passed in between those values?

Also, you still have the bookRoom call inside your while, which isn't going to treat you right.

-Lee
 
I have this for the test class for the last assignment I done which I failed because I couldn't get the GUI done then.

It was near enough the same as this Hotel booking system but it was a Carpark system.

I only got the first part done on that carpark one but this is what I had for the test class.

Would any of this go in to the test class for the hotel system just changed a bit?

Code:
import java.net.*;

public class Test {
	
    public static void main(String[] args)
    {
     
       Car myCar = new Car("234234", 8);
       System.out.println(myCar.toString());
       
       Lorry myLorry = new Lorry("234234", 22);
       System.out.println(myLorry.toString());
       
       Coach myCoach = new Coach("234234", 22);
       System.out.println(myCoach.toString());
       
    
    
    }
    
}
 
I have this for the test class for the last assignment I done which I failed because I couldn't get the GUI done then.

It was near enough the same as this Hotel booking system but it was a Carpark system.

I only got the first part done on that carpark one but this is what I had for the test class.

Would any of this go in to the test class for the hotel system just changed a bit?

What do you think?

Seriously, do you think it's a suitable example of a test or not? With the replies you already have, you should be able to answer this question yourself, if you understand what's going on. If you don't understand, then you probably won't be able to answer.

If you're trying to avoid the need to understand, by asking for advice here, then exactly what is your purpose? So far, it seems like your only purpose is to get credit for something you don't understand, and which you feel you won't ever have to understand, but I could be misreading the more recent posts.
 
What do you think?

Seriously, do you think it's a suitable example of a test or not? With the replies you already have, you should be able to answer this question yourself, if you understand what's going on. If you don't understand, then you probably won't be able to answer.

If you're trying to avoid the need to understand, by asking for advice here, then exactly what is your purpose? So far, it seems like your only purpose is to get credit for something you don't understand, and which you feel you won't ever have to understand, but I could be misreading the more recent posts.

My purpose is to get this first bit done. I don't understand it and it is only worth 10% of the marks which I have got most of the first part done it just needs the tests etc done which are probably worth around 5% but it needs to be done to move on to the next part of it. I have got the GUI done myself it is just this part of the Assignment that I do not understand at all.

I do want to learn how to understand this part and would go back and look at my lecture notes and learn it from the start again but because it has took me so long to get other parts of the Assignment done I have not got time to do that as the assignment has to be in for the 20th.

I just need this part done as soon as I can so I have got time to get as much of the other parts of the Assignment done as possible which are worth more marks.

I appreciate I may be asking stupid/obvious questions and may not have understood fully what has been said in other posts in this thread but that is because I am new to Java. Not everybody can just look at something straight away and full understand it that is why I was asking more questions to get clarification.

What is your problem anyway. You have not posted in this thread at all to give help or otherwise yet you just come in just to post that?
 
I do want to learn how to understand this part...

I was happy to try to provide help up until you said this. I (and others) are happy to help people, even if they have absolutely no idea what's going on. Everybody starts somewhere. But when you assert that you have no interest in understanding something, that means that you want us to get something working for you instead of helping you learn how to get it working. This is far less noble of a goal in my opinion.

I disagree with the idea that in 11-12 days you couldn't look at some notes and re-familiarize yourself with basic vocabulary, syntax, techniques, etc. and approach the problem as a beginner instead of approaching it blindly.

If you don't understand something you shouldn't get credit recognizing that you do. I understand you didn't come here for a lecture, and if you had kept the piece about not caring to understand to yourself you wouldn't have gotten one, at least not for me. I bit my tongue a number of times, hoping gentle nudging would get things to click a bit.

Having someone tell you exactly what to do is the same as copying their work. Not sure if you are at either of these schools, but I'm sure wherever you go feels similarly:
Newcastle University
http://www.ncl.ac.uk/library/resin/writing_up/academic_integrity/plagiarism.php

Northumbria University
http://www.northumbria.ac.uk/sd/central/uso/Fraud_pol_1/?view=Standard

If this was a hobby, I'd be glad to write some code for you to get something going, but academic integrity is important and handing you code or telling you exactly how to do something is compromising this. I try to give people the benefit of the doubt, but you came straight out and said you don't want to understand.

I hope you put a bit of time in and either return with a new commitment to understanding what's going on, get some help, and turn something in that earns you a decent mark. Good luck if that's the path you choose to pursue.

-Lee
 
I did not say that I didn't want to understand it I do I just don't think I would be able to fully understand it in time to get it done and have time for other parts of the assignment which are worth the most on the assignment.

I understand what you are saying though in your post and know what you mean about plagiarism and I appreciate all the help you and others have gave me in this thread. I will carry on working on it and trying to understand it and try to get it done and if I do then great but if I don't then thats life lol.
 
My purpose is to get this first bit done. I don't understand it and it is only worth 10% of the marks which I have got most of the first part done it just needs the tests etc done which are probably worth around 5% but it needs to be done to move on to the next part of it. I have got the GUI done myself it is just this part of the Assignment that I do not understand at all.

I do want to learn how to understand this part and would go back and look at my lecture notes and learn it from the start again but because it has took me so long to get other parts of the Assignment done I have not got time to do that as the assignment has to be in for the 20th.

I just need this part done as soon as I can so I have got time to get as much of the other parts of the Assignment done as possible which are worth more marks.

I appreciate I may be asking stupid/obvious questions and may not have understood fully what has been said in other posts in this thread but that is because I am new to Java. Not everybody can just look at something straight away and full understand it that is why I was asking more questions to get clarification.

I appreciate that programming is difficult. OOP can be hard to grasp, perhaps even more so if you've worked with none-OOP languages in the past. It's not that your questions are stupid. It's that you're not really asking the right ones. People are usually more than happy to answer even basic questions but they won't do your homework for you.

"Getting it done" will not work. You need to understand the concepts before you can do it. That's not just the preferred approach; it's the only one that will work. The way I learned was by studying the idea, then try and code an assignment. If I had difficulties, I would go back to the concepts and see if I'd misunderstood something. Then back to the code, etc.

As stated, I think your approach is wrong. 9 days should be enough to do it using the right approach. If you keep going about it as you do, I suspect 9 days will not be enough.

However, it's not really fair to tell someone their approach is wrong without at least sketching out the right one. Actually, there are a number of "right" approaches. The one students usually take is writing the code, then writing the test code because they are told to do so, and that approach usually works if the assignment is fairly simple and they have a good understanding on what's going on. However, there is a approach that gives you more control and hopefully more insight into what you're trying to do.

Obviously, I haven't followed the course so I don't know if the following is the way your professor wants you to go about it, but it will certainly work. It is known as Test Driven Development and is well described in the literature. You can Google it to find more info. In real life, people use more advanced tools but the basic concepts are not so difficult and would seem to be more than enough to ace this assignment.

First of all, forget that the tests only account for a certain percentage of the grade. The tests are there to ensure that the application code is correct and I would imagine that is a substantial part of the grade.

What we are trying to accomplish is a correct implementation of the Room class and its subclasses. At this stage, we don't care about the GUI. The GUI will be built on top of the correctly implemented Room, BedRoom, and MeetingRoom classes. So lets begin...

Step 1. Implementing the UML description.

I'm assuming the UML diagram is part of the handout. We can implement the description in a so called stub class in order for our test class to have something to work with. This is what the Room class should look like:

Code:
public class Room {
    private int roomNumber;
    private double rate;
    private String customer;
    
    public Room() {
        //TODO
    }
    
    public Room(int n, double t) {
        //TODO
    }
    
    public void bookRoom(String cust) {
        //TODO
    }
    
    public void clearBooking() {
        //TODO
    }
    
    public String getCustomer() {
        //TODO
        return null;
    }
    
    public double getRate() {
        //TODO
        return -1;
    }
    
    public int getRoomNumber() {
        //TODO
        return -1;
    }
    
    public String toString() {
        //TODO
        return null;
    }
}

This is a minimal implementation of the Room class. It obviously won't work but it is the bare minimum that follows the UML diagram and that the Java compiler will accept.

Step 2. Writing a test

Now let's write a test case.

Code:
public class TestRoom {

    public static void main(String[] args) {
        // The method to bookRoom(String) will set the customer name.
        Room room = new Room(1, 2);
        room.bookRoom("foo");
        if (!"foo".equals(room.getCustomer())) {
            throw new RuntimeException("bookRoom did not set customer name. Expected: foo. Actual: " + room.getCustomer());
        }

        System.out.println("All tests passed :-)");
    }
}

If you compile and run, this is what is outputted:

Code:
Exception in thread "main" java.lang.RuntimeException: bookRoom did not set customer name. Expected: foo. Actual: null

This is perhaps not so surprising but at least we have a running program. What now?

Step 3. Fixing the code.

What we want to do now is fix our Room class to avoid the test failing. This is what our new Room class looks like:

Code:
public class Room {
    private int roomNumber;
    private double rate;
    private String customer;

    public Room() {
        //TODO
    }

    public Room(int n, double t) {
        //TODO
    }

    public void bookRoom(String cust) {
        customer = cust;
    }

    public void clearBooking() {
        //TODO
    }

    public String getCustomer() {
        return customer;
    }

    public double getRate() {
        //TODO
        return -1;
    }

    public int getRoomNumber() {
        //TODO
        return -1;
    }

    public String toString() {
        //TODO
        return null;
    }
}

Running the code again gives us this:

Code:
All tests passed :-)

Our Room class now gives us the correct behaviour but it's still incomplete. What to do?

Step 4. Write another test.

This is actually just step 2 again. We want to write another test that expresses the behaviour that Room should have:

Code:
public class TestRoom {

    public static void main(String[] args) {
        // The method to bookRoom(String) will set the customer name.
        Room room = new Room(1, 2);
        room.bookRoom("foo");
        if (!"foo".equals(room.getCustomer())) {
            throw new RuntimeException("bookRoom did not set customer name. Expected: foo. Actual: " + room.getCustomer());
        }

        // The method clearBooking() will set the customer to null
        room.clearBooking();
        if (room.getCustomer() != null) {
            throw new RuntimeException("clearBooking did not set customer name to null. Expected: null. Actual: " + room.getCustomer());
        }

        System.out.println("All tests passed :-)");
    }
}

Running this code gives us:

Code:
Exception in thread "main" java.lang.RuntimeException: clearBooking did not set customer name to null. Expected: null. Actual: foo

Again, our test fails. You might have guessed the next step.

Step 5. Fixing the code (again)

Code:
public class Room {
    private int roomNumber;
    private double rate;
    private String customer;

    public Room() {
        //TODO
    }

    public Room(int n, double t) {
        //TODO
    }

    public void bookRoom(String cust) {
        customer = cust;
    }

    public void clearBooking() {
        customer = null;
    }

    public String getCustomer() {
        return customer;
    }

    public double getRate() {
        //TODO
        return -1;
    }

    public int getRoomNumber() {
        //TODO
        return -1;
    }

    public String toString() {
        //TODO
        return null;
    }
}

If we run this, we get

Code:
All tests passed :-)

---

So the approach here is to keep doing step 2 and step 3 until all the tests pass. When they do, we can be fairly convinced that the class is correctly implemented. Then we move on to BedRoom and MeetingRoom. Note that this depends on the quality of the tests. You have to try and break your code in the test phase so don't focus on writing tests that passes but instead write tests that fail and elucidate possible problems with the code. Also, create new Room classes whenever possible instead of using the old ones. I actually cheated slightly in the above and reused a Room object to make the code shorter. A longer version that is more to the point would look like this:

Code:
public class TestRoom {

    public static void main(String[] args) {
        testThatTheMethodBookRoomWillSetTheCustomerName();
        testThatTheMethodClearBookingWillSetTheCustomerToNull();
        System.out.println("All tests passed :-)");
    }

    private static void testThatTheMethodBookRoomWillSetTheCustomerName() {
        Room room = new Room(1, 2);
        room.bookRoom("foo");
        if (!"foo".equals(room.getCustomer())) {
            throw new RuntimeException("bookRoom did not set customer name. Expected: foo. Actual: " + room.getCustomer());
        }
    }

    private static void testThatTheMethodClearBookingWillSetTheCustomerToNull() {
        Room room = new Room(1, 2);
        room.bookRoom("foo");
        room.clearBooking();
        if (room.getCustomer() != null) {
            throw new RuntimeException("clearBooking did not set customer name to null. Expected: null. Actual: " + room.getCustomer());
        }
    }
}

I hope this will be enough to get you started. Feel free to ask questions but understand that people will respond more constructively to well thought out questions about the concepts that are difficult instead of "I did this. It didn't work. Why?".

In closing, this ought to be fun and if you do it the way I've described, you should get a lot of small victories along the way. One of the reasons for this approach is that it actually gives you a sense of accomplishment along the way instead of just getting a "works? yes/no" in the end. Another reason is that if you want to modify or expand your code later on, you can keep your tests in the code base and it will give you a lot of feedback, making sure that you don't accidentally break something along the way. This is, of course, not that relevant here but in real life it is a very important concern. You might have heard the term "If it ain't broke, don't fix it". Sometimes code have been patched for such a long time that it becomes impossible to understand and/or manage. Unit tests, when done right, give developers the confidence to change the code without too much fear of the consequences.

When I come across code that works but is unmaintainable I actually write tests to try an get an idea of the expected behaviour. Then I pull the code apart and back again (this is known as 'refactoring') trying to make it more clear what is going on. Because I have tests I can be fairly certain that this doesn't completely mess up the application.

So try to have fun with it and realize that there are very valid reasons for the way your professor wants you to go about the assignment.
 
Thanks for the detailed reply. I really appreciate the help you have given me to get it started properly.

I am going to start again tomorrow doing part one from the beginning.

I just have a question if you don't mind answering it.

Once I have tested the room class and I move on to testing the bedRoom, MeetingRoom class can I do this in the same test class or is it best for me to create separate test classes for each one ie. testRoom, testBedroom, testMeetingRoom?

I see on my assignment sheet it tells me "Write a separate class with a main method that will test the three subclasses."

So I am guessing they want me to just do it in one test class but would it be easier to just write them all in separate testClasses first then once they are working merge them in to one testClass?
 
An excellent description, macsmurf. Thanks for saving me from having to write it.


I would also like to remind the OP that he himself posted the list of tests that the main method should implement:
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

Every one of these is testable by calling one or more of the methods defined for Room or its subclasses. I suggest that these tests should be written and run in the order given, using the approach outlined by macsmurf of writing a test, seeing it fail, and then writing the code to make it pass.

All rooms have a room number and room rate.
Create a Room object. Confirm it has a valid room number and room rate. Repeat for each Room subclass.
All rooms may have a customer. Initially the customer will be null.
Create a Room object. Confirm its customer is null. Repeat for each Room subclass.

If a room is not booked, the customer is null.
Confirm that an unbooked Room object has null customer. A newly created Room is presumed to be unbooked, as is a Room with a cleared booking. Repeat for each Room subclass.

The method to bookRoom(String) will set the customer name.
The method clearBooking() will set the customer to null
These were already illustrated by macsmurf's test-driven example. Repeat for each Room subclass.

And so on for all the tests.

Now, given the question asked in post #34:
Would any of this go in to the test class for the hotel system just changed a bit?
Code:
import java.net.*;

public class Test {
	
    public static void main(String[] args)
    {
     
       Car myCar = new Car("234234", 8);
       System.out.println(myCar.toString());
       
       Lorry myLorry = new Lorry("234234", 22);
       System.out.println(myLorry.toString());
       
       Coach myCoach = new Coach("234234", 22);
       System.out.println(myCoach.toString());
    }
}

The OP should be able to answer his own question, if he understands what a test is supposed to do, and what the assignment asks for. More specifically, exactly what does the code test for, and is that a test required in the assignment?
 
Thanks for the detailed reply. I really appreciate the help you have given me to get it started properly.

I am going to start again tomorrow doing part one from the beginning.

Good choice! :)

I just have a question if you don't mind answering it.

Once I have tested the room class and I move on to testing the bedRoom, MeetingRoom class can I do this in the same test class or is it best for me to create separate test classes for each one ie. testRoom, testBedroom, testMeetingRoom?

I see on my assignment sheet it tells me "Write a separate class with a main method that will test the three subclasses."

So I am guessing they want me to just do it in one test class but would it be easier to just write them all in separate testClasses first then once they are working merge them in to one testClass?

Good question. You do have that choice and you can follow the assignment religiously or go another way. The first part of your proposed solution is very good but you can work around the second. You could do something like this:

Code:
public class TestSuite {

    public static void main(String[] args) {
        TestRoom testRoom = new TestRoom();
        testRoom.doTests();

        //TestBedroom testBedroom = new TestBedRoom();
        //testBedRoom.doTests();

        // etc.

        System.out.println("All tests passed :-)");
    }
}

Your TestRoom class would then look similar to this:

Code:
public class TestRoom {

    public void doTests() {
        testThatTheMethodBookRoomWillSetTheCustomerName();
        testThatTheMethodClearBookingWillSetTheCustomerToNull();
    }

    private void testThatTheMethodBookRoomWillSetTheCustomerName() {
        Room room = new Room(1, 2);
        room.bookRoom("foo");
        if (!"foo".equals(room.getCustomer())) {
            throw new RuntimeException("bookRoom did not set customer name. Expected: foo. Actual: " + room.getCustomer());
        }
    }

    private void testThatTheMethodClearBookingWillSetTheCustomerToNull() {
        Room room = new Room(1, 2);
        room.bookRoom("foo");
        room.clearBooking();
        if (room.getCustomer() != null) {
            throw new RuntimeException("clearBooking did not set customer name to null. Expected: null. Actual: " + room.getCustomer());
        }
    }
}

This is more in line with actual unit testing where one tries to test every class in isolation.

I mentioned that in real life, people use more advanced tools to test. This is just bonus information so don't worry about the details. I just want to show you that it's not that different from what you are supposed to do here. This is what an actual (JUnit) test class would look like:

Code:
import org.junit.Test;

import static org.junit.Assert.*;

public class TestRoom {

    @Test
    public void testThatTheMethodBookRoomWillSetTheCustomerName() {
        Room room = new Room(1, 2);
        String customer = "foo";
        room.bookRoom(customer);
        assertEquals(customer, room.getCustomer());
    }

    @Test
    public void testThatTheMethodClearBookingWillSetTheCustomerToNull() {
        Room room = new Room(1, 2);
        String customer = "foo";
        room.bookRoom(customer);
        room.clearBooking();
        assertNull(room.getCustomer());
    }
}

If this test class is run on the stub implementation, this is what happens:

Code:
java.lang.AssertionError: expected:<foo> but was:<null>
...

I think one of the difficulties that one could run into when studying (one meaning me :) ) is to think: "Yeah, what the lecturer is saying is all well and good but outside the ivory tower people must surely be more sensible. I know better". In other words: Arrogance. That might be true in some courses but in this case, they do have a good point for having you do it in a similar way as the above. Even with toy examples there are important lessons to take away from it.

By the way, if you are asked if you did the assignment by yourself you should just refer them to this thread. There is nothing in here (yet) that they would fail you on (unless they're evil bastards) and it can be easily googled anyway. In that vein, bear in mind that this is a very public way to ask questions.
 
I have been playing about with this all day. I have been trying to test the getType method in the Bedroom class.

I can't work out a way to specify the roomNumber I want to test. Because say I want to test room 202 so the output of getType should be Single as if the room number is between 201-203 the roomtype should be set to Single.

If I try and put the roomNumber in the constructor I get two errors.

capturebp.jpg


Code:
public class Bedroom extends Room
{
    String roomtype;

    Bedroom()
    {
		
    }

    Bedroom(int n, double t, String customer)
    {
	   if( n >= 201 && n <= 203)
	    {
		    t = 35;
		    roomtype = "Single";
		}   
		else if(n>= 204 && n <= 212)
		{
			t = 60;
			roomtype = "Double";
		}
		
		
		System.out.println("Please enter a correct room number");
			
		
	   
	}

    String getType()
    {
       return roomtype;
    }

    public String toString()
    {
		
        return null;
    }



}

Code:
public class TestBedroom
{

    public static void main(String[] args) 
    {
	    Bedroom tbroom = new Bedroom(202);
	    tbroom.getType("Single");
	    if (!"Ross".equals(tbroom.getType())) 
	    {
            throw new RuntimeException("GetType did not get the Roomtype. Expected: Single. Actual: " + tbroom.getType());
        }
        
        System.out.println("Test Passed!");
        
    }
    
}
 
You don't have a constructor that only takes one parameter. You would need to pass in the room number and a rate to your constructor that takes two parameters. If you don't care about the rate, you can pass 0.

getType does not have any parameters. You can compare its return value to "Single" to see if it matches.

-Lee
 
macsmurf had a good suggestion about using JUnit tests.

It is not worth the effort now but if you used an IDE rather than hand coding you would likely avoid compiler errors. Also it would have been easier for you to write test cases using the IDE (the IDE creates the stub method for you).


Also it seems odd that "Room" would not be an abstract class. Did you write the class specification yourself or copy it from somewhere? Your methods such as getRate and bookRoom should be abstract in the Room class.
 
RossMc said:
I wrote the classes according to the UML diagram I was given.
As lee pointed out, you are trying to use a constructor that the class doesn't understand, new Bedroom(roomNumber) when it should be new Bedroom(roomNumber, roomRate, roomCustomer)

Also tbroom.getType("Single"); isn't required, it does nothing except cause you an issue (getType() doesn't accept a parameter)
 
Thanks.

Also for another bit I have on the GUI. Is it possible to create a vector of JLabels? I tried searching online but I couldn't find anything about if it was possible to create vector of JLabels.
 
It may be worth while double checking with the person who gave you the assignment. The assignment really seems geared towards teaching Inheritance and Abstraction. In which case I'm wondering if you are missing a call to the parent class's constructor in each of the sub-classes constructors.

Not to confuse your further as I'm not sure how familiar you are with Java, but I would make the room type an enumerated type, not a string. It would make checking easier and more explicit.
 
I am not sure what enumerated means but I think I am just going to leave it as it says on the assignment just in case.

Thanks for the suggestion though.

I am also having problems with this MeetingRoom toString method.

Code:
@Override public String toString()
    {
      MeetingRoom mtroom = new MeetingRoom();
        return customer + " is in: " + roomNumber + " " + "the capacity of this room is" + mtroom.getCapacity() + " " + " " + " at a price of " + rate;
    }

The part of that which is giving me an error is.

Code:
MeetingRoom mtroom = new MeetingRoom();

I don't understand why though as it works for my Room & Bedroom class with the names changed respectively.

Code:
@Override public String toString()
    {
		Bedroom bdRoom1 = new Bedroom();
        
		return customer + " is in: " + roomNumber + " " + "this is a" + bdRoom1.getType() + " " + "room"  + " " + " at a price of " + rate;
		
    }

Code:
public String toString() 
    {
        Room room = new Room();
        return customer + " is in: " + roomNumber + " " + " at a price of " + rate;
       
    }
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.