Skip to content

Commit e3b0620

Browse files
author
jsquared21
committed
Add Ex31_03
1 parent ce30a1a commit e3b0620

File tree

6 files changed

+214
-1
lines changed

6 files changed

+214
-1
lines changed

Exercise_31/Exercise_31_01/Exercise31_01Server.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public void start(Stage primaryStage) {
2525
// Create a server socket
2626
ServerSocket serverSocket = new ServerSocket(8000);
2727
Platform.runLater(() ->
28-
ta.appendText("Exercise31_01Server started at" + new Date() + '\n'));
28+
ta.appendText("Exercise31_01Server started at "
29+
+ new Date() + '\n'));
2930

3031
// Listen for a connection request
3132
Socket socket = serverSocket.accept();
Binary file not shown.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Binary file not shown.
Binary file not shown.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import java.util.Date;
4+
import javafx.application.Application;
5+
import javafx.application.Platform;
6+
import javafx.scene.Scene;
7+
import javafx.scene.control.TextArea;
8+
import javafx.scene.control.ScrollPane;
9+
import javafx.stage.Stage;
10+
11+
public class Exercise31_03Server extends Application {
12+
// Text area for displaying contents
13+
private TextArea ta = new TextArea();
14+
15+
// Number a client
16+
private int clientNo = 0;
17+
18+
@Override // Override the start method in the Application class
19+
public void start(Stage primaryStage) {
20+
21+
// Create a scene and place it in the stage
22+
Scene scene = new Scene(new ScrollPane(ta), 400, 200);
23+
primaryStage.setTitle("Exercise31_03Server"); // Set the stage title
24+
primaryStage.setScene(scene); // Place the scene in the stage
25+
primaryStage.show(); // Display the stage
26+
27+
new Thread(() -> {
28+
try {
29+
// Create a server socket
30+
ServerSocket serverSocket = new ServerSocket(8000);
31+
Platform.runLater(() ->
32+
ta.appendText("Exercise31_03Server started at "
33+
+ new Date() + '\n'));
34+
35+
while (true) {
36+
// Listen for a new connection request
37+
Socket socket = serverSocket.accept();
38+
39+
Platform.runLater(() -> {
40+
// Display the client number
41+
ta.appendText("Starting thread for client " + ++clientNo +
42+
" at " + new Date() + '\n');
43+
});
44+
45+
// Create and start a new thread for the connection
46+
new Thread(new HandleAClient(socket)).start();
47+
}
48+
}
49+
catch(IOException ex) {
50+
System.err.println(ex);
51+
}
52+
}).start();
53+
}
54+
55+
// Define the thread class for handling new connection
56+
class HandleAClient implements Runnable {
57+
private Socket socket; // A connected socket
58+
59+
/** Construct a thread */
60+
public HandleAClient(Socket socket) {
61+
this.socket = socket;
62+
}
63+
64+
/** Run a thread */
65+
public void run() {
66+
try {
67+
// Create data input and output streams
68+
DataInputStream inputFromClient = new DataInputStream(
69+
socket.getInputStream());
70+
DataOutputStream outputToClient = new DataOutputStream(
71+
socket.getOutputStream());
72+
73+
// Continuously serve the client
74+
while (true) {
75+
// Receive loan information from the client
76+
double annualInterestRate = inputFromClient.readDouble();
77+
int numberOfYears = inputFromClient.readInt();
78+
double loanAmount = inputFromClient.readDouble();
79+
80+
// Compute monthly payments and total payment
81+
double monthlyInterestRate = annualInterestRate / 1200;
82+
double monthlyPayment = loanAmount * monthlyInterestRate / (
83+
1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
84+
double totalPayment = monthlyPayment * numberOfYears * 12;
85+
86+
// Send monthly payment and total payment back to the client
87+
outputToClient.writeDouble(monthlyPayment);
88+
outputToClient.writeDouble(totalPayment);
89+
90+
Platform.runLater(() -> {
91+
ta.appendText("Annual Interest Rate: " +
92+
annualInterestRate + '\n');
93+
ta.appendText("Number Of Years: " + numberOfYears + '\n');
94+
ta.appendText("Loan Amount: " + loanAmount + '\n');
95+
ta.appendText("monthlyPayment: " + monthlyPayment + '\n');
96+
ta.appendText("totalPayment: " + totalPayment + '\n');
97+
});
98+
}
99+
}
100+
catch(IOException ex) {
101+
ex.printStackTrace();
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)