Rewards App JavaFX Starter Code
READ THIS IMPORTANT MESSAGE
Complete ALL steps in this section before looking at errors. You will have errors after Step 1. Don't panic!! Complete ALL steps before you attempt to fix bugs.
Step 1: Set up your project files
Set up a project in the IDE of your choice (we recommend intelliJ) and add the following classes to your project:
- In
HelloApplication.java, change the size of the scene to640by480. - From your text-based project:
- Address.java record. We are including this code below as well.
- Reward.java record. We are including this code below as well.
- For the JavaFX version listed below:
- Customer.java: This class represents a customer in the app and has the CustomerContactInfo, customer id number, and date joined.
- CustomerContactInfo.java: This class represents a customer's first name, last name, email address, and mailing address
- RewardsApps.java: This class can be used to create a RewardsApps object, which includes a list of subscribers and available offers.This class also has a static variable for the customerID. NOTE: This is different from the text-based version of the program.
- JavaFX Controllers:
- HelloController.java: This class initializes the rewards app with the data stored in the text files and provides functionality to the registration and rewards buttons.
- RegistrateionController.java: This class provides the data that is stored in the rewards and customer data files and provides functionality for registering and changing screens.
- RewardsController.java: This class provides the data that is stored in the rewrads and customer data files and provides functionality to show the available rewards.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="TOP_CENTER" prefHeight="764.0" prefWidth="670.0" spacing="20.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.rewardsappjavafx.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<ImageView fitHeight="303.0" fitWidth="357.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@sandwichlogo.jpg" />
</image>
</ImageView>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="630.0">
<children>
<Button fx:id="RegistrationButton" layoutX="120.0" layoutY="60.0" onAction="#onRegisterButtonClick" text="Register" textAlignment="CENTER" />
<Button fx:id="rewardsButton" layoutX="425.0" layoutY="60.0" mnemonicParsing="false" onAction="#onRewardsButtonClick" text="Rewards" textAlignment="CENTER" />
<Label layoutX="132.0" layoutY="33.0" prefHeight="17.0" prefWidth="367.0" text="Click Register to sign-up and Rewards to see your available rewards" />
</children>
</AnchorPane>
</VBox>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox alignment="TOP_CENTER" prefHeight="764.0" prefWidth="670.0" spacing="20.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.rewardsappweb.RegistrationController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<ImageView fitHeight="321.0" fitWidth="458.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@sandwichlogohorizonatal.jpg" />
</image>
</ImageView>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="630.0">
<children>
<Button fx:id="RegistrationButton" layoutX="120.0" layoutY="60.0" onAction="#onRegisterButtonClick" text="Register" textAlignment="CENTER" />
<Button fx:id="rewardsButton" layoutX="425.0" layoutY="60.0" mnemonicParsing="false" onAction="#onRewardsButtonClick" text="Rewards" textAlignment="CENTER" />
<Label layoutX="120.0" layoutY="29.0" prefHeight="17.0" prefWidth="362.0" text="Click Register to sign-up and Rewards to see your available rewards" />
</children>
</AnchorPane>
<AnchorPane fx:id="registrationInfoInput" prefHeight="250.0" prefWidth="200.0">
<children>
<TextField fx:id="firstNameText" layoutX="41.0" layoutY="22.0" prefHeight="25.0" prefWidth="228.0" promptText="First Name" />
<TextField fx:id="lastNameText" layoutX="315.0" layoutY="22.0" prefHeight="25.0" prefWidth="271.0" promptText="Last Name" />
<TextField fx:id="streetAddress1Text" layoutX="41.0" layoutY="63.0" prefHeight="25.0" prefWidth="541.0" promptText="Street Address 1" />
<TextField fx:id="streetAddress2Text" layoutX="41.0" layoutY="100.0" prefHeight="25.0" prefWidth="541.0" promptText="Street Address 2" />
<TextField fx:id="cityText" layoutX="41.0" layoutY="142.0" prefHeight="25.0" prefWidth="307.0" promptText="City" />
<TextField fx:id="stateText" layoutX="362.0" layoutY="142.0" prefHeight="25.0" prefWidth="51.0" promptText="State" />
<TextField fx:id="zipText" layoutX="437.0" layoutY="142.0" promptText="Zip" />
<TextField fx:id="emailAddressText" layoutX="41.0" layoutY="191.0" prefHeight="25.0" prefWidth="541.0" promptText="Email" />
</children>
</AnchorPane>
<AnchorPane fx:id="customIdPane" prefHeight="200.0" prefWidth="200.0">
<children>
<Label fx:id="accountSetUpLabel" layoutX="276.0" layoutY="76.0" prefHeight="25.0" prefWidth="338.0" text="CONGRATULATION! You have set up your account!" visible="false">
<font>
<Font size="14.0" />
</font>
</Label>
<Label fx:id="customerIdLabel" layoutX="39.0" layoutY="89.0" prefHeight="27.0" prefWidth="78.0" text="Customer Id: " />
<Label fx:id="customerIdText" layoutX="360.0" layoutY="115.0" prefHeight="27.0" prefWidth="78.0" />
<Button fx:id="submitRegistrationButton" layoutX="534.0" layoutY="22.0" mnemonicParsing="false" onAction="#onSubmitRegistrationButtonClick" text="Submit" />
</children>
</AnchorPane>
</VBox>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" prefHeight="764.0" prefWidth="670.0" spacing="20.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.rewardsappweb.RewardsController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<ImageView fitHeight="321.0" fitWidth="458.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@sandwichlogohorizonatal.jpg" />
</image>
</ImageView>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="100.0" prefWidth="600.0">
<children>
<Button fx:id="RegistrationButton" layoutX="120.0" layoutY="60.0" onAction="#onRegisterButtonClick" text="Register" textAlignment="CENTER" />
<Button fx:id="rewardsButton" layoutX="425.0" layoutY="60.0" mnemonicParsing="false" onAction="#onRewardsButtonClick" text="Rewards" textAlignment="CENTER" />
<Label layoutX="118.0" layoutY="14.0" maxHeight="-Infinity" prefHeight="17.0" prefWidth="365.0" text="Click Register to sign-up and Rewards to see your available rewards" />
</children>
</AnchorPane>
<AnchorPane fx:id="registrationInfoInput" prefHeight="200.0" prefWidth="200.0">
<children>
<TextField fx:id="customerIdText" layoutX="34.0" layoutY="36.0" prefHeight="25.0" prefWidth="280.0" promptText="Enter Your Customer Id" />
<Button layoutX="444.0" layoutY="61.0" mnemonicParsing="false" onAction="#showRewards" text="viewRewardsButton" />
</children></AnchorPane>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Label fx:id="displayRewardsLabel" layoutX="44.0" layoutY="25.0" prefHeight="140.0" prefWidth="555.0" />
</children>
</AnchorPane>
</VBox>
Step 2: FXML Resources
Add the following to your resources with the code provided:
- hello-view.fxml: Provides the opening scene
- registration-view.fxml: Provides the registration scene to register new customers
- rewards-view.fxml: Provides the rewards scene to display the rewards available for a customer.
Each of these files have a line that includes something like com.example.rewardsappjavafx.HelloController. You will need to change this to point to the folder where your controller is stored. Your change will be something like com.example.WhateverYouNamedYourProject.HelloController.
- in hello-view.fxml it will be:
com.example.rewardsappjavafx.HelloController - in registration-view.fxml it will be:
com.example.rewardsappjavafx.RegistrationController - in rewards-view.fxml it will be:
com.example.rewardsappjavafx.RewardsController
Step 3: Text Files Needed
In your resources, add the following:
- customers.txt: You may want to add some sample customer information to this file to start you off or you can keep this file blank and it will grow as you add customers to your app.
- rewards.txt: Add a few rewards to the text file. Each reward has a name, discount amount, and expiration date. These should be separated by a comma and there is no space between the entries. The date should be in the format: year-month-day. Here is an example reward: 10%off,0.1,2025-02-06
- In each of the three controller files, you will need to add the path to your newly created customers.txt and rewards.txt files in three separate places:
onRegisterButtonClick,onRewardsButtonClick, andinitialize.
Step 4: Images Needed
In your resources, add the following:
A jpg named sandwichlogo.jpg: you can use the logo here:

A jpg named sandwichlogohorizonatal.jpg: you can use the logo here:
