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

Commit0f71d81

Browse files
author
jsquared21
committed
Add Ex31_01
1 parent5940cf1 commit0f71d81

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed
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+
importjavafx.application.Application;
4+
importjavafx.geometry.Insets;
5+
importjavafx.geometry.Pos;
6+
importjavafx.scene.Scene;
7+
importjavafx.scene.control.Label;
8+
importjavafx.scene.control.ScrollPane;
9+
importjavafx.scene.control.TextArea;
10+
importjavafx.scene.control.TextField;
11+
importjavafx.scene.control.Button;
12+
importjavafx.scene.layout.GridPane;
13+
importjavafx.scene.layout.BorderPane;
14+
importjavafx.stage.Stage;
15+
16+
publicclassExercise31_01ClientextendsApplication {
17+
// IO streams
18+
DataOutputStreamtoServer =null;
19+
DataInputStreamfromServer =null;
20+
21+
// Create text fields for loan information
22+
privateTextFieldtfAnnualInterestRate =newTextField();
23+
privateTextFieldtfNumberOfYears =newTextField();
24+
privateTextFieldtfLoanAmount =newTextField();
25+
26+
// Create Button for submitting loan information to server
27+
privateButtonbtSubmit =newButton("Submit");
28+
29+
@Override// Override the start method in th Application class
30+
publicvoidstart(StageprimaryStage) {
31+
BorderPanepane =newBorderPane();
32+
33+
tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT);
34+
tfNumberOfYears.setAlignment(Pos.BASELINE_RIGHT);
35+
tfLoanAmount.setAlignment(Pos.BASELINE_RIGHT);
36+
37+
GridPanepaneForLoanInfo =newGridPane();
38+
paneForLoanInfo.add(newLabel("Annual Interest Rate"),0,0);
39+
paneForLoanInfo.add(tfAnnualInterestRate,1,0);
40+
paneForLoanInfo.add(newLabel("Number Of Years"),0,1);
41+
paneForLoanInfo.add(tfNumberOfYears,1,1);
42+
paneForLoanInfo.add(btSubmit,2,1);
43+
paneForLoanInfo.add(newLabel("Loan Amount"),0,2);
44+
paneForLoanInfo.add(tfLoanAmount,1,2);
45+
46+
// Text area to display contents
47+
TextAreata =newTextArea();
48+
pane.setTop(paneForLoanInfo);
49+
pane.setCenter(newScrollPane(ta));
50+
51+
// Create a scene and place it in the stage
52+
Scenescene =newScene(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+
doubleannualInterestRate =Double.parseDouble(
61+
tfAnnualInterestRate.getText().trim());
62+
63+
intnumberOfYears =Integer.parseInt(
64+
tfNumberOfYears.getText().trim());
65+
66+
doubleloanAmount =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+
DoublemonthlyPayment =fromServer.readDouble();
77+
DoubletotalPayment =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 (IOExceptionex) {
87+
System.err.println(ex);
88+
}
89+
});
90+
91+
try {
92+
// Create a socket to connect to the server
93+
Socketsocket =newSocket("localhost",8000);
94+
95+
// Create an input stream to receive data from the server
96+
fromServer =newDataInputStream(socket.getInputStream());
97+
98+
// Create an output stream to send data to the server
99+
toServer =newDataOutputStream(socket.getOutputStream());
100+
}
101+
catch (IOExceptionex) {
102+
ta.appendText(ex.toString() +'\n');
103+
}
104+
}
105+
}
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.ScrollPane;
8+
importjavafx.scene.control.TextArea;
9+
importjavafx.stage.Stage;
10+
11+
publicclassExercise31_01ServerextendsApplication {
12+
@Override// Override the start method in the Application class
13+
publicvoidstart(StageprimaryStage) {
14+
// Text area for displaying contents
15+
TextAreata =newTextArea();
16+
17+
// Create a scene and place it in the stage
18+
Scenescene =newScene(newScrollPane(ta),450,200);
19+
primaryStage.setTitle("Exercise31_01Server");// Set the stage title
20+
primaryStage.setScene(scene);// Place the scene in the stage
21+
primaryStage.show();// Display the stage
22+
23+
newThread(() -> {
24+
try {
25+
// Create a server socket
26+
ServerSocketserverSocket =newServerSocket(8000);
27+
Platform.runLater(() ->
28+
ta.appendText("Exercise31_01Server started at" +newDate() +'\n'));
29+
30+
// Listen for a connection request
31+
Socketsocket =serverSocket.accept();
32+
33+
// Create data input and output streams
34+
DataInputStreaminputFromClient =newDataInputStream(
35+
socket.getInputStream());
36+
DataOutputStreamoutputToClient =newDataOutputStream(
37+
socket.getOutputStream());
38+
39+
while (true) {
40+
DatedateClientConnected =newDate();
41+
// Receive loan information from the client
42+
doubleannualInterestRate =inputFromClient.readDouble();
43+
intnumberOfYears =inputFromClient.readInt();
44+
doubleloanAmount =inputFromClient.readDouble();
45+
46+
// Compute monthly payment and total payment
47+
doublemonthlyInterestRate =annualInterestRate /1200;
48+
doublemonthlyPayment =loanAmount *monthlyInterestRate / (1
49+
-1 /Math.pow(1 +monthlyInterestRate,numberOfYears *12));
50+
doubletotalPayment =monthlyPayment *numberOfYears *12;
51+
52+
// Send monthly payment and total payment back to the client
53+
outputToClient.writeDouble(monthlyPayment);
54+
outputToClient.writeDouble(totalPayment);
55+
56+
Platform.runLater(() -> {
57+
ta.appendText("Connected to a client at " +
58+
dateClientConnected +'\n');
59+
ta.appendText("Annual Interest Rate: " +
60+
annualInterestRate +'\n');
61+
ta.appendText("Number Of Years: " +numberOfYears +'\n');
62+
ta.appendText("Loan Amount: " +loanAmount +'\n');
63+
ta.appendText("monthlyPayment: " +monthlyPayment +'\n');
64+
ta.appendText("totalPayment: " +totalPayment +'\n');
65+
});
66+
}
67+
}
68+
catch(IOExceptionex) {
69+
ex.printStackTrace();
70+
}
71+
}).start();
72+
}
73+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp