In: Computer Science
HW1 code
sample.fxml
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.collections.*?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.text.Font?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="TOP_CENTER" hgap="10" vgap="10">
    <columnConstraints>
        <ColumnConstraints percentWidth="50"/>
        <ColumnConstraints percentWidth="50"/>
    </columnConstraints>
    <padding>
        <Insets topRightBottomLeft="5"/>
    </padding>
    <Label GridPane.rowIndex="0" GridPane.columnIndex="0" text="Name">
    </Label>
    <Label GridPane.rowIndex="1" GridPane.columnIndex="0" text="Street"  >
    </Label>
    <Label GridPane.rowIndex="2" GridPane.columnIndex="0" text="City">
    </Label>
    <Label GridPane.rowIndex="3" GridPane.columnIndex="0" text="State" >
    </Label>
    <Label GridPane.rowIndex="4" GridPane.columnIndex="0" text="Zip" >
    </Label>
    <Label GridPane.rowIndex="5" GridPane.columnIndex="0" text="Choose One"  >
    </Label>
    <Label GridPane.rowIndex="6" GridPane.columnIndex="0" text="Type of Music" >
    </Label>
    <Label GridPane.rowIndex="7" GridPane.columnIndex="0" text="Type of App" >
    </Label>
    <Label GridPane.rowIndex="8" GridPane.columnIndex="0" text="Enter Title">
    </Label>
    <Label GridPane.rowIndex="9" GridPane.columnIndex="0" text="Date Purchased" >
    </Label>
    <Label GridPane.rowIndex="10" GridPane.columnIndex="0" text="Account Number" >
    </Label>
    <Button text="SUBMIT" GridPane.rowIndex="11" GridPane.columnIndex="0" prefWidth="Infinity" style ="-fx-background-radius:30"/>
    <Button text="FINISH" GridPane.rowIndex="11" GridPane.columnIndex="1" prefWidth="Infinity" style ="-fx-background-radius:30"/>
    <TextField GridPane.rowIndex="0" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="1" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="2" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="3" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="4" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="8" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="9" GridPane.columnIndex="1"/>
    <TextField GridPane.rowIndex="10" GridPane.columnIndex="1"/>
    <HBox GridPane.rowIndex ="7" GridPane.columnIndex="1">
        <fx:define>
            <ToggleGroup fx:id="ToggleGroup"/>
        </fx:define>
        <RadioButton text="GAME" toggleGroup="$ToggleGroup" prefWidth="120"/>
        <RadioButton text="PRODUCTIVITY" toggleGroup="$ToggleGroup" prefWidth="120"/>
        <RadioButton text="EDUCATION" toggleGroup="$ToggleGroup" prefWidth="120"/>
    </HBox>
    <HBox GridPane.rowIndex="5" GridPane.columnIndex="1">
        <CheckBox fx:id ="appButton" text="APP"  prefWidth="175"  />
        <CheckBox fx:id="musicButton" text="MUSIC" prefWidth="175" />
    </HBox>
    <ComboBox fx:id="chooseOne" GridPane.rowIndex="6" GridPane.columnIndex ="1" >
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value="CHOOSE ONE"/>
                <String fx:value="My very first favorite kind of music are R n B and Pop Ballad"/>
                <String fx:value="My very second favorite kind if music is Rock n roll and Rap"/>
            </FXCollections>
        </items>
        <value>
            <String fx:value="CHOOSE ONE"/>
        </value>
    </ComboBox>
</GridPane>
module-info.java
module HW2 {
    requires javafx.fxml;
    requires javafx.controls;
    opens sample;
}
main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 390));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
controller.java
package sample;
public class Controller{ }
Question The user will enter the data and click the SUBMIT button for each customer. On each submit, your program will check to make sure that data was entered in each field. If not, you must tell the user what is missing and place the cursor in that field. Once all data is entered, a new customer will be created and added to the customer array. Data will be stored in app.txt file If APP box is checked and data will be stored in music.txt file if the MUSIC box is checked. You will then clear all fields and set the focus in the name field. When completely finished, the user will click the FINISH button and program will terminat
main.java
package sample;
//importing all required classes
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 420));
        primaryStage.show();
    }
// Starting application
    public static void main(String[] args) {
        launch(args);
    }
}
sample.fxml
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.collections.*?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.text.Font?>
<!--gird pane with controller-->
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="TOP_CENTER" hgap="10" vgap="10">
    <columnConstraints>
        <ColumnConstraints percentWidth="50"/>
        <ColumnConstraints percentWidth="50"/>
    </columnConstraints>
    <padding>
        <Insets topRightBottomLeft="5"/>
    </padding>
    <Label GridPane.rowIndex="0" GridPane.columnIndex="0" text="Name">
    </Label>
    <Label GridPane.rowIndex="1" GridPane.columnIndex="0" text="Street"  >
    </Label>
    <Label GridPane.rowIndex="2" GridPane.columnIndex="0" text="City">
    </Label>
    <Label GridPane.rowIndex="3" GridPane.columnIndex="0" text="State" >
    </Label>
    <Label GridPane.rowIndex="4" GridPane.columnIndex="0" text="Zip" >
    </Label>
    <Label GridPane.rowIndex="5" GridPane.columnIndex="0" text="Choose One"  >
    </Label>
    <Label GridPane.rowIndex="6" GridPane.columnIndex="0" text="Type of Music" >
    </Label>
    <Label GridPane.rowIndex="7" GridPane.columnIndex="0" text="Type of App" >
    </Label>
    <Label GridPane.rowIndex="8" GridPane.columnIndex="0" text="Enter Title">
    </Label>
    <Label GridPane.rowIndex="9" GridPane.columnIndex="0" text="Date Purchased" >
    </Label>
    <Label GridPane.rowIndex="10" GridPane.columnIndex="0" text="Account Number" >
    </Label>
    <Button text="SUBMIT" GridPane.rowIndex="11" GridPane.columnIndex="0" prefWidth="Infinity" style ="-fx-background-radius:30" onAction="#dataProcess"/>
    <Button text="FINISH" GridPane.rowIndex="11" GridPane.columnIndex="1" prefWidth="Infinity" style ="-fx-background-radius:30" onAction="#exit"/>
    <!-- Here all text field is written-->
    <TextField GridPane.rowIndex="0" GridPane.columnIndex="1"  fx:id="name"/>
    <TextField GridPane.rowIndex="1" GridPane.columnIndex="1"  fx:id="street"/>
    <TextField GridPane.rowIndex="2" GridPane.columnIndex="1"  fx:id="city"/>
    <TextField GridPane.rowIndex="3" GridPane.columnIndex="1"  fx:id="state"/>
    <TextField GridPane.rowIndex="4" GridPane.columnIndex="1"  fx:id="zip"/>
    <TextField GridPane.rowIndex="8" GridPane.columnIndex="1"  fx:id="title"/>
    <TextField GridPane.rowIndex="9" GridPane.columnIndex="1"  fx:id="purchase_date"/>
    <TextField GridPane.rowIndex="10" GridPane.columnIndex="1" fx:id="acc_no"/>
    <!-- Grid Pane in HBox    -->
    <HBox GridPane.rowIndex ="7" GridPane.columnIndex="1">
        <fx:define>
            <ToggleGroup fx:id="ToggleGroup"/>
        </fx:define>
        <RadioButton text="GAME" toggleGroup="$ToggleGroup" prefWidth="120"/>
        <RadioButton text="PRODUCTIVITY" toggleGroup="$ToggleGroup" prefWidth="120"/>
        <RadioButton text="EDUCATION" toggleGroup="$ToggleGroup" prefWidth="120"/>
    </HBox>
    <HBox GridPane.rowIndex="5" GridPane.columnIndex="1">
        <CheckBox fx:id ="appButton" text="APP"  prefWidth="175"  />
        <CheckBox fx:id="musicButton" text="MUSIC" prefWidth="175" />
    </HBox>
<!-- Combo box-->
    <ComboBox fx:id="chooseOne" GridPane.rowIndex="6" GridPane.columnIndex ="1" >
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value="CHOOSE ONE"/>
                <String fx:value="My very first favorite kind of music are R n B and Pop Ballad"/>
                <String fx:value="My very second favorite kind if music is Rock n roll and Rap"/>
            </FXCollections>
        </items>
        <value>
            <String fx:value="CHOOSE ONE"/>
        </value>
    </ComboBox>
