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 .control .TextArea ;
7+ import javafx .scene .control .Button ;
8+ import javafx .scene .control .ScrollPane ;
9+ import javafx .scene .control .Label ;
10+ import javafx .scene .control .TextField ;
11+ import javafx .scene .layout .GridPane ;
12+ import javafx .scene .layout .BorderPane ;
13+ import javafx .geometry .Pos ;
14+ import javafx .stage .Stage ;
15+
16+ public class Exercise31_05Client extends Application {
17+ // IO streams
18+ ObjectOutputStream toServer = null ;
19+ DataInputStream fromServer = null ;
20+
21+ // Text fields for loan info
22+ private TextField tfAnnualInterestRate = new TextField ();
23+ private TextField tfNumberOfYears = new TextField ();
24+ private TextField tfLoanAmount = new TextField ();
25+
26+ // Button for submitting loan objet to server
27+ private Button btSubmit = new Button ("Submit" );
28+
29+ // Create text area
30+ private TextArea ta = new TextArea ();
31+
32+ @ Override // Override the start method in the Application class
33+ public void start (Stage primaryStage ) {
34+ // Set text fields alignment right
35+ tfAnnualInterestRate .setAlignment (Pos .BASELINE_RIGHT );
36+ tfNumberOfYears .setAlignment (Pos .BASELINE_RIGHT );
37+ tfLoanAmount .setAlignment (Pos .BASELINE_RIGHT );
38+
39+ // Create a pane to hold loan infomation
40+ GridPane paneForLoanInfo = new GridPane ();
41+ paneForLoanInfo .add (new Label ("Annual Interest Rate" ), 0 , 0 );
42+ paneForLoanInfo .add (tfAnnualInterestRate , 1 , 0 );
43+ paneForLoanInfo .add (new Label ("Number Of Years" ), 0 , 1 );
44+ paneForLoanInfo .add (tfNumberOfYears , 1 , 1 );
45+ paneForLoanInfo .add (btSubmit , 2 , 1 );
46+ paneForLoanInfo .add (new Label ("Loan Amount" ), 0 , 2 );
47+ paneForLoanInfo .add (tfLoanAmount , 1 , 2 );
48+
49+ BorderPane pane = new BorderPane ();
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_05Client" ); // 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 loan info from text fields and create a loan object
62+ Loan loan = new Loan (
63+ Double .parseDouble (tfAnnualInterestRate .getText ().trim ()),
64+ Integer .parseInt (tfNumberOfYears .getText ().trim ()),
65+ Double .parseDouble (tfLoanAmount .getText ().trim ()));
66+
67+ // Send the loan object to the server
68+ toServer .writeObject (loan );
69+ toServer .flush ();
70+
71+ // Get monthly payment and total payment from the server
72+ double monthlyPayment = fromServer .readDouble ();
73+ double totalPayment = fromServer .readDouble ();
74+
75+ // Display to text area
76+ ta .appendText ("Annual Interest Rate: " +
77+ loan .getAnnualInterestRate () + '\n' );
78+ ta .appendText ("Number Of Years: " + loan .getNumberOfYears () + '\n' );
79+ ta .appendText ("Loan Amount: " + loan .getLoanAmount () + '\n' );
80+ ta .appendText ("monthlyPayment: " + monthlyPayment + '\n' );
81+ ta .appendText ("totalPayment: " + totalPayment + '\n' );
82+ }
83+ catch (IOException ex ) {
84+ System .err .println (ex );
85+ }
86+ });
87+
88+ try {
89+ // Create a socket to connect to the server
90+ Socket socket = new Socket ("localhost" , 8000 );
91+
92+ // Create an input stream to receive data from the server
93+ fromServer = new DataInputStream (socket .getInputStream ());
94+
95+ // Create an output stream to send objects to the server
96+ toServer = new ObjectOutputStream (socket .getOutputStream ());
97+ }
98+ catch (IOException ex ) {
99+ ta .appendText (ex .toString () + '\n' );
100+ }
101+ }
102+ }
0 commit comments