Question

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?> &lt

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

Solutions

Expert Solution

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


Related Solutions

This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write a description of class SpellChecker here. * * @author (your name) * @version (a version number or a date) */ public class SpellChecker { private ArrayList words; private DictReader reader; /** * Constructor for objects of class SpellChecker */ public SpellChecker() { reader = new DictReader("words.txt"); words = reader.getDictionary(); } /** * This method returns the number of words in the dictionary. * Change...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage");...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class...
I'm getting an error for this code? it won't compile import java.util.*; import java.io.*; public class Qup3 implements xxxxxlist {// implements interface    // xxxxxlnk class variables    // head is a pointer to beginning of rlinked list    private node head;    // no. of elements in the list    // private int count; // xxxxxlnk class constructor    Qup3() {        head = null;        count = 0;    } // end Dersop3 class constructor   ...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution {...
Convert this java code from hashmap into arraylist. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); HashMap labs = new HashMap(); while (true) { System.out.println("Choose operation : "); System.out.println("1. Create a Lab"); System.out.println("2. Modify a Lab"); System.out.println("3. Delete a Lab"); System.out.println("4. Assign a pc to a Lab"); System.out.println("5. Remove a pc from a Lab"); System.out.println("6. Quit"); int choice = sc.nextInt(); String name=sc.nextLine(); switch (choice) { case 1:...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT