Package

Source Code of Client

import bank.AccountInfo;
import bank.BankApplication;
import bank.BankApplicationHome;
import bank.ClientId;

import java.rmi.RemoteException;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

/**
* @author S.Chassande-Barrioz
*/
public class Client {
    final static String[] agencyNames = new String[]{"Paris", "London", "New York"};
    final static ClientId[] clientsNames = new ClientId[]{
        new ClientId("Chassande-Barrioz", "Sebastien", "Gieres"),
        new ClientId("Dechamboux", "Pascal", "Saint Ismier"),
        new ClientId("Lefebvre", "Alexandre", "Grenoble")
    };

    public static void main(String args[]) {

        Context initialContext = null;
        try {
            initialContext = new InitialContext();
        } catch (Exception e) {
            System.err.println("Cannot get initial context for JNDI: " + e);
            System.exit(2);
        }

        System.out.print("Looking up BankApplicationHome ...");
        // Connecting to BankApplicationHome thru JNDI
        BankApplicationHome bah = null;
        try {
            bah = (BankApplicationHome) PortableRemoteObject.narrow(
                initialContext.lookup("BankApplicationHome"),
                BankApplicationHome.class);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Cannot lookup BankApplicationHome: " + e);
            System.exit(2);
        }
        System.out.println("done");

/*
        System.out.print("Looking up java:comp/UserTransaction ...");
        // We want to start transactions from client: get UserTransaction
        UserTransaction utx = null;
        try {
            utx = (UserTransaction) initialContext.lookup("java:comp/UserTransaction");
        } catch (Exception e) {
            System.err.println("Cannot lookup UserTransaction: " + e);
            System.exit(2);
        }
        System.out.println("done");
*/
       
        BankApplication ba = null;
        String[] accountNumbers = new String[clientsNames.length];
        try {
            ba = bah.create();
            System.out.println("Bank created or loaded");

            for (int i = 0; i < agencyNames.length; i++) {
                ba.createAgency(agencyNames[i]);
                System.out.println("Agency '" + agencyNames[i] + "' created");
            }
            for (int i = 0; i < clientsNames.length; i++) {
                ba.createClient(clientsNames[i], agencyNames[0]);
                System.out.println("Client '" + clientsNames[i] + "' created");
                accountNumbers[i] = ba.createAccount(clientsNames[i], agencyNames[0]);
                System.out.println("Account '" + accountNumbers[i] + "' created");
            }

            printBank(ba);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace(System.err);
        }

    }

    public static void printBank(BankApplication ba) throws RemoteException {
        List agencies = ba.getAgencies();
        for (int i = 0; i < agencies.size(); i++) {
            String an = (String) agencies.get(i);
            System.out.println("Agency '" + an + "':");
            List clients = ba.getClients(an);
            for (int j = 0; j < clients.size(); j++) {
                ClientId cid = (ClientId) clients.get(j);
                System.out.println("\t- Client '" + cid + "':");
                List accounts = ba.getAccounts(an, cid);
                for (int k = 0; k < accounts.size(); k++) {
                    AccountInfo ai = (AccountInfo) accounts.get(k);
                    System.out.println("\t\t- Account " + ai.number + " : " + ai.solde);
                }
            }
        }
    }
}
TOP

Related Classes of Client

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.