Friday, 3 March 2017

Chapter 31 Exercise 3, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

31.3 (Loan server for multiple clients) Revise Programming Exercise 31.1 to write a server for multiple clients.


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 javax.swing.*;

public class Exercise03s extends JFrame {
 private static final long serialVersionUID = 1L;
 private int ClientNumber = 1;

 private JTextArea jta = new JTextArea();

 public static void main(String[] args) {
  new Exercise03s();
 }

 public Exercise03s() {
  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;
  
  public HandleAClient(Socket socket) {
   this.socket = socket;   
  }
  
  @Override
  public void run() {
   try {
    DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
    DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
    
    while (true) {
     double interest = inputFromClient.readDouble();
     int year = inputFromClient.readInt();
     double loanAmount = inputFromClient.readDouble();

     Loan loan = new Loan(interest, year, loanAmount);
     double monthlyPayment = loan.getMonthlyPayment();
     double totalPayment = loan.getTotalPayment();
     
     outputToClient.writeDouble(monthlyPayment);
     outputToClient.writeDouble(totalPayment);
     
     
     jta.append("Annual Interest Rate " + interest + "\n");
     jta.append("Number Of Years " + year + "\n");
     jta.append("Loan Amount " + loanAmount + "\n");
     jta.append("monthlyPayment " + monthlyPayment + "\n");
     jta.append("totalPayment " + totalPayment + "\n");
 
    }
   } catch (IOException e) {
    e.printStackTrace();
   }

  }
  
 }
 
 class Loan {
  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.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Exercise03c 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 DataOutputStream toServer;
 private DataInputStream fromServer;

 public static void main(String[] args) {
  new Exercise03c();
 }

 public Exercise03c() {
  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("Exercise03c");
  setSize(500, 300);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setLocationRelativeTo(null);
  setVisible(true);

  Socket socket;
  try {
   socket = new Socket("localhost", 8000);
   fromServer = new DataInputStream(socket.getInputStream());
   toServer = new DataOutputStream(socket.getOutputStream());
  } catch (IOException ex) {
   jta.append(ex.toString() + '\n');
  }
 }

 private class Listener implements ActionListener {
  @Override
  public void actionPerformed(ActionEvent e) {
   try {
    double interest = Double.parseDouble(jtfAnnualInterestRate.getText());
    int year = Integer.parseInt(jtfNumberOfYears.getText());
    double loanAmount = Double.parseDouble(jtfLoanAmount.getText());
    toServer.writeDouble(interest);
    toServer.writeInt(year);
    toServer.writeDouble(loanAmount);
    toServer.flush();
    
    double monthlyPayment = fromServer.readDouble();
    double totalPayment = fromServer.readDouble();
    
    jta.append("Annual Interest Rate " + interest + "\n");
    jta.append("Number Of Years " + year + "\n");
    jta.append("Loan Amount " + loanAmount + "\n");
    jta.append("monthlyPayment " + monthlyPayment + "\n");
    jta.append("totalPayment " + totalPayment + "\n");
   } catch (IOException ex) {
    System.err.println(ex);
   }
  }
 }
}

1 comment :