1+ import java .io .*;
2+ import java .net .*;
3+ import javafx .application .Application ;
4+ import javafx .geometry .Insets ;
5+ import javafx .geometry .Pos ;
6+ import javafx .scene .Scene ;
7+ import javafx .scene .control .Label ;
8+ import javafx .scene .control .ScrollPane ;
9+ import javafx .scene .control .TextArea ;
10+ import javafx .scene .control .TextField ;
11+ import javafx .scene .control .Button ;
12+ import javafx .scene .layout .GridPane ;
13+ import javafx .scene .layout .BorderPane ;
14+ import javafx .stage .Stage ;
15+
16+ public class Exercise31_01Client extends Application {
17+ // IO streams
18+ DataOutputStream toServer =null ;
19+ DataInputStream fromServer =null ;
20+
21+ // Create text fields for loan information
22+ private TextField tfAnnualInterestRate =new TextField ();
23+ private TextField tfNumberOfYears =new TextField ();
24+ private TextField tfLoanAmount =new TextField ();
25+
26+ // Create Button for submitting loan information to server
27+ private Button btSubmit =new Button ("Submit" );
28+
29+ @ Override // Override the start method in th Application class
30+ public void start (Stage primaryStage ) {
31+ BorderPane pane =new BorderPane ();
32+
33+ tfAnnualInterestRate .setAlignment (Pos .BASELINE_RIGHT );
34+ tfNumberOfYears .setAlignment (Pos .BASELINE_RIGHT );
35+ tfLoanAmount .setAlignment (Pos .BASELINE_RIGHT );
36+
37+ GridPane paneForLoanInfo =new GridPane ();
38+ paneForLoanInfo .add (new Label ("Annual Interest Rate" ),0 ,0 );
39+ paneForLoanInfo .add (tfAnnualInterestRate ,1 ,0 );
40+ paneForLoanInfo .add (new Label ("Number Of Years" ),0 ,1 );
41+ paneForLoanInfo .add (tfNumberOfYears ,1 ,1 );
42+ paneForLoanInfo .add (btSubmit ,2 ,1 );
43+ paneForLoanInfo .add (new Label ("Loan Amount" ),0 ,2 );
44+ paneForLoanInfo .add (tfLoanAmount ,1 ,2 );
45+
46+ // Text area to display contents
47+ TextArea ta =new TextArea ();
48+ pane .setTop (paneForLoanInfo );
49+ pane .setCenter (new ScrollPane (ta ));
50+
51+ // Create a scene and place it in the stage
52+ Scene scene =new Scene (pane ,355 ,200 );
53+ primaryStage .setTitle ("Exercise31_01Client" );// Set the stage title
54+ primaryStage .setScene (scene );// Place the scene in the stage
55+ primaryStage .show ();// Display the stage
56+
57+ btSubmit .setOnAction (e -> {
58+ try {
59+ // Get the Annual Interest Rate from the text field
60+ double annualInterestRate =Double .parseDouble (
61+ tfAnnualInterestRate .getText ().trim ());
62+
63+ int numberOfYears =Integer .parseInt (
64+ tfNumberOfYears .getText ().trim ());
65+
66+ double loanAmount =Double .parseDouble (
67+ tfLoanAmount .getText ().trim ());
68+
69+ // Send the loan information to the server
70+ toServer .writeDouble (annualInterestRate );
71+ toServer .writeInt (numberOfYears );
72+ toServer .writeDouble (loanAmount );
73+ toServer .flush ();
74+
75+ // Get monthly payment and total payment from the server
76+ Double monthlyPayment =fromServer .readDouble ();
77+ Double totalPayment =fromServer .readDouble ();
78+
79+ // Display to teat area
80+ ta .appendText ("Annual Interest Rate: " +annualInterestRate +'\n' );
81+ ta .appendText ("Number Of Years: " +numberOfYears +'\n' );
82+ ta .appendText ("Loan Amount: " +loanAmount +'\n' );
83+ ta .appendText ("monthlyPayment: " +monthlyPayment +'\n' );
84+ ta .appendText ("totalPayment: " +totalPayment +'\n' );
85+ }
86+ catch (IOException ex ) {
87+ System .err .println (ex );
88+ }
89+ });
90+
91+ try {
92+ // Create a socket to connect to the server
93+ Socket socket =new Socket ("localhost" ,8000 );
94+
95+ // Create an input stream to receive data from the server
96+ fromServer =new DataInputStream (socket .getInputStream ());
97+
98+ // Create an output stream to send data to the server
99+ toServer =new DataOutputStream (socket .getOutputStream ());
100+ }
101+ catch (IOException ex ) {
102+ ta .appendText (ex .toString () +'\n' );
103+ }
104+ }
105+ }