11
11
import java .io .DataInputStream ;
12
12
import java .io .DataOutputStream ;
13
13
import java .io .IOException ;
14
+ import java .io .ObjectOutputStream ;
14
15
import java .net .Socket ;
16
+
15
17
public class Exercise31_02Client extends Application {
16
- DataOutputStream toServer = null ;
18
+ ObjectOutputStream toServer = null ;
17
19
DataInputStream fromServer = null ;
20
+ private TextField weightTextInput = new TextField ();
21
+ private TextField heightTextInput = new TextField ();
18
22
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
24
24
public void start (Stage primaryStage ) {
25
- // Main pane
26
25
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 );
33
28
Button btSubmit = new Button ("Submit" );
34
-
35
- // Pane to hold BMI information and submit button
36
29
GridPane paneForBmiInfo = new GridPane ();
37
30
paneForBmiInfo .add (new Label ("Weight in pounds" ), 0 , 0 );
38
- paneForBmiInfo .add (tfWeight , 1 , 0 );
31
+ paneForBmiInfo .add (weightTextInput , 1 , 0 );
39
32
paneForBmiInfo .add (new Label ("Height in inches" ), 0 , 1 );
40
- paneForBmiInfo .add (tfHeight , 1 , 1 );
33
+ paneForBmiInfo .add (heightTextInput , 1 , 1 );
41
34
paneForBmiInfo .add (btSubmit , 2 , 1 );
42
-
43
- // Text Area to display contents
44
35
TextArea ta = new TextArea ();
45
36
pane .setTop (paneForBmiInfo );
46
37
pane .setCenter (new ScrollPane (ta ));
47
38
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 ();
53
43
54
44
btSubmit .setOnAction (e -> {
55
45
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 );
60
49
// Send the BMI information to the server
61
- toServer .writeDouble (weight );
62
- toServer .writeDouble (height );
50
+ toServer .writeObject (dto );
63
51
toServer .flush ();
64
52
65
53
// Get string from the server
@@ -69,8 +57,7 @@ public void start(Stage primaryStage) {
69
57
ta .appendText ("Weight: " + weight + '\n' );
70
58
ta .appendText ("Height: " + height + '\n' );
71
59
ta .appendText (bmi + '\n' );
72
- }
73
- catch (IOException ex ) {
60
+ } catch (IOException ex ) {
74
61
System .err .println (ex );
75
62
}
76
63
});
@@ -83,9 +70,8 @@ public void start(Stage primaryStage) {
83
70
fromServer = new DataInputStream (socket .getInputStream ());
84
71
85
72
// 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 ) {
89
75
ta .appendText (ex .toString () + '\n' );
90
76
}
91
77
}
0 commit comments