I am trying to create a server and client where the client can contact the server to create an account and deposit and withdraw into the account. The client should be able to open more than one account.
I have no idea how to code the the Client bit.
// The package containing our stubs. import AccountApp.*; // HelloServer will use the naming service. import org.omg.CosNaming.*; // The package containing special exceptions thrown by the name service. import org.omg.CosNaming.NamingContextPackage.*; // All CORBA applications need these classes. import org.omg.CORBA.*; import java.util.List; import java.util.LinkedList; public class Server { public static void main(String args[]) { try{ // Create and initialize the ORB ORB orb = ORB.init(args, null); // Create the servant and register it with the ORB AccountImpl accRef = new AccountImpl(); orb.connect(accRef); // Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Bind the object reference in naming // Make sure there are no spaces between "" NameComponent nc = new NameComponent("Account", ""); NameComponent path[] = {nc}; ncRef.rebind(path, accRef); // Wait for invocations from clients java.lang.Object sync = new java.lang.Object(); synchronized(sync){ sync.wait(); } } catch(Exception e) { System.err.println("ERROR: " + e); e.printStackTrace(System.out); } } } class AccountImpl extends _AccountImplBase{ private String accountNo; private String ownersName; private String pin; private float balance; //private List<Account> accounts; //private int number_of_accounts; public AccountImpl(){ accountNo = "0987"; pin = "1234"; ownersName = "John"; balance = 0; //accounts = new LinkedList<Account>(); //number_of_accounts = 0; } public float getBalance(String PIN) throws WrongPIN{ pin = PIN; if (!PIN.equals(pin)) throw new WrongPIN(); return balance; } public float deposit(String PIN, float amount) throws WrongPIN { pin = PIN; if (!PIN.equals(pin)) throw new WrongPIN(); balance += amount; return balance; } public float withdraw(String PIN, float amount) throws WrongPIN, InsufficientFunds { pin = PIN; if (!PIN.equals(pin)) throw new WrongPIN(); if (amount > balance) throw new InsufficientFunds(); balance -= amount; return balance; } public void create(String name, float balance, String PIN){ //++number_of_accounts; // Account res = new AccountImpl( Integer.toString(number_of_accounts) // , PIN // , name // , balance); // accounts.add( res ); } }
class Client { public static void main(String args[]) { try{ // Create and initialize the ORB ORB orb = ORB.init(args, null); // Get the root naming context org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService"); NamingContext ncRef = NamingContextHelper.narrow(objRef); // Resolve the object reference in naming // make sure there are no spaces between "" NameComponent nc = new NameComponent("Account", ""); NameComponent path[] = {nc}; Account accRef = AccountHelper.narrow(ncRef.resolve(path)); } catch(Exception e) { System.out.println("ERROR : " + e); e.printStackTrace(System.out); } } }