Assalam Alaikum

Could any one help pleeeeeeas
This is a JAVA code written using Array data structure !
I have to convert it using a binary tree data structure !
I really face difficulty in doing that !!!
I mean, I don't know where to start in the process :""((
especially I'm a beginner in JAVA !

I will be really appreciate you quick help :"))

:
This is the complete code :

import java.util.Scanner;

class ReservationF 
{ 
          Scanner input = new Scanner(System.in);    
      private static int index=11;
      private static String resRom[] = new String[index];
      private static int roomNum;
      private static int rNo;
      private static String A="Available";
      private static String R="Reserved";
      private static int rNo1;
      private static int ask;
      private static int enter;

   public static void main(String args[])
    {
          Scanner input = new Scanner(System.in);
          System.out.print( "\t**************************************************\n\tWelcome To University Housing Reservation System \n\t\t\t\t ( UHRS )\n\t**************************************************");
          System.out.print( "\t\n\nEnter your choice:\n\t 1 : Administrator\n\t 2 : User\n\t" );
          int AU = input.nextInt();
          if( AU == 1 )
            { 
                System.out.print("\n\t Welcome Administrator :) \n");
                System.out.print("What do you want to do ?");
                System.out.print("\n\nAdmin Menu \n [1] - Add a Room \n [2] - Remove a Room \n [3] - Exit"); 
                System.out.print("\n\n\nEnter Admin Menu: ");
                int AdminMenu = input.nextInt();

                switch(AdminMenu)
                {
                    case 1:

                        System.out.print("Number of Rooms existing now is\t");
                        System.out.println(index);
                        System.out.print("Make the number of Rooms to be : ");
                        int AddNo = input.nextInt();
                        index = AddNo;
                        System.out.print("The Number of Rooms become\t");
                        System.out.println(index);
                        break;

                    case 2:
                        System.out.print("\nEnter the Room Number you want to be deleted :");
                        int DelNo = input.nextInt();
                        resRom[DelNo+1] = resRom[DelNo];
                        break;

                    case 3:

                        System.exit(0);
                        break;


                }
            } //end of Admin

                if ( AU == 2 )
                {
                    System.out.print("\t\n Welcome User :)  ");
                     System.out.print("STEP [1]:\n Please Enter Your personal information to let us contact you when the reservation is approved.\n");
                     System.out.print("Enter your First Name:");
                     String Fname = input.next();
                     System.out.print("Enter your MIDDLE Name:\t");
                     String Mname = input.next();
                     System.out.print("Enter your Last Name:");
                     String Lname = input.next();
                     System.out.print("Enter your ID number:\t");
                     int idNo = input.nextInt();
                     System.out.print("Phone:");
                     int phone = input.nextInt();
                     System.out.print("Enter your Email Address:\t");
                     String Email = input.next();
                     System.out.println("-------------------------------------------------------------------------------------------------- ");        
                     System.out.println("STEP [2]:\n Reservation.\n"); 
                     System.out.println("\nReservation STEPS \n [1] - Reserve a room  \n [2] - Total Cost \n [3] - Confirm/Cancel Reservation \n [4] - Exit/Do another Reservation"); 
                     roomDis();
                     menu();

                }

            }

             static void roomDis() 
    {
        System.out.println("\nROOM #.     STATUS\n\n");
        roomNum=1;
        for(int i=1;i<index;i++)
        {   
            resRom[i]=A;
            System.out.println("   " +roomNum + "\t    " +resRom[i]);
            roomNum++;
        }   
    }//end roomDis

    static void roomDisOrg() 
    {   
        System.out.println("\nROOM #.     STATUS\n\n");
        roomNum=1;
        for(int i=1;i<index;i++) 
        {   
            System.out.println("   " +roomNum + "\t    " +resRom[i]);
            roomNum++;
        }       
    }//end roomDisOrg

