Statefull Bean
package pack;
import javax.ejb.Remote;
@Remote
public interface Account {
public void deposit(int amount);
public boolean withdraw(int amount);
public int getBalance();
}
package pack;
import java.util.Scanner;
import javax.naming.InitialContext;
public class StatefullTest {
public static void main(String[] args) {
try {
InitialContext ctx=new InitialContext();
Account acc=(Account)ctx.lookup("statefullAccountBean");
Scanner in=new Scanner(System.in);
System.out.println("Enter amount to be deposit....");
int amt=in.nextInt();
acc.deposit(amt);
System.out.println("Enter amount to be withdraw");
amt=in.nextInt();
boolean b=acc.withdraw(amt);
if(b){
System.out.println("amount withdraw successfull and remaining amount is....");
int rem=acc.getBalance();
System.out.println(rem);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
package pack;
import javax.ejb.Remote;
@Remote
public interface Account {
public void deposit(int amount);
public boolean withdraw(int amount);
public int getBalance();
}
package pack;
import javax.ejb.Stateful;
@Stateful(mappedName="statefullAccountBean")
public class StatefullSessionBean implements Account{
int balance;
public void deposit(int amount) {
balance+=amount;
}
public int getBalance() {
return balance;
}
public boolean withdraw(int amount) {
if(amount > balance){
return false;
}else {
balance =balance-amount;
return true;
}
}
}