Q:

(Send loan information in an object) Revise Exercise 31.1 for the client to send a loan object that contains annual interest rate, number of years, and loan amount and for the server to send the monthly payment and total payment

0

(Send loan information in an object) Revise Exercise 31.1 for the client to send a loan object that contains annual interest rate, number of years, and loan amount and for the server to send the monthly payment and total payment.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

public class Exercise31_05Client extends Application {
	// IO streams
	ObjectOutputStream toServer = null;
	DataInputStream fromServer = null;

	// Text fields for loan info
	private TextField tfAnnualInterestRate = new TextField();
	private TextField tfNumberOfYears = new TextField();
	private TextField tfLoanAmount = new TextField();

	// Button for submitting loan objet to server
	private Button btSubmit = new Button("Submit");

	// Create text area
	private TextArea ta = new TextArea();

	@Override // Override the start method in the Application class
	public void start(Stage primaryStage) {
		// Set text fields alignment right
		tfAnnualInterestRate.setAlignment(Pos.BASELINE_RIGHT);
		tfNumberOfYears.setAlignment(Pos.BASELINE_RIGHT);
		tfLoanAmount.setAlignment(Pos.BASELINE_RIGHT);

		// Create a pane to hold loan infomation
		GridPane paneForLoanInfo = new GridPane();
		paneForLoanInfo.add(new Label("Annual Interest Rate"), 0, 0);
		paneForLoanInfo.add(tfAnnualInterestRate, 1, 0);
		paneForLoanInfo.add(new Label("Number Of Years"), 0, 1);
		paneForLoanInfo.add(tfNumberOfYears, 1, 1);
		paneForLoanInfo.add(btSubmit, 2, 1);
		paneForLoanInfo.add(new Label("Loan Amount"), 0, 2);
		paneForLoanInfo.add(tfLoanAmount, 1, 2);

		BorderPane pane = new BorderPane();
		pane.setTop(paneForLoanInfo);
		pane.setCenter(new ScrollPane(ta));

		// Create a scene and place it in the stage
		Scene scene = new Scene(pane, 355, 200);
		primaryStage.setTitle("Exercise31_05Client"); // Set the stage title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage

		btSubmit.setOnAction(e -> {
			try {
				// Get loan info from text fields and create a loan object
				Loan loan = new Loan(
					Double.parseDouble(tfAnnualInterestRate.getText().trim()),
					Integer.parseInt(tfNumberOfYears.getText().trim()),
					Double.parseDouble(tfLoanAmount.getText().trim()));

				// Send the loan object to the server
				toServer.writeObject(loan);
				toServer.flush();

				// Get monthly payment and total payment from the server
				double monthlyPayment = fromServer.readDouble();
				double totalPayment = fromServer.readDouble();

				// Display to text area
				ta.appendText("Annual Interest Rate: " + 
					loan.getAnnualInterestRate() + '\n');
				ta.appendText("Number Of Years: " + loan.getNumberOfYears() + '\n');
				ta.appendText("Loan Amount: " + loan.getLoanAmount() + '\n');
				ta.appendText("monthlyPayment: " + monthlyPayment + '\n');
				ta.appendText("totalPayment: " + totalPayment + '\n');
			}
			catch (IOException ex) {
				System.err.println(ex);
			}
		});

		try {
			// Create a socket to connect to the server
			Socket socket = new Socket("localhost", 8000);

			// Create an input stream to receive data from the server
			fromServer = new DataInputStream(socket.getInputStream());

			// Create an output stream to send objects to the server
			toServer = new ObjectOutputStream(socket.getOutputStream());
		}
		catch (IOException ex) {
			ta.appendText(ex.toString() + '\n');
		}
	}	
}

Exercise31_05Server.java

import java.io.*;
import java.net.*;
import java.util.Date;
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 Exercise31_05Server extends Application {
	@Override // Override the start method in the Application class 
	public void start(Stage primaryStage) {
		// Text area for displaying contents
		TextArea ta = new TextArea();

		// Create a scene and place it in the stage
		Scene scene = new Scene(new ScrollPane(ta), 400, 200);
		primaryStage.setTitle("Exercise31_05Server"); // Set the stage title
		primaryStage.setScene(scene); // Place the scene in the stage
		primaryStage.show(); // Display the stage

		new Thread(() -> {
			try {
				// Create a server socket
				ServerSocket serverSocket = new ServerSocket(8000);
				Platform.runLater(() -> 
					ta.appendText("Exercise31_05Server started at "
						+ new Date() + '\n'));

				// Listen for a connection request
				Socket socket = serverSocket.accept();

				// Create object input stream
				ObjectInputStream inputFromClient = new ObjectInputStream(
					socket.getInputStream());

				// Create data output stream
				DataOutputStream outputToClient = new DataOutputStream(
					socket.getOutputStream());
					
				while (true) {
					Date date = new Date();

					// Receive loan object from client 
					Loan loan = (Loan)inputFromClient.readObject();

					// Compute monthly payment and total payment
					double monthlyInterestRate = loan.getAnnualInterestRate() / 1200;
					double monthlyPayment = loan.getLoanAmount() * monthlyInterestRate 
						/ ( 1 - 1 / Math.pow(1 + monthlyInterestRate, 
						loan.getNumberOfYears() * 12));
					double totalPayment = monthlyPayment * 
						loan.getNumberOfYears() * 12;

					// send monthly payment and total payment back to the client
					outputToClient.writeDouble(monthlyPayment);
					outputToClient.writeDouble(totalPayment);

					Platform.runLater(() -> {
						ta.appendText("Connected to a client at " + date + '\n');
						ta.appendText("Annual interest Rate: " + 
							loan.getAnnualInterestRate() + '\n');
						ta.appendText("Number Of Years: " + 
							loan.getNumberOfYears() + '\n');
						ta.appendText("Loan Amount: " + loan.getLoanAmount() + '\n');
						ta.appendText("monthlyPayment: " + monthlyPayment + '\n');
						ta.appendText("totalPayment: " + totalPayment + '\n');
					});
				}
			}
			catch (IOException ex) {
				ex.printStackTrace();
			}
			catch (ClassNotFoundException ex) {
				ex.printStackTrace();
			}
		}).start(); 
	}
}

Loan.java

public class Loan implements java.io.Serializable {
	private double annualInterestRate;
	private int numberOfYears;
	private double loanAmount;


	public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
		this.annualInterestRate = annualInterestRate;
		this.numberOfYears = numberOfYears;
		this.loanAmount = loanAmount;
	}

	public double getAnnualInterestRate() {
		return annualInterestRate;
	}

	public int getNumberOfYears() {
		return numberOfYears;
	}

	public double getLoanAmount() {
		return loanAmount;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now