</GridPane>
Controller.java
package sample;
// importing required packages and classes
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Controller {
    @FXML
    public TextField name;
    @FXML
    public TextField street;
    @FXML
    public TextField city;
    @FXML
    public TextField state;
    @FXML
    public TextField zip;
    @FXML
    public TextField title;
    @FXML
    public TextField purchase_date;
    @FXML
    public TextField acc_no;
    @FXML
    public CheckBox appButton;
    @FXML
    public CheckBox musicButton;
    @FXML
    public ComboBox chooseOne;
    @FXML
    ToggleGroup ToggleGroup;
    String n,s,c,st,z,choose_one,app_btn,m_btn,t,p_date,ac_no;
    // creating new alert object
    Alert alert = new Alert(Alert.AlertType.NONE);
// for file to create
    String file_name;
    // to exit the application
    public void exit() {
        Platform.exit();
        System.exit(0);
    }
    public void dataProcess() {
        String[] customer = new String[11];     // creating customer array
        // get text from name field store it to n
        n= name.getText();
        // get text from street field store it to s
        s= street.getText();
        // get text from city field store it to c
        c= city.getText();
        // get text from state  field store it to st
        st= state.getText();
        // get text from zip field store it to z
        z= zip.getText();
        // get value from appButton checkbox store it to app_btn
        app_btn = appButton.getText();
        // get value from musicButton checkbox store it to m_btn
        m_btn = musicButton.getText();
        // get value from Choose one combo box store it to choose_one
        choose_one = chooseOne.getValue().toString();
//       get radio button from toggle group and store it to radiobutton
        RadioButton radioButton = (RadioButton) ToggleGroup.getSelectedToggle();
        // get text from title field store it to t
        t= title.getText();
        // get text from purchase_date field store it to p_date
        p_date= purchase_date.getText();
        // get text from acc_no field store it to ac_no
        ac_no= acc_no.getText();
//        checking name is empty or not "you can use regex for more valid input"
        if(n.isEmpty()) {
            showAlert("Enter your name!!"); //showing alert
            name.requestFocus(); //
        }
        else if(s.isEmpty()) {// checking street is empty or not
            showAlert("Enter your street!!");  //showing alert
            street.requestFocus(); // returning focus to the current field
        }
        else if(c.isEmpty()) {// checking city is empty or not
            showAlert("Enter your city!!");  //showing alert
            city.requestFocus(); // returning focus to the current field
        }
        else if(st.isEmpty()) {// checking state is empty or not
            showAlert("Enter your state!!"); //showing alert
            state.requestFocus(); // returning focus to the current field
        }
        else if(z.isEmpty()) {// checking zip is empty or not
            showAlert("Enter your zip!!");  //showing alert
            zip.requestFocus(); // returning focus to the current field
        }
        else if(!appButton.isSelected() && !musicButton.isSelected()) { // checking both checkbox is not selected or not
            showAlert("Select Choose One Music or App"); //showing alert
            appButton.requestFocus(); // returning focus to the current field
        }
        else if(appButton.isSelected() && musicButton.isSelected()) {// checking both checkbox is selected or not
            showAlert("Select Choose One Music or App"); //showing alert
            appButton.requestFocus(); // returning focus to the current field
        }
        else if(choose_one.equals("CHOOSE ONE")) { // checking selected first item is "CHOOSE ONE" or not
            showAlert("Select valid option"); //showing alert
            chooseOne.requestFocus(); // returning focus to the current field
        }
        else if(radioButton== null)  // checking radio button is null
        {
            showAlert("Select Type of app"); //showing alert
        }   else if(t.isEmpty()) { // checking title is empty or not
            showAlert("Enter your Title!!"); //showing alert
            title.requestFocus(); // returning focus to the current field
        }
        else if(p_date.isEmpty()) { // checking purchase date is empty or not
            showAlert("Enter your Purchase Date!!"); //showing alert
            purchase_date.requestFocus(); // returning focus to the current field
        }
        else if(ac_no.isEmpty()) {   // checking account no is empty or not
            showAlert("Enter your Accoutn no!!"); //showing alert
            acc_no.requestFocus(); // returning focus to the current field
        }
        else
        {
//            checking which check box is selected
            if(appButton.isSelected())
            {
//                if appButton is selected then
                customer[6]=app_btn;
                file_name="app.txt";
            }else if(musicButton.isSelected())
            {
                // if musicButton is selected then
                customer[6]=m_btn;
                file_name="music.txt";
            }
//            assigning fields value in customer array with index based
            customer[0]=n;                      // name
            customer[1]=s;                      // street
            customer[2]=c;                      // city
            customer[3]=st;                     // state
            customer[4]=z;                      // zip
            customer[5]=choose_one;             // CHOOSE ONE
            customer[7]=radioButton.getText();  // Type of app
            customer[8]=t;                      // title
            customer[9]=p_date;                 // purchase date
            customer[10]=ac_no;                 // account no
            createFile(customer);               // creating file
        }
    }
    private void createFile(String[] customer) {
//       assigning path name with file name
        Path path = Paths.get(file_name);
        StringBuilder stringBuilder = new StringBuilder();
//        appending customer array in one line with space "You can replace with \t with your requirement"
        for(int i=0;i<11;i++)
            stringBuilder.append(customer[i]+"\t\t");
            stringBuilder.append("\n");
        try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
            writer.write(stringBuilder.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
//        clearing all fields
        clearFields();
        name.requestFocus();                                // Returning focus to name field
    }
// This function will show alert
    public void showAlert(String text){
//        type of error
         alert.setAlertType(AlertType.ERROR);
//       error text
         alert.setContentText(text);
         alert.show();
    }
//    resetting all fields
    public void clearFields(){
        name.clear();                                        // name field clear
        street.clear();                                      // street field clear
        city.clear();                                        // city field clear
        state.clear();                                       // state field clear
        zip.clear();                                         // zip field clear
        appButton.setSelected(false);
        musicButton.setSelected(false);
        chooseOne.getSelectionModel().select(0);
        ToggleGroup.getSelectedToggle().setSelected(false);
        title.clear();                                      // title field clear
        purchase_date.clear();                              // purchase date field clear
        acc_no.clear();                                     // account no field clear
    }
}
Output
error..



complete data

code output
main.java

smaple.fxml





Controller.java









