Answer to Question #287907 in Java | JSP | JSF for Mengal

Question #287907

Design a JavaFx application for “Course Module”. You need to design a registration screen that contains controls for id (TextField), course name (comboBox), Start date (Datepicker), Fees (TextField), and an Add button. The entered detail must be saved to the File using the java.io library. Before saving data to the file data must be validated. To validate data, perform the following:


Create a class named “Validation” that has a method named “checkId” and “checkFees”

responsible for validation of the id and fees entered.

1) The id value must contain a two-digit number otherwise an InvalidIdValueException will be

thrown.

2) The fees value must be between 13000 to 15000 otherwise as InvalidFeesException will be thrown.


Create classes for the mentioned Custom Exception with an appropriate constructor with string parameters to display the error message.


1
Expert's answer
2022-01-16T17:51:25-0500
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.FileWriter;

public class Main extends Application {

    private void saveToFile(String data) {
        try (FileWriter fileWriter = new FileWriter("courseModule.txt")) {
            fileWriter.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        TextField idTextField = new TextField();
        idTextField.setPromptText("ID");
        idTextField.setMaxWidth(100);
        TextField feesTextField = new TextField();
        feesTextField.setPromptText("Fees");
        feesTextField.setMaxWidth(100);
        ComboBox<String> courseNameComboBox = new ComboBox<>(FXCollections.observableArrayList("C++", "Java", "Math"));
        courseNameComboBox.getSelectionModel().select(0);
        DatePicker startDateDatePicker = new DatePicker();
        Button addButton = new Button("Add");
        addButton.setOnMouseReleased(mouseEvent -> {
            try {
                Validation.checkID(idTextField.getText());
                Validation.checkFees(feesTextField.getText());
                saveToFile("ID: " + idTextField.getText() + "\nFees: " + feesTextField.getText()
                        + "\nCourse name: " + courseNameComboBox.getSelectionModel().getSelectedItem()
                        + "\nStart date: " + startDateDatePicker.getValue());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        VBox vBox = new VBox(10,idTextField, feesTextField, courseNameComboBox, startDateDatePicker, addButton);
        vBox.setAlignment(Pos.CENTER);
        primaryStage.setScene(new Scene(new StackPane(vBox), 400, 300));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


public class InvalidFeesException extends RuntimeException {
    public InvalidFeesException() {
        super("The fees value must be between 13000 to 15000");
    }

    public InvalidFeesException(String message) {
        super(message);
    }
}


public class InvalidValueException extends RuntimeException {
    public InvalidValueException() {
        super("The id value must contain a two-digit number");
    }

    public InvalidValueException(String message) {
        super(message);
    }
}


public class Validation {
    public static void checkID(String id) {
        try {
            int numericID = Integer.parseInt(id);
            if (numericID < 10 || numericID > 99) {
                throw new InvalidValueException();
            }
        } catch (Exception e) {
            throw new InvalidValueException();
        }
    }

    public static void checkFees(String fees) {
        try {
            int numericFees = Integer.parseInt(fees);
            if (numericFees < 13000 || numericFees > 15000) {
                throw new InvalidFeesException();
            }
        } catch (Exception e) {
            throw new InvalidFeesException();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog