Friday, 18 October 2013

RMI(Remote Method Invocation) program on Interest calculator

InterestCalculatorServerInterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface InterestCalculatorServerInterface extends Remote{
    public Double getInterestAmount(double interestRate,int numberOfYears,double loanAmount) throws RemoteException;
    public Double getTotalAmount(double loanAmount,double interestAmount) throws RemoteException;
}

InterestCalculatorServerInterfaceImpl.java

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class InterestCalculatorServerInterfaceImpl extends UnicastRemoteObject implements InterestCalculatorServerInterface{
    public InterestCalculatorServerInterfaceImpl() throws RemoteException{
    }
    public Double getInterestAmount(double interestRate,int numberOfYears,double loanAmount) throws RemoteException{
        double interestAmount = (interestRate * numberOfYears * loanAmount)/100;
        return(new Double(interestAmount));
    }
    public Double getTotalAmount(double loanAmount,double interestAmount) throws RemoteException{
        double totalAmount = loanAmount + interestAmount;
        return(new Double(totalAmount));
    }
}

 RegisterWithRMIRegistry.java

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class RegisterWithRMIRegistry{
    public static void main(String []args){
        try{
            InterestCalculatorServerInterface obj = new InterestCalculatorServerInterfaceImpl();
            Registry registry = LocateRegistry.getRegistry();
            registry.rebind("InterestCalculatorServerInterfaceImpl",obj);
            System.out.println("Object "+obj+" registered");
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

InterestCalculatorServerInterfaceClient.java

import java.rmi.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class InterestCalculatorServerInterfaceClient extends JApplet{
    InterestCalculatorServerInterface interest;
    private JTextField jtfAnnualInterestRate = new JTextField();
    private JTextField jtfNumberOfYears = new JTextField();
    private JTextField jtfLoanAmount = new JTextField();
    private JTextArea jtaResult = new JTextArea(10,50);
    private JButton jbtSubmit = new JButton("Submit");
    private JScrollPane scroller = new JScrollPane(jtaResult);
    public void init(){
        initializeRMI();
        setLayout(new FlowLayout());
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4,2,10,10));
        panel.add(new JLabel("Annual Interest Rate "));
        panel.add(jtfAnnualInterestRate);
        panel.add(new JLabel("Number Of Years "));
        panel.add(jtfNumberOfYears);
        panel.add(new JLabel("Loan Amount "));
        panel.add(jtfLoanAmount);
        panel.add(jbtSubmit);
        add(panel,BorderLayout.CENTER);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        add(scroller,BorderLayout.SOUTH);
        jbtSubmit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                try{
                    Double annualInterestRate = Double.parseDouble(jtfAnnualInterestRate.getText().trim());
                    Integer numberOfYears = Integer.parseInt(jtfNumberOfYears.getText().trim());
                    Double loanAmount = Double.parseDouble(jtfLoanAmount.getText().trim());
                    double interestAmount = interest.getInterestAmount(annualInterestRate.doubleValue(),numberOfYears.intValue(),loanAmount.doubleValue());
                    double totalAmount = interest.getTotalAmount(loanAmount,interestAmount);
                    jtaResult.append("Annual Interest Rate :"+annualInterestRate.toString()+"\n");
                    jtaResult.append("Number of Years :"+numberOfYears.toString()+"\n");
                    jtaResult.append("Annual Interest Rate :"+loanAmount.toString()+"\n");
                    jtaResult.append("interest Amount :"+(new Double(interestAmount)).toString()+"\n");
                    jtaResult.append("total Amount :"+new Double(totalAmount)+"\n");
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }
    protected void initializeRMI(){
            try{
                Registry registry = LocateRegistry.getRegistry("localhost");
                interest = (InterestCalculatorServerInterface)registry.lookup("InterestCalculatorServerInterfaceImpl");
                System.out.println("Server object "+interest+" found");
            }catch(Exception ex){
                System.out.println(ex);
            }
    }
    public static void main(String[]args){
        InterestCalculatorServerInterfaceClient applet = new InterestCalculatorServerInterfaceClient();
        JFrame frame = new JFrame();
        frame.setTitle("InterestCalculatorServerInterfaceClient");
        frame.add(applet,BorderLayout.CENTER);
        frame.setSize(300,300);
        applet.init();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(3);
    }
}

No comments:

Post a Comment