-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoan server
More file actions
86 lines (66 loc) · 3.49 KB
/
Copy pathLoan server
File metadata and controls
86 lines (66 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<---Main Server Class--->
package taoshiflex.lab11;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Exercise31_01Server {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(8000)) {
System.out.println("Exercise31_01Server started at " + new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(new Date()));
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Connected to a client at " + new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(new Date()));
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
double annualInterestRate = inputFromClient.readDouble();
int numberOfYears = inputFromClient.readInt();
double loanAmount = inputFromClient.readDouble();
// Calculate monthly and total payment
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
double totalPayment = monthlyPayment * numberOfYears * 12;
outputToClient.writeDouble(monthlyPayment);
outputToClient.writeDouble(totalPayment);
System.out.printf("Annual Interest Rate: %.2f\n", annualInterestRate);
System.out.printf("Number of Years: %d\n", numberOfYears);
System.out.printf("Loan Amount: %.2f\n", loanAmount);
System.out.printf("Monthly Payment: %.2f\n", monthlyPayment);
System.out.printf("Total Payment: %.2f\n", totalPayment);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
<---Client Class--->
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
class Exercise31_01Client {
public static void main(String[] args) {
try (Socket socket = new Socket("localhost", 8000)) {
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
Scanner scanner = new Scanner(System.in);
System.out.print("Enter annual interest rate: ");
double annualInterestRate = scanner.nextDouble();
System.out.print("Enter number of years: ");
int numberOfYears = scanner.nextInt();
System.out.print("Enter loan amount: ");
double loanAmount = scanner.nextDouble();
outputToServer.writeDouble(annualInterestRate);
outputToServer.writeInt(numberOfYears);
outputToServer.writeDouble(loanAmount);
double monthlyPayment = inputFromServer.readDouble();
double totalPayment = inputFromServer.readDouble();
System.out.printf("Monthly Payment: %.2f\n", monthlyPayment);
System.out.printf("Total Payment: %.2f\n", totalPayment);
scanner.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}