31.5 (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.
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.
import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; class Loan implements Serializable{ private static final long serialVersionUID = 1L; private double annualInterestRate; private int numberOfYears; private double loanAmount; private java.util.Date loanDate; /** Default constructor */ public Loan() { this(2.5, 1, 1000); } /** Construct a loan with specified annual interest rate, number of years, and loan amount */ public Loan(double annualInterestRate, int numberOfYears, double loanAmount) { this.annualInterestRate = annualInterestRate; this.numberOfYears = numberOfYears; this.loanAmount = loanAmount; loanDate = new java.util.Date(); } /** Return annualInterestRate */ public double getAnnualInterestRate() { return annualInterestRate; } /** Set a new annualInterestRate */ public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } /** Return numberOfYears */ public int getNumberOfYears() { return numberOfYears; } /** Set a new numberOfYears */ public void setNumberOfYears(int numberOfYears) { this.numberOfYears = numberOfYears; } /** Return loanAmount */ public double getLoanAmount() { return loanAmount; } /** Set a newloanAmount */ public void setLoanAmount(double loanAmount) { this.loanAmount = loanAmount; } /** Find monthly payment */ public double getMonthlyPayment() { double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12))); return monthlyPayment; } /** Find total payment */ public double getTotalPayment() { double totalPayment = getMonthlyPayment() * numberOfYears * 12; return totalPayment; } /** Return loan date */ public java.util.Date getLoanDate() { return loanDate; } }
import java.awt.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import javax.swing.*; public class Exercise05s extends JFrame { private static final long serialVersionUID = 1L; private int ClientNumber = 1; private JTextArea jta = new JTextArea(); public static void main(String[] args) { new Exercise05s(); } public Exercise05s() { setLayout(new BorderLayout()); add(new JScrollPane(jta), BorderLayout.CENTER); setTitle("Exercise03"); setSize(500, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8000); jta.append("Exercise01s started at " + new Date() + '\n'); while (true) { Socket socket = serverSocket.accept(); jta.append("Startig thread for client " + ClientNumber + " at + " + new Date() + "\n"); InetAddress inetAddress = socket.getInetAddress(); jta.append("Client " + ClientNumber + "'s host name is " + inetAddress.getHostName() + "\n"); jta.append("Client " + ClientNumber + "'s Ip address is " + inetAddress.getHostAddress() + "\n"); HandleAClient task = new HandleAClient(socket); new Thread(task).start(); ClientNumber++; } } catch (IOException ex) { System.err.println(ex); } } class HandleAClient implements Runnable { private Socket socket; private ObjectInputStream input; private ObjectOutputStream output; public HandleAClient(Socket socket) { this.socket = socket; } @Override public void run() { try { input = new ObjectInputStream(socket.getInputStream()); output = new ObjectOutputStream(socket.getOutputStream()); while(true) { Object object = input.readObject(); Loan loan = (Loan)object; double annualInterestRate = loan.getAnnualInterestRate(); int numOfYears = loan.getNumberOfYears(); double loanAmount = loan.getLoanAmount(); double totalPayment = loanAmount*annualInterestRate/100*numOfYears + loanAmount; double monthlyPayment = totalPayment/numOfYears/12; output.writeObject(loan); output.flush(); jta.append("Annual Interest Rate " + annualInterestRate + "\n"); jta.append("Number Of Years " + numOfYears + "\n"); jta.append("Loan Amount " + loanAmount + "\n"); jta.append("monthlyPayment " + monthlyPayment + "\n"); jta.append("totalPayment " + totalPayment + "\n"); } } catch (IOException e) { e.printStackTrace(); }catch (ClassNotFoundException ex) { ex.printStackTrace(); } } } }
import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Exercise05c extends JFrame { private static final long serialVersionUID = 1L; private JTextField jtfAnnualInterestRate = new JTextField(); private JTextField jtfNumberOfYears = new JTextField(); private JTextField jtfLoanAmount = new JTextField(); private JTextArea jta = new JTextArea(); private ObjectInputStream response; private ObjectOutputStream request; public static void main(String[] args) { new Exercise05c(); } public Exercise05c() { JPanel jPanel1 = new JPanel(new GridLayout(3, 2, 5, 5)); jPanel1.add(new JLabel("Annual Interest Rate")); jPanel1.add(jtfAnnualInterestRate); jPanel1.add(new JLabel("Number Of Years")); jPanel1.add(jtfNumberOfYears); jPanel1.add(new JLabel("Loan Amount")); jPanel1.add(jtfLoanAmount); JPanel jPanel2 = new JPanel(new BorderLayout(5, 5)); jPanel2.add(jPanel1, BorderLayout.CENTER); JButton jButton1 = new JButton("Submit"); jPanel2.add(jButton1, BorderLayout.EAST); setLayout(new BorderLayout()); add(jPanel2, BorderLayout.NORTH); add(new JScrollPane(jta), BorderLayout.CENTER); jButton1.addActionListener(new Listener()); setTitle("Exercise05c"); setSize(500, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); Socket socket; try { socket = new Socket("localhost", 8000); request = new ObjectOutputStream(socket.getOutputStream()); response = new ObjectInputStream(socket.getInputStream()); } catch (IOException ex) { jta.append(ex.toString() + '\n'); } } private class Listener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { try { Double rate = Double.parseDouble(jtfAnnualInterestRate.getText()); int numOfYears = Integer.parseInt(jtfNumberOfYears.getText()); Double loanAmount = Double.parseDouble(jtfLoanAmount.getText()); request.writeObject(new Loan(rate, numOfYears, loanAmount)); request.flush(); Loan loan = (Loan)response.readObject(); double monthlyPayment = loan.getMonthlyPayment(); double totalPayment = loan.getTotalPayment(); jta.append("Annual Interest Rate " + rate + "\n"); jta.append("Number Of Years " + numOfYears + "\n"); jta.append("Loan Amount " + loanAmount + "\n"); jta.append("monthlyPayment " + monthlyPayment + "\n"); jta.append("totalPayment " + totalPayment + "\n"); } catch (IOException ex) { System.err.println(ex); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } finally { try { response.close(); request.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } }
No comments :
Post a Comment