1111import  java .io .DataInputStream ;
1212import  java .io .DataOutputStream ;
1313import  java .io .IOException ;
14+ import  java .io .ObjectOutputStream ;
1415import  java .net .Socket ;
16+ 1517public  class  Exercise31_02Client  extends  Application  {
16-  DataOutputStream  toServer  = null ;
18+  ObjectOutputStream  toServer  = null ;
1719 DataInputStream  fromServer  = null ;
20+  private  TextField  weightTextInput  = new  TextField ();
21+  private  TextField  heightTextInput  = new  TextField ();
1822
19-  // Text fields for BMI information 
20-  private  TextField  tfWeight  = new  TextField ();
21-  private  TextField  tfHeight  = new  TextField ();
22- 23-  @ Override  // Override the start method in the Application class 
23+  @ Override 
2424 public  void  start (Stage  primaryStage ) {
25-  // Main pane 
2625 BorderPane  pane  = new  BorderPane ();
27- 28-  // Set text field alignment right 
29-  tfWeight .setAlignment (Pos .BASELINE_RIGHT );
30-  tfHeight .setAlignment (Pos .BASELINE_RIGHT );
31- 32-  // Create button to send BMI info to server 
26+  weightTextInput .setAlignment (Pos .BASELINE_RIGHT );
27+  heightTextInput .setAlignment (Pos .BASELINE_RIGHT );
3328 Button  btSubmit  = new  Button ("Submit" );
34- 35-  // Pane to hold BMI information and submit button 
3629 GridPane  paneForBmiInfo  = new  GridPane ();
3730 paneForBmiInfo .add (new  Label ("Weight in pounds" ), 0 , 0 );
38-  paneForBmiInfo .add (tfWeight , 1 , 0 );
31+  paneForBmiInfo .add (weightTextInput , 1 , 0 );
3932 paneForBmiInfo .add (new  Label ("Height in inches" ), 0 , 1 );
40-  paneForBmiInfo .add (tfHeight , 1 , 1 );
33+  paneForBmiInfo .add (heightTextInput , 1 , 1 );
4134 paneForBmiInfo .add (btSubmit , 2 , 1 );
42- 43-  // Text Area to display contents 
4435 TextArea  ta  = new  TextArea ();
4536 pane .setTop (paneForBmiInfo );
4637 pane .setCenter (new  ScrollPane (ta ));
4738
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 
39+  Scene  scene  = new  Scene (pane , 560 , 300 );
40+  primaryStage .setTitle ("Exercise31_02Client" );
41+  primaryStage .setScene (scene );
42+  primaryStage .show ();
5343
5444 btSubmit .setOnAction (e  -> {
5545 try  {
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- 46+  double  weight  = Double .parseDouble (weightTextInput .getText ().trim ());
47+  double  height  = Double .parseDouble (heightTextInput .getText ().trim ());
48+  BmiDto  dto  = new  BmiDto (weight , height );
6049 // Send the BMI information to the server 
61-  toServer .writeDouble (weight );
62-  toServer .writeDouble (height );
50+  toServer .writeObject (dto );
6351 toServer .flush ();
6452
6553 // Get string from the server 
@@ -69,8 +57,7 @@ public void start(Stage primaryStage) {
6957 ta .appendText ("Weight: "  + weight  + '\n' );
7058 ta .appendText ("Height: "  + height  + '\n' );
7159 ta .appendText (bmi  + '\n' );
72-  }
73-  catch  (IOException  ex ) {
60+  } catch  (IOException  ex ) {
7461 System .err .println (ex );
7562 }
7663 });
@@ -83,9 +70,8 @@ public void start(Stage primaryStage) {
8370 fromServer  = new  DataInputStream (socket .getInputStream ());
8471
8572 // Create an output stream to send data to the server 
86-  toServer  = new  DataOutputStream (socket .getOutputStream ());
87-  }
88-  catch  (IOException  ex ) {
73+  toServer  = new  ObjectOutputStream (socket .getOutputStream ());
74+  } catch  (IOException  ex ) {
8975 ta .appendText (ex .toString () + '\n' );
9076 }
9177 }
0 commit comments