1,105,229 Community Members

make my program simple

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ");
}}
Member Avatar
Reverend Jim
Noli mentula
5,387 posts since Aug 2010
Reputation Points: 744 [?]
Q&As Helped to Solve: 643 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

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

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
0
 

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)

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,290 posts since Jan 2007
Reputation Points: 935 [?]
Q&As Helped to Solve: 774 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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)?

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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)

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i am using netbeans

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yh im using neteans java7..

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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
2
 

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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-5
 

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!!

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
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

Member Avatar
riahc3
 
1,397 posts since May 2008
Reputation Points: 45 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 18 [?]
Team Colleague
 
2
 

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 #### 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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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

Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

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
}
Member Avatar
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article