    static void menu() 
    {
         System.out.println("------------------------------------------[STEP 1] - Reserve a room---------------------------------- ");
         int Room = step1();
         System.out.println("------------------------------------------[STEP 2] - Cost-------------------------------------------- "); 
         int S = extraS();
         int RoomReserved = step2(Room,S);
         System.out.println("------------------------------------------[STEP 3] - Confirm/Cancel Reservation---------------------- ");  
         step3(RoomReserved);
         System.out.println("------------------------------------------[STEP 4] - Exit/Do another Reservation--------------------- ");
         step4();
    }////end menu 
    //================================
static int step1()
{          Scanner input = new Scanner(System.in);    
           System.out.print("Enter the room #: ");
                    rNo = input.nextInt();

                        if(resRom[rNo]==A) 
                        {   
                            resRom[rNo]=R;
                            roomDisOrg();
                            System.out.println("\n\nRoom number " + rNo + " reserved!"); 

                        }//end if
                        else
                        {
                           System.out.println("\n");
                           roomDisOrg();
                           System.out.println("\n\nRoom number " + rNo + " already reserved!");
                           menu();

                        }//end else
                        return rNo ;
}
//================================
    static int step2(int R, int S)
    {
    int r= R;
    int TotalCost = 0;
    if(R >=1 && R<5)
    {
        System.out.printf("\n Room %d cost 800 SR\n",r);
        TotalCost = 800 + S ;
        System.out.printf("\n Total Cost %d SR\n", TotalCost);
    }
    else
        {
            System.out.printf("\n Room %d cost 500 SR\n",r);
            TotalCost = 500 + S ;
            System.out.printf("\n Total Cost %d SR\n", TotalCost);
        } 
        return r;
}
//================================
    static void step3(int RR)
    {          Scanner input = new Scanner(System.in);    

                    int rNo1=RR;

                    System.out.print("\n[1]-Cancel/[2]-Confirm : ");
                    ask = input.nextInt();

                        if(ask==1)
                        {       
                            if(resRom[rNo1]==R)
                            {       
                               System.out.println("\n");
                               resRom[rNo1] = A;
                               roomDisOrg();
                               System.out.println("\n\nRoom number " + rNo1 + " is now " + A);

                            }//end if
                            else
                            {   
                               System.out.println("\n");
                               roomDisOrg();
                               System.out.println("\n\nRoom number " + rNo1 + " is already " + A);

                            }//end else     
                        }//end if 

                        else if(ask==2) 
                        {       System.out.println("\n");
                                roomDisOrg();
                        }//end else if 

                        else 
                        {   
                            System.out.println("Invalid Input...\n");          
                        }//end else 

                    }
//================================
    static void step4()
    {          
            Scanner input = new Scanner(System.in);    
            System.out.println(" Enter 1 to Do another reservation and 2 to Exit ");
            enter = input.nextInt();
        switch(enter)
        {
            case 1:
                menu();
                break;

            case 2:
            System.exit(0);
            break;
            default :
            break;
        }
    }
    //================================


    static int extraS()
    {
        Scanner input = new Scanner(System.in); 
        System.out.print("\n\t-------------------- UHRS Special Offers --------------------\n");
        System.out.print("\t| First Offer [1] |\n1- Food\n\t 2- Landury\n\t 3- Clean\n\t 4- Medical\n\t 5- Maintenance\n\t");
        System.out.print("Cost 600SR !\n");
        System.out.print("\t| Second Offer [2] |\n1- Food\n\t 2- Landury\n\t 3- Clean\n\t");
        System.out.print("Cost 300SR !\n");
        System.out.print("\t| Third Offer [3] |\nONLY FOOD SERVICE !\n\t");
        System.out.print("Cost 100SR !\n");
        System.out.print("\t| [3] DO NOT WANT EXTRA SERVICES\n");

        System.out.print("Enter the Offer Number you choose :");
        int ONo = input.nextInt();
        int v =0;
        switch(ONo)
         {
             case 1:
             v = 600;
             break;
             case 2:
             v =  300;
             break;
             case 3:
             v =  100;
             break;
             case 4:
             v =0 ;
             break;
          }
          return v ;
        }//end extraS

    }//end class Reservation

Edited 4 Years Ago by Ezzaral: Fixed code formatting.

Start by writing a simple testing class that creates the tree and adds nodes to it. Define a Node class with the necessary members for linking the left and right next nodes and the data.

This article has been dead for over six months. Start a new discussion instead.