Skip to content

Commit c1d3896

Browse files
committed
commit Exercise_31_01 and Exercise_31_02
1 parent 4ac1f2d commit c1d3896

File tree

4 files changed

+337
-0
lines changed

4 files changed

+337
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package exercise_31_01Client;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
import javafx.application.Application;
8+
import javafx.geometry.Pos;
9+
import javafx.scene.Scene;
10+
import javafx.scene.control.Button;
11+
import javafx.scene.control.Label;
12+
import javafx.scene.control.ScrollPane;
13+
import javafx.scene.control.TextArea;
14+
import javafx.scene.control.TextField;
15+
import javafx.scene.layout.BorderPane;
16+
import javafx.scene.layout.GridPane;
17+
import javafx.stage.Stage;
18+
19+
public class Exercise_31_01Client extends Application {
20+
private DataInputStream inputFromServer = null;
21+
private DataOutputStream outputToServer = null;
22+
23+
@Override
24+
public void start(Stage primaryStage) {
25+
BorderPane borderPane = new BorderPane();
26+
27+
TextArea taStatus = new TextArea();
28+
borderPane.setCenter(new ScrollPane(taStatus));
29+
30+
GridPane grid = new GridPane();
31+
32+
TextField tfRate = new TextField();
33+
tfRate.setAlignment(Pos.BASELINE_RIGHT);
34+
tfRate.setPrefColumnCount(10);
35+
36+
TextField tfYears = new TextField();
37+
tfYears.setAlignment(Pos.BASELINE_RIGHT);
38+
tfYears.setPrefColumnCount(10);
39+
40+
TextField tfAmount = new TextField();
41+
tfAmount.setAlignment(Pos.BASELINE_RIGHT);
42+
tfAmount.setPrefColumnCount(10);
43+
44+
Button btSubmit = new Button("Submit");
45+
46+
grid.add(new Label("Annual Interest Rate"), 0, 0);
47+
grid.add(tfRate, 1, 0);
48+
grid.add(new Label("Number Of Years"), 0, 1);
49+
grid.add(tfYears, 1, 1);
50+
grid.add(btSubmit, 2, 1);
51+
grid.add(new Label("Loan Amount"), 0, 2);
52+
grid.add(tfAmount, 1, 2);
53+
borderPane.setTop(grid);
54+
55+
Scene scene = new Scene(borderPane, 300, 200);
56+
primaryStage.setTitle("Exercise_31_01Client");
57+
primaryStage.setScene(scene);
58+
primaryStage.show();
59+
60+
try {
61+
Socket socket = new Socket("localhost", 8000);
62+
inputFromServer = new DataInputStream(socket.getInputStream());
63+
outputToServer = new DataOutputStream(socket.getOutputStream());
64+
}
65+
catch (IOException ex) {
66+
taStatus.appendText(ex.toString() + "\n");
67+
}
68+
69+
btSubmit.setOnAction(e -> {
70+
try {
71+
double interestRate = Double.parseDouble(tfRate.getText().trim());
72+
int numberOfYears = Integer.parseInt(tfYears.getText().trim());
73+
double amount = Double.parseDouble(tfAmount.getText().trim());
74+
75+
outputToServer.writeDouble(interestRate);
76+
outputToServer.writeInt(numberOfYears);
77+
outputToServer.writeDouble(amount);
78+
79+
taStatus.appendText("Annual interest rate: " + interestRate + "\n" +
80+
"Number of Years: " + numberOfYears + "\n" + "Loan amount: " + amount + "\n");
81+
82+
double monthlyPayment = inputFromServer.readDouble();
83+
double totalPayment = inputFromServer.readDouble();
84+
taStatus.appendText("monthlyPayment: " + monthlyPayment + "\n" +
85+
"totalPayment: " + totalPayment + "\n");
86+
}
87+
catch (IOException ex) {
88+
System.err.println(ex);
89+
}
90+
});
91+
}
92+
93+
public static void main(String[] args) {
94+
launch(args);
95+
}
96+
97+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package exercise_31_01Server;
2+
3+
import exercise_17_06.Loan;
4+
import java.io.DataInputStream;
5+
import java.io.DataOutputStream;
6+
import java.io.IOException;
7+
import java.net.ServerSocket;
8+
import java.net.Socket;
9+
import java.util.Date;
10+
import javafx.application.Application;
11+
import javafx.application.Platform;
12+
import javafx.scene.Scene;
13+
import javafx.scene.control.ScrollPane;
14+
import javafx.scene.control.TextArea;
15+
import javafx.stage.Stage;
16+
17+
public class Exercise_31_01Server extends Application {
18+
19+
@Override
20+
public void start(Stage primaryStage) {
21+
TextArea taStatus = new TextArea();
22+
23+
Scene scene = new Scene(new ScrollPane(taStatus), 400, 150);
24+
primaryStage.setTitle("Exercise_31_01Server");
25+
primaryStage.setScene(scene);
26+
primaryStage.show();
27+
28+
new Thread(() -> {
29+
try {
30+
ServerSocket serverSocket = new ServerSocket(8000);
31+
Platform.runLater(() -> taStatus.appendText("Exercise_31_01Server started at " + new Date() + "\n"));
32+
33+
Socket socket = serverSocket.accept();
34+
Platform.runLater(() -> taStatus.appendText("Connected to a client at " + new Date() + "\n"));
35+
36+
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
37+
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
38+
39+
while(true) {
40+
double interestRate = inputFromClient.readDouble();
41+
int numberOfYears = inputFromClient.readInt();
42+
double amount = inputFromClient.readDouble();
43+
Platform.runLater(() -> taStatus.appendText("Annual interest rate: " + interestRate + "\n" +
44+
"Number of Years: " + numberOfYears + "\n" + "Loan amount: " + amount + "\n"));
45+
46+
Loan loan = new Loan(interestRate, numberOfYears, amount);
47+
48+
double monthlyPayment = loan.getMonthlyPayment();
49+
double totalPayment = loan.getTotalPayment();
50+
51+
outputToClient.writeDouble(monthlyPayment);
52+
outputToClient.writeDouble(totalPayment);
53+
Platform.runLater(() -> taStatus.appendText("monthlyPayment: " + monthlyPayment + "\n" +
54+
"totalPayment: " + totalPayment));
55+
}
56+
}
57+
catch (IOException ex) {
58+
ex.printStackTrace();
59+
}
60+
}).start();
61+
}
62+
63+
public static void main(String[] args) {
64+
launch(args);
65+
}
66+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package exercise_31_02Client;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
import javafx.application.Application;
8+
import javafx.geometry.Pos;
9+
import javafx.scene.Scene;
10+
import javafx.scene.control.Button;
11+
import javafx.scene.control.Label;
12+
import javafx.scene.control.ScrollPane;
13+
import javafx.scene.control.TextArea;
14+
import javafx.scene.control.TextField;
15+
import javafx.scene.layout.BorderPane;
16+
import javafx.scene.layout.GridPane;
17+
import javafx.stage.Stage;
18+
19+
public class Exercise_31_02Client extends Application {
20+
private DataInputStream inputFromServer = null;
21+
private DataOutputStream outputToServer = null;
22+
23+
@Override
24+
public void start(Stage primaryStage) {
25+
BorderPane borderPane = new BorderPane();
26+
27+
TextArea taStatus = new TextArea();
28+
borderPane.setCenter(new ScrollPane(taStatus));
29+
30+
GridPane grid = new GridPane();
31+
32+
TextField tfWeight = new TextField();
33+
tfWeight.setAlignment(Pos.BASELINE_RIGHT);
34+
tfWeight.setPrefColumnCount(10);
35+
36+
TextField tfHeight = new TextField();
37+
tfHeight.setAlignment(Pos.BASELINE_RIGHT);
38+
tfHeight.setPrefColumnCount(10);
39+
40+
Button btSubmit = new Button("Submit");
41+
42+
grid.add(new Label("Weight in kgs"), 0, 0);
43+
grid.add(tfWeight, 1, 0);
44+
grid.add(new Label("Height in meters"), 0, 1);
45+
grid.add(tfHeight, 1, 1);
46+
grid.add(btSubmit, 2, 1);
47+
borderPane.setTop(grid);
48+
49+
Scene scene = new Scene(borderPane, 300, 200);
50+
primaryStage.setTitle("Exercise_31_02Client");
51+
primaryStage.setScene(scene);
52+
primaryStage.show();
53+
54+
try {
55+
Socket socket = new Socket("localhost", 8000);
56+
inputFromServer = new DataInputStream(socket.getInputStream());
57+
outputToServer = new DataOutputStream(socket.getOutputStream());
58+
}
59+
catch (IOException ex) {
60+
taStatus.appendText(ex.toString() + "\n");
61+
}
62+
63+
btSubmit.setOnAction(e -> {
64+
try {
65+
double weight = Double.parseDouble(tfWeight.getText().trim());
66+
double height = Double.parseDouble(tfHeight.getText().trim());
67+
68+
outputToServer.writeDouble(weight);
69+
outputToServer.writeDouble(height);
70+
outputToServer.flush();
71+
72+
taStatus.appendText("Weight: " + weight + "\n" + "Height: " + height + "\n");
73+
74+
String status = inputFromServer.readUTF();
75+
taStatus.appendText(status + "\n");
76+
}
77+
catch (IOException ex) {
78+
System.err.println(ex);
79+
}
80+
});
81+
}
82+
83+
public static void main(String[] args) {
84+
launch(args);
85+
}
86+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package exercise_31_02Server;
2+
3+
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.net.ServerSocket;
7+
import java.net.Socket;
8+
import java.util.Date;
9+
import javafx.application.Application;
10+
import javafx.application.Platform;
11+
import javafx.scene.Scene;
12+
import javafx.scene.control.ScrollPane;
13+
import javafx.scene.control.TextArea;
14+
import javafx.stage.Stage;
15+
16+
public class Exercise_31_02Server extends Application {
17+
18+
@Override
19+
public void start(Stage primaryStage) {
20+
TextArea taStatus = new TextArea();
21+
22+
Scene scene = new Scene(new ScrollPane(taStatus), 400, 150);
23+
primaryStage.setTitle("Exercise_31_02Server");
24+
primaryStage.setScene(scene);
25+
primaryStage.show();
26+
27+
new Thread(() -> {
28+
try {
29+
ServerSocket serverSocket = new ServerSocket(8000);
30+
Platform.runLater(() -> taStatus.appendText("Exercise_31_02Server started at " + new Date() + "\n"));
31+
32+
Socket socket = serverSocket.accept();
33+
Platform.runLater(() -> taStatus.appendText("Connected to a client at " + new Date() + "\n"));
34+
35+
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
36+
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
37+
38+
while(true) {
39+
double weight = inputFromClient.readDouble();
40+
double height = inputFromClient.readDouble();
41+
Platform.runLater(() -> taStatus.appendText("Weight: " + weight + "\n" +
42+
"Height: " + height + "\n"));
43+
44+
BodyMassIndex BMI = new BodyMassIndex(weight, height);
45+
String status = "BMI is " + String.format("%.2f", BMI.bmi) + ". " + BMI.getStatus();
46+
47+
outputToClient.writeUTF(status);
48+
Platform.runLater(() -> taStatus.appendText(status + "\n"));
49+
}
50+
}
51+
catch (IOException ex) {
52+
ex.printStackTrace();
53+
}
54+
}).start();
55+
}
56+
57+
private class BodyMassIndex {
58+
double weight;
59+
double height;
60+
double bmi;
61+
62+
public BodyMassIndex(double weight, double height) {
63+
this.weight = weight;
64+
this.height = height;
65+
bmi = weight / (Math.pow(height, 2));
66+
}
67+
68+
public String getStatus() {
69+
if(bmi < 18.5) {
70+
return "Underweight";
71+
}
72+
else if(bmi < 25) {
73+
return "Normal";
74+
}
75+
else if(bmi < 30) {
76+
return "Overweight";
77+
}
78+
else {
79+
return "Obese";
80+
}
81+
}
82+
}
83+
84+
public static void main(String[] args) {
85+
launch(args);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)