Repository navigation
Expand file tree
/
Copy pathCalculatorController.java
More file actions
94 lines (86 loc) · 3.4 KB
/
Copy pathCalculatorController.java
File metadata and controls
94 lines (86 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.example.loanproject;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CalculatorController implements Initializable {
@FXML
private TextField tfAnnualInterestRate ;
@FXML
private TextField tfNumberOfYears;
@FXML
private TextField tfLoanAmount;
@FXML
private TextField tfMonthlyPayment ;
@FXML
private TextField tfTotalPayment ;
@FXML
private Button btCalculate;
@FXML
private Button button_home;
Alert a = new Alert(Alert.AlertType.NONE);
@Override
public void initialize(URL location, ResourceBundle resources) {
button_home.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource("logged-in.fxml"));
Scene scene = new Scene(fxmlLoader.load());
Stage stage = new Stage();
stage.setTitle("Home page");
stage.setScene(scene);
stage.show();
((Node) (event.getSource())).getScene().getWindow().hide();
} catch (IOException e) {
Logger logger = Logger.getLogger(getClass().getName());
logger.log(Level.SEVERE, "Failed", e);
}
}
});
}
@FXML
private void handle (ActionEvent event) {
try {
double interest = Double.parseDouble(tfAnnualInterestRate.getText());
int year = Integer.parseInt(tfNumberOfYears.getText());
double loanAmount = Double.parseDouble(tfLoanAmount.getText());
double EMi = getMonthlyPayment(interest, year, loanAmount);
double Tot = getTotalPayment(interest, year, loanAmount);
tfMonthlyPayment.setText(String.format("₹%.2f", EMi));
tfTotalPayment.setText(String.format("₹%.2f", Tot));
}
catch (Exception e)
{
System.out.println("Wrong input ,try again");
Alert.AlertType type = Alert.AlertType.ERROR;
Alert alert =new Alert(type,"");
alert.getDialogPane().setContentText("Wrong input ,try again");
alert.getDialogPane().setHeaderText("Error!");
alert.showAndWait();
}
}
/** Find monthly payment */
public double getMonthlyPayment(double interest,int year,double loanAmount ) {
double monthlyInterestRate = interest / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, year * 12)));
return monthlyPayment;
}
/** Find total payment */
public double getTotalPayment(double interest ,int year,double loanAmount) {
double totalPayment = getMonthlyPayment(interest,year,loanAmount) * year * 12;
return totalPayment;
}
}