• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hotel reservation booking project.

 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, new to Java and have got a project to write a program for hotel bookings. Getting stuck when trying to tell the user the total for the first two nights (which are at a rate of €80) combined to the 3rd and subsequent nights (which are at a different rate). How would you approach this? Can you set an int to have different values for numberOfNights <=2 and >2?

Thanks for any help!

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I've edited your post to add code tags and break up the long lines so it's easier to read.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you get a pencil and paper out and write down in detail how you would solve this for each possible scenario. It may help to then look for the things you have to do for every scenario and the things which are dependent on other factors such as number of guests. I also suggest you don't have a load of if statements for each possible scenario that each print out their own result, but rather see if you can pre-compute the information to put into the output and have a single print at the end of the main method.
Ultimately you should be looking to get most of this code out of the main method and into discrete methods but that may be beyond your current level.
 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Thanks for that, Tony. Appreciate it.

There are four different scenarios. The first two scenarios are for one person staying upto two nights and the second scenario is more than one person staying upto two nights. I've tested these two and they work.

The remaining two scenarios are one person staying more than two nights and more than one person staying more than two nights.

Say a single user inputs staying for 4 nights, that would mean 2 nights would be charged at one rate and 2 nights would be charged at a different rate and then added together. How would I differentiate that if a user inputs 4, the first 2 of that four inputted represents a different value than the second 2 of that four? That's what is confusing me. Sorry if this is too long. Not sure about code tags? Thanks again!

if (numberOfNights <=2 && numberOfGuests == 1) {System.out.println("For" + numberOfGuests + " guest staying for " + numberOfNights + " nights, it will cost " + "€" + singleRoom);}
else if (numberOfNights <=2 && numberOfGuests >1) {System.out.println("For " + numberOfGuests + " guests staying for " + numberOfNights + " nights, it will cost " + "€" + doubleRoom);}
else if (numberOfNights >2 && numberOfGuests == 1) {System.out.println("For " + numberOfGuests + " guest staying for " + numberOfNights + " nights, it will cost " + "€" + );}
else if (numberOfNights >2 && numberOfGuests > 1) {System.out.println("For " + numberOfGuests + " guests staying for " + numberOfNights + " nights, it will cost " + "€" + );}
}
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geronimo Finch wrote:Say a single user inputs staying for 4 nights, that would mean 2 nights would be charged at one rate and 2 nights would be charged at a different rate and then added together. How would I differentiate that if a user inputs 4, the first 2 of that four inputted represents a different value than the second 2 of that four? That's what is confusing me.



If that's the case, then this line of your code:



isn't the right thing to do, is it?

Like Tony, my preference would be to write a separate method to do that calculation. However if you haven't learned about methods yet then you should replace that code by some more complex code which does the right calculation. Again, you should get out your pencil and figure out what that calculation should be before writing the code.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geronimo Finch wrote:There are four different scenarios. The first two scenarios are for one person staying upto two nights and the second scenario is more than one person staying upto two nights. I've tested these two and they work.
The remaining two scenarios are one person staying more than two nights and more than one person staying more than two nights....


You're trying to turn this into a logical - or purely procedural - problem, and solve it with CODE. Now, you might be able to do it; but it will simply be an exercise in how well you can think like a computer.

My advice: Don't.

Java is an OBJECT-oriented language; and that means that you get objects (or classes) to do the work for you; not your inbuilt "Java brain".

Right now, you have a class that describes your program - HotelBookings - but absolutely none that describe your problem.

A "Hotel Bookings" program is going to involve a Hotel, isn't it? And a Hotel is likely to have a number of Rooms. And then you'll probably have "customers" (I prefer Client myself, just because it's shorter to type ). And there's been lots of mention of 'nights'; so what about a Night class?

Do you see what I'm doing here? I'm thinking about classes based (and named) on the requirements. When I create them, I don't necessarily need to know exactly what they do - that comes later - I just need to know that they're probably going to be required. It takes about five lines of code to create a class, so don't be afraid to do it - even if you never end up using it.

And they DON'T all have to be crowded in with your HotelBookings class (which you probably will still want), they're simply a way of removing logic (or CODE) from that main() method, which otherwise is likely to get huge. It might be an idea to put them all in the same package - ie, directory - though (hotel?).

Writing a good Java program is far more than just CODING. It takes a lot of thought, so you have to get past the point of treating it simply as "code" that has to be solved.

HIH. For more information, have a look at the StopCoding (←click→) and WhatNotHow pages.

Winston
 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

System.out.println("Okay. Thanks for all the input. Back to the drawing board. ");
 
geronimo Finch
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it.


 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done but the only part of each of the output statement that requires a different calculation is the cost so that is the only bit you should be doing in the if blocks. The print out should be done after the if statements.

You will, of course, be adding some bounds checking won't you or the user can enter -2 guests or -3 nights etc and get a really cheap rate.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

geronimo Finch wrote:Got it.


Well done! But it's still simply a "one method" class. And if you try to do all the things that Tony suggests, that method is only going to get bigger and more complicated. Just imagine what it would look like if you had to deal with dollars and pounds as well...

However, it's early days, and you get a cow for posting your solution.

And once again: well done.

Winston

PS: I've broken up your lines because they were far too long. Please read the link.
Thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic