Question: Using JavaFx Write a server for a client. The client sends loan information (annual interest rate, number of years, and loan amount) to the server

Using JavaFx Write a server for a client. The client sends loan information (annual interest rate, number of years, and loan amount) to the server (see Figure 31.17a). The server computes monthly payment and total payment, and sends them back to the client (see Figure 31.17b). Name the client Exercise31_01Client and the server Exercise31_01Server. This what I have thus far Loan this part appears correct public class Loan implements java.io.Serializable{ private double annualInterestRate; private int numberOfYears; private double loanAmount; private java.util.Date loanDate; public Loan() { this (2.5, 1, 1000); } public Loan(double intRate, int years, double amt) { annualInterestRate= intRate; numberOfYears = years; loanAmount = amt; loanDate = new java.util.Date(); } public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate/1200; double monthlyPayment = loanAmount * monthlyInterestRate/(1-(Math.pow(1/(1+monthlyInterestRate), numberOfYears*12))); return monthlyPayment; } }

I am having issues connecting to the Client:

import java.io.*; import java.net. *; import java.util. *; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; import javafx.stage.Stage;

public class LoanServer extends Application { private TextArea ta = new TextArea(); public void start (Stage primaryStage) { ta.setWrapText(true); //create scene and place on stage Scene scene = new Scene (new ScrollPane(ta), 200,200); primaryStage.setTitle("Loan Server"); primaryStage.setScene(scene); primaryStage.show(); new Thread (() -> connectToClient() ).start(); } public void connectToClient() { try { //create a server socket ServerSocket serverSocket = new ServerSocket(8000); Platform.runLater() -> ta.appendText("Loan Server start at" + new Date() + 'n')); Socket connectToClient = serverSocket.accept(); //client number Platform.runLater {() -> (ta.appendText("Connected to a client" + "at" + new Date() +' ');)); //continuously server the client while (true) { double annualInterestRate = isFromClient.readDouble(); int numOfYears = isFromClient.readInt(); double loanAmount = isFromClient.readDouble(); //compute monthly payment and total payment Loan mortgage = new Loan (annualInterestRate, numOfYears, loanAmount); double monthlyPayment = mortgage.getMonthlyPayment(); double totalPayment = mortgage.getTotalPayment(); //send results back to the client osToClient.writeDouble(monthlyPayment); osToClient.writeDouble(totalPayment); Platform.runLater ( ()-> { ta.appendText("Annual Interest Rate:" + annualInterestRate + " Number of Years:" + numOfYears + " Loan Amount:" + loanAmount + " "); ta.appendText("Monthly Payment: " + monthlyPayment + " TOTAL PAYMENT:" + totalPayment + " "); )); } //end of Platform }// end of while statement catch (IOException e){ System.err.println(e); } } } } }

I am also having problems connecting to the server:

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.*; import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage;

public class LoanServer_Client extends Application{ private TextField tfAnnualInterestRate; private TextField tfNumOfYears; private TextField tfLoanAmount; private Button btSubmit = new Button ("Submit"); private TextArea ta; //io stream DataOutputStream osToServer; DataInputStream isFromServer;

public LoanServer_Client() { this.ta = new TextArea; this.tfNumOfYears = new TextField; this.tfAnnualInterestRate = new TextField; } public void start(Stage primaryStage) { ta.setWrapText(true); GridPane gridPane = new GridPane(); gridPane.add(new Label("Annual Interest Rate"), 0,0); gridPane.add(new Label("Number of Years"), 0,1); gridPane.add(new Label("Loan Amount"), 0,2); gridPane.add(tfAnnualInterestRate, 1,0); gridPane.add(tfNumOfYears, 1,1); gridPane.add(tfLoanAmount, 1,2); gridPane.add(btSubmit, 1,1); tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT); tfNumOfYears.setAlignment(Pos.BASELINE_RIGHT); tfLoanAmount.setAlignment(Pos.BASELINE_RIGHT); BorderPane pane = new BorderPane(); pane.setCenter(new ScrollPane(ta)); pane.setTop(gridPane); Scene scene = new Scene(pane, 200,200); primaryStage.setTitle("Loan Server"); primaryStage.setScene(scene); primaryStage.show(); btSubmit.setOnAction(e -> connectToServer()); try { //create a Socket to connect to the server Socket connectToServer = new Socket ("localhost:, 8000"); //create input stream isFromServer = new DataInputStream(connectToServer.getInputStream()); //create input stream osToServer = new DataOutputStream(connectToServer.getOutputStream()); } catch (IOException e) { ta.appendText (e.toString() + " "); } }

public void connectToServer() {

try{ double annualInterestRate = Double.parseDouble(tfAnnualInterestRate.getText.trim()); int numOfYears = Integer.parseInt(tfNumOfYears.getText()); double loanAmount = Double.parseDouble(tfLoanAmount.getText().trim());

//send interest to server osToServer.writeDouble(annualInterestRate); osToServer.writeDouble(numOfYears); osToServer.writeDouble(loanAmount);

osToServer.flush();

//get data from server double monthlyPayment = isFromServer.readDouble(); double totalPayment = isFromServer.readDouble();

ta.appendText("Annual Interest Rate:" + annualInterestRate + " Number of Years: " + numOfYears + " Loan Amount:" + loanAmount + "n");

ta.appendText("MONTHLY PAYMENT: " + monthlyPayment + " TOTAL PAYMENT:" totalPayment + " "); } } }

catch (IOException e) {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!