├── README.md ├── build.gradle └── src ├── main ├── java │ ├── ca │ │ └── pragmaticcoding │ │ │ └── beginners │ │ │ ├── part1 │ │ │ └── Main.java │ │ │ ├── part10 │ │ │ ├── Customer.java │ │ │ ├── CustomerAlreadyOnFileException.java │ │ │ ├── CustomerBroker.java │ │ │ ├── CustomerController.java │ │ │ ├── CustomerDAO.java │ │ │ ├── CustomerDatabase.java │ │ │ ├── CustomerInteractor.java │ │ │ ├── CustomerModel.java │ │ │ ├── CustomerViewBuilder.java │ │ │ ├── Main.java │ │ │ └── SlowCustomerDatabase.java │ │ │ ├── part2 │ │ │ └── Main.java │ │ │ ├── part3 │ │ │ └── Main.java │ │ │ ├── part4 │ │ │ ├── Main.java │ │ │ └── Main2.java │ │ │ ├── part5 │ │ │ ├── CustomerController.java │ │ │ ├── CustomerInteractor.java │ │ │ ├── CustomerModel.java │ │ │ ├── CustomerViewBuilder.java │ │ │ └── Main.java │ │ │ ├── part6 │ │ │ ├── Customer.java │ │ │ ├── CustomerBroker.java │ │ │ ├── CustomerController.java │ │ │ ├── CustomerDAO.java │ │ │ ├── CustomerDatabase.java │ │ │ ├── CustomerInteractor.java │ │ │ ├── CustomerModel.java │ │ │ ├── CustomerViewBuilder.java │ │ │ └── Main.java │ │ │ ├── part8 │ │ │ ├── Customer.java │ │ │ ├── CustomerBroker.java │ │ │ ├── CustomerController.java │ │ │ ├── CustomerDAO.java │ │ │ ├── CustomerDatabase.java │ │ │ ├── CustomerInteractor.java │ │ │ ├── CustomerModel.java │ │ │ ├── CustomerViewBuilder.java │ │ │ ├── Main.java │ │ │ └── SlowCustomerDatabase.java │ │ │ └── part9 │ │ │ ├── Customer.java │ │ │ ├── CustomerBroker.java │ │ │ ├── CustomerController.java │ │ │ ├── CustomerDAO.java │ │ │ ├── CustomerDatabase.java │ │ │ ├── CustomerInteractor.java │ │ │ ├── CustomerModel.java │ │ │ ├── CustomerViewBuilder.java │ │ │ ├── Main.java │ │ │ └── SlowCustomerDatabase.java │ └── module-info.java └── resources │ └── css │ ├── beginners.css │ └── customer.css └── test └── java └── ca └── pragmaticcoding └── beginners ├── part10 ├── CustomerBrokerTest.java ├── CustomerDatabaseTest.java └── CustomerInteractorTest.java ├── part6 ├── CustomerBrokerTest.java ├── CustomerDatabaseTest.java └── CustomerInteractorTest.java └── part8 ├── CustomerBrokerTest.java ├── CustomerDatabaseTest.java └── CustomerInteractorTest.java /README.md: -------------------------------------------------------------------------------- 1 | # AbsoluteBeginnersFX 2 | Companion project to the "Absolute Beginners Guide to JavaFX" series 3 | 4 | # The Absolute Beginners Guide to JavaFX 5 | 6 | This is a series of articles intended to teach how to program with JavaFX the "right" way, and without FXML. It starts from the very beginning, showing how to write a simple "Hello World" screen, then adding user interaction and styling. After that the series walks, step by step, through building an actual CRUD application that maintains a simulated Customer database. 7 | 8 | Along the way, you'll learn about layout classes, builders, styling, using background threads, handling exceptions, implementing business rules and just about everything you need to know to build a real, robust application. 9 | 10 | If you are interested, the course can be found at https://www.pragmaticcoding.ca/beginners/intro 11 | 12 | # Using this Project 13 | 14 | This is a Gradle based project. It works very well with Intellij Idea. Simply download the code and put it in a directory. Then open the build.gradle file with your Gradle compatible IDE. It should build automically. 15 | 16 | The project is divided up into packages with a single package holding the complete source code for the application at the end of each part, or lesson, in the series. So you'll find packages like ca.pragmaticcoding.beginners.part1 and ...part2 and so on. 17 | 18 | The best way to run the code is via the Gradle "run" task, in the "application" submenu. 19 | 20 | In each package, the class with the main() method is called "Main". To run different versions of the application from different packages, you'll need to modify a single line in the build.gradle file: 21 | 22 | ``` java 23 | application { 24 | mainModule = 'ca.pragmaticcoding.beginners' 25 | mainClass = 'ca.pragmaticcoding.beginners.part1.Main' 26 | } 27 | ``` 28 | 29 | Change the package of the mainClass definition from "part1" to whichever version you want to run. 30 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'application' 4 | id 'org.openjfx.javafxplugin' version '0.0.10' 5 | id 'org.beryx.jlink' version '2.24.1' 6 | } 7 | 8 | group 'ca.pragmaticcoding' 9 | version '1.0-SNAPSHOT' 10 | 11 | repositories { 12 | mavenCentral() 13 | } 14 | 15 | ext { 16 | junitVersion = '5.8.2' 17 | } 18 | 19 | sourceCompatibility = '16' 20 | targetCompatibility = '16' 21 | 22 | tasks.withType(JavaCompile) { 23 | options.encoding = 'UTF-8' 24 | } 25 | 26 | application { 27 | mainModule = 'ca.pragmaticcoding.beginners' 28 | mainClass = 'ca.pragmaticcoding.beginners.part10.Main' 29 | } 30 | 31 | javafx { 32 | version = '16' 33 | modules = ['javafx.controls', 'javafx.graphics'] 34 | } 35 | 36 | dependencies { 37 | 38 | testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}") 39 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") 40 | } 41 | 42 | test { 43 | useJUnitPlatform() 44 | } 45 | 46 | jlink { 47 | imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip") 48 | options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] 49 | launcher { 50 | name = 'app' 51 | } 52 | } 53 | 54 | jlinkZip { 55 | group = 'distribution' 56 | } -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part1/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part1; 2 | 3 | import javafx.application.Application; 4 | import javafx.scene.Scene; 5 | import javafx.scene.control.Label; 6 | import javafx.scene.layout.Region; 7 | import javafx.stage.Stage; 8 | 9 | public class Main extends Application { 10 | 11 | public static void main(String[] args) { 12 | launch(args); 13 | } 14 | 15 | @Override 16 | public void start(Stage primaryStage) { 17 | Scene scene = new Scene(createContents()); 18 | primaryStage.setScene(scene); 19 | primaryStage.show(); 20 | } 21 | 22 | private Region createContents() { 23 | return new Label("Hello World"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/Customer.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | public class Customer { 4 | 5 | private String accountNumber = ""; 6 | private String name = ""; 7 | 8 | 9 | public String getAccountNumber() { 10 | return accountNumber; 11 | } 12 | 13 | public void setAccountNumber(String accountNumber) { 14 | this.accountNumber = accountNumber; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerAlreadyOnFileException.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | public class CustomerAlreadyOnFileException extends Exception { 4 | 5 | public CustomerAlreadyOnFileException(String accountNumber) { 6 | super("Customer account: " + accountNumber + " is already on file"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerBroker.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerBroker { 7 | 8 | private final CustomerDAO dao = new CustomerDAO(); 9 | 10 | public int saveCustomer(Customer customer) throws CustomerAlreadyOnFileException { 11 | return dao.saveCustomer(createCustomerRecord(customer)); 12 | } 13 | 14 | Map createCustomerRecord(Customer customer) { 15 | Map customerRecord = new HashMap<>(); 16 | customerRecord.put("name", customer.getName()); 17 | customerRecord.put("account_number", customer.getAccountNumber()); 18 | return customerRecord; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerController.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import javafx.concurrent.Task; 4 | import javafx.scene.control.Alert; 5 | import javafx.scene.layout.Region; 6 | import javafx.util.Builder; 7 | 8 | public class CustomerController { 9 | 10 | private final Builder viewBuilder; 11 | private final CustomerInteractor interactor; 12 | 13 | public CustomerController() { 14 | CustomerModel model = new CustomerModel(); 15 | interactor = new CustomerInteractor(model); 16 | viewBuilder = new CustomerViewBuilder(model, this::saveCustomer); 17 | } 18 | 19 | private void saveCustomer(Runnable postTaskGuiActions) { 20 | Task saveTask = new Task<>() { 21 | @Override 22 | protected Boolean call() { 23 | return interactor.saveCustomer(); 24 | 25 | } 26 | }; 27 | saveTask.setOnSucceeded(evt -> { 28 | postTaskGuiActions.run(); 29 | if (!saveTask.getValue()) { 30 | Alert alert = new Alert(Alert.AlertType.ERROR); 31 | alert.setContentText("This customer is already on file, cannot save."); 32 | alert.show(); 33 | } 34 | }); 35 | Thread saveThread = new Thread(saveTask); 36 | saveThread.start(); 37 | } 38 | 39 | public Region getView() { 40 | return viewBuilder.build(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerDAO.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import java.util.Map; 4 | 5 | public class CustomerDAO { 6 | 7 | static CustomerDatabase database = new SlowCustomerDatabase(3000); 8 | 9 | public int saveCustomer(Map customerRecord) throws CustomerAlreadyOnFileException { 10 | return database.saveNewCustomer(customerRecord); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Optional; 6 | 7 | public class CustomerDatabase { 8 | 9 | private final Map> data = new HashMap<>(); 10 | private Integer nextKey = 0; 11 | 12 | public int saveNewCustomer(Map customerRecord) throws CustomerAlreadyOnFileException, IllegalArgumentException { 13 | Optional accountNumberOptional = getAccount(customerRecord); 14 | if (accountNumberOptional.isPresent()) { 15 | String accountNumber = accountNumberOptional.get(); 16 | if (!isAccountOnFile(accountNumber)) { 17 | return saveCustomer(customerRecord); 18 | } else { 19 | throw new CustomerAlreadyOnFileException(accountNumber); 20 | } 21 | } else { 22 | throw new IllegalArgumentException("Account number must be included in Customer Record"); 23 | } 24 | } 25 | 26 | int saveCustomer(Map customerRecord) { 27 | customerRecord.put("_id", (++nextKey).toString()); 28 | data.put(nextKey, customerRecord); 29 | return nextKey; 30 | } 31 | 32 | boolean isAccountOnFile(String accountNumber) { 33 | return data.values().stream().anyMatch(record -> getAccount(record).map(value -> value.equals(accountNumber)).orElse(false)); 34 | } 35 | 36 | private Optional getAccount(Map customerRecord) { 37 | return Optional.ofNullable(customerRecord.get("account_number")); 38 | } 39 | 40 | Map> getData() { 41 | return data; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerInteractor.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import javafx.beans.binding.Bindings; 4 | 5 | public class CustomerInteractor { 6 | private final CustomerModel model; 7 | private final CustomerBroker broker = new CustomerBroker(); 8 | 9 | public CustomerInteractor(CustomerModel model) { 10 | this.model = model; 11 | model.okToSaveProperty().bind(Bindings.createBooleanBinding(this::isDataValid, model.accountNumberProperty(), model.customerNameProperty())); 12 | } 13 | 14 | private boolean isDataValid() { 15 | return !model.getAccountNumber().isEmpty() && !model.getCustomerName().isEmpty(); 16 | } 17 | 18 | public boolean saveCustomer() { 19 | String customerName = model.getCustomerName(); 20 | String account = model.getAccountNumber(); 21 | try { 22 | int recordId = broker.saveCustomer(createCustomerFromModel()); 23 | System.out.println("Saving account: " + account + " Name: " + customerName + " Result: " + recordId); 24 | return true; 25 | } catch (CustomerAlreadyOnFileException e) { 26 | return false; 27 | } 28 | } 29 | 30 | Customer createCustomerFromModel() { 31 | Customer customer = new Customer(); 32 | customer.setAccountNumber(model.getAccountNumber()); 33 | customer.setName(model.getCustomerName()); 34 | return customer; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerModel.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import javafx.beans.property.BooleanProperty; 4 | import javafx.beans.property.SimpleBooleanProperty; 5 | import javafx.beans.property.SimpleStringProperty; 6 | import javafx.beans.property.StringProperty; 7 | 8 | public class CustomerModel { 9 | 10 | private final StringProperty accountNumber = new SimpleStringProperty(""); 11 | private final StringProperty customerName = new SimpleStringProperty(""); 12 | private final BooleanProperty okToSave = new SimpleBooleanProperty(false); 13 | 14 | public String getAccountNumber() { 15 | return accountNumber.get(); 16 | } 17 | 18 | public StringProperty accountNumberProperty() { 19 | return accountNumber; 20 | } 21 | 22 | public void setAccountNumber(String accountNumber) { 23 | this.accountNumber.set(accountNumber); 24 | } 25 | 26 | public String getCustomerName() { 27 | return customerName.get(); 28 | } 29 | 30 | public StringProperty customerNameProperty() { 31 | return customerName; 32 | } 33 | 34 | public void setCustomerName(String customerName) { 35 | this.customerName.set(customerName); 36 | } 37 | 38 | public boolean isOkToSave() { 39 | return okToSave.get(); 40 | } 41 | 42 | public BooleanProperty okToSaveProperty() { 43 | return okToSave; 44 | } 45 | 46 | public void setOkToSave(boolean okToSave) { 47 | this.okToSave.set(okToSave); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/CustomerViewBuilder.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import javafx.beans.property.StringProperty; 4 | import javafx.geometry.Insets; 5 | import javafx.geometry.Pos; 6 | import javafx.scene.Node; 7 | import javafx.scene.control.Button; 8 | import javafx.scene.control.Label; 9 | import javafx.scene.control.TextField; 10 | import javafx.scene.layout.BorderPane; 11 | import javafx.scene.layout.HBox; 12 | import javafx.scene.layout.Region; 13 | import javafx.scene.layout.VBox; 14 | import javafx.util.Builder; 15 | 16 | import java.util.Objects; 17 | import java.util.function.Consumer; 18 | 19 | public class CustomerViewBuilder implements Builder { 20 | 21 | private final CustomerModel model; 22 | private final Consumer saveHandler; 23 | public CustomerViewBuilder(CustomerModel model, Consumer saveHandler) { 24 | this.model = model; 25 | this.saveHandler = saveHandler; 26 | } 27 | 28 | @Override 29 | public Region build() { 30 | BorderPane results = new BorderPane(); 31 | results.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("/css/customer.css")).toExternalForm()); 32 | results.setTop(headingLabel("Customer Information")); 33 | results.setCenter(createCentre()); 34 | results.setBottom(createButtons()); 35 | return results; 36 | } 37 | 38 | private Node createCentre() { 39 | VBox results = new VBox(6, accountBox(), nameBox()); 40 | results.setPadding(new Insets(20)); 41 | return results; 42 | } 43 | 44 | private Node accountBox() { 45 | return new HBox(6,promptLabel("Account #:"), boundTextField(model.accountNumberProperty())); 46 | } 47 | 48 | private Node nameBox() { 49 | return new HBox(6, promptLabel("Name:"), boundTextField(model.customerNameProperty())); 50 | } 51 | 52 | private Node createButtons() { 53 | Button saveButton = new Button("Save"); 54 | saveButton.disableProperty().bind(model.okToSaveProperty().not()); 55 | saveButton.setOnAction(evt -> { 56 | saveButton.disableProperty().unbind(); 57 | saveButton.setDisable(true); 58 | saveHandler.accept(() -> saveButton.disableProperty().bind(model.okToSaveProperty().not())); 59 | }); 60 | HBox results = new HBox(10, saveButton); 61 | results.setAlignment(Pos.CENTER_RIGHT); 62 | return results; 63 | } 64 | 65 | private Node boundTextField(StringProperty boundProperty) { 66 | TextField textField = new TextField(); 67 | textField.textProperty().bindBidirectional(boundProperty); 68 | return textField; 69 | } 70 | 71 | private Node promptLabel(String contents) { 72 | return styledLabel(contents, "prompt-label"); 73 | } 74 | 75 | private Node headingLabel(String contents) { 76 | return styledLabel(contents, "heading-label"); 77 | } 78 | 79 | private Node styledLabel(String contents, String styleClass) { 80 | Label label = new Label(contents); 81 | label.getStyleClass().add(styleClass); 82 | return label; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import javafx.application.Application; 4 | import javafx.scene.Scene; 5 | import javafx.stage.Stage; 6 | 7 | public class Main extends Application { 8 | @Override 9 | public void start(Stage primaryStage) throws Exception { 10 | primaryStage.setScene(new Scene(new CustomerController().getView())); 11 | primaryStage.show(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part10/SlowCustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import java.util.Map; 4 | 5 | public class SlowCustomerDatabase extends CustomerDatabase { 6 | 7 | private final int delay; 8 | 9 | public SlowCustomerDatabase(int delay) { 10 | this.delay = delay; 11 | } 12 | 13 | @Override 14 | public int saveNewCustomer(Map customerRecord) throws CustomerAlreadyOnFileException { 15 | delay(); 16 | return super.saveNewCustomer(customerRecord); 17 | } 18 | 19 | private void delay() { 20 | try { 21 | Thread.sleep(delay); 22 | } catch (InterruptedException e) { 23 | throw new RuntimeException(e); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part2/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part2; 2 | 3 | import javafx.application.Application; 4 | import javafx.geometry.Insets; 5 | import javafx.geometry.Pos; 6 | import javafx.scene.Scene; 7 | import javafx.scene.control.Label; 8 | import javafx.scene.control.TextField; 9 | import javafx.scene.layout.HBox; 10 | import javafx.scene.layout.Region; 11 | import javafx.stage.Stage; 12 | 13 | public class Main extends Application { 14 | 15 | public static void main(String[] args) { 16 | launch(args); 17 | } 18 | 19 | @Override 20 | public void start(Stage primaryStage) { 21 | Scene scene = new Scene(createContents(), 400, 200); 22 | primaryStage.setScene(scene); 23 | primaryStage.show(); 24 | } 25 | 26 | private Region createContents() { 27 | HBox results = new HBox(new Label("Name:"), new TextField("")); 28 | results.setSpacing(6); 29 | results.setPadding(new Insets(0,0,0,50)); 30 | results.setAlignment(Pos.CENTER_LEFT); 31 | results.setStyle("-fx-border-color : red;-fx-border-width : 0.5;"); 32 | return results; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part3/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part3; 2 | 3 | import javafx.application.Application; 4 | import javafx.beans.property.SimpleStringProperty; 5 | import javafx.beans.property.StringProperty; 6 | import javafx.geometry.Insets; 7 | import javafx.geometry.Pos; 8 | import javafx.scene.Node; 9 | import javafx.scene.Scene; 10 | import javafx.scene.control.Button; 11 | import javafx.scene.control.Label; 12 | import javafx.scene.control.TextField; 13 | import javafx.scene.layout.HBox; 14 | import javafx.scene.layout.Region; 15 | import javafx.scene.layout.VBox; 16 | import javafx.stage.Stage; 17 | 18 | public class Main extends Application { 19 | 20 | public static void main(String[] args) { 21 | launch(args); 22 | } 23 | 24 | private final StringProperty greeting = new SimpleStringProperty(""); 25 | private final StringProperty name = new SimpleStringProperty(""); 26 | 27 | @Override 28 | public void start(Stage primaryStage) { 29 | Scene scene = new Scene(createContents(), 400, 200); 30 | primaryStage.setScene(scene); 31 | primaryStage.show(); 32 | } 33 | 34 | private Region createContents() { 35 | VBox results = new VBox(20, createInputRow(), createOutputLabel(), createButton()); 36 | results.setAlignment(Pos.CENTER); 37 | return results; 38 | } 39 | 40 | private Button createButton() { 41 | Button results = new Button("Hello"); 42 | results.setOnAction(evt -> setGreeting()); 43 | return results; 44 | } 45 | 46 | private HBox createInputRow() { 47 | TextField textField = new TextField(""); 48 | textField.textProperty().bindBidirectional(name); 49 | HBox hBox = new HBox(6, new Label("Name:"), textField); 50 | hBox.setAlignment(Pos.CENTER); 51 | return hBox; 52 | } 53 | 54 | private Node createOutputLabel() { 55 | Label results = new Label(""); 56 | results.textProperty().bind(greeting); 57 | return results; 58 | } 59 | 60 | private void setGreeting() { 61 | greeting.set("Hello " + name.get()); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part4/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part4; 2 | 3 | import javafx.application.Application; 4 | import javafx.beans.property.SimpleStringProperty; 5 | import javafx.beans.property.StringProperty; 6 | import javafx.geometry.Pos; 7 | import javafx.scene.Node; 8 | import javafx.scene.Scene; 9 | import javafx.scene.control.Button; 10 | import javafx.scene.control.Label; 11 | import javafx.scene.control.TextField; 12 | import javafx.scene.layout.HBox; 13 | import javafx.scene.layout.Region; 14 | import javafx.scene.layout.VBox; 15 | import javafx.stage.Stage; 16 | 17 | public class Main extends Application { 18 | 19 | public static void main(String[] args) { 20 | launch(args); 21 | } 22 | 23 | private final StringProperty greeting = new SimpleStringProperty(""); 24 | private final StringProperty name = new SimpleStringProperty(""); 25 | 26 | @Override 27 | public void start(Stage primaryStage) { 28 | Scene scene = new Scene(createContents(), 400, 200); 29 | primaryStage.setScene(scene); 30 | primaryStage.show(); 31 | } 32 | 33 | private Region createContents() { 34 | VBox results = new VBox(20, createInputRow(), createOutputLabel(), createButton()); 35 | results.setAlignment(Pos.CENTER); 36 | results.getStylesheets().add(this.getClass().getResource("/css/beginners.css").toExternalForm()); 37 | return results; 38 | } 39 | 40 | private Button createButton() { 41 | Button results = new Button("Hello"); 42 | results.setOnAction(evt -> setGreeting()); 43 | return results; 44 | } 45 | 46 | private HBox createInputRow() { 47 | TextField textField = new TextField(""); 48 | textField.textProperty().bindBidirectional(name); 49 | Label namePrompt = new Label("Name:"); 50 | namePrompt.getStyleClass().add("prompt-label"); 51 | HBox hBox = new HBox(6, namePrompt, textField); 52 | hBox.setAlignment(Pos.CENTER); 53 | return hBox; 54 | } 55 | 56 | private Node createOutputLabel() { 57 | Label results = new Label(""); 58 | results.getStyleClass().add("greeting-label"); 59 | results.textProperty().bind(greeting); 60 | return results; 61 | } 62 | 63 | private void setGreeting() { 64 | greeting.set("Hello " + name.get()); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part4/Main2.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part4; 2 | 3 | import javafx.application.Application; 4 | import javafx.beans.property.SimpleStringProperty; 5 | import javafx.beans.property.StringProperty; 6 | import javafx.geometry.Pos; 7 | import javafx.scene.Node; 8 | import javafx.scene.Scene; 9 | import javafx.scene.control.Button; 10 | import javafx.scene.control.Label; 11 | import javafx.scene.control.TextField; 12 | import javafx.scene.layout.HBox; 13 | import javafx.scene.layout.Region; 14 | import javafx.scene.layout.VBox; 15 | import javafx.stage.Stage; 16 | 17 | public class Main2 extends Application { 18 | 19 | public static void main(String[] args) { 20 | launch(args); 21 | } 22 | 23 | private final StringProperty greeting = new SimpleStringProperty(""); 24 | private final StringProperty name = new SimpleStringProperty(""); 25 | 26 | @Override 27 | public void start(Stage primaryStage) { 28 | Scene scene = new Scene(createContents(), 400, 200); 29 | primaryStage.setScene(scene); 30 | primaryStage.show(); 31 | } 32 | 33 | private Region createContents() { 34 | VBox results = new VBox(20, createInputRow(), createOutputLabel(), createButton()); 35 | results.setAlignment(Pos.CENTER); 36 | results.getStylesheets().add(this.getClass().getResource("/css/beginners.css").toExternalForm()); 37 | return results; 38 | } 39 | 40 | private Button createButton() { 41 | Button results = new Button("Hello"); 42 | results.setOnAction(evt -> setGreeting()); 43 | return results; 44 | } 45 | 46 | private HBox createInputRow() { 47 | TextField textField = new TextField(""); 48 | textField.textProperty().bindBidirectional(name); 49 | HBox hBox = new HBox(6, styledLabel("Name:", "prompt-label"), textField); 50 | hBox.setAlignment(Pos.CENTER); 51 | return hBox; 52 | } 53 | 54 | private Node createOutputLabel() { 55 | Label results = styledLabel("", "greeting-label"); 56 | results.textProperty().bind(greeting); 57 | return results; 58 | } 59 | 60 | private Label styledLabel(String contents, String classSelector) { 61 | Label results = new Label(contents); 62 | results.getStyleClass().add(classSelector); 63 | return results; 64 | } 65 | 66 | private void setGreeting() { 67 | greeting.set("Hello " + name.get()); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part5/CustomerController.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part5; 2 | 3 | import javafx.scene.layout.Region; 4 | import javafx.util.Builder; 5 | 6 | public class CustomerController { 7 | 8 | private Builder viewBuilder; 9 | private CustomerInteractor interactor; 10 | 11 | public CustomerController() { 12 | CustomerModel model = new CustomerModel(); 13 | viewBuilder = new CustomerViewBuilder(model); 14 | interactor = new CustomerInteractor(model); 15 | } 16 | 17 | public Region getView() { 18 | return viewBuilder.build(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part5/CustomerInteractor.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part5; 2 | 3 | public class CustomerInteractor { 4 | private CustomerModel model; 5 | 6 | public CustomerInteractor(CustomerModel model) { 7 | this.model = model; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part5/CustomerModel.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part5; 2 | 3 | public class CustomerModel { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part5/CustomerViewBuilder.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part5; 2 | 3 | import javafx.scene.layout.Region; 4 | import javafx.scene.layout.VBox; 5 | import javafx.util.Builder; 6 | 7 | public class CustomerViewBuilder implements Builder { 8 | 9 | private final CustomerModel model; 10 | public CustomerViewBuilder(CustomerModel model) { 11 | this.model = model; 12 | } 13 | 14 | @Override 15 | public Region build() { 16 | return new VBox(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part5/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part5; 2 | 3 | import javafx.application.Application; 4 | import javafx.scene.Scene; 5 | import javafx.stage.Stage; 6 | 7 | public class Main extends Application { 8 | @Override 9 | public void start(Stage primaryStage) throws Exception { 10 | primaryStage.setScene(new Scene(new CustomerController().getView())); 11 | primaryStage.show(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/Customer.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | public class Customer { 4 | 5 | private String accountNumber = ""; 6 | private String name = ""; 7 | 8 | 9 | public String getAccountNumber() { 10 | return accountNumber; 11 | } 12 | 13 | public void setAccountNumber(String accountNumber) { 14 | this.accountNumber = accountNumber; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerBroker.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerBroker { 7 | 8 | private final CustomerDAO dao = new CustomerDAO(); 9 | 10 | public int saveCustomer(Customer customer) { 11 | return dao.saveCustomer(createCustomerRecord(customer)); 12 | } 13 | 14 | Map createCustomerRecord(Customer customer) { 15 | Map customerRecord = new HashMap<>(); 16 | customerRecord.put("name", customer.getName()); 17 | customerRecord.put("account_number", customer.getAccountNumber()); 18 | return customerRecord; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerController.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import javafx.scene.layout.Region; 4 | import javafx.util.Builder; 5 | 6 | public class CustomerController { 7 | 8 | private final Builder viewBuilder; 9 | private final CustomerInteractor interactor; 10 | 11 | public CustomerController() { 12 | CustomerModel model = new CustomerModel(); 13 | interactor = new CustomerInteractor(model); 14 | viewBuilder = new CustomerViewBuilder(model,interactor::saveCustomer); 15 | } 16 | 17 | public Region getView() { 18 | return viewBuilder.build(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerDAO.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import java.util.Map; 4 | 5 | public class CustomerDAO { 6 | 7 | static CustomerDatabase database = new CustomerDatabase(); 8 | 9 | public int saveCustomer(Map customerRecord) { 10 | return database.saveCustomer(customerRecord); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerDatabase { 7 | 8 | private Map> data = new HashMap<>(); 9 | private Integer nextKey = 0; 10 | 11 | public int saveCustomer(Map customerRecord) { 12 | customerRecord.put("_id", (++nextKey).toString()); 13 | data.put(nextKey, customerRecord); 14 | return nextKey; 15 | } 16 | 17 | Map> getData() { 18 | return data; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerInteractor.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | public class CustomerInteractor { 4 | private final CustomerModel model; 5 | private final CustomerBroker broker = new CustomerBroker(); 6 | 7 | public CustomerInteractor(CustomerModel model) { 8 | this.model = model; 9 | } 10 | 11 | public void saveCustomer() { 12 | int result = broker.saveCustomer(createCustomerFromModel()); 13 | System.out.println("Saving account: " + model.getAccountNumber() + " Name: " + model.getCustomerName() + " Result: " + result); 14 | } 15 | 16 | Customer createCustomerFromModel() { 17 | Customer customer = new Customer(); 18 | customer.setAccountNumber(model.getAccountNumber()); 19 | customer.setName(model.getCustomerName()); 20 | return customer; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerModel.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import javafx.beans.property.SimpleStringProperty; 4 | import javafx.beans.property.StringProperty; 5 | 6 | public class CustomerModel { 7 | 8 | private final StringProperty accountNumber = new SimpleStringProperty(""); 9 | private final StringProperty customerName = new SimpleStringProperty(""); 10 | 11 | public String getAccountNumber() { 12 | return accountNumber.get(); 13 | } 14 | 15 | public StringProperty accountNumberProperty() { 16 | return accountNumber; 17 | } 18 | 19 | public void setAccountNumber(String accountNumber) { 20 | this.accountNumber.set(accountNumber); 21 | } 22 | 23 | public String getCustomerName() { 24 | return customerName.get(); 25 | } 26 | 27 | public StringProperty customerNameProperty() { 28 | return customerName; 29 | } 30 | 31 | public void setCustomerName(String customerName) { 32 | this.customerName.set(customerName); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/CustomerViewBuilder.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import javafx.beans.property.StringProperty; 4 | import javafx.geometry.Insets; 5 | import javafx.geometry.Pos; 6 | import javafx.scene.Node; 7 | import javafx.scene.control.Button; 8 | import javafx.scene.control.Label; 9 | import javafx.scene.control.TextField; 10 | import javafx.scene.layout.BorderPane; 11 | import javafx.scene.layout.HBox; 12 | import javafx.scene.layout.Region; 13 | import javafx.scene.layout.VBox; 14 | import javafx.util.Builder; 15 | 16 | import java.util.Objects; 17 | 18 | public class CustomerViewBuilder implements Builder { 19 | 20 | private final CustomerModel model; 21 | private final Runnable saveHandler; 22 | public CustomerViewBuilder(CustomerModel model, Runnable saveHandler) { 23 | this.model = model; 24 | this.saveHandler = saveHandler; 25 | } 26 | 27 | @Override 28 | public Region build() { 29 | BorderPane results = new BorderPane(); 30 | results.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("/css/customer.css")).toExternalForm()); 31 | results.setTop(headingLabel("Customer Information")); 32 | results.setCenter(createCentre()); 33 | results.setBottom(createButtons()); 34 | return results; 35 | } 36 | 37 | private Node createCentre() { 38 | VBox results = new VBox(6, accountBox(), nameBox()); 39 | results.setPadding(new Insets(20)); 40 | return results; 41 | } 42 | 43 | private Node accountBox() { 44 | return new HBox(6,promptLabel("Account #:"), boundTextField(model.accountNumberProperty())); 45 | } 46 | 47 | private Node nameBox() { 48 | return new HBox(6, promptLabel("Name:"), boundTextField(model.customerNameProperty())); 49 | } 50 | 51 | private Node createButtons() { 52 | Button saveButton = new Button("Save"); 53 | saveButton.setOnAction(evt -> saveHandler.run()); 54 | HBox results = new HBox(10, saveButton); 55 | results.setAlignment(Pos.CENTER_RIGHT); 56 | return results; 57 | } 58 | 59 | private Node boundTextField(StringProperty boundProperty) { 60 | TextField textField = new TextField(); 61 | textField.textProperty().bindBidirectional(boundProperty); 62 | return textField; 63 | } 64 | 65 | private Node promptLabel(String contents) { 66 | return styledLabel(contents, "prompt-label"); 67 | } 68 | 69 | private Node headingLabel(String contents) { 70 | return styledLabel(contents, "heading-label"); 71 | } 72 | 73 | private Node styledLabel(String contents, String styleClass) { 74 | Label label = new Label(contents); 75 | label.getStyleClass().add(styleClass); 76 | return label; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part6/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import javafx.application.Application; 4 | import javafx.scene.Scene; 5 | import javafx.stage.Stage; 6 | 7 | public class Main extends Application { 8 | @Override 9 | public void start(Stage primaryStage) throws Exception { 10 | primaryStage.setScene(new Scene(new CustomerController().getView())); 11 | primaryStage.show(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/Customer.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | public class Customer { 4 | 5 | private String accountNumber = ""; 6 | private String name = ""; 7 | 8 | 9 | public String getAccountNumber() { 10 | return accountNumber; 11 | } 12 | 13 | public void setAccountNumber(String accountNumber) { 14 | this.accountNumber = accountNumber; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerBroker.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerBroker { 7 | 8 | private final CustomerDAO dao = new CustomerDAO(); 9 | 10 | public int saveCustomer(Customer customer) { 11 | return dao.saveCustomer(createCustomerRecord(customer)); 12 | } 13 | 14 | Map createCustomerRecord(Customer customer) { 15 | Map customerRecord = new HashMap<>(); 16 | customerRecord.put("name", customer.getName()); 17 | customerRecord.put("account_number", customer.getAccountNumber()); 18 | return customerRecord; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerController.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import javafx.concurrent.Task; 4 | import javafx.scene.layout.Region; 5 | import javafx.util.Builder; 6 | 7 | public class CustomerController { 8 | 9 | private final Builder viewBuilder; 10 | private final CustomerInteractor interactor; 11 | 12 | public CustomerController() { 13 | CustomerModel model = new CustomerModel(); 14 | interactor = new CustomerInteractor(model); 15 | viewBuilder = new CustomerViewBuilder(model,this::saveCustomer); 16 | } 17 | 18 | private void saveCustomer(Runnable postTaskGuiActions) { 19 | Task saveTask = new Task<>() { 20 | @Override 21 | protected Void call() { 22 | interactor.saveCustomer(); 23 | return null; 24 | } 25 | }; 26 | saveTask.setOnSucceeded(evt -> postTaskGuiActions.run()); 27 | Thread saveThread = new Thread(saveTask); 28 | saveThread.start(); 29 | } 30 | 31 | public Region getView() { 32 | return viewBuilder.build(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerDAO.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import java.util.Map; 4 | 5 | public class CustomerDAO { 6 | 7 | static CustomerDatabase database = new SlowCustomerDatabase(10000); 8 | 9 | public int saveCustomer(Map customerRecord) { 10 | return database.saveCustomer(customerRecord); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerDatabase { 7 | 8 | private Map> data = new HashMap<>(); 9 | private Integer nextKey = 0; 10 | 11 | public int saveCustomer(Map customerRecord) { 12 | customerRecord.put("_id", (++nextKey).toString()); 13 | data.put(nextKey, customerRecord); 14 | return nextKey; 15 | } 16 | 17 | Map> getData() { 18 | return data; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerInteractor.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | public class CustomerInteractor { 4 | private final CustomerModel model; 5 | private final CustomerBroker broker = new CustomerBroker(); 6 | 7 | public CustomerInteractor(CustomerModel model) { 8 | this.model = model; 9 | } 10 | 11 | public void saveCustomer() { 12 | String customerName = model.getCustomerName(); 13 | String account = model.getAccountNumber(); 14 | int result = broker.saveCustomer(createCustomerFromModel()); 15 | System.out.println("Saving account: " + account + " Name: " + customerName + " Result: " + result); 16 | } 17 | 18 | Customer createCustomerFromModel() { 19 | Customer customer = new Customer(); 20 | customer.setAccountNumber(model.getAccountNumber()); 21 | customer.setName(model.getCustomerName()); 22 | return customer; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerModel.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import javafx.beans.property.SimpleStringProperty; 4 | import javafx.beans.property.StringProperty; 5 | 6 | public class CustomerModel { 7 | 8 | private final StringProperty accountNumber = new SimpleStringProperty(""); 9 | private final StringProperty customerName = new SimpleStringProperty(""); 10 | 11 | public String getAccountNumber() { 12 | return accountNumber.get(); 13 | } 14 | 15 | public StringProperty accountNumberProperty() { 16 | return accountNumber; 17 | } 18 | 19 | public void setAccountNumber(String accountNumber) { 20 | this.accountNumber.set(accountNumber); 21 | } 22 | 23 | public String getCustomerName() { 24 | return customerName.get(); 25 | } 26 | 27 | public StringProperty customerNameProperty() { 28 | return customerName; 29 | } 30 | 31 | public void setCustomerName(String customerName) { 32 | this.customerName.set(customerName); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/CustomerViewBuilder.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import javafx.beans.property.StringProperty; 4 | import javafx.geometry.Insets; 5 | import javafx.geometry.Pos; 6 | import javafx.scene.Node; 7 | import javafx.scene.control.Button; 8 | import javafx.scene.control.Label; 9 | import javafx.scene.control.TextField; 10 | import javafx.scene.layout.BorderPane; 11 | import javafx.scene.layout.HBox; 12 | import javafx.scene.layout.Region; 13 | import javafx.scene.layout.VBox; 14 | import javafx.util.Builder; 15 | 16 | import java.util.Objects; 17 | import java.util.function.Consumer; 18 | 19 | public class CustomerViewBuilder implements Builder { 20 | 21 | private final CustomerModel model; 22 | private final Consumer saveHandler; 23 | public CustomerViewBuilder(CustomerModel model, Consumer saveHandler) { 24 | this.model = model; 25 | this.saveHandler = saveHandler; 26 | } 27 | 28 | @Override 29 | public Region build() { 30 | BorderPane results = new BorderPane(); 31 | results.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("/css/customer.css")).toExternalForm()); 32 | results.setTop(headingLabel("Customer Information")); 33 | results.setCenter(createCentre()); 34 | results.setBottom(createButtons()); 35 | return results; 36 | } 37 | 38 | private Node createCentre() { 39 | VBox results = new VBox(6, accountBox(), nameBox()); 40 | results.setPadding(new Insets(20)); 41 | return results; 42 | } 43 | 44 | private Node accountBox() { 45 | return new HBox(6,promptLabel("Account #:"), boundTextField(model.accountNumberProperty())); 46 | } 47 | 48 | private Node nameBox() { 49 | return new HBox(6, promptLabel("Name:"), boundTextField(model.customerNameProperty())); 50 | } 51 | 52 | private Node createButtons() { 53 | Button saveButton = new Button("Save"); 54 | saveButton.setOnAction(evt -> { 55 | saveButton.setDisable(true); 56 | saveHandler.accept(() -> saveButton.setDisable(false)); 57 | }); 58 | HBox results = new HBox(10, saveButton); 59 | results.setAlignment(Pos.CENTER_RIGHT); 60 | return results; 61 | } 62 | 63 | private Node boundTextField(StringProperty boundProperty) { 64 | TextField textField = new TextField(); 65 | textField.textProperty().bindBidirectional(boundProperty); 66 | return textField; 67 | } 68 | 69 | private Node promptLabel(String contents) { 70 | return styledLabel(contents, "prompt-label"); 71 | } 72 | 73 | private Node headingLabel(String contents) { 74 | return styledLabel(contents, "heading-label"); 75 | } 76 | 77 | private Node styledLabel(String contents, String styleClass) { 78 | Label label = new Label(contents); 79 | label.getStyleClass().add(styleClass); 80 | return label; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import javafx.application.Application; 4 | import javafx.scene.Scene; 5 | import javafx.stage.Stage; 6 | 7 | public class Main extends Application { 8 | @Override 9 | public void start(Stage primaryStage) throws Exception { 10 | primaryStage.setScene(new Scene(new CustomerController().getView())); 11 | primaryStage.show(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part8/SlowCustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import java.util.Map; 4 | 5 | public class SlowCustomerDatabase extends CustomerDatabase { 6 | 7 | private final int delay; 8 | 9 | public SlowCustomerDatabase(int delay) { 10 | this.delay = delay; 11 | } 12 | 13 | @Override 14 | public int saveCustomer(Map customerRecord) { 15 | delay(); 16 | return super.saveCustomer(customerRecord); 17 | } 18 | 19 | private void delay() { 20 | try { 21 | Thread.sleep(delay); 22 | } catch (InterruptedException e) { 23 | throw new RuntimeException(e); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/Customer.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | public class Customer { 4 | 5 | private String accountNumber = ""; 6 | private String name = ""; 7 | 8 | 9 | public String getAccountNumber() { 10 | return accountNumber; 11 | } 12 | 13 | public void setAccountNumber(String accountNumber) { 14 | this.accountNumber = accountNumber; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerBroker.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerBroker { 7 | 8 | private final CustomerDAO dao = new CustomerDAO(); 9 | 10 | public int saveCustomer(Customer customer) { 11 | return dao.saveCustomer(createCustomerRecord(customer)); 12 | } 13 | 14 | Map createCustomerRecord(Customer customer) { 15 | Map customerRecord = new HashMap<>(); 16 | customerRecord.put("name", customer.getName()); 17 | customerRecord.put("account_number", customer.getAccountNumber()); 18 | return customerRecord; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerController.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import javafx.concurrent.Task; 4 | import javafx.scene.layout.Region; 5 | import javafx.util.Builder; 6 | 7 | public class CustomerController { 8 | 9 | private final Builder viewBuilder; 10 | private final CustomerInteractor interactor; 11 | 12 | public CustomerController() { 13 | CustomerModel model = new CustomerModel(); 14 | interactor = new CustomerInteractor(model); 15 | viewBuilder = new CustomerViewBuilder(model,this::saveCustomer); 16 | } 17 | 18 | private void saveCustomer(Runnable postTaskGuiActions) { 19 | Task saveTask = new Task<>() { 20 | @Override 21 | protected Void call() { 22 | interactor.saveCustomer(); 23 | return null; 24 | } 25 | }; 26 | saveTask.setOnSucceeded(evt -> postTaskGuiActions.run()); 27 | Thread saveThread = new Thread(saveTask); 28 | saveThread.start(); 29 | } 30 | 31 | public Region getView() { 32 | return viewBuilder.build(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerDAO.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import java.util.Map; 4 | 5 | public class CustomerDAO { 6 | 7 | static CustomerDatabase database = new SlowCustomerDatabase(10000); 8 | 9 | public int saveCustomer(Map customerRecord) { 10 | return database.saveCustomer(customerRecord); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class CustomerDatabase { 7 | 8 | private Map> data = new HashMap<>(); 9 | private Integer nextKey = 0; 10 | 11 | public int saveCustomer(Map customerRecord) { 12 | customerRecord.put("_id", (++nextKey).toString()); 13 | data.put(nextKey, customerRecord); 14 | return nextKey; 15 | } 16 | 17 | Map> getData() { 18 | return data; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerInteractor.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import javafx.beans.binding.Bindings; 4 | 5 | public class CustomerInteractor { 6 | private final CustomerModel model; 7 | private final CustomerBroker broker = new CustomerBroker(); 8 | 9 | public CustomerInteractor(CustomerModel model) { 10 | this.model = model; 11 | model.okToSaveProperty().bind(Bindings.createBooleanBinding(this::isDataValid, model.accountNumberProperty(), model.customerNameProperty())); 12 | } 13 | 14 | private boolean isDataValid() { 15 | return !model.getAccountNumber().isEmpty() && !model.getCustomerName().isEmpty(); 16 | } 17 | 18 | public void saveCustomer() { 19 | String customerName = model.getCustomerName(); 20 | String account = model.getAccountNumber(); 21 | int result = broker.saveCustomer(createCustomerFromModel()); 22 | System.out.println("Saving account: " + account + " Name: " + customerName + " Result: " + result); 23 | } 24 | 25 | Customer createCustomerFromModel() { 26 | Customer customer = new Customer(); 27 | customer.setAccountNumber(model.getAccountNumber()); 28 | customer.setName(model.getCustomerName()); 29 | return customer; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerModel.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import javafx.beans.property.BooleanProperty; 4 | import javafx.beans.property.SimpleBooleanProperty; 5 | import javafx.beans.property.SimpleStringProperty; 6 | import javafx.beans.property.StringProperty; 7 | 8 | public class CustomerModel { 9 | 10 | private final StringProperty accountNumber = new SimpleStringProperty(""); 11 | private final StringProperty customerName = new SimpleStringProperty(""); 12 | private final BooleanProperty okToSave = new SimpleBooleanProperty(false); 13 | 14 | public String getAccountNumber() { 15 | return accountNumber.get(); 16 | } 17 | 18 | public StringProperty accountNumberProperty() { 19 | return accountNumber; 20 | } 21 | 22 | public void setAccountNumber(String accountNumber) { 23 | this.accountNumber.set(accountNumber); 24 | } 25 | 26 | public String getCustomerName() { 27 | return customerName.get(); 28 | } 29 | 30 | public StringProperty customerNameProperty() { 31 | return customerName; 32 | } 33 | 34 | public void setCustomerName(String customerName) { 35 | this.customerName.set(customerName); 36 | } 37 | 38 | public boolean isOkToSave() { 39 | return okToSave.get(); 40 | } 41 | 42 | public BooleanProperty okToSaveProperty() { 43 | return okToSave; 44 | } 45 | 46 | public void setOkToSave(boolean okToSave) { 47 | this.okToSave.set(okToSave); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/CustomerViewBuilder.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import javafx.beans.property.StringProperty; 4 | import javafx.geometry.Insets; 5 | import javafx.geometry.Pos; 6 | import javafx.scene.Node; 7 | import javafx.scene.control.Button; 8 | import javafx.scene.control.Label; 9 | import javafx.scene.control.TextField; 10 | import javafx.scene.layout.BorderPane; 11 | import javafx.scene.layout.HBox; 12 | import javafx.scene.layout.Region; 13 | import javafx.scene.layout.VBox; 14 | import javafx.util.Builder; 15 | 16 | import java.util.Objects; 17 | import java.util.function.Consumer; 18 | 19 | public class CustomerViewBuilder implements Builder { 20 | 21 | private final CustomerModel model; 22 | private final Consumer saveHandler; 23 | public CustomerViewBuilder(CustomerModel model, Consumer saveHandler) { 24 | this.model = model; 25 | this.saveHandler = saveHandler; 26 | } 27 | 28 | @Override 29 | public Region build() { 30 | BorderPane results = new BorderPane(); 31 | results.getStylesheets().add(Objects.requireNonNull(this.getClass().getResource("/css/customer.css")).toExternalForm()); 32 | results.setTop(headingLabel("Customer Information")); 33 | results.setCenter(createCentre()); 34 | results.setBottom(createButtons()); 35 | return results; 36 | } 37 | 38 | private Node createCentre() { 39 | VBox results = new VBox(6, accountBox(), nameBox()); 40 | results.setPadding(new Insets(20)); 41 | return results; 42 | } 43 | 44 | private Node accountBox() { 45 | return new HBox(6,promptLabel("Account #:"), boundTextField(model.accountNumberProperty())); 46 | } 47 | 48 | private Node nameBox() { 49 | return new HBox(6, promptLabel("Name:"), boundTextField(model.customerNameProperty())); 50 | } 51 | 52 | private Node createButtons() { 53 | Button saveButton = new Button("Save"); 54 | saveButton.disableProperty().bind(model.okToSaveProperty().not()); 55 | saveButton.setOnAction(evt -> { 56 | saveButton.disableProperty().unbind(); 57 | saveButton.setDisable(true); 58 | saveHandler.accept(() -> saveButton.disableProperty().bind(model.okToSaveProperty().not())); 59 | }); 60 | HBox results = new HBox(10, saveButton); 61 | results.setAlignment(Pos.CENTER_RIGHT); 62 | return results; 63 | } 64 | 65 | private Node boundTextField(StringProperty boundProperty) { 66 | TextField textField = new TextField(); 67 | textField.textProperty().bindBidirectional(boundProperty); 68 | return textField; 69 | } 70 | 71 | private Node promptLabel(String contents) { 72 | return styledLabel(contents, "prompt-label"); 73 | } 74 | 75 | private Node headingLabel(String contents) { 76 | return styledLabel(contents, "heading-label"); 77 | } 78 | 79 | private Node styledLabel(String contents, String styleClass) { 80 | Label label = new Label(contents); 81 | label.getStyleClass().add(styleClass); 82 | return label; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/Main.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import javafx.application.Application; 4 | import javafx.scene.Scene; 5 | import javafx.stage.Stage; 6 | 7 | public class Main extends Application { 8 | @Override 9 | public void start(Stage primaryStage) throws Exception { 10 | primaryStage.setScene(new Scene(new CustomerController().getView())); 11 | primaryStage.show(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/ca/pragmaticcoding/beginners/part9/SlowCustomerDatabase.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part9; 2 | 3 | import java.util.Map; 4 | 5 | public class SlowCustomerDatabase extends CustomerDatabase { 6 | 7 | private final int delay; 8 | 9 | public SlowCustomerDatabase(int delay) { 10 | this.delay = delay; 11 | } 12 | 13 | @Override 14 | public int saveCustomer(Map customerRecord) { 15 | delay(); 16 | return super.saveCustomer(customerRecord); 17 | } 18 | 19 | private void delay() { 20 | try { 21 | Thread.sleep(delay); 22 | } catch (InterruptedException e) { 23 | throw new RuntimeException(e); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/module-info.java: -------------------------------------------------------------------------------- 1 | module ca.pragmaticcoding.beginners { 2 | requires javafx.controls; 3 | requires javafx.graphics; 4 | requires javafx.base; 5 | 6 | exports ca.pragmaticcoding.beginners.part1; 7 | exports ca.pragmaticcoding.beginners.part2; 8 | exports ca.pragmaticcoding.beginners.part3; 9 | exports ca.pragmaticcoding.beginners.part4; 10 | exports ca.pragmaticcoding.beginners.part5; 11 | exports ca.pragmaticcoding.beginners.part6; 12 | exports ca.pragmaticcoding.beginners.part8; 13 | exports ca.pragmaticcoding.beginners.part9; 14 | exports ca.pragmaticcoding.beginners.part10; 15 | } -------------------------------------------------------------------------------- /src/main/resources/css/beginners.css: -------------------------------------------------------------------------------- 1 | .root { 2 | prompt-colour: #a04000; 3 | greeting-colour: #6000b0; 4 | } 5 | 6 | .greeting-label { 7 | -fx-text-fill: greeting-colour; 8 | -fx-font-size: 32px; 9 | } 10 | 11 | .prompt-label { 12 | -fx-text-fill: prompt-colour; 13 | -fx-font-size: 16px; 14 | -fx-font-weight: bold; 15 | } -------------------------------------------------------------------------------- /src/main/resources/css/customer.css: -------------------------------------------------------------------------------- 1 | .root { 2 | prompt-colour: #a04000; 3 | heading-colour: #2090c0; 4 | } 5 | 6 | .heading-label { 7 | -fx-text-fill: heading-colour; 8 | -fx-font-size: 32px; 9 | } 10 | 11 | .prompt-label { 12 | -fx-text-fill: prompt-colour; 13 | -fx-font-size: 16px; 14 | -fx-font-weight: bold; 15 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part10/CustomerBrokerTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class CustomerBrokerTest { 8 | 9 | @Test 10 | void createCustomerRecord_AccountNumberTest() { 11 | CustomerBroker broker = new CustomerBroker(); 12 | Customer customer = new Customer(); 13 | customer.setAccountNumber("1234"); 14 | customer.setName("Fred"); 15 | assertEquals("1234", broker.createCustomerRecord(customer).get("account_number"), "Account number check"); 16 | } 17 | 18 | @Test 19 | void createCustomerRecord_CustomerNameTest() { 20 | CustomerBroker broker = new CustomerBroker(); 21 | Customer customer = new Customer(); 22 | customer.setAccountNumber("1234"); 23 | customer.setName("Fred"); 24 | assertEquals("Fred", broker.createCustomerRecord(customer).get("name"), "Customer name check"); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part10/CustomerDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import org.junit.jupiter.api.BeforeEach; 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import static org.junit.jupiter.api.Assertions.*; 10 | 11 | class CustomerDatabaseTest { 12 | 13 | CustomerDatabase dataBase; 14 | 15 | @BeforeEach 16 | void init() { 17 | dataBase = new CustomerDatabase(); 18 | } 19 | 20 | @Test 21 | void saveCustomerIdIncrementTest() { 22 | assertEquals(1, dataBase.saveCustomer(createFred()), "Id is incremented"); 23 | } 24 | 25 | @Test 26 | void saveCustomerIdIncrementTwiceTest() { 27 | dataBase.saveCustomer(createFred()); 28 | assertEquals(2, dataBase.saveCustomer(createGeorge()), "Id is incremented"); 29 | } 30 | 31 | @Test 32 | void saveCustomerIdInsertionTest() { 33 | dataBase.saveCustomer(createFred()); 34 | assertEquals("1", dataBase.getData().get(1).get("_id"), "Id is inserted"); 35 | } 36 | 37 | @Test 38 | void saveCustomerRecordCorrectTest() { 39 | dataBase.saveCustomer(createFred()); 40 | assertEquals("Fred", dataBase.getData().get(1).get("name"), "Id is inserted"); 41 | } 42 | 43 | @Test 44 | void findAccountTest_Found() { 45 | dataBase.saveCustomer(createFred()); 46 | dataBase.saveCustomer(createGeorge()); 47 | assertTrue(dataBase.isAccountOnFile("123"), "Lookup Fred"); 48 | } 49 | 50 | @Test 51 | void findAccountTest_NotFound() { 52 | dataBase.saveCustomer(createFred()); 53 | dataBase.saveCustomer(createGeorge()); 54 | assertFalse(dataBase.isAccountOnFile("7777"), "Lookup someone not present"); 55 | } 56 | 57 | @Test 58 | void saveWithoutAccountNumber() { 59 | HashMap customerRecord = new HashMap<>(); 60 | customerRecord.put("name", "Fred"); 61 | assertThrows(IllegalArgumentException.class, () -> dataBase.saveNewCustomer(customerRecord)); 62 | } 63 | 64 | @Test 65 | void saveRecordTwiceTest() { 66 | dataBase.saveCustomer(createFred()); 67 | assertThrows(CustomerAlreadyOnFileException.class, () -> dataBase.saveNewCustomer(createFred())); 68 | } 69 | 70 | private Map createFred() { 71 | HashMap customerRecord = new HashMap<>(); 72 | customerRecord.put("name", "Fred"); 73 | customerRecord.put("account_number", "123"); 74 | return customerRecord; 75 | } 76 | 77 | private Map createGeorge() { 78 | HashMap customerRecord = new HashMap<>(); 79 | customerRecord.put("name", "George"); 80 | customerRecord.put("account_number", "567"); 81 | return customerRecord; 82 | } 83 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part10/CustomerInteractorTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part10; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class CustomerInteractorTest { 8 | 9 | @Test 10 | void createCustomer_NameTest() { 11 | CustomerModel model = new CustomerModel(); 12 | CustomerInteractor interactor = new CustomerInteractor(model); 13 | model.setCustomerName("Fred"); 14 | model.setAccountNumber("ABCDE"); 15 | assertEquals("Fred", interactor.createCustomerFromModel().getName(),"Check customer name"); 16 | } 17 | 18 | @Test 19 | void createCustomer_AccountTest() { 20 | CustomerModel model = new CustomerModel(); 21 | CustomerInteractor interactor = new CustomerInteractor(model); 22 | model.setCustomerName("Fred"); 23 | model.setAccountNumber("ABCDE"); 24 | assertEquals("ABCDE", interactor.createCustomerFromModel().getAccountNumber(),"Check customer name"); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part6/CustomerBrokerTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.*; 6 | 7 | class CustomerBrokerTest { 8 | 9 | @Test 10 | void createCustomerRecord_AccountNumberTest() { 11 | CustomerBroker broker = new CustomerBroker(); 12 | Customer customer = new Customer(); 13 | customer.setAccountNumber("1234"); 14 | customer.setName("Fred"); 15 | assertEquals("1234", broker.createCustomerRecord(customer).get("account_number"), "Account number check"); 16 | } 17 | 18 | @Test 19 | void createCustomerRecord_CustomerNameTest() { 20 | CustomerBroker broker = new CustomerBroker(); 21 | Customer customer = new Customer(); 22 | customer.setAccountNumber("1234"); 23 | customer.setName("Fred"); 24 | assertEquals("Fred", broker.createCustomerRecord(customer).get("name"), "Customer name check"); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part6/CustomerDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import java.util.HashMap; 6 | 7 | import static org.junit.jupiter.api.Assertions.*; 8 | 9 | class CustomerDatabaseTest { 10 | 11 | @Test 12 | void saveCustomerIdIncrementTest() { 13 | CustomerDatabase dataBase = new CustomerDatabase(); 14 | assertEquals(1,dataBase.saveCustomer(new HashMap()), "Id is incremented"); 15 | } 16 | 17 | @Test 18 | void saveCustomerIdIncrementTwiceTest() { 19 | CustomerDatabase dataBase = new CustomerDatabase(); 20 | dataBase.saveCustomer(new HashMap()); 21 | assertEquals(2,dataBase.saveCustomer(new HashMap()), "Id is incremented"); 22 | } 23 | 24 | @Test 25 | void saveCustomerIdInsertionTest() { 26 | CustomerDatabase dataBase = new CustomerDatabase(); 27 | HashMap customerRecord = new HashMap<>(); 28 | dataBase.saveCustomer(new HashMap()); 29 | assertEquals("1",dataBase.getData().get(1).get("_id"), "Id is inserted"); 30 | } 31 | 32 | @Test 33 | void saveCustomerRecordCorrectTest() { 34 | CustomerDatabase dataBase = new CustomerDatabase(); 35 | HashMap customerRecord = new HashMap<>(); 36 | customerRecord.put("name", "Fred"); 37 | dataBase.saveCustomer(customerRecord); 38 | assertEquals("Fred",dataBase.getData().get(1).get("name"), "Id is inserted"); 39 | } 40 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part6/CustomerInteractorTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part6; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.*; 6 | 7 | class CustomerInteractorTest { 8 | 9 | @Test 10 | void createCustomer_NameTest() { 11 | CustomerModel model = new CustomerModel(); 12 | CustomerInteractor interactor = new CustomerInteractor(model); 13 | model.setCustomerName("Fred"); 14 | model.setAccountNumber("ABCDE"); 15 | assertEquals("Fred", interactor.createCustomerFromModel().getName(),"Check customer name"); 16 | } 17 | 18 | @Test 19 | void createCustomer_AccountTest() { 20 | CustomerModel model = new CustomerModel(); 21 | CustomerInteractor interactor = new CustomerInteractor(model); 22 | model.setCustomerName("Fred"); 23 | model.setAccountNumber("ABCDE"); 24 | assertEquals("ABCDE", interactor.createCustomerFromModel().getAccountNumber(),"Check customer name"); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part8/CustomerBrokerTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class CustomerBrokerTest { 8 | 9 | @Test 10 | void createCustomerRecord_AccountNumberTest() { 11 | CustomerBroker broker = new CustomerBroker(); 12 | Customer customer = new Customer(); 13 | customer.setAccountNumber("1234"); 14 | customer.setName("Fred"); 15 | assertEquals("1234", broker.createCustomerRecord(customer).get("account_number"), "Account number check"); 16 | } 17 | 18 | @Test 19 | void createCustomerRecord_CustomerNameTest() { 20 | CustomerBroker broker = new CustomerBroker(); 21 | Customer customer = new Customer(); 22 | customer.setAccountNumber("1234"); 23 | customer.setName("Fred"); 24 | assertEquals("Fred", broker.createCustomerRecord(customer).get("name"), "Customer name check"); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part8/CustomerDatabaseTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | 6 | import java.util.HashMap; 7 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; 9 | 10 | class CustomerDatabaseTest { 11 | 12 | @Test 13 | void saveCustomerIdIncrementTest() { 14 | CustomerDatabase dataBase = new CustomerDatabase(); 15 | assertEquals(1,dataBase.saveCustomer(new HashMap()), "Id is incremented"); 16 | } 17 | 18 | @Test 19 | void saveCustomerIdIncrementTwiceTest() { 20 | CustomerDatabase dataBase = new CustomerDatabase(); 21 | dataBase.saveCustomer(new HashMap()); 22 | assertEquals(2,dataBase.saveCustomer(new HashMap()), "Id is incremented"); 23 | } 24 | 25 | @Test 26 | void saveCustomerIdInsertionTest() { 27 | CustomerDatabase dataBase = new CustomerDatabase(); 28 | HashMap customerRecord = new HashMap<>(); 29 | dataBase.saveCustomer(new HashMap()); 30 | assertEquals("1",dataBase.getData().get(1).get("_id"), "Id is inserted"); 31 | } 32 | 33 | @Test 34 | void saveCustomerRecordCorrectTest() { 35 | CustomerDatabase dataBase = new CustomerDatabase(); 36 | HashMap customerRecord = new HashMap<>(); 37 | customerRecord.put("name", "Fred"); 38 | dataBase.saveCustomer(customerRecord); 39 | assertEquals("Fred",dataBase.getData().get(1).get("name"), "Id is inserted"); 40 | } 41 | } -------------------------------------------------------------------------------- /src/test/java/ca/pragmaticcoding/beginners/part8/CustomerInteractorTest.java: -------------------------------------------------------------------------------- 1 | package ca.pragmaticcoding.beginners.part8; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; 6 | 7 | class CustomerInteractorTest { 8 | 9 | @Test 10 | void createCustomer_NameTest() { 11 | CustomerModel model = new CustomerModel(); 12 | CustomerInteractor interactor = new CustomerInteractor(model); 13 | model.setCustomerName("Fred"); 14 | model.setAccountNumber("ABCDE"); 15 | assertEquals("Fred", interactor.createCustomerFromModel().getName(),"Check customer name"); 16 | } 17 | 18 | @Test 19 | void createCustomer_AccountTest() { 20 | CustomerModel model = new CustomerModel(); 21 | CustomerInteractor interactor = new CustomerInteractor(model); 22 | model.setCustomerName("Fred"); 23 | model.setAccountNumber("ABCDE"); 24 | assertEquals("ABCDE", interactor.createCustomerFromModel().getAccountNumber(),"Check customer name"); 25 | } 26 | } --------------------------------------------------------------------------------