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