Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Remote Method InvocationRSS Feeds

RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user

Posted By: Milind Mishra     Category: Java     Views: 7946

Develop a small RMI application for getting the final total price of the
shopping list with some of the following items and quantities chosen by the
user from the client side. On the server, the item list contains the prices.
As example potatoes 17 Rs per 5 kg, tomatoes 8 Rs per kg, onions 20 Rs per
5 kg, and spinach 12 Rs per kg, etc.

Code for RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user in Java

// PriceRmi Interfacepublicinterface 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;

publicclass 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();
    }
  }

  publicdouble 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.*;

publicclass PriceRMIServer
{

 publicstaticvoid 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;


publicclass PriceRMIClient
{ 
  publicstaticvoid 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[] = newdouble[10],totalamt;
    double perkg[] = newdouble[10];
    int itemno[] = newint[10],choice;
    double quantity[] = newdouble[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
  
Share: 



Milind Mishra
Milind Mishra author of RMI application for getting the final total price of the shopping list with some of the following items and quantities chosen by the user is from India.
 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in Java:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!