make my program simple
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 ");
}}
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
You can start by posting your question in the java forum instead of VB.Net. Consider it moved.
Reverend Jim
Carpe per diem
3,588 posts since Aug 2010
Reputation Points: 561
Solved Threads: 445
Skill Endorsements: 32
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)
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
i am trying to make seperate the procedures in this code...
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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)?
stultuske
Industrious Poster
4,366 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 23
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.
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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)
JamesCherrill
... trying to help
8,497 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
Netbeans is your development environment. It works with various recent versions of Java, including the most recent - Java 7
JamesCherrill
... trying to help
8,497 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
yh im using neteans java7..
i tried doing the code u gave me but it crashed :(
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
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.
JamesCherrill
... trying to help
8,497 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
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!!
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0
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
JamesCherrill
... trying to help
8,497 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
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.
riahc3
1,282 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11
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.
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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?
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 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;
}
uknown2
Junior Poster in Training
52 posts since Feb 2013
Reputation Points: -4
Solved Threads: 0
Skill Endorsements: 0