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

    Related Post:
  1. Program to declare and initialize Boolean variables and print them on the Standard Output Device

  2. Program to display attributes of a specified file

  3. Program to show the use of Conditional AND Operator (&&) in If Statements

  4. Servlet which takes input from an HTML client page and calculates and outputs the premium amount

  5. An applet program that creates string dynamically and displays it

  6. Corba program of reverse string

  7. Program to show the use of Break Statement

  8. CORBA based application for addition of two mathematical expressions of the form (ax + by) and displaying the result on client side

  9. Program that demonstrates type casting

  10. Program to show an example of searching Strings for subStrings

  11. Program to compute the sum of the digits of a given integer number. eg: I/o=4 then O/p=4+3+2+1=10

  12. Program which calls the method sort(int []a) which throws the Exception ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException

  13. Program to show an example of Multiple Levels of Inheritance

  14. Program to show an example of modifying String objects

  15. Given a list of marks ranging form 0 to 100, write a program to compute and print the number of students marks

  16. Program to generate Fibonacci Series of a given number

  17. Program to show an example of using constructors in a class to initialize its data members

  18. Program of UDP news server

  19. RMI based application, which reads a file with list of marks of student from a client, send it to server and find how many students having distinct

  20. Program to show an example of Concatination of Strings