I am trying to pass the ArrayList of customers back to the first Scene because I want to display the customers inside a table view. I add the customers to the Bank object inside the Second Scene Controller. I was able to get it to run the only issue is that I had to create a new instance of the Bank object. I am not sure where I would instantiate the Bank object that way it is not getting reset everytime I create an account and go back to the main screen
public class MainRun extends Application
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/sample/mainMenu.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add("my_style.css");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
This is my first scene class
public class FirstSceneController implements Initializable
private Scene scene;
private Stage stage;
private Parent root;
@FXML
private MenuItem exit_menuItem;
@FXML
MenuBar menu_Bar;
@FXML
private Button createAccount;
@FXML
private GridPane gridPane_root;
@FXML
private StackPane parentContainer;
public void closeWindow(ActionEvent actionEvent) {
stage = (Stage) menu_Bar.getScene().getWindow();
stage.close();
}
public void switchToCreateAccountWindow(ActionEvent event) throws IOException {
root = FXMLLoader.load(getClass().getResource("addAccount.fxml"));
scene = createAccount.getScene();
root.translateYProperty().set(scene.getHeight());
parentContainer.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateYProperty(), 0, Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(event1 -> {
parentContainer.getChildren().remove(gridPane_root);
});
timeline.play();
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
ImageView exitImage = new ImageView(new Image("exit_icon.jpg"));
exitImage.setSmooth(true);
exitImage.setPreserveRatio(true);
exitImage.setFitHeight(16);
exitImage.setFitWidth(16);
exit_menuItem.setGraphic(exitImage);
}
Second Scene Class
public class SecondSceneController implements Initializable
ObservableList<String> list = FXCollections.observableArrayList("Checking", "Savings");
private Parent root;
private Scene scene;
private Stage stage;
private StackPane parentContainer;
@FXML
private GridPane gridPane_root;
@FXML
private MenuBar menu_Bar;
@FXML
ComboBox<String> account_types;
@FXML
private Button cancel_button;
@FXML
private TextField firstNameField;
@FXML
private TextField lastNameField;
@FXML
private TextField socialSecurityField;
@FXML
private TextField depositField;
@FXML
MenuItem exit_menuItem;
private double amount = 0.0;
Account account = null;
private Bank bank;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
account_types.setItems(list);
ImageView exitImage = new ImageView(new Image("exit_icon.jpg"));
exitImage.setSmooth(true);
exitImage.setPreserveRatio(true);
exitImage.setFitHeight(16);
exitImage.setFitWidth(16);
exit_menuItem.setGraphic(exitImage);
}
public void goBackToMainScreen() throws IOException {
root = FXMLLoader.load(getClass().getResource("mainMenu.fxml"));
scene = cancel_button.getScene();
root.translateXProperty().set(scene.getWidth());
parentContainer = (StackPane) scene.getRoot();
parentContainer.getChildren().add(root);
Timeline timeline = new Timeline();
KeyValue kv = new KeyValue(root.translateXProperty(), 0, Interpolator.EASE_IN);
KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
timeline.getKeyFrames().add(kf);
timeline.setOnFinished(event1 -> {
parentContainer.getChildren().remove(gridPane_root);
});
timeline.play();
}
public void createAccountActionPerformed(ActionEvent event) throws IOException {
boolean isValid;
StringBuilder stringBuilder = new StringBuilder();
String firstName = "", lastName = "", ssn = "";
if (firstNameField.getText().isEmpty()) {
System.out.println("First name must not be empty");
} else {
firstName = firstNameField.getText();
}
if (lastNameField.getText().isEmpty()) {
System.out.println("Last name must not be empty");
} else {
lastName = lastNameField.getText();
}
if (socialSecurityField.getText().isEmpty()) {
System.out.println("SSN must not be empty");
}
if (!socialSecurityField.getText().matches("\\d{3}-\\d{2}-\\d{4}")) {
System.out.println("Invalid SSN");
} else {
ssn = socialSecurityField.getText().replace("-", "");
}
if (depositField.getText().isEmpty()) {
System.out.println("Enter an amount!");
isValid = false;
} else {
try {
amount = round(Double.parseDouble(depositField.getText()), 2);
isValid = true;
} catch (NumberFormatException e) {
System.out.println("Deposit must be a number!");
isValid = false;
}
}
if (account_types.getSelectionModel().isEmpty()) {
System.out.println("Please select an account type!");
isValid = false;
} else if (account_types.getSelectionModel().equals("Checking")) {
account = new Checking(amount);
isValid = true;
} else if (account_types.getSelectionModel().equals("Savings")) {
account = new Savings(amount);
isValid = true;
}
Customer customer = new Customer(firstName, lastName, ssn, account);
bank.addCustomer(customer);
if (isValid) {
for (Customer c : bank.getCustomers()) {
System.out.println(c.firstName + " " + c.lastName);
}
goBackToMainScreen();
}
}
public void closeWindow(ActionEvent event) {
stage = (Stage) menu_Bar.getScene().getWindow();
stage.close();
}
private static double round(double number, int places) {
if (places < 0) {
throw new IllegalArgumentException();
}
BigDecimal bd = new BigDecimal(number);
bd = bd.setScale(places, RoundingMode.FLOOR);
return bd.doubleValue();
}
Bank Class that holds an arraylist of customers public class Bank
List<Customer> customers;
public Bank() {
customers = new ArrayList<>();
}
public void addCustomer(Customer customer) {
customers.add(customer);
}
public List<Customer> getCustomers() {
return customers;
}