Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3b0620

Browse files
author
jsquared21
committed
Add Ex31_03
1 parentce30a1a commite3b0620

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
ServerSocketserverSocket =newServerSocket(8000);
2727
Platform.runLater(() ->
28-
ta.appendText("Exercise31_01Server started at" +newDate() +'\n'));
28+
ta.appendText("Exercise31_01Server started at "
29+
+newDate() +'\n'));
2930

3031
// Listen for a connection request
3132
Socketsocket =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+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.scene.Scene;
6+
importjavafx.scene.layout.GridPane;
7+
importjavafx.scene.layout.BorderPane;
8+
importjavafx.scene.control.Button;
9+
importjavafx.scene.control.ScrollPane;
10+
importjavafx.scene.control.TextArea;
11+
importjavafx.scene.control.TextField;
12+
importjavafx.scene.control.Label;
13+
importjavafx.geometry.Pos;
14+
importjavafx.stage.Stage;
15+
16+
publicclassExercise31_03ClientextendsApplication {
17+
// Create streams
18+
DataInputStreamfromServer =null;
19+
DataOutputStreamtoServer =null;
20+
21+
// Create text fields for loan information
22+
privateTextFieldtfAnnuealInterestRate =newTextField();
23+
privateTextFieldtfNumberOfYears =newTextField();
24+
privateTextFieldtfLoanAmount =newTextField();
25+
26+
// Create button for submitting loan info to the server
27+
privateButtonbtSubmit =newButton("Submit");
28+
29+
@Override// Override the start method in the Application class
30+
publicvoidstart(StageprimaryStage) {
31+
BorderPanepane =newBorderPane();
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+
GridPanepaneForLoanInfo =newGridPane();
40+
paneForLoanInfo.add(newLabel("Annual Interest Rate"),0,0);
41+
paneForLoanInfo.add(tfAnnuealInterestRate,1,0);
42+
paneForLoanInfo.add(newLabel("Number Of Years"),0,1);
43+
paneForLoanInfo.add(tfNumberOfYears,1,1);
44+
paneForLoanInfo.add(btSubmit,2,1);
45+
paneForLoanInfo.add(newLabel("Loan Amount"),0,2);
46+
paneForLoanInfo.add(tfLoanAmount,1,2);
47+
48+
// Text area to display contents
49+
TextAreata =newTextArea();
50+
pane.setTop(paneForLoanInfo);
51+
pane.setCenter(newScrollPane(ta));
52+
53+
// Create a scene and place it in the stage
54+
Scenescene =newScene(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+
doubleannualInterestRate =Double.parseDouble(
63+
tfAnnuealInterestRate.getText().trim());
64+
65+
intnumberOfYears =Integer.parseInt(
66+
tfNumberOfYears.getText().trim());
67+
68+
doubleloanAmount =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+
doublemonthlyPayment =fromServer.readDouble();
79+
doubletotalPayment =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 (IOExceptionex) {
89+
System.err.println(ex);
90+
}
91+
});
92+
93+
try {
94+
// Create a socket to connect to the server
95+
Socketsocket =newSocket("localhost",8000);
96+
97+
// Create an input stream to receive data from the server
98+
fromServer =newDataInputStream(socket.getInputStream());
99+
100+
// Create an output stream to send data to the server
101+
toServer =newDataOutputStream(socket.getOutputStream());
102+
}
103+
catch (IOExceptionex) {
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+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.application.Platform;
6+
importjavafx.scene.Scene;
7+
importjavafx.scene.control.TextArea;
8+
importjavafx.scene.control.ScrollPane;
9+
importjavafx.stage.Stage;
10+
11+
publicclassExercise31_03ServerextendsApplication {
12+
// Text area for displaying contents
13+
privateTextAreata =newTextArea();
14+
15+
// Number a client
16+
privateintclientNo =0;
17+
18+
@Override// Override the start method in the Application class
19+
publicvoidstart(StageprimaryStage) {
20+
21+
// Create a scene and place it in the stage
22+
Scenescene =newScene(newScrollPane(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+
newThread(() -> {
28+
try {
29+
// Create a server socket
30+
ServerSocketserverSocket =newServerSocket(8000);
31+
Platform.runLater(() ->
32+
ta.appendText("Exercise31_03Server started at "
33+
+newDate() +'\n'));
34+
35+
while (true) {
36+
// Listen for a new connection request
37+
Socketsocket =serverSocket.accept();
38+
39+
Platform.runLater(() -> {
40+
// Display the client number
41+
ta.appendText("Starting thread for client " + ++clientNo +
42+
" at " +newDate() +'\n');
43+
});
44+
45+
// Create and start a new thread for the connection
46+
newThread(newHandleAClient(socket)).start();
47+
}
48+
}
49+
catch(IOExceptionex) {
50+
System.err.println(ex);
51+
}
52+
}).start();
53+
}
54+
55+
// Define the thread class for handling new connection
56+
classHandleAClientimplementsRunnable {
57+
privateSocketsocket;// A connected socket
58+
59+
/** Construct a thread */
60+
publicHandleAClient(Socketsocket) {
61+
this.socket =socket;
62+
}
63+
64+
/** Run a thread */
65+
publicvoidrun() {
66+
try {
67+
// Create data input and output streams
68+
DataInputStreaminputFromClient =newDataInputStream(
69+
socket.getInputStream());
70+
DataOutputStreamoutputToClient =newDataOutputStream(
71+
socket.getOutputStream());
72+
73+
// Continuously serve the client
74+
while (true) {
75+
// Receive loan information from the client
76+
doubleannualInterestRate =inputFromClient.readDouble();
77+
intnumberOfYears =inputFromClient.readInt();
78+
doubleloanAmount =inputFromClient.readDouble();
79+
80+
// Compute monthly payments and total payment
81+
doublemonthlyInterestRate =annualInterestRate /1200;
82+
doublemonthlyPayment =loanAmount *monthlyInterestRate / (
83+
1 -1 /Math.pow(1 +monthlyInterestRate,numberOfYears *12));
84+
doubletotalPayment =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(IOExceptionex) {
101+
ex.printStackTrace();
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp