Java Blog

 
 
 
// PriceRmi Interface

public interface PriceRMI extends java.rmi.Remote
{
    double calctotal(double prices[],double perkg[],int itemno[],double quantity[],int Count) throws java.rmi.RemoteException;
}


// PriceRMIImpl.java, PriceRMI implementation

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class PriceRMIImpl extends UnicastRemoteObject
       implements PriceRMI
{

  public PriceRMIImpl(String name) throws RemoteException
  {
    super();
    try
    {
      Naming.rebind(name, this);
    } catch (Exception e)
    { System.out.println(\"Exception: \" + e.getMessage());
      e.printStackTrace();
    }
  }

  public double calctotal(double prices[],double perkg[],int itemno[],double quantity[],int count) throws RemoteException
  { 
    double total=0.0;
    for(int i = 0;i<count;i++)
    {
        total = total+(quantity[i] * (prices[itemno[i]-1]/perkg[itemno[i]-1]));
    }
    return total;    


  }


}


// PriceRMIServer.java

import java.rmi.*;
import java.rmi.server.*;

public class PriceRMIServer
{

 public static void main(String args[])
 {

   // Create and install the security manager
   System.setSecurityManager(new RMISecurityManager());

   try
   {
     // Create ProceRMIImpl
     PriceRMIImpl myPrice = new PriceRMIImpl(\"//Binita/myPriceRMI\");
     System.out.println(\"PriceRMI Server ready.\");
   } catch (Exception e)
   { System.out.println(\"Exception: \" + e.getMessage());
     e.printStackTrace();
   }
 }
}


// PriceRMIClient.java  RMI Price client

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.*;
import java.lang.*;
import java.io.DataInputStream;


public class PriceRMIClient
{ 
  public static void main(String args[]) throws Exception
  { // Create and install the security manager
    System.setSecurityManager(new RMISecurityManager());

    FileReader fr = new FileReader(\"pricedetails.txt\");
    StreamTokenizer tok = new StreamTokenizer(fr);
    DataInputStream in = new DataInputStream(System.in);
    String items[]=new String[10];
    int flag1=0,flag2=1,i=0;
    double prices[] = new double[10],totalamt;
    double perkg[] = new double[10];
    int itemno[] = new int[10],choice;
    double quantity[] = new double[10];    
    tok.eolIsSignificant(true);
    try
    { 
    PriceRMI myPrice = (PriceRMI)Naming.lookup(\"//\"
                          + args[0] + \"/\" + \"myPriceRMI\");


    while( tok.nextToken() != tok.TT_EOF)
    {
        if(tok.ttype == tok.TT_EOL)
        {
            i++;
            flag1 = 0;
            //System.out.println(\"one record read\");
        }

        if(tok.ttype == tok.TT_WORD && flag1 == 0)
        {
            items[i]= tok.sval;
            flag1 = 1;
        }
        if(tok.ttype == tok.TT_NUMBER && perkg[i] <= 0.0 && prices[i] > 0.0)
        {
            perkg[i] = tok.nval;    
            //System.out.println(\"perkg is:\"+perkg[i]);
        }

        if(tok.ttype == tok.TT_NUMBER && prices[i] <= 0.0)
        {        
            prices[i] = tok.nval;    
            //System.out.println(\"price is:\"+prices[i]);
        }

    }
    fr.close();
    System.out.println(\"Item no   Item name\");
    for(int k = 0;k<=i;k++)
    {
        System.out.println(k+1 +\"\\t  \" + items[k]);
    }
    i=0;
    do
    {
        System.out.println(\"Enter the item no\");
        itemno[i] = Integer.parseInt(in.readLine());
        System.out.println(\"Enter Quantity\");
        quantity[i]=Double.parseDouble(in.readLine());
        i++;
        System.out.println(\"Enter 1 for enter item otherwise enter 0\");
        choice = Integer.parseInt(in.readLine());
    }while(choice != 0);            

    totalamt = myPrice.calctotal(prices,perkg,itemno,quantity,i);    
    System.out.println(\"The total of purchased items is:\"+totalamt);
    } catch(Exception e)
    { System.err.println(\"System Exception\" + e);
    }
   System.exit(0);
  }
}


// OUTPUT

-----  pricedetails.txt  ------

potatoes Rs 17 per 5 kg
tomatoes Rs 8 per 1 kg
onions Rs 20 per 5 kg
spanich Rs 12 per 1 kg
cabage Rs 15 per 2 kg
carrat Rs 14 per 3 kg
---------------------------



Item no   Item name
1         potatoes
2         tomatoes
3         onions
4         spanich
5         cabage
6         carrat
Enter the item no
1
Enter Quantity
20
Enter 1 for enter item otherwise enter 0
1
Enter the item no
3
Enter Quantity
40
Enter 1 for enter item otherwise enter 0
1
Enter the item no
4
Enter Quantity
50
Enter 1 for enter item otherwise enter 0
0
The total of purchased items is:828.0


---------


Item no   Item name
1         potatoes
2         tomatoes
3         onions
4         spanich
5         cabage
6         carrat
Enter the item no
2
Enter Quantity
20
Enter 1 for enter item otherwise enter 0
1
Enter the item no
6
Enter Quantity
70
Enter 1 for enter item otherwise enter 0
1
Enter the item no
5
Enter Quantity
30
Enter 1 for enter item otherwise enter 0
0
The total of purchased items is:711.6666666666667