-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.java
89 lines (78 loc) · 2.28 KB
/
Controller.java
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
package com.internshala.javafx;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
public Label welcome;
@FXML
public ChoiceBox<String> choice;
@FXML
public TextField field;
@FXML
public Button convert;
private static final String C_F = "Celsius to Fahrenheit";
private static final String F_C = "Fahrenheit to Celsius";
boolean IsC_F= true;
@Override
public void initialize(URL location, ResourceBundle resources) {
choice.getItems().add(C_F);
choice.getItems().add(F_C);
choice.setValue(C_F);
choice.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if(newValue.equals(C_F))
{
IsC_F = true;
}
else
IsC_F = false;
});
convert.setOnAction(event -> {button();});
}
private void button() {
String inp =field.getText();
float temp = 0.0f;
try {
temp = Float.parseFloat(inp);
}
catch(Exception ex)
{
warning();
return;
}
float ntemp =0.0f;
if(IsC_F)
{
ntemp= (temp*9/5)+32;
}
else
{
ntemp=(temp-32)*5/9;
}
display(ntemp);
}
private void warning() {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Invalid temperature entered");
alert.setContentText("Please enter a valid temperature");
alert.show();
}
private void display(float ntemp) {
String unit = IsC_F? "F":"C";
System.out.println(ntemp+" "+unit);
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Result");
alert.setContentText("The Temperature is :"+ntemp+" "+unit);
alert.show();
}
}