Commit a3d062d6 authored by Josselin ENET's avatar Josselin ENET

Added class to reserve for an agent by admin #16

Added class which handles the reservation of a travel for an agent from
an admin account.
parent 3d859c91
package fr.unantes.software.construction.ui;
import fr.unantes.software.construction.calendar.Calendar;
import fr.unantes.software.construction.calendar.Travel;
import fr.unantes.software.construction.people.Agent;
import fr.unantes.software.construction.people.Person;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;
import java.util.List;
import static javafx.scene.control.Alert.AlertType.ERROR;
import static javafx.scene.control.Alert.AlertType.INFORMATION;
public class AdminTravelReservationController extends SceneController {
public AdminTravelReservationController(GUI gui) { super(gui); }
@Override
public Scene createScene() {
gui.setTitle("Travel Reservation for Agent");
GridPane root = new GridPane();
// users combo box
ComboBox usersComboBox = new ComboBox();
updateUsersComboBox(usersComboBox);
Label lbUsersComboBox = new Label("Name:");
lbUsersComboBox.setLabelFor(usersComboBox);
Callback<ListView<Person>, ListCell<Person>> userCellFactory = new Callback<ListView<Person>, ListCell<Person>>() {
@Override
public ListCell<Person> call(ListView<Person> l) {
return new ListCell<Person>() {
@Override
protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getName() + " / " + item.getRole());
}
}
};
}
};
usersComboBox.setCellFactory(userCellFactory);
ListCell<Person> userButtonCell = new ListCell<Person>() {
@Override
protected void updateItem(Person item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getName() + " / " + item.getRole());
} else {
setText(null);
}
}
};
usersComboBox.setButtonCell(userButtonCell);
//travels combo box
ComboBox travelsComboBox = new ComboBox();
updateTravelsComboBox(travelsComboBox);
Label lbTravelsComboBox = new Label("Travel:");
lbTravelsComboBox.setLabelFor(travelsComboBox);
Callback<ListView<Travel>, ListCell<Travel>> travelCellFactory = new Callback<ListView<Travel>, ListCell<Travel>>() {
@Override
public ListCell<Travel> call(ListView<Travel> l) {
return new ListCell<Travel>() {
@Override
protected void updateItem(Travel item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setText(item.getFirstStep().getStartCity().getName() + " - " + item.getLastStep().getDestinationCity().getName());
}
}
};
}
};
travelsComboBox.setCellFactory(travelCellFactory);
ListCell<Travel> travelButtonCell = new ListCell<Travel>() {
@Override
protected void updateItem(Travel item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getFirstStep().getStartCity().getName() + " - " + item.getLastStep().getDestinationCity().getName());
} else {
setText(null);
}
}
};
travelsComboBox.setButtonCell(travelButtonCell);
// reserve button
Button reserveButton = new Button();
reserveButton.setText("Reserve");
reserveButton.setOnAction(event -> {
Travel selectedTravel = (Travel) travelsComboBox.getValue();
Agent selectedUser = (Agent) usersComboBox.getValue();
if (selectedUser == null) {
AlertHandler.showAlert(ERROR, "No user selected.");
} else if (selectedTravel == null) {
AlertHandler.showAlert(ERROR, "No travel selected.");
} else {
handleReserve(selectedUser, selectedTravel);
}
});
// back button
Button backButton = new Button();
backButton.setText("Back");
backButton.setOnAction(event -> {
gui.setController(new AdminHomeController(gui));
});
root.add(lbUsersComboBox,0,0);
root.add(usersComboBox,2,0);
root.add(lbTravelsComboBox,0,2);
root.add(travelsComboBox,2,2);
root.add(reserveButton,0,4);
root.add(backButton,0,6);
Scene result = new Scene(root,400,250);
return result;
}
private void updateUsersComboBox(ComboBox usersComboBox) {
List<Person> usersList = gui.getUsersDB().getAllAgent();
ObservableListImplementation<Person> travelsObservableList = new ObservableListImplementation<>(usersList);
usersComboBox.setItems(travelsObservableList);
}
private void updateTravelsComboBox(ComboBox travelsComboBox) {
List<Travel> travelsList = gui.getTravelsDB().getAllTravel();
ObservableListImplementation<Travel> travelsObservableList = new ObservableListImplementation<>(travelsList);
travelsComboBox.setItems(travelsObservableList);
}
public void handleReserve(Agent user, Travel travel) {
Calendar userCalendar = user.getCalendarAssociation().get();
if (userCalendar.getTravelAssociation().contains(travel)) {
AlertHandler.showAlert(ERROR, "This user has already booked this travel.");
}
else {
gui.getCurrentUser().addTravelTo(travel, user);
AlertHandler.showAlert(INFORMATION, "Travel booked.");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment