diff --git a/src/main/java/com/gitrekt/resort/controller/EditPricesDialogController.java b/src/main/java/com/gitrekt/resort/controller/EditPricesDialogController.java new file mode 100644 index 0000000..608348d --- /dev/null +++ b/src/main/java/com/gitrekt/resort/controller/EditPricesDialogController.java @@ -0,0 +1,63 @@ +package com.gitrekt.resort.controller; + +import com.gitrekt.resort.model.services.RoomService; +import java.net.URL; +import java.util.Optional; +import java.util.ResourceBundle; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; +import javafx.scene.control.Button; +import javafx.scene.control.ButtonType; +import javafx.scene.control.TextField; +import javafx.stage.Stage; + +public class EditPricesDialogController implements Initializable { + + @FXML + private Button cancelButton; + + @FXML + private Button editButton; + + @FXML + private TextField editPriceField; + + @Override + public void initialize(URL location, ResourceBundle resources) { + + } + + public void onCancelButtonClicked() { + Stage dialogStage = (Stage) cancelButton.getScene().getWindow(); + dialogStage.close(); + ScreenManager.getInstance().switchToScreen("/fxml/EditPricesScreen.fxml"); + + } + + public void onEditButtonClicked() { + boolean isGoodInput = true; + double d = 0; + try { + d = Double.valueOf(editPriceField.getText()); + } catch (NumberFormatException e) { + isGoodInput = false; + } + if (isGoodInput == true) { + RoomService roomservice = new RoomService(); + + Alert a = new Alert(AlertType.CONFIRMATION); + a.setTitle("Price Change Confirmation"); + a.setHeaderText("Confirm the Price Change?"); + Optional result = a.showAndWait(); + if (result.get() == ButtonType.OK) { + roomservice.editRoomPrice(EditPricesScreenController.service, d); + Stage dialogStage = (Stage) cancelButton.getScene().getWindow(); + dialogStage.close(); + ScreenManager.getInstance().switchToScreen("/fxml/EditPricesScreen.fxml"); + } + } + } + +} diff --git a/src/main/java/com/gitrekt/resort/controller/EditPricesScreenController.java b/src/main/java/com/gitrekt/resort/controller/EditPricesScreenController.java index c185c95..7434ca2 100644 --- a/src/main/java/com/gitrekt/resort/controller/EditPricesScreenController.java +++ b/src/main/java/com/gitrekt/resort/controller/EditPricesScreenController.java @@ -2,6 +2,7 @@ import com.gitrekt.resort.model.entities.RoomCategory; import com.gitrekt.resort.model.services.RoomService; +import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import javafx.beans.property.SimpleDoubleProperty; @@ -9,38 +10,40 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.Parent; +import javafx.scene.Scene; import javafx.scene.control.*; +import javafx.scene.image.Image; +import javafx.stage.Modality; +import javafx.stage.Stage; -/** - * The FXML controller class for the edit prices screen. - */ public class EditPricesScreenController implements Initializable { + @FXML + private Button backButton; + @FXML + private Button editPriceButton; @FXML private TableView roomTableView; - @FXML private TableColumn roomNameColumn; - @FXML private TableColumn roomDescriptionColumn; - @FXML private TableColumn roomPriceColumn; private ObservableList room; - /** - * Initializes the FXML controller class. - * - * @param location - * @param resources - */ + static RoomCategory service; + + @Override public void initialize(URL location, ResourceBundle resources) { room = FXCollections.observableArrayList(); roomTableView.setItems(room); + roomTableView.refresh(); roomNameColumn.setCellValueFactory((param) -> { return new SimpleStringProperty( @@ -58,34 +61,46 @@ public void initialize(URL location, ResourceBundle resources) { return new SimpleDoubleProperty( param.getValue().getBasePrice() ).asObject(); - }); + } ); loadData(); + } - /** - * Pops up a dialog that prompts the user to change the price. - */ @FXML - private void onEditPriceClickedButton() { - // TODO + private void onEditPriceClickedButton() throws IOException { + + Stage editPriceDialogStage = new Stage(); + Parent editPriceDialogRoot = FXMLLoader.load( + getClass().getResource("/fxml/EditPriceDialog.fxml") + ); + service = roomTableView.getSelectionModel().getSelectedItem(); + Scene editPriceDialog = new Scene(editPriceDialogRoot); + + editPriceDialogStage.getIcons().add(new Image("images/Logo.png")); + editPriceDialogStage.setScene(editPriceDialog); + editPriceDialogStage.initModality(Modality.APPLICATION_MODAL); + editPriceDialogStage.initOwner(editPriceButton.getScene().getWindow()); + editPriceDialogStage.setResizable(false); + editPriceDialogStage.setTitle("Edit Price"); + editPriceDialogStage.centerOnScreen(); + editPriceDialogStage.show(); + } - /** - * Returns to the staff home screen. - */ @FXML private void onBackButtonClicked() { ScreenManager.getInstance().switchToScreen("/fxml/StaffHomeScreen.fxml"); } - /** - * Loads the pricing data for the resort. - */ private void loadData() { RoomService roomService = new RoomService(); room.addAll(roomService.getAllRoomCategories()); } + + + + } diff --git a/src/main/java/com/gitrekt/resort/model/entities/RoomCategory.java b/src/main/java/com/gitrekt/resort/model/entities/RoomCategory.java index 3b3f232..36b3527 100644 --- a/src/main/java/com/gitrekt/resort/model/entities/RoomCategory.java +++ b/src/main/java/com/gitrekt/resort/model/entities/RoomCategory.java @@ -7,11 +7,11 @@ import javax.persistence.Id; /** - * The type of room, containing properties such as the number of beds, a text description of the - * room, images of the room, etc. + * The type of room, containing properties such as the number of beds, a text + * description of the room, images of the room, etc. * - * Because all resort rooms in the same category share the same core set of features, there is no - * need to tie these properties to the room object itself. + * Because all resort rooms in the same category share the same core set of + * features, there is no need to tie these properties to the room object itself. */ @Entity public class RoomCategory { @@ -37,7 +37,7 @@ public class RoomCategory { } public RoomCategory(String name, String description, - String imagePath, String bedsInfo, Double basePrice) { + String imagePath, String bedsInfo, Double basePrice) { this.name = name; this.description = description; @@ -55,8 +55,8 @@ public String getDescription() { } /** - * The image representing this room category, based on the file path string provided when the - * category was created. + * The image representing this room category, based on the file path string + * provided when the category was created. */ public Image getImage() { return new Image(this.imageFilePath); @@ -69,11 +69,12 @@ public String getBedsInfo() { /** * DANGER! * - * This method only gives you the base price of a room, which is just a part of what goes into - * the pricing of a room. + * This method only gives you the base price of a room, which is just a part + * of what goes into the pricing of a room. * - * Other factors like resort capacity, etc. affect this price. This method should only be used - * to calculate the final price of the room within the appropriate service class. + * Other factors like resort capacity, etc. affect this price. This method + * should only be used to calculate the final price of the room within the + * appropriate service class. * * @return The base price of the room. */ @@ -81,6 +82,10 @@ public Double getBasePrice() { return basePrice; } + public void setBasePrice(double price) { + this.basePrice = price; + } + /** * Generated by NetBeans. Compares only the name of the room category. */ diff --git a/src/main/java/com/gitrekt/resort/model/services/RoomService.java b/src/main/java/com/gitrekt/resort/model/services/RoomService.java index 5979efb..03b2bf8 100644 --- a/src/main/java/com/gitrekt/resort/model/services/RoomService.java +++ b/src/main/java/com/gitrekt/resort/model/services/RoomService.java @@ -5,6 +5,7 @@ import com.gitrekt.resort.model.entities.RoomCategory; import java.util.List; import javax.persistence.EntityManager; +import javax.persistence.PersistenceException; import javax.persistence.Query; /** @@ -49,6 +50,18 @@ public List getAllRoomsInCategory(String categoryName) { return q.getResultList(); } + public void editRoomPrice(RoomCategory room, double price) { + room.setBasePrice(price); + try { + entityManager.getTransaction().begin(); + entityManager.merge(room); + entityManager.getTransaction().commit(); + } catch (PersistenceException e) { + entityManager.getTransaction().rollback(); + throw e; + } + } + public List getAllRoomCategories() { return entityManager.createQuery("FROM RoomCategory").getResultList(); } diff --git a/src/main/resources/fxml/EditPriceDialog.fxml b/src/main/resources/fxml/EditPriceDialog.fxml new file mode 100644 index 0000000..232a228 --- /dev/null +++ b/src/main/resources/fxml/EditPriceDialog.fxml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +