Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. // Author: Joshua Ferrell
  2. // Date: 3/25/2017
  3.  
  4. import javafx.application.Application;
  5. import javafx.geometry.Pos;
  6. import javafx.geometry.HPos;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.TextField;
  11. import javafx.scene.layout.GridPane;
  12. import javafx.stage.Stage;
  13.  
  14. A program that caculates the return on an investment.
  15.  
  16. public class Exercise15_5 extends Application {
  17.  
  18. // globals
  19. private TextField tfInvestmentAmount = new TextField();
  20. private TextField tfNumYears = new TextField();
  21. private TextField tfAnnualInterestRate = new TextField();
  22. private TextField tfFutureValue = new TextField();
  23. private Button btCalc = new Button("Calculate");
  24.  
  25. @Override
  26. public void start(Stage primaryStage) {
  27.  
  28. // Create UI
  29. GridPane gridPane = new GridPane();
  30. gridPane.setHgap(5);
  31. gridPane.setVgap(5);
  32. gridPane.add(new Label("Investment Amount:"), 0, 0);
  33. gridPane.add(tfInvestmentAmount, 1, 0);
  34. gridPane.add(new Label("Number of Years:"), 0, 1);
  35. gridPane.add(tfNumYears, 1, 1);
  36. gridPane.add(new Label("Annual Interest Rate:"), 0, 2);
  37. gridPane.add(tfAnnualInterestRate, 1, 2);
  38. gridPane.add(new Label("Future Value:"), 0, 3);
  39. gridPane.add(tfFutureValue, 1, 3);
  40. gridPane.add(btCalc, 1, 4);
  41.  
  42. // Set Properties for UI
  43. gridPane.setAlignment(Pos.CENTER);
  44. tfInvestmentAmount.setAlignment(Pos.BOTTOM_RIGHT);
  45. tfNumYears.setAlignment(Pos.BOTTOM_RIGHT);
  46. tfAnnualInterestRate.setAlignment(Pos.BOTTOM_RIGHT);
  47. tfFutureValue.setAlignment(Pos.BOTTOM_RIGHT);
  48. tfFutureValue.setEditable(false);
  49. gridPane.setHalignment(btCalc, HPos.RIGHT);
  50.  
  51. // Process event (caculate future value)
  52. btCalc.setOnAction(e -> calculateFutureValue());
  53.  
  54. // Create a scene and place it in the stage
  55. primaryStage.setScene(new Scene(gridPane, 300, 200));
  56. primaryStage.setTitle("Exercise15_05"); // Set Title
  57. primaryStage.show(); // Display the Stage
  58. }
  59.  
  60. private void calculateFutureValue() {
  61.  
  62. // Get Values from text fields
  63. double amount = Double.parseDouble(tfInvestmentAmount.getText());
  64. double interest = Double.parseDouble(tfAnnualInterestRate.getText());
  65. int years = Integer.parseInt(tfNumYears.getText());
  66.  
  67. // Get Monthly interest rate
  68. double monthlyInterestRate = interest / 12 / 100;
  69.  
  70. // Get Future Value
  71. double futureValue = amount * Math.pow(1 + monthlyInterestRate, years * 12);
  72.  
  73. tfFutureValue.setText(String.format("$%.2f", futureValue));
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement