Jump to content

Hi.

 

I have just read through the book "Beginning programming with Java" - For Dummies.

I have been programming Java for 2 weeks, so my skills and knowledge is very basic.

 

I got a task from the book. I was to make a booking system for a motel with 10 rooms. So I did it, and this is the outcome:

import java.util.Scanner;public class GregarianClub {	public static void main(String args []) {		Scanner keyboard = new Scanner(System.in);		int whichRoom, numGuests;		int guestsIn[];		guestsIn = new int[10];				// Initialise the array		for (int roomNum = 0 ; roomNum < 10 ; roomNum++)		{ guestsIn[roomNum] = 0; }			do {			System.out.print("Which room do you want? (0-9) ");			whichRoom = keyboard.nextInt();			System.out.print("How many are staying in that room? ");			numGuests = keyboard.nextInt();				// Ask the user if it wants to run the program again			guestsIn[whichRoom] = numGuests;			System.out.println();			System.out.println("Do another? (Y/N)");				} while (keyboard.findWithinHorizon(".", 0).charAt(0) == 'Y');					// Print out an overview of the rooms		System.out.println();		System.out.println("Room\tGuests");		for (int roomNum = 0; roomNum < 10; roomNum++) {			System.out.print(roomNum);			System.out.print("\t");			System.out.println(guestsIn[roomNum]);		}		System.out.println("Thank you for using our hotel!");		keyboard.close();	}}

Worked well enough, but it was too simple. You could easily double-book rooms etc. I decided to try and make it bait more advanced. Trying to make it check if the room was already booked. 

So I made this:
 

import java.util.Scanner;public class HotelBooking {		static Scanner keyboard = new Scanner(System.in);	static int whichRoom;	static int numGuests;	static int guestsIn[];	static int choosenRoom;	static int roomNum;	public static void main(String args[]) {				guestsIn = new int[10];				// Initialise the array		for (int roomNum = 0 ; roomNum < 10 ; roomNum++)		{ guestsIn[roomNum] = 0; }				booking(args);	}		public static void booking(String args[]) {				do {			choosenRoom = numGuests;						System.out.print("Which room do you want? (0-9)");			whichRoom = keyboard.nextInt();			System.out.print("How many are staying in that room?");			numGuests = keyboard.nextInt();						// Check if the room is already taken, choosenRoom used		if (choosenRoom != 0) {			System.out.println("I am sorry. That room is taken!");			System.out.println("You have to go back and choose another one");			System.out.println();						/* Restart the method without wiping the memory of thegu			* first booked room			*/			booking(args);		}						// Check if the user wants to do another booking			guestsIn[whichRoom] = numGuests;			System.out.println();			System.out.println("Do another? (Y/N)");					} while (keyboard.findWithinHorizon(".", 0).charAt(0) == 'Y');				System.out.println();		printOut(args);		}		// Print out an overview of the rooms	public static void printOut(String args[]) {		System.out.println();		System.out.println("Room\tGuests");		for (int roomNum = 0; roomNum < 10; roomNum++) {			System.out.print(roomNum);			System.out.print("\t");			System.out.println(guestsIn[roomNum]);		}	}}

If you are a programmer, I guess you can easily see that this program won't work the way it is intended to work. The program will tell you that the room is overbooked the second time you book, even if you try to book a free room. 

 

I am just wondering if I have started on a task that is too difficult for my level, or if there is just a small mistake in my very, very basic algorithm here. 
This program included most of the aspects of programming I know. I know how to write/read from files and the different loops in addition to this. Just to let you know what I can do.

 

​I would like to note that all help is appreciated - BUT!! I don't want the answer. I want a hint in the right direction (If you think I am capable of doing it with my current knowledge of programming and Java).

 

Thanks. 

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to comment
https://linustechtips.com/topic/202743-slight-java-issue-very-basic/
Share on other sites

Link to post
Share on other sites

			// Check if the room is already taken, choosenRoom used		if (choosenRoom != 0) {

well you want to check whether the room is empty or not, and you're doing this checking the "choosenRoom" variable

does that variable represent the 'prenotation state' of a room?

nope
			choosenRoom = numGuests;

this line looks quite odd if you think of the meaning of the variables, in fact you'll see you don't need it

Link to post
Share on other sites

well you want to check whether the room is empty or not, and you're doing this checking the "choosenRoom" variable

does that variable represent the 'prenotation state' of a room?

nope
			choosenRoom = numGuests;

this line looks quite odd if you think of the meaning of the variables, in fact you'll see you don't need it

 

I am not sure what you mean with prenotation state. Removing the statement doesn't make it any better, obviously. I have no clue what I am supposed to check since that is not working.

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

I am not sure what you mean with prenotation state. Removing the statement doesn't make it any better, obviously. I have no clue what I am supposed to check since that is not working.

You want to put prople into the room number 'whichRoom'

You can only di that if that room is empty

Numerically, this meana that you can put People in that room only if it has currently 0 People in it

You do have a variable that keeps track of the number of People in each room, so you just need to check the correct value in that variable (array)

Also, i think you Got an incorrect understanding of what the variable args means: There is no point in passing it as a parameter in every function

Also, you could use the 'continue' statement instead of booking(args) to restart the loop

Link to post
Share on other sites

You want to put prople into the room number 'whichRoom'

You can only di that if that room is empty

Numerically, this meana that you can put People in that room only if it has currently 0 People in it

You do have a variable that keeps track of the number of People in each room, so you just need to check the correct value in that variable (array)

Also, i think you Got an incorrect understanding of what the variable args means: There is no point in passing it as a parameter in every function

Also, you could use the 'continue' statement instead of booking(args) to restart the loop

Alright, thank you.

Yes I am still a bit unsure about how arrays work. I will look into that, and how to keep track of the people in each room.

 

Thank you for the help!

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

Alright, thank you.

Yes I am still a bit unsure about how arrays work. I will look into that, and how to keep track of the people in each room.

 

Thank you for the help!

The "Args" statement is used only when the program is starting (In the main function).

 

If you run a program like "java programName Argument1 Argument2", then args would have 2 parts to the array.

 

args[0] would be "Argument1"

args[1] would be "Argument2".

 

You don't need to put Args in every function because it would mean that you have to pass a parameter that is not used.

----------------------------------

----------------------------------

 

This array can be thought of like this:

 

Create an array called Hotel with 10 elements, then you can reference each 'segment' of the array with the room id.

 

                    ------------------------------------------------------------

                    | int |  int |  int |  int |  int |  int |  int |  int |  int |  int |

                    ------------------------------------------------------------

 

Room Num:    0      1    2     3       4      5      6     7      8     9 

 

e.g: hotel[roomNumber] = guestNumber;

 

----------------------------------

----------------------------------

Edited to remove answer: sorry.

Link to post
Share on other sites

​I would like to note that all help is appreciated - BUT!! I don't want the answer. I want a hint in the right direction

-posts the answer-

the first part of your post should be enough, if he wants to work out the solution by himself

Link to post
Share on other sites

import java.util.Scanner;public class HotelBooking {		static Scanner keyboard = new Scanner(System.in);	static int whichRoom;	static int numGuests;	static int guestsIn[];	static int choosenRoom;	static int roomNum;	public static void main(String args[]) {				guestsIn = new int[10];				// Initialise the array		for (int roomNum = 0 ; roomNum < 10 ; roomNum++)		{ guestsIn[roomNum] = 0; }		booking(args);	}		public static void booking(String args[]) {				do {						System.out.print("Which room do you want? (0-9)");			whichRoom = keyboard.nextInt();			System.out.println("How many are staying in that room?");			numGuests = keyboard.nextInt();						// Check if the room is already taken, choosenRoom used					if (guestsIn[whichRoom] != 0) {			System.out.println("I am sorry. That room is taken!");			System.out.println("You have to go back and choose another one");			System.out.println();			/* Restart the method without wiping the memory of the			* first booked room			*/			booking(args);		}						// Check if the user wants to do another booking			guestsIn[whichRoom] = numGuests;			System.out.println();			System.out.println("Do another? (Y/N)");					} while (keyboard.findWithinHorizon(".", 0).charAt(0) == 'Y');				System.out.println();		printOut(args);		}		// Print out an overview of the rooms	public static void printOut(String args[]) {		System.out.println();		System.out.println("Room\tGuests");		for (int roomNum = 0; roomNum < 10; roomNum++) {			System.out.print(roomNum);			System.out.print("\t");			System.out.println(guestsIn[roomNum]);		}	}}

I would just like to say that I have managed to finish the program! Thanks for the help.

Running Arch with i3-gaps on a Thinkpad X1 Extreme
Data Science Postgrad

 

Link to post
Share on other sites

You can remove the Args part from the booking function and the print function. It is not needed at all.

 

Other then that, well done.

import java.util.Scanner;public class HotelBooking {        static Scanner keyboard = new Scanner(System.in);    static int whichRoom;    static int numGuests;    static int guestsIn[];    static int choosenRoom;    static int roomNum;    public static void main(String args[]) {                guestsIn = new int[10];                // Initialise the array        for (int roomNum = 0 ; roomNum < 10 ; roomNum++)        { guestsIn[roomNum] = 0; }        booking();    }        public static void booking() {                do {                        System.out.print("Which room do you want? (0-9)");            whichRoom = keyboard.nextInt();            System.out.println("How many are staying in that room?");            numGuests = keyboard.nextInt();                        // Check if the room is already taken, choosenRoom used                    if (guestsIn[whichRoom] != 0) {            System.out.println("I am sorry. That room is taken!");            System.out.println("You have to go back and choose another one");            System.out.println();            /* Restart the method without wiping the memory of the            * first booked room            */            booking();        }                        // Check if the user wants to do another booking            guestsIn[whichRoom] = numGuests;            System.out.println();            System.out.println("Do another? (Y/N)");                    } while (keyboard.findWithinHorizon(".", 0).charAt(0) == 'Y');                System.out.println();        printOut();        }        // Print out an overview of the rooms    public static void printOut() {        System.out.println();        System.out.println("Room\tGuests");        for (int roomNum = 0; roomNum < 10; roomNum++) {            System.out.print(roomNum);            System.out.print("\t");            System.out.println(guestsIn[roomNum]);        }    }}
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×