Results 1 to 4 of 4
  1. #1
    unknown2 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Make my main 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 {

    /**
    * @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 ");
    }}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Make my main program simple

    Hello and welcome to the forum!

    Please help us help you by editing your post above and surrounding your posted code with [code] [/code] tags. This will allow your code to retain its formatting and hopefully be more readable. Please be sure to use standard and regular indentation, to avoid inadequate use of whitespace (you want to avoid having }} for instance) and avoid over-use of whitespace too.

    Best of luck!

  3. #3
    unknown2 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Make my main program simple

    Java Code:
    import java.util.*;
    public class reservation {
    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 ");}}
    i hope that is better

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Make my main program simple

    Quote Originally Posted by unknown2 View Post
    i hope that is better
    Surely you can see that it isn't. All your code usually isn't full left justified is it? Surely you use indentations wisely, right? Please, again, edit your *original* post since we only need to see your code once, and please, again, only post well formatted code with regular and consistent indentation to begin with. Again if we can easily read and understand your code we can more easily help you. If we can't read it, well, then we're all out of luck.

Similar Threads

  1. Replies: 11
    Last Post: 10-30-2012, 10:28 AM
  2. Replies: 1
    Last Post: 06-05-2012, 05:31 PM
  3. Replies: 11
    Last Post: 01-25-2012, 04:06 PM
  4. Replies: 4
    Last Post: 07-23-2011, 09:08 PM
  5. Replies: 4
    Last Post: 01-01-2009, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •