dimanche 29 mars 2015

Program loops where it shouldnt [duplicate]




This question already has an answer here:




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


}


}




Aucun commentaire:

Enregistrer un commentaire