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

Commitebfad91

Browse files
author
jsquared21
committed
Add Ex31_05
1 parent780e8d0 commitebfad91

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed
Binary file not shown.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
importjava.io.*;
2+
importjava.net.*;
3+
importjava.util.Date;
4+
importjavafx.application.Application;
5+
importjavafx.scene.Scene;
6+
importjavafx.scene.control.TextArea;
7+
importjavafx.scene.control.Button;
8+
importjavafx.scene.control.ScrollPane;
9+
importjavafx.scene.control.Label;
10+
importjavafx.scene.control.TextField;
11+
importjavafx.scene.layout.GridPane;
12+
importjavafx.scene.layout.BorderPane;
13+
importjavafx.geometry.Pos;
14+
importjavafx.stage.Stage;
15+
16+
publicclassExercise31_05ClientextendsApplication {
17+
// IO streams
18+
ObjectOutputStreamtoServer =null;
19+
DataInputStreamfromServer =null;
20+
21+
// Text fields for loan info
22+
privateTextFieldtfAnnualInterestRate =newTextField();
23+
privateTextFieldtfNumberOfYears =newTextField();
24+
privateTextFieldtfLoanAmount =newTextField();
25+
26+
// Button for submitting loan objet to server
27+
privateButtonbtSubmit =newButton("Submit");
28+
29+
// Create text area
30+
privateTextAreata =newTextArea();
31+
32+
@Override// Override the start method in the Application class
33+
publicvoidstart(StageprimaryStage) {
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+
GridPanepaneForLoanInfo =newGridPane();
41+
paneForLoanInfo.add(newLabel("Annual Interest Rate"),0,0);
42+
paneForLoanInfo.add(tfAnnualInterestRate,1,0);
43+
paneForLoanInfo.add(newLabel("Number Of Years"),0,1);
44+
paneForLoanInfo.add(tfNumberOfYears,1,1);
45+
paneForLoanInfo.add(btSubmit,2,1);
46+
paneForLoanInfo.add(newLabel("Loan Amount"),0,2);
47+
paneForLoanInfo.add(tfLoanAmount,1,2);
48+
49+
BorderPanepane =newBorderPane();
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_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+
Loanloan =newLoan(
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+
doublemonthlyPayment =fromServer.readDouble();
73+
doubletotalPayment =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 (IOExceptionex) {
84+
System.err.println(ex);
85+
}
86+
});
87+
88+
try {
89+
// Create a socket to connect to the server
90+
Socketsocket =newSocket("localhost",8000);
91+
92+
// Create an input stream to receive data from the server
93+
fromServer =newDataInputStream(socket.getInputStream());
94+
95+
// Create an output stream to send objects to the server
96+
toServer =newObjectOutputStream(socket.getOutputStream());
97+
}
98+
catch (IOExceptionex) {
99+
ta.appendText(ex.toString() +'\n');
100+
}
101+
}
102+
}
Binary file not shown.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_05ServerextendsApplication {
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),400,200);
19+
primaryStage.setTitle("Exercise31_05Server");// 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_05Server started at "
29+
+newDate() +'\n'));
30+
31+
// Listen for a connection request
32+
Socketsocket =serverSocket.accept();
33+
34+
// Create object input stream
35+
ObjectInputStreaminputFromClient =newObjectInputStream(
36+
socket.getInputStream());
37+
38+
// Create data output stream
39+
DataOutputStreamoutputToClient =newDataOutputStream(
40+
socket.getOutputStream());
41+
42+
while (true) {
43+
Datedate =newDate();
44+
45+
// Receive loan object from client
46+
Loanloan = (Loan)inputFromClient.readObject();
47+
48+
// Compute monthly payment and total payment
49+
doublemonthlyInterestRate =loan.getAnnualInterestRate() /1200;
50+
doublemonthlyPayment =loan.getLoanAmount() *monthlyInterestRate
51+
/ (1 -1 /Math.pow(1 +monthlyInterestRate,
52+
loan.getNumberOfYears() *12));
53+
doubletotalPayment =monthlyPayment *
54+
loan.getNumberOfYears() *12;
55+
56+
// send monthly payment and total payment back to the client
57+
outputToClient.writeDouble(monthlyPayment);
58+
outputToClient.writeDouble(totalPayment);
59+
60+
Platform.runLater(() -> {
61+
ta.appendText("Connected to a client at " +date +'\n');
62+
ta.appendText("Annual interest Rate: " +
63+
loan.getAnnualInterestRate() +'\n');
64+
ta.appendText("Number Of Years: " +
65+
loan.getNumberOfYears() +'\n');
66+
ta.appendText("Loan Amount: " +loan.getLoanAmount() +'\n');
67+
ta.appendText("monthlyPayment: " +monthlyPayment +'\n');
68+
ta.appendText("totalPayment: " +totalPayment +'\n');
69+
});
70+
}
71+
}
72+
catch (IOExceptionex) {
73+
ex.printStackTrace();
74+
}
75+
catch (ClassNotFoundExceptionex) {
76+
ex.printStackTrace();
77+
}
78+
}).start();
79+
}
80+
}

‎Exercise_31/Exercise_31_05/Loan.class

563 Bytes
Binary file not shown.

‎Exercise_31/Exercise_31_05/Loan.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
publicclassLoanimplementsjava.io.Serializable {
2+
privatedoubleannualInterestRate;
3+
privateintnumberOfYears;
4+
privatedoubleloanAmount;
5+
6+
7+
publicLoan(doubleannualInterestRate,intnumberOfYears,doubleloanAmount) {
8+
this.annualInterestRate =annualInterestRate;
9+
this.numberOfYears =numberOfYears;
10+
this.loanAmount =loanAmount;
11+
}
12+
13+
publicdoublegetAnnualInterestRate() {
14+
returnannualInterestRate;
15+
}
16+
17+
publicintgetNumberOfYears() {
18+
returnnumberOfYears;
19+
}
20+
21+
publicdoublegetLoanAmount() {
22+
returnloanAmount;
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp