0% found this document useful (0 votes)
88 views83 pages

Program: / Downserver - Java / Import Java - Rmi.

The document describes a weather monitoring system implemented using CORBA. It defines a Weather interface with methods to adjust temperature, get current/max/min temperature, call a function and shutdown. A Weather client program initializes the ORB, looks up the Weather reference and calls methods by passing temperature data stored in arrays to simulate temperature readings from different cities.

Uploaded by

Mathi Sankar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views83 pages

Program: / Downserver - Java / Import Java - Rmi.

The document describes a weather monitoring system implemented using CORBA. It defines a Weather interface with methods to adjust temperature, get current/max/min temperature, call a function and shutdown. A Weather client program initializes the ORB, looks up the Weather reference and calls methods by passing temperature data stored in arrays to simulate temperature readings from different cities.

Uploaded by

Mathi Sankar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 83

PROGRAM: /* DOWNSERVER.JAVA */ import java.rmi.*; import java.io.

*; public interface downserver extends Remote { String download(File f) throws RemoteException; }

/* DCLIENT.JAVA */ import java.rmi.*; import java.io.*; public class dclient { public static void main(String args[]) { try { String file,dir; BufferedReader din=new BufferedReader(new InputStreamReader

(System.in)); System.out.println("Enter directory name:"); dir=din.readLine(); System.out.println("Enter the file name:"); file=din.readLine(); File ff=new File(dir,file); downserver dr=(downserver)Naming.lookup("rmi://localhost/downimpl"); String s=dr.download(ff); System.out.println(s); } catch(Exception e) {} } }

/* DOWNIMPL.JAVA */

import java.rmi.*; import java.io.*; import java.rmi.server.*; public class downimpl extends UnicastRemoteObject implements downserver { public downimpl() throws RemoteException { } public String download(File f) { String str=" "; try { FileInputStream in=new FileInputStream(f); int l=in.available(); for(int i=1;i<=l;i++) { str+=(char)in.read(); }

} catch(Exception e) {} return str; } public static void main(String args[]) { try { downimpl d=new downimpl(); Naming.rebind("rmi://localhost/downimpl",d); } catch(Exception e) {} }}

OUTPUT:

PROGRAM:

import java.io.*; import java.lang.*; import java.util.*; import java.beans.*; import java.awt.*; import java.awt.event.*; public class Tcanvas1 extends Canvas implements Serializable{ public static final int WIDTH=200; public static final int HEIGHT=200; public int width=WIDTH; public int height=HEIGHT; public int leftMargin=5; public int topMargin=5; public String text=""; public Boolean border=true; public Tcanvas1() { super(); } public Dimension getPrefferedsize()

{ return new Dimension(width,height); } public synchronized void paint(Graphics g) { if(border) g.drawRect(0,0,width-1,height-1); if(border) g.drawOval(20,20,20,20); if(border) g.drawArc(20,40,60,80,90,100); Font font=g.getFont(); FontMetrics fm=g.getFontMetrics(font); int lineheight=fm.getHeight(); int y=fm.getLeading()+fm.getAscent(); StringTokenizer tokenizer=new StringTokenizer(text,"i"); String line; while(tokenizer.hasMoreTokens()) line=tokenizer.nextToken(); if(border)

g.drawString(line,leftMargin+1,topMargin+y+1); else g.drawString(line,leftMargin,topMargin+y); y+=lineheight; } } public String getText() { return text; } public void setText(String newTextvalue) { text=newTextvalue; } public int getWidth() { return width; } } Tcanvas.mft

Name: Tcanvas1.class java-Bean: True

OUTPUT:

OUTPUT:

PROGRAM: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace currency { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int bb = 0; bb = Int32.Parse(textBox1.Text); if (radioButton1.Checked == true) { bb = bb * 352; textBox2.Text = "" + bb; } if (radioButton2.Checked == true)

{ bb = bb * 352; textBox2.Text = "" + bb; } if (radioButton3.Checked == true) { bb = bb * 352; textBox2.Text = "" + bb; } if (radioButton4.Checked == true) { bb = bb * 352; textBox2.Text = "" + bb; } if (radioButton5.Checked == true) { bb = bb * 352; textBox2.Text = "" + bb; } if (radioButton6.Checked == true) { bb = bb * 352; textBox2.Text = "" + bb; } } private void button2_Click(object sender, EventArgs e) {

textBox1.Text = ""; textBox2.Text = ""; } private void button3_Click(object sender, EventArgs e) { Close(); } private void Form1_Load(object sender, EventArgs e) { }

} }

PROGRAM: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace encryption { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string str = Convert.ToString(textBox1.Text); char a; int ch = 0, i = 0, key = 2; while (i != str.Length) { ch = Convert.ToInt32(str[i]); ch = ch + key; a= Convert.ToChar(ch);

textBox2.Text = textBox2.Text + Convert.ToString(a); i++; } }

private void button4_Click(object sender, EventArgs e) { string str=textBox2.Text; char a; int ch = 0, i = 0, key = 2; while (i != str.Length) { ch = Convert.ToInt32(str[i]); ch = ch-key; a = Convert.ToChar(ch); textBox3.Text = textBox3.Text + Convert.ToString(a); i++; } } private void button2_Click(object sender, EventArgs e) { Close(); } private void button3_Click(object sender, EventArgs e) {

textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } } }

OUTPUT:

PROGRAM: Stock.idl module StockApp { interface Stock { double input(in double unit,in double rate); }; }; Stockserver.java import StockApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA;

class StockImpl extends StockPOA { private ORB n;

public void setORB(ORB j) { n=j; } public double input(double unit,double no) { return unit*no; } }

public class StockServer { public static void main(String arg[]) { try { ORB orb=ORB.init(arg,null); POA rootpoa=POAHelper.narrow(orb.resolve_initial_references("RootPOA")); rootpoa.the_POAManager().activate(); StockImpl w=new StockImpl();

w.setORB(orb); org.omg.CORBA.Object ref=rootpoa.servant_to_reference(w); Stock href=StockHelper.narrow(ref); org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService"); NamingContextExt ncRef=NamingContextExtHelper.narrow(objRef); String name="p1"; NameComponent path[]=ncRef.to_name(name); ncRef.rebind(path,href); System.out.println("Server Ready and Waiting "); orb.run(); } catch(Exception e) { System.out.println("Error occur"+e); e.printStackTrace(System.out); } } }

Stockclient.java import StockApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; import java.io.*; public class StockClient { static Stock k; public static void main(String arg[]) { String[] uid={"M124","P1190","M890","K345"}; String[] p={"Hutch","Infosys","Hutch","Infosys"}; double[] unit={5,6,5,6}; double[] noshares={10,4,7,3}; double[] total={0,0,0,0}; int ch; try {

ORB orb=ORB.init(arg,null); org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService") ; NamingContextExt ncRef=NamingContextExtHelper.narrow(objRef); String name="p1"; k=StockHelper.narrow(ncRef.resolve_str(name)); for(int i=0;i<4;i++) { total[i]=k.input(unit[i],noshares[i]); } System.out.println("\tSTOCK MARKET EXCHAGE "); System.out.println("\t\t\t STATUS OF NSE"); System.out.println("\t---------------------------\n"); System.out.println("\t Uid CompanyName \t Unit\t Noofshares\t total"); System.out.println("\t---------------------------\n"); for(int i=0;i<4;i++) { System.out.println("\t"+uid[i]+"\t"+p[i]+"\t\t"+unit[i]+"\t\t"+noshares[i]+"\t\t"+total[i]); } System.out.println("\t---------------------------"); BufferedReader k1=new BufferedReader(new InputStreamReader(System.in));

String c="y"; do { System.out.println("\t\t Menu Options"); System.out.println(" System.out.println(" System.out.println(" 1.Buy \n"); 2.Sell \n"); 3.Display \n");

System.out.println("Enter your choice: "); ch=Integer.parseInt(k1.readLine()); switch(ch) { case 1: System.out.println("Enter your Buyer Id: "); String bid=k1.readLine(); System.out.println("Enter Company Name: "); String co=k1.readLine(); System.out.println("No. of share you want to Buy: "); double sh=Double.parseDouble(k1.readLine()); System.out.println("Enter Seller Id: "); String sid=k1.readLine();

for(int i=0;i<4;i++) { if(uid[i].equals(bid)) { if(p[i].equals(co)) { noshares[i]+=sh; total[i]=unit[i]*noshares[i]; } } } for(int i=0;i<4;i++) { if(uid[i].equals(sid)) { if(p[i].equals(co)) { noshares[i]-=sh; total[i]=unit[i]*noshares[i]; }

} } break; case 2: System.out.println("Enter your Seller Id: "); String sid1=k1.readLine(); System.out.println("Enter Company Name: "); String co1=k1.readLine(); System.out.println("No. of share you want to Sell: "); double sh1=Double.parseDouble(k1.readLine()); System.out.println("Enter Buyer Id: "); String bid1=k1.readLine(); for(int i=0;i<4;i++) { if(uid[i].equals(sid1)) { if(p[i].equals(co1)) { noshares[i]-=sh1; total[i]=unit[i]*noshares[i];

} } } for(int i=0;i<4;i++) { if(uid[i].equals(bid1)) { if(p[i].equals(co1)) { noshares[i]-=sh1; total[i]=unit[i]*noshares[i]; } } } break; case 3: System.out.println("\t\tSTATUS OF NSE"); System.out.println("\t-----------------------------------------"); System.out.println("\tUid\t\tCompanyName\t\tUnit\tNoofshares\ttotal"); System.out.println("\t-----------------------------------------");

for(int i=0;i<4;i++) { System.out.println("\t"+uid[i]+"\t\t"+p[i]+"\t\t"+unit[i]+"\t\t"+noshares[i] +"\t\t"+total[i]+"\n\n"); } break; } System.out.println("Do you want to continue(y/n): "); c=k1.readLine(); } while(c.equals("y")); } catch(Exception e) { System.out.println("Error occur"+e); e.printStackTrace(System.out); } } }

OUTPUT:

OUTPUT:

PROGRAM: Weather.idl module WeatherApp { interface Weather { void adjusttemp(in double r); double getcurrent(); double getMax(); double getMin(); void call(in double k); oneway void shutdown(); }; }; Weatherclnt.java import WeatherApp.*; import org.omg.CosNaming.*; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CORBA.*; public class Weather_clnt

{ static Weather wt; public static void main(String args[]) { double[][] wr={{-1.5,2.5,4.2,-4.5},{-2.3,-7.8,6.2,-4.7},{4.3,-3.6,5.9,-1.7}}; String[] wc={"Chennai","Mumbai","Delhi","Calcutta"}; int i,j; try { ORB w=ORB.init(args,null); org.omg.CORBA.Object wf=w.resolve_initial_references("NameService"); NamingContextExt ncref=NamingContextExtHelper.narrow(wf); String name="Weather"; wt=WeatherHelper.narrow(ncref.resolve_str(name)); System.out.println("Obtained Handle on Object Server"+wt); System.out.println("\t WEATHER FORECASTING"); System.out.println("\t***********************"); wt.call(20.0); for(i=0;i<3;i++)

{ System.out.println(wc[i]); System.out.println("*******************"); for(j=0;j<4;j++) wt.adjusttemp(wr[i][j]); System.out.println("\t Max Temp: "+Math.rint(wt.getMax())); System.out.println("\t Min Temp: "+Math.rint(wt.getMin())); System.out.println("\t Current Temp: "+Math.rint(wt.getcurrent())); } wt.shutdown(); } catch(Exception e) { System.out.println(e); } } }

Weatherserver.java import WeatherApp.*; import org.omg.CosNaming.*; import org.omg.CORBA.*; import org.omg.PortableServer.*; import org.omg.PortableServer.POA.*; import java.util.Properties; class WeatherImp extends WeatherPOA { private ORB w; private double current,Max,Min; public void setORB(ORB orb_val) { w=orb_val; } public void adjusttemp(double g) { current +=g; if(current>Max) {

Max=current; } else if(current<Min) { Min=current; } } public double getMax() { return Max; } public double getMin() { return Min; } public double getcurrent() { return current; } public void call(double f)

{ Min=Max=current=f; } public void shutdown() { w.shutdown(false); } } public class Weather_ser { public static void main(String args[]) { try { ORB w=ORB.init(args,null); POA wpoa=POAHelper.narrow(w.resolve_initial_references("RootPOA")); wpoa.the_POAManager().activate(); WeatherImp w1=new WeatherImp(); w1.setORB(w); org.omg.CORBA.Object ref1=wpoa.servant_to_reference(w1);

Weather wref=WeatherHelper.narrow(ref1); org.omg.CORBA.Object wf=w.resolve_initial_references("NameService"); NamingContextExt ncref=NamingContextExtHelper.narrow(wf); String name="Weather"; NameComponent path[]=ncref.to_name(name); ncref.rebind(path,wref); System.out.println("Hello!!! SERVER READY AND WAITING!!"); w.run(); } catch(Exception e) { System.out.println(e); } } } OUTPUT:

\ PROGRAM:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Retrievemessage { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("ERROR MESSAGE","MESSAGE",MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Error); } private void button4_Click(object sender, EventArgs e) { MessageBox.Show("WARNING","ERROR MESSAGE",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning); } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("STOP MESSAGE","STOP",MessageBoxButtons.YesNo,MessageBoxIcon.Stop); } private void button5_Click(object sender, EventArgs e) { MessageBox.Show("QUESTION", "QUESTIONS ARE", MessageBoxButtons.OKCancel,MessageBoxIcon.Question); } private void button3_Click(object sender, EventArgs e)

{ Close(); } } }

Public Class UserControl1 Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Public Function readfile(ByVal fna As String) Dim fname As String Dim fno As Integer Dim str As String fname = fna fno = FreeFile() If Not bFileExists(fname) Then

MsgBox("FILE NOT EXIST ,PLEASE TRY AGAIN") End End If FileOpen(fno, fname, OpenMode.Input, OpenAccess.Read, OpenShare.Default, -1) If (fno = 0) Then MsgBox("FILE NOT FOUND") End If Input(fno, str) FileClose(fno) Return (str) End Function Public Function bFileExists(ByVal sFile As String) As Boolean If Dir$(sFile) <> "" Then bFileExists = True Else bFileExists = False End If End Function Public Function writefile(ByVal fnam As String, ByVal content As String) Dim fno1 As Integer Dim str1 As String fno1 = FreeFile() str1 = content FileOpen(fno1, fnam, OpenMode.Output, OpenAccess.Write, OpenShare.Default, -1) Write(fno1, content) FileClose(fno1) End Function End Class

BANKBEAN.JAVA:

//CLIENT import bank.BHome;

import bank.BBean; import javax.rmi.PortableRemoteObject; import javax.naming.InitialContext; import javax.naming.Context; import java.io.*;

public class BankBean { public static void main(String args[]) { int c,val,amt,cn,n; int cno[]=new int[10]; String cname[]=new String[10]; int camt[]=new int[10]; DataInputStream d=new DataInputStream(System.in); try { Context initial =new InitialContext(); Context myEnv = (Context) initial.lookup("java:comp/env"); Object objref=myEnv.lookup("ejb/BANKBEAN"); BHome home=(BHome) PortableRemoteObject. narrow(objref,BHome.class); BBean b=home.create(); do {

System.out.print("\nMenu:\n1.Enter Details\n2.Deposit\n 3.Withdraw\n4.View\n5.Exit\n\nEnter the choice: "); c=Integer.parseInt(d.readLine()); switch(c) { case 1: System.out.println("Enter the number of customers:"); n=Integer.parseInt(d.readLine()); for(int i=0;i<n;i++) { System.out.println("Enter the customer number:"); cno[i]=Integer.parseInt(d.readLine()); System.out.println("Enter the customer name:"); cname[i]=d.readLine();

System.out.println("Enter the initial amount:"); camt[i]=Integer.parseInt(d.readLine()); } int a=b.details(n,cno,cname,camt); if(a==1) System.out.println("Details saved successfully"); break; case 2:

System.out.println("DEPOSIT"); System.out.println("Enter the customer number:"); cn=Integer.parseInt(d.readLine()); System.out.println("Enter the amount to be deposited:"); amt=Integer.parseInt(d.readLine()); val=b.deposit(cn,amt); if(val==1) System.out.println("Amount deposited successfully!"); else if(val==0) System.out.println("There is no customer!"); break; case 3: System.out.println("WITHDRAW"); System.out.println("Enter the customer number:"); cn=Integer.parseInt(d.readLine()); System.out.println("Enter the amount to be withdrawn:"); amt=Integer.parseInt(d.readLine()); val=b.withdraw(cn,amt); if(val==1) System.out.println("Amount withdrawn successfully!"); else if(val==-1) System.out.println("Balance should be 500!"); else if(val==0) System.out.println("There is no customer!");

break; case 4: System.out.println("VIEW"); System.out.println("Enter the customer number:"); cn=Integer.parseInt(d.readLine()); String[] str=b.view(cn); System.out.println("\n==================== = =================\n"); System.out.println("\nID\tNAME\t\tBALANCE\n"); System.out.println("\n===================== = ================\n"); System.out.print( "\n"+str[0] +"\t"+str[1]+"\t"+str[2]); System.out.println("\n===================== = ================\n"); break; }

}while(c >0 && c<5); System.exit(0); } catch(Exception e){System.out.println(e);} }

BBEAN.JAVA:

//BEAN INTERFACE package bank; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.math.*; public interface BBean extends EJBObject { public int details(int n,int cid[],String name[],int amt[]) throws java.rmi.RemoteException; public int withdraw(int id,int amt) throws java.rmi.RemoteException; public int deposit(int id,int amt) throws java.rmi.RemoteException; public String[] view(int id) throws RemoteException; }

BHOME.JAVA:

//HOME INTERFACE package bank; public interface BHome extends javax.ejb.EJBHome {

public BBean create() throws javax.ejb.CreateException,java.rmi.RemoteException; }

BBEANCLASS.JAVA:

//BEAN CLASS package bank; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext;

public class BBeanClass implements SessionBean { int cno[]=new int[10]; String cname[]=new String[10]; int camt[]=new int[10]; int tn; public int details(int n,int cid[],String name[],int amt[]) throws java.rmi.RemoteException

{ tn=n; for(int i=0;i<n;i++) { cno[i]=cid[i]; cname[i]=name[i]; camt[i]=amt[i]; } return 1; } public int withdraw(int id,int amt) throws RemoteException { for(int i=0;i<tn;i++) { if(id==cno[i]) { if((camt[i]-amt)>=500) { camt[i]=camt[i]-amt; return(1); } else { return(-1); }

} } return 0; } public int deposit(int id,int amt) throws RemoteException { for(int i=0;i<tn;i++) { if(id==cno[i]) {

camt[i]=camt[i]+amt; return(1); } } return 0; } public String[] view(int id) throws RemoteException {

for(int i=0;i<tn;i++) { if(id==cno[i])

{ String[] str=new String[3]; str[0]=String.valueOf(cno[i]); str[1]=cname[i]; str[2]=String.valueOf(camt[i]); return(str); }

} return null; } //Standard EJB methods public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void ejbCreate(){} public void setSessionContext(SessionContext context){} }

Output:

C:\bank\bank>set path=C:\j2sdk1.4.0-rc\bin

C:\bank\bank>set classpath= C:\Sun\AppServer\lib\j2ee.jar; .;C:\Sun\AppServer\bin;

C:\bank\bank>set j2ee.home=C:\Sun\AppServer

C:\bank\bank>set java.home=C:\j2sdk1.4.0-rc

C:\bank\bank>javac *.java Note: BankBean.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details.

C:\bank\bank>set APPCPATH=C:\bank\bank\BankejbClient.jar

C:\bank\bank>set path=C:\Sun\AppServer\bin

C:\bank\bank>appclient -client bankejb.ear -name ejbclient -textauth

Menu: 1.Enter Details 2.Deposit 3.Withdraw 4.View 5.Exit Enter the choice: 1

Enter the number of customers: 3 Enter the customer number: 1 Enter the customer name: Devi Enter the initial amount: 7000 Enter the customer number: 2 Enter the customer name: Ram

Enter the initial amount: 5000

Enter the customer number: 3 Enter the customer name: Krishnan Enter the initial amount: 9000 Details saved successfully

Menu: 1.Enter Details 2.Deposit 3.Withdraw 4.View 5.Exit Enter the choice: 2

DEPOSIT ======== Enter the customer number: 2

Enter the amount to be deposited: 500 Amount deposited successfully!

Menu: 1.Enter Details 2.Deposit 3.Withdraw 4.View 5.Exit

Enter the choice: 3

WITHDRAW =========== Enter the customer number: 1 Enter the amount to be withdrawn: 500 Amount withdrawn successfully!

Menu: 1.Enter Details 2.Deposit 3.Withdraw 4.View 5.Exit

Enter the choice: 4

VIEW ===== Enter the customer number: 2

====================================== ID NAME BALANCE

====================================== 2 Ram 5500

======================================

Menu: 1.Enter Details 2.Deposit 3.Withdraw

4.View 5.Exit

Enter the choice: 5 PROGRAM: LibBean.java:

package beans; import javax.ejb.EJBObject; import java.rmi.RemoteException; import java.math.*; public interface LibBean extends EJBObject { public int details(int n,int cid[],String name[],int totc[]) throws java.rmi.RemoteException; public int issue(int id) throws java.rmi.RemoteException; public int return1(int id) throws java.rmi.RemoteException; public String[] view(int id) throws RemoteException; }

LibHome .java:

package beans; public interface LibHome extends javax.ejb.EJBHome {

public LibBean create() throws javax.ejb.CreateException,java.rmi.RemoteException; }

LibBeanClass .java:

package beans; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class LibBeanClass implements SessionBean { int cno[]=new int[10]; String cname[]=new String[10]; int totcp[]=new int[10]; int acp[]=new int[10]; int tn; public int details(int n,int cid[],String name[],int totc[]) throws java.rmi.RemoteException { tn=n; for(int i=0;i<n;i++) { cno[i]=cid[i];

cname[i]=name[i]; totcp[i]=totc[i]; acp[i]=totc[i]; } return 1;

} public int issue(int id) throws RemoteException { for(int i=0;i<tn;i++) { if(id==cno[i]) { if((acp[i]==0)) { return(-1); } else { acp[i]-=1; return 1; } }

} return 0; } public int return1(int id) throws RemoteException { for(int i=0;i<tn;i++) { if(id==cno[i]) { if((acp[i]==totcp[i])) { return(-1); } else { acp[i]+=1; return 1; } } } return 0;}

public String[] view(int id) throws RemoteException {

for(int i=0;i<tn;i++) { if(id==cno[i]) { String[] str=new String[3]; str[0]=String.valueOf(cno[i]); str[1]=cname[i]; str[2]=String.valueOf(acp[i]); return(str); } } return null; }

//Standard EJB methods public void ejbActivate(){} public void ejbPassivate(){} public void ejbRemove(){} public void ejbCreate(){} public void setSessionContext(SessionContext context){} }

LibBeanClient.java:

package client;

import beans.LibHome; import beans.LibBean; import javax.rmi.PortableRemoteObject; import javax.naming.InitialContext; import javax.naming.Context; import java.io.*; public class LibBeanClient { public static void main(String args[]) { int c,val,amt,cn,n; int bno[]=new int[10]; String bname[]=new String[10]; int btotc[]=new int[10]; DataInputStream d=new DataInputStream(System.in); try { Context initial =new InitialContext();

Context myEnv = (Context) initial.lookup("java:comp/env"); Object objref=myEnv.lookup("ejb/LIBRARYBEAN"); LibHome home=(LibHome) PortableRemoteObject. narrow(objref,LibHome.class); LibBean b=home.create();

do { System.out.print("\nMenu:\n1.Enter Details\n2.issue\n3.return\n4.View\n5.Exit\n\nEnter the choice: "); c=Integer.parseInt(d.readLine()); switch(c) { case 1: System.out.println("Enter the number of books:"); n=Integer.parseInt(d.readLine()); for(int i=0;i<n;i++) { System.out.println("Enter the book number:"); bno[i]=Integer.parseInt(d.readLine()); System.out.println("Enter the book name:"); bname[i]=d.readLine(); System.out.println("Enter the total copies:"); btotc[i]=Integer.parseInt(d.readLine()); } int a=b.details(n,bno,bname,btotc); if(a==1) System.out.println("Details saved successfully"); break; case 2:

System.out.println("ISSUE"); System.out.println("Enter the book number:"); cn=Integer.parseInt(d.readLine()); val=b.issue(cn); if(val==1) System.out.println("Book borrowed successfully!"); else if(val==-1) System.out.println("The Book is not available"); else if(val==0) System.out.println("There is no book!"); break; case 3: System.out.println("RETURN"); System.out.println("Enter the book number:"); cn=Integer.parseInt(d.readLine());

val=b.return1(cn); if(val==1) System.out.println("Book Returned successfully!"); else if(val==-1) System.out.println("The Book was not issued "); else if(val==0) System.out.println("There is no book with this number!");

break; case 4: System.out.println("VIEW"); System.out.println("Enter the book number:"); cn=Integer.parseInt(d.readLine()); String[] str=b.view(cn); System.out.println("\n================\n"); System.out.println("\nID\tNAME\t\tAVAILABLE COPIES\n"); System.out.println("\n======================\n" ); System.out.print( "\n"+str[0] +"\t"+str[1]+"\t"+str[2]+"\n"); System.out.println("\n======================= ===\n"); break; } }while(c >0 && c<5); System.exit(0); } catch(Exception e){System.out.println(e);} } }

OUTPUT: Menu: 1.Enter Details 2.issue 3.return 4.View 5.Exit

Enter the choice: 1 Enter the number of books: 3

Enter the book number: 10 Enter the book name: C

Enter the total copies: 5 Enter the book number: 11 Enter the book name: C# Enter the total copies: 10 Enter the book number: 12 Enter the book name: EJB Enter the total copies: 5 Details saved successfully

Menu: 1.Enter Details 2.issue 3.return 4.View 5.Exit

Enter the choice: 2 ISSUE

Enter the book number: 11 Book borrowed successfully!

Menu: 1.Enter Details 2.issue 3.return 4.View 5.Exit

Enter the choice: 4 VIEW Enter the book number: 11

========================================= ID NAME AVAILABLE COPIES

========================================== 11 C# 9

==========================================

Menu: 1.Enter Details 2.issue 3.return 4.View 5.Exit

Enter the choice: 3 RETURN

Enter the book number: 10 The Book was not issued

Menu: 1.Enter Details 2.issue 3.return 4.View 5.Exit

Enter the choice: 3 RETURN

Enter the book number: 11 Book Returned successfully!

Menu: 1.Enter Details 2.issue 3.return 4.View 5.Exit

Enter the choice: 4 VIEW Enter the book number: 11

=========================================== ID NAME AVAILABLE COPIES

=========================================== 11 C# 10

===========================================

Menu: 1.Enter Details 2.issue 3.return 4.View

5.Exit

Enter the choice:5

You might also like