|
| 1 | +package exercise_31_03Client; |
| 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_03Client 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 | +} |
0 commit comments