Hi,

I am trying to do a hotel system where i have written up this code, however i was wondering if the there was a way to make the main program simple.

So what im trying to do is the code that ‘Views All rooms’ and ‘Adds customer to room’, put them into separate procedures and a menu so When i press ‘A’ is it will do the Add
procedure, and when i press ‘V’ the View procedure.

Below is the code i have written. I would really appreciate if anyone can help me

`package reservation;

import java.util.*;
/**
*
* @author alex
*/

public class reservation {package reservation;

import java.util.*;
/**
*
* @author alex
*/
public class reservation {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here


Scanner input = new Scanner(System.in);
String roomName;
int roomNum = 0;
String[] hotel = new String[7];
for (int x = 0; x < 6; x++ ) hotel[x] = ""; //initialise
initialise(hotel); //better to initialise in a procedure
while ( roomNum < 6 )
{

System.out.println("Enter room number (0-5) or 6 to stop:" ) ;
roomNum = input.nextInt();
System.out.println("Enter name for room " + roomNum +" :" ) ;
roomName = input.next();
hotel[roomNum] = roomName ;

for (int x = 0; x < 6; x++ )
{
System.out.println("room " + x + " occupied by " + hotel[x]);
}
}
}
private static void initialise( String hotelRef[] ) {
for (int x = 0; x < 6; x++ ) hotelRef[x] = "e";
System.out.println( "initilise ");


Scanner input = new Scanner(System.in);
Room[] myHotel = new Room[4];
myHotel[0] = new Room();
myHotel[1] = new Room();
myHotel[2] = new Room();
myHotel[3] = new Room();
String roomName;
int roomNum = 0;
initialise(myHotel);
while (roomNum < 4) {
for (int x = 0; x < 4; x++ )
if (myHotel[x].mainName.equals("e"))System.out.println("room " + x + " is empty");

System.out.println("Enter room number (0-3) or 4 to stop:"); //error with 4
roomNum = input.nextInt();
System.out.println("Enter name for room " + roomNum + " :");
roomName = input.next();
myHotel[roomNum].mainName = roomName ;
// myHotel[roomNum].setName(roomName);
for (int x = 0; x < 4; x++) {
System.out.println("room " + x + " occupied by " + myHotel[x].mainName);
// System.out.println("room " + x + " occupied by " + myHotel[x].getName());
}
}
}
private static void initialise( Room hotelRef[] ) {
for (int x = 0; x < 4; x++ ) hotelRef[x].mainName = "e";
System.out.println( "initilise ");
}}

Recommended Answers

All 82 Replies

You can start by posting your question in the java forum instead of VB.Net. Consider it moved.

You can start by posting your question in the java forum instead of VB.Net. Consider it moved.

I doubt it but maybe he wanted to make it simple by porting it to VB .NET (doubt it but anything is possible)

i am trying to make seperate the procedures in this code...

so, just to clear the doubts raised earlier:
what language do you want this in? Java?
if so, Java has methods, not procedures.
and what exactly do you want in methods(procedures)?

In java..

i want to put the code that ‘Views All rooms’ and ‘Adds customer to room’,
into separate procedures(methods) just to see if it will work. as this would make my main program simpler as it would jump to do this method then the next etc..

I want a menu system so the user can select what they want. for example what i have thought of doing is
Enter an ‘A’ to add a customer to a room,
a ‘V’ to view all rooms so When an ‘A’ is pressed it should do the Add procedure, or a ‘V’ should do the View procedure
E: Display Empty rooms,
D: Delete customer from room,
F: Find room from customer name,
S: Store program data in to file,
L: Load program data from file.
O: View rooms Ordered alphabetically by name.

Just prompt the user for one of those chars, then use a switch to call the appropriate methods,eg

switch (inputChar) {
   case 'V' {viewAllRooms(); break}
   case 'A' {addCustomer()....  
   ... etc etc

(If you are using Java 7 you can have a switch using a String, but earlier Javas need a single char)

i am using netbeans

Netbeans is your development environment. It works with various recent versions of Java, including the most recent - Java 7

yh im using neteans java7..

i tried doing the code u gave me but it crashed :(

The "... etc etc" was intended to tell you that the code was incomplete. I'm not going to write your program for you. You can find information and tutorials on the switch statement, then use the example I gave you to apply that information to your own program.
I have pointed you in the right direction, but you have to make the journey yourself.

i never said i used the etc etc i know what that means and i aint stupid to write that in the program...
i never said write the damn program for me!!
NO THANKS FOR UR HELP!!

commented: Nice attitude :( -3
commented: Check your attitude, boy. -1

No need to take it personally. No insult was intended. You would probably be surprised how many beginners would literally copy and paste a fragment like that. You didn't show any code, so I had to guess what it was that crashed.
Anyway, I'll stay out of this thread if that's what you want.
J

i never said i used the etc etc i know what that means and i aint stupid to write that in the program...
i never said write the damn program for me!!
NO THANKS FOR UR HELP!!

First you need to calm the fuck down. Thats first off.
Second, posting in the wrong section and being told that Java 7 has this feature and answering you are using Netbeans means that Java is pretty new for you (Id say even programming in general is new for you but oh well...)

I see you have 2 initialise functions with different types of parameters. You should really name them differenly to know exactly what is going on in your program.

We need some more explainations on what exactly you want to acomplish.

public static Scanner sc = new Scanner(System.in);

        // TODO code application logic here
 String names[]=new String[0];
        int rooms[]=new int[0]; 
            while(true){
                int choice=0;
                System.out.println("\n\nMENU ");//menu
                System.out.println("A) Add Guest");
                System.out.println("V) View All Rooms");
                System.out.println("E) Display Empty Rooms");
                System.out.println("F) Find Room From Customer Name");
                System.out.println("S) Store Program data into file");
                                System.out.println("L) Load Program from data file");
                                System.out.println("L) View Rooms Alphabetically");
                System.out.print("Choice: ");
                choice=sc.nextInt();
                if(choice==10||choice ==0)//either or
                    break;
        switch(choice)

I have started again and decided to do the menu first and use the suggested switch case statment.
I stuck on how to do letter selection (choices) rather than numbers.

i am new to java and i am only writing on here to get help and guidence not to be insulted.
if you would like to help them please do if not thanks anyways.

public class Hotelprogram {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        // TODO code application logic here
 String names[]=new String[0];
        int rooms[]=new int[0]; 
            while(true)
            {
                int inputChar=0;
                System.out.println("\n\nMENU ");//menu
                System.out.println("1) Add Guest");
                System.out.println("2) View All Rooms");
                System.out.println("3) Display Empty Rooms");
                System.out.println("4) Find Room From Customer Name");
                System.out.println("5) Store Program data into file");
                                System.out.println("6) Load Program from data file");
                                System.out.println("7) View Rooms Alphabetically");
                System.out.print("Choice: ");

switch (inputChar) {
   case 'V' {viewAllRooms(); break}
   case 'A' {addCustomer(); break}
   case 'E' {displayEmptyRooms(); break}
   case 'F' {FindRoomFromCustomerame(); break}
   case 'S' {StoreProgramInFile(); break}
   case 'L' {LoadProgramFromFile(); break}
   case 'O' {ViewRoomsInAlphabeticalByName(); break 

                    case 'V':
                    {
                        //Add Guest
                        sc.nextLine();
                        System.out.print("Please enter your guest: ");
                        String guest=sc.nextLine();
                        names=addName(guest,names);
                        System.out.print("Room: ");
                        int room=sc.nextInt();
                        rooms=addRoom(room,rooms);
                        //sort rooms by ascending... don't use arrays.sort!
                        for(int x=0;x<rooms.length;x++)
                        {
                            for(int y=0;y<rooms.length;y++)
                            {
                                if(rooms[x]<rooms[y])
                                {
                                    int dummy=rooms[x];
                                    String dummy2=names[x];
                                    rooms[x]=rooms[y];
                                    names[x]=names[y];
                                    rooms[y]=dummy;
                                    names[y]=dummy2;
                                }
                            }
                        }
                        break;
                    }//

please can someone tel me what i am doing wrong

For starters your switch statment's syntax is not correct.

it follow this format.

switch(char){
    case 'A':  //notice no brackets anround individual cases
        //call some method
    break;  //notice the semicolon
    case 'B':
        //call some other method
    break;
    . . . ect.
}

once you fix that if you still have problems post the stack trace.

one side note: No one here is intentionaly going to insult you we are all here to help and we simply post the first possible problem. Dont take anything said on the forum personaly and be patient with those trying to help you. We dont know what is going on as well as you do and we dont have knowledge of what you already know.

Just try to describe the problem as best you can and tell us what you already tried and you will get help much faster.

Thank you i have changed the code to how u suggested but there was an error...
the code switch(char) came up with a red line under it.. when i ran the program is was the error i faced.

MENU
1) Add Guest
2) View All Rooms
3) Display Empty Rooms
4) Find Room From Customer Name
5) Store Program data into file
6) Load Program from data file
7) View Rooms Alphabetically
Choice: 1

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unexpected type
  required: value
  found:    class
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:38)
Java Result: 1

What have a i done wrong?

im sorry about that i forgot char is an identifier in java so it cant be used as a variable also i noticed in your earlier post you had inputchar as an int and you never read from the consol to set it so your switch statement wont work anyway.

so you need to create a Scanner to read from the consol
Scanner scan = new Scanner(System.in); System.in is the standard input method i.e. theconsol
then read the value that the user enters and store in inputChar.
inputChar = scan.nextChar();
next comes your switch statement to find out what the user entered.

you may want to create a default case in case the user enters something you did not expect.
e.g. add

default:
    //what to do when something unexpected happens.
break;

to the end of your switch statement.

last thing: line 33 and on of your previous post should be outside of the switch statement and inside of your different methods such as

addCustomer(){
//code goes here
}

I HAVE PUT IN THE SCANNER AT THE TOP AND THE INPUTCHAR SCAN NEXT TO THE SWITCH STATEMENT BUT THERE IS STILL AN ERROR. I DONT UNDERSTAND WHY.

 public static Scanner sc = new Scanner(System.in);
   Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO code application logic here

 String names[]=new String[0];
        int rooms[]=new int[0]; 
            while(true)
            {
                int choice=0;
                System.out.println("\n\nMENU ");//menu
                System.out.println("1) Add Guest");
                System.out.println("2) View All Rooms");
                System.out.println("3) Display Empty Rooms");
                System.out.println("4) Find Room From Customer Name");
                System.out.println("5) Store Program data into file");
                                System.out.println("6) Load Program from data file");
                                System.out.println("7) View Rooms Alphabetically");
                System.out.print("Choice: ");
                choice=sc.nextInt();
                if(choice==10||choice ==0)//either or
                    break;



                          inputChar = scan.nextChar();
            switch(Char){

                     case 'A':
                    {
                        //Add Guest
                        sc.nextLine();
                        System.out.print("Please enter your guest: ");
                        String guest=sc.nextLine();
                        names=addName(guest,names);
                        System.out.print("Room: ");
                        int room=sc.nextInt();
                        rooms=addRoom(room,rooms);
                        //sort rooms by ascending... don't use arrays.sort!
                        for(int x=0;x<rooms.length;x++)
                        {
                            for(int y=0;y<rooms.length;y++)
                            {
                                if(rooms[x]<rooms[y])
                                {
                                    int dummy=rooms[x];
                                    String dummy2=names[x];
                                    rooms[x]=rooms[y];
                                    names[x]=names[y];
                                    rooms[y]=dummy;
                                    names[y]=dummy2;
                                }
                            }
                        }
                        break;
                    }

you missed part of it.

line 28 change char to inputChar

also you only need one scanner just call the function you need from the one also remove the public static and move the scanner creation inside of the main method.

your menu is requesting integers as a way of selecting different options if you wnat to do that instead of characters replace the 'A' with just 1 and the 'V' with just 2 . . .

i copied your code and corrected a few things the rest is for you to fill in:

switch(inputChar)  //so compair the character you read the ones provided for each case
    case 'A':
    addGuest();  //all the code that was here is replaced by this line which points to the code you had here
    break;
    case 'V':
    viewRooms();
    break;
    //. . . ect
    default:
    //if you want to do something when the user enteres an invalid character put it here.
    break;
}
public void addGuest(){  //moved all your code inside of case A down here.
    //Add Guest
    sc.nextLine();
    System.out.print("Please enter your guest: ");
    String guest=sc.nextLine();
    names=addName(guest,names);
    System.out.print("Room: ");
    int room=sc.nextInt();
    rooms=addRoom(room,rooms);
    //sort rooms by ascending... don't use arrays.sort!
    for(int x=0;x<rooms.length;x++)
    {
        for(int y=0;y<rooms.length;y++)
        {
            if(rooms[x]<rooms[y])
            {
                int dummy=rooms[x];
                String dummy2=names[x];
                rooms[x]=rooms[y];
                names[x]=names[y];
                rooms[y]=dummy;
                names[y]=dummy2;
            }
        }
    }
}
public void viewRooms(){
    //code to view rooms
}
//more functions go down here

Edit: almost forgot if you decide to chage to using integers instead of characters make sure you change the data type of inputChar to int and read an int with the scanner instead of a char like you are now.

hope i answered all your questions.

if not ill be back in a few hours and answer any more you have.

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at hotelprogram.Hotelprogram.main(Hotelprogram.java:37)
Java Result: 1

I HAVE EDITIED AS ABOVE BUT I AM STILL GETTING ERRORS ALOT MORE THIS TIME.

The string variables arent being used in the switch method. I have tried to move it but not still cannot get it working.

 public static Scanner sc = new Scanner(System.in);
   Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        // TODO code application logic here

 String names[]=new String[0];
        int rooms[]=new int[0]; 
   while(true)
   {
                int choice=0;
                System.out.println("\n\nMENU ");//menu
                System.out.println("1) Add Guest");
                System.out.println("2) View All Rooms");
                System.out.println("3) Display Empty Rooms");
                System.out.println("4) Find Room From Customer Name");
                System.out.println("5) Store Program data into file");
                                System.out.println("6) Load Program from data file");
                                System.out.println("7) View Rooms Alphabetically");
                System.out.print("Choice: ");
                choice=sc.nextInt();
                if(choice==10||choice ==0)//either or
                    break;

                              inputChar = scan.nextChar();
                              switch(inputChar)
                                case 'A':
                                addGuest();
                                break;
                               case 'V':
                               viewRooms();
                                break;
                               default:
                                   break;
                              }}}
                              public void addGuest(){
                        //Add Guest
                        sc.nextLine();
                        System.out.print("Please enter your guest: ");
                        String guest=sc.nextLine();
                        names=addName(guest,names);
                        System.out.print("Room: ");
                        int room=sc.nextInt();
                        rooms=addRoom(room,rooms);
                        //sort rooms by ascending... don't use arrays.sort!
                        for(int x=0;x<rooms.length;x++)
                        {
                            for(int y=0;y<rooms.length;y++)
                            {
                                if(rooms[x]<rooms[y])
                                {
                                    int dummy=rooms[x];
                                    String dummy2=names[x];
                                    rooms[x]=rooms[y];
                                    names[x]=names[y];
                                    rooms[y]=dummy;
                                    names[y]=dummy2;
                                }
                            }
                        }
                              }

                     public void viewRooms(){
                    {
                        //List All Guests and Room Numbers
                        System.out.println("ROOM\t\t\tGUEST");
                        for(int x=0;x<rooms.length;x++)
                        {
                            System.out.println(rooms[x]+"\t\t\t"+ names[x]);
                        }

                                        }}

here is your problem you are scanning when there is nothing to scan. you need to choose to do your menu options with either numbers or characters.

line 1: delete

line 2: move inside of main (like i said above)

line 21: your reading an int as the user's selection then on line 25 your reading the consol again for the user's selection. Since you already read it there is nothing to read in consol. This is what i was talking about earlier when i said you need to pick to use characters or integers. (note: you can use the character version of the numbers if you want to use v and other characters.)

--also change all sc to scan.

line 26: your missing a brace after the switch(inputChar) (sorry about that i made a typo above when i told you how to write the switch)

also where is your class declairation? e.g. public class HotelProgram {
this should come right after your import statements.

If you dont understand any part of this please say so i will explain it.

Im downloading the Java 7 JDK.....

Just like others have said, we are trying to help you but we have no ideas what your main goal is and/or what you currently know.

Work from this:

package tests;

import java.util.Scanner;

public class Tests 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        String names[]=new String[0];
        int rooms[]=new int[0];

        do
        {
             int choice=0;
             System.out.println("\n\nMENU ");//menu
             System.out.println("1) Add Guest");
             System.out.println("2) View All Rooms");
             System.out.println("3) Display Empty Rooms");
             System.out.println("4) Find Room From Customer Name");
             System.out.println("5) Store Program data into file");
             System.out.println("6) Load Program from data file");
             System.out.println("7) View Rooms Alphabetically");
             System.out.print("Choice: ");
             choice=scan.nextInt();
             if((choice==10) || (choice ==0))//either or
             {
                 break;
             }
             char inputChar = (char)scan.nextLine().charAt(0); //No idea what you are trying to do here
             switch(inputChar)
             {
                case 'A':
                 addGuest(names);
                 break;
                 case 'V':
                 viewRooms();
                 break;
                 default:
                 break;
             }


        }while(true);


    }


     public static void addGuest(String[] names)
     {
         //Add Guest
         Scanner sc = new Scanner(System.in);
         //sc.nextLine();
         System.out.print("Please enter your guest: ");
         String guest=sc.nextLine();
         names=addName(guest,names);
         System.out.print("Room: ");
         int room=sc.nextInt();
         rooms=addRoom(room,rooms);
         //sort rooms by ascending... don't use arrays.sort!
         for(int x=0;x<rooms.length;x++)
         {
             for(int y=0;y<rooms.length;y++)
             {
                 if(rooms[x]<rooms[y])
                 {
                     int dummy=rooms[x];
                     String dummy2=names[x];
                     rooms[x]=rooms[y];
                     names[x]=names[y];
                     rooms[y]=dummy;
                     names[y]=dummy2;
                 }
             }
         }
      }



}

BTW, thats just "working" code. I have no idea what you want to acomplish.

MY AIM IS USING THE FIRST CODE I POSTED TO EDIT THAT TO DO THE FOLLOWING..

i want to put the code that ‘Views All rooms’ and ‘Adds customer to room’,
into separate Methods just.

MENU SYSTEM
I want a menu system so the user can select what they want using the menu. for example what i have thought of doing is when u
Enter an ‘A’ to it will run the method add a customer to a room,
‘V’ to view all rooms
E: Display Empty rooms,
D: Delete customer from room,
F: Find room from customer name,
S: Store program data in to file,
L: Load program data from file.
O: View rooms Ordered alphabetically by name.

The menu system is all based on entering the letter to run the methods, i dont want anything to do with number seletion.

I am a new to java and so i will know the basics not alot doe. I have been going through various sites to find tutorials to understand evrything.

I dont understand this bit of code which is on line 21. did change the int to char then nextChar but there is an error. :/

line 21: your reading an int as the user's selection then on line 25 your reading the consol again for the user's selection. Since you already read it there is nothing to read in consol. This is what i was talking about earlier when i said you need to pick to use characters or integers.

since you want to read a character not an integer correct your menu to match and only read one letter from the consol right now your reading an integer at line 29 and a char at line 34
here is the correction based on what you asked. I added comments to all the lines that you appear to be having trouble with.

do
  {
    char choice;                                //i changed choice from int type to char type
    System.out.println("\n\nMENU ");//menu
    System.out.println("A) Add Guest");
    System.out.println("V) View All Rooms");
    System.out.println("D) Display Empty Rooms");
    System.out.println("F) Find Room From Customer Name");
    System.out.println("S) Store Program data into file");
    System.out.println("L) Load Program from data file");
    System.out.println("R) View Rooms Alphabetically");
    System.out.println("E) Exit program");
    System.out.print("Choice: ");
    choice=scan.nextChar();  //read the users selection and store it in choice
    if(choice=='E')          //if the selection is E exit the loop
    {
      break;
    }
    switch(Choice)           //compairs the value placed in paranthesis to the value of each case
                             //until it finds a match.  If it does not find a match it will go to 
                             //default case.  If there is no default case it exits the switch statement. 
    {
      case 'A':          //If the letter inputed equals A call addGuest();
        addGuest(names); //Calls add guest
      break;
      case 'V':          //If the letter inputed equals V call viewRooms()
        viewRooms();     //Calls viewRooms()
      break;
      //add cases for other leters here
      default:
      break;
    }
  }while(true);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.