11package ch_31 .exercise31_02 ;
22
33import javafx .application .Application ;
4- import javafx .event .ActionEvent ;
5- import javafx .event .EventHandler ;
6- import javafx .geometry .HPos ;
74import javafx .geometry .Pos ;
8- import javafx .geometry .VPos ;
95import javafx .scene .Scene ;
10- import javafx .scene .control .Button ;
11- import javafx .scene .control .Label ;
12- import javafx .scene .control .TextArea ;
13- import javafx .scene .control .TextField ;
6+ import javafx .scene .control .*;
7+ import javafx .scene .layout .BorderPane ;
148import javafx .scene .layout .GridPane ;
159import javafx .stage .Stage ;
1610
17- import java .io .*;
11+ import java .io .DataInputStream ;
12+ import java .io .DataOutputStream ;
13+ import java .io .IOException ;
1814import java .net .Socket ;
19- 2015public class Exercise31_02Client extends Application {
21- private TextField weightTextInput = new TextField ();
22- private TextField heightTextInput = new TextField ();
23- private TextArea displayResultTextArea = new TextArea ();
16+ DataOutputStream toServer = null ;
17+ DataInputStream fromServer = null ;
2418
25- private Button submitButton = new Button ("Submit" );
19+ // Text fields for BMI information
20+ private TextField tfWeight = new TextField ();
21+ private TextField tfHeight = new TextField ();
2622
27- // Host name or ip
28- String host = "localhost" ;
23+ @ Override // Override the start method in the Application class
24+ public void start (Stage primaryStage ) {
25+ // Main pane
26+ BorderPane pane = new BorderPane ();
2927
30- private ObjectOutputStream toServer ;
31- private DataInputStream fromServer ;
28+ // Set text field alignment right
29+ tfWeight .setAlignment (Pos .BASELINE_RIGHT );
30+ tfHeight .setAlignment (Pos .BASELINE_RIGHT );
3231
33- public Exercise31_02Client () {
34- System .out .println ("##-_-_--_--_--__-_-- Starting: Exercise31_02Client --_-__--__--__--_--_--##" );
35- }
32+ // Create button to send BMI info to server
33+ Button btSubmit = new Button ("Submit" );
3634
37- @ Override
38- public void start (Stage primaryStage ) {
39- Stage stage = new Stage ();
40- GridPane pane = new GridPane ();
41- pane .add (new Label ("Weight in pounds" ), 0 , 0 );
42- pane .add (weightTextInput , 1 , 0 );
43- pane .add (new Label ("Height in inches" ), 0 , 1 );
44- pane .add (heightTextInput , 1 , 1 );
45- pane .add (submitButton , 1 , 3 );
46- pane .add (displayResultTextArea , 1 , 4 );
47- GridPane .setHalignment (submitButton , HPos .RIGHT );
48- GridPane .setValignment (displayResultTextArea , VPos .BOTTOM );
49- pane .setAlignment (Pos .CENTER );
50- weightTextInput .setPrefColumnCount (15 );
51- heightTextInput .setPrefColumnCount (15 );
52- submitButton .setOnAction (new ButtonListener ());
53- 54- Scene scene = new Scene (pane , 450 , 200 );
55- stage .setTitle ("BMI Client" );
56- stage .setScene (scene );
57- stage .show ();
58- 59- try (Socket socket = new Socket (host , 8000 )) {
60- // Create an output stream to the server
61- toServer = new ObjectOutputStream (socket .getOutputStream ());
62- // Create an input stream from the server
63- fromServer = new DataInputStream (socket .getInputStream ());
64- } catch (IOException ex ) {
65- System .out .println ("##----------- Client Error: IOException: " + ex .getMessage () + " -----------##" );
66- }
67- }
35+ // Pane to hold BMI information and submit button
36+ GridPane paneForBmiInfo = new GridPane ();
37+ paneForBmiInfo .add (new Label ("Weight in pounds" ), 0 , 0 );
38+ paneForBmiInfo .add (tfWeight , 1 , 0 );
39+ paneForBmiInfo .add (new Label ("Height in inches" ), 0 , 1 );
40+ paneForBmiInfo .add (tfHeight , 1 , 1 );
41+ paneForBmiInfo .add (btSubmit , 2 , 1 );
42+ 43+ // Text Area to display contents
44+ TextArea ta = new TextArea ();
45+ pane .setTop (paneForBmiInfo );
46+ pane .setCenter (new ScrollPane (ta ));
6847
69- /**
70- * Custom event handler for the submit button
71- */
72- private class ButtonListener implements EventHandler <ActionEvent > {
73- @ Override
74- public void handle (ActionEvent e ) {
48+ // Create a scene and place it in the stage
49+ Scene scene = new Scene (pane , 400 , 200 );
50+ primaryStage .setTitle ("Exercise31_01Client" ); // Set the stage title
51+ primaryStage .setScene (scene ); // Place the scene in the stage
52+ primaryStage .show (); // Display the stage
53+ 54+ btSubmit .setOnAction (e -> {
7555 try {
76- // Get weight and height from the text fields
77- double weight = Double .parseDouble (weightTextInput .getText ().trim ());
78- double height = Double .parseDouble (heightTextInput .getText ().trim ());
79- // Create a BmiDto and send to the server
80- BmiDto s = new BmiDto (weight , height );
81- toServer .writeObject (s );
56+ // Get the weight and height from the text fields
57+ double weight = Double .parseDouble (tfWeight .getText ().trim ());
58+ double height = Double .parseDouble (tfHeight .getText ().trim ());
59+ 60+ // Send the BMI information to the server
61+ toServer .writeDouble (weight );
62+ toServer .writeDouble (height );
8263 toServer .flush ();
83- // Get resulting BMI from the server
84- double bmi = fromServer .readDouble ();
85- // Display to the text area
86- displayResultTextArea .setText ("Weight: " + weight + "\n Height: " + height + "\n BMI is: " + bmi
87- + "\n " + getCategory (bmi ));
88- } catch (IOException ex ) {
89- System .out .println ("##----------- Client Error: IOException: " + ex .getMessage () + " -----------##" );
64+ 65+ // Get string from the server
66+ String bmi = fromServer .readUTF ();
67+ 68+ // Display to text area
69+ ta .appendText ("Weight: " + weight + '\n' );
70+ ta .appendText ("Height: " + height + '\n' );
71+ ta .appendText (bmi + '\n' );
9072 }
91- }
92- }
73+ catch (IOException ex ) {
74+ System .err .println (ex );
75+ }
76+ });
9377
94- private String getCategory (double bmi ) {
95- if (bmi < 18.5 ) {
96- return "Underweight" ;
97- } else if (bmi < 25 ) {
98- return "Normal" ;
99- } else if (bmi < 30 ) {
100- return "Overweight" ;
101- } else {
102- return "Obese" ;
78+ try {
79+ // Create a socket to connect to the server
80+ Socket socket = new Socket ("localhost" , 8000 );
81+ 82+ // Create an input stream to receive data from the server
83+ fromServer = new DataInputStream (socket .getInputStream ());
84+ 85+ // Create an output stream to send data to the server
86+ toServer = new DataOutputStream (socket .getOutputStream ());
87+ }
88+ catch (IOException ex ) {
89+ ta .appendText (ex .toString () + '\n' );
10390 }
10491 }
105- 10692}
0 commit comments