0

I want the interface to go back to 'Enter a command' menu after it has completed a command (unless the user inputs "exit")but it keeps repeating the same interface for that command over and over again. e.g when the user inputs "register" the system should ask for name then phone number then email address and then says the staff member has been created (given the user input the correct type of input). The system should then ask for another command but instead it will ask for name, phone number and email address and will register another staff member. Please help?

public static void main(String args[]){
        RoomBookSystem rbs = new RoomBookSystem();

        //add rooms to the system
        Room room1=new Room();
        Room room2=new Room();
        Room room3=new Room();
        Room room4=new Room();

        room1.setName("1");
        room1.setCapacity(247);
        room1.setEquipment("Projector");;
        room1.setID(2);

        room2.setName("2");
        room2.setCapacity(18);
        room2.setEquipment("Waffle maker, television");
        room2.setID(2);

        room3.setName("3");
        room3.setCapacity(10);
        room3.setEquipment("Television");
        room3.setID(3);

        room4.setName("4");
        room4.setCapacity(12);
        room4.setEquipment("Piano");
        room1.setID(4);

        rbs.addRoom(room1);
        rbs.addRoom(room2);
        rbs.addRoom(room3);
        rbs.addRoom(room4);

        //adds staff to the system
        Staff kai = new Staff();
        Staff sehun = new Staff();
        Staff xiumin = new Staff();
        Staff chen = new Staff();

        kai.setName("Kai");
        kai.setPhoneNumber("123456789");
        kai.setEmailAddress("kai@exo.com");

        sehun.setName("Sehun");
        sehun.setPhoneNumber("6758302");
        sehun.setEmailAddress("yehet@exo.com");

        xiumin.setName("Xiumin");
        xiumin.setPhoneNumber("90");
        xiumin.setEmailAddress("xiurista@exo.com");


        chen.setName("Chen");
        chen.setPhoneNumber("609090900");
        chen.setEmailAddress("chensingmachine@exo.com");

        rbs.registerStaff(kai);
        rbs.registerStaff(xiumin);
        rbs.registerStaff(sehun);
        rbs.registerStaff(chen);


        //Starts the room booking interface
         System.out.println("*************************************************");
        System.out.println("Welcome to the room booking system!");
        System.out.println("*************************************************");
        System.out.println("The following rooms are available: ");
        rbs.printRooms();   
        System.out.println("-------------------------------------------------");
        System.out.println("Enter a command: (register, book, rooms, print, cancel, exit)");


        Scanner scan = new Scanner(System.in);
        String userCommand = scan.nextLine();
        //while(userCommand!="exit"){
        while(userCommand=="register"){

            System.out.println("enter your name: ");
            Staff newStaff = new Staff();
            String name = scan.nextLine();
            newStaff.setName(name);

            System.out.println("Enter your email address: ");
            String emailAddress = scan.nextLine();
            newStaff.setEmailAddress(emailAddress);

            System.out.println("Enter your phone number");
            String phoneNumber=scan.nextLine();
            newStaff.setPhoneNumber(phoneNumber);

            rbs.registerStaff(newStaff);

            System.out.println("Staff member: "+name+", "+emailAddress+", "+phoneNumber+" has been registered");


        }  if(userCommand=="book"){
            System.out.println("Booking a new meeting: ");
            System.out.println("Enter staff name");

            String staffName = scan.nextLine();

            if(rbs.isRegistered(staffName)){
            Staff staffBooker = new Staff();
                for(int i =0; i<rbs.currentStaff.length;i++){
                    if(rbs.currentStaff[i].getName()==staffName){
                        staffBooker=rbs.currentStaff[i];
                    }

                Room newRoom = new Room();
                System.out.println("Enter a room ID: ");
                int roomID = scan.nextInt();

                System.out.println("Enter a month: ");
                int month= scan.nextInt();

                System.out.println("Enter day: ");
                int day = scan.nextInt();

                System.out.println("Enter starting hour: ");
                int startingHour=scan.nextInt();

                System.out.println("Enter duration: ");
                int duration = scan.nextInt();  

                newRoom.setID(roomID);

                TimeInterval newTimeInterval = new TimeInterval(startingHour,day,month,duration);

                Meeting newMeeting = new Meeting(staffBooker, newTimeInterval, newRoom);

                rbs.bookMeeting(newMeeting);

                }
            }

        } else if(userCommand=="rooms"){
            Room newRoom = new Room();
            System.out.println("Add a new room");
            System.out.println("Enter room ID: ");
            int roomID = scan.nextInt();

            System.out.println("Enter room capacity: ");
            int roomCapacity = scan.nextInt();

            System.out.println("Enter equipment available in room: ");
           String equipment = scan.nextLine();

            newRoom.setID(roomID);
            newRoom.setCapacity(roomCapacity);
            newRoom.setEquipment(equipment);

            rbs.addRoom(newRoom);

        } else if(userCommand=="print"){
            rbs.printRooms();
            rbs.printMeeting();
            rbs.printStaff();

                scan.close();
                System.out.println("Goodbye");
                System.exit(0);


    }

}

Kieran
  • 43
  • 3
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – PM 77-1 Mar 30 '15 at 03:27
  • Have you debugged your code? That's probably the best way you can fine the flaw. Using IDEs help as well (being aware of all warnings in code makes life a lot easier). – Nader Ghanbari Mar 30 '15 at 03:28
  • 1
    I'd also consider writing a method which displays the menu options and prompts the user for input, returning what ever they enter, but that's just me – MadProgrammer Mar 30 '15 at 03:30
  • 1
    @MadProgrammer No no. Me too. It's not just you :) – candied_orange Mar 30 '15 at 03:31
  • @MadProgrammer - Very unfortunately but Java is taught in a way that for quite some time students are let to believe that all code should always go to `main()` method. – PM 77-1 Mar 30 '15 at 03:32
  • @PM77-1 Yep, OO is to "scary" for beginners :P – MadProgrammer Mar 30 '15 at 03:33
  • @MadProgrammer- I have a feeling that OO is still "scary" for many teachers as well. So they make students write procedural code using Java syntax. – PM 77-1 Mar 30 '15 at 03:34

1 Answers1

-1

Can you please change while(usercomman==register) to an if statement and give it a shot?

Eymen
  • 141
  • 1
  • 10