home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / ODesign / SetupPSE.exe / data.z / Banking.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  6.2 KB  |  234 lines

  1. package COM.odi.demo.threads;
  2.  
  3. /**
  4.  *      <H3>Copyright (C) Object Design Inc. 1996, 1997</H3>
  5.  *
  6.  * main program which starts three threads for three tellers. Each Teller
  7.  * performs some activities like
  8.  *   Update Account for a specific SSN 
  9.  *   Add new Account into the database
  10.  *   Remove an account depending on SSN from the database.
  11.  * The database is created in the Load object and the name of the database
  12.  * is a command line parameter. Command line for this program is :
  13.  *   java -DtellerDB=g:\testDB\teller.odb COM.odi.demo.threads.Banking
  14.  *
  15.  * The Roots in the database start from Institution/Bank. Institution Object
  16.  * contains a vector of Account holders which can be browsed through the 
  17.  * OSVector class and more elements can be added or removed from this vector.
  18.  */
  19.  
  20. import java.io.*;
  21.  
  22. import COM.odi.*;
  23. import COM.odi.util.OSVector;
  24.  
  25. public class Banking {
  26.   /* Database name. */
  27.  
  28.   public static String dbName; 
  29.  
  30.   /* Transaction used by all threads */
  31.  
  32.   public static Transaction t; 
  33.  
  34.   public static void main(String args[]) {
  35.     dbName = System.getProperty("tellerDB");
  36.     ObjectStore.initialize(null, null);
  37.  
  38.     if (dbName == null)
  39.       dbName = "teller.odb";
  40.  
  41.     /* Start with a fresh database for this demo. */
  42.  
  43.     try {
  44.       Database.open(dbName, ObjectStore.OPEN_UPDATE).destroy();
  45.     } catch (DatabaseNotFoundException e) {
  46.     }
  47.  
  48.     /* populate Database */
  49.  
  50.     new Load(dbName);
  51.  
  52.     Database db = Database.open(dbName, ObjectStore.OPEN_UPDATE);
  53.     Teller t1 = new Teller("Teller1", "Bank1", Thread.currentThread());
  54.     Teller t2 = new Teller("Teller2", "Bank1", Thread.currentThread());
  55.     Teller t3 = new Teller("Teller3", "Bank1", Thread.currentThread());
  56.     /* start threads */
  57.     t1.start();
  58.     t2.start();
  59.     t3.start();
  60.  
  61.     /* Wait for completion of all threads */
  62.     try {
  63.       t1.join();
  64.       t2.join();
  65.       t3.join();
  66.     } catch (InterruptedException e1) { }
  67.       db.close();
  68.       ObjectStore.shutdown(true);
  69.       System.exit(0);
  70.   }
  71. };
  72.  
  73. /* the thread class */
  74. class Teller extends Thread {
  75.   final int READ = 1;
  76.   final int ADD = 2;
  77.   final int UPDATE = 3;
  78.   final int REMOVE = 4;
  79.  
  80.   /* name of the teller */
  81.  
  82.   private String name; 
  83.  
  84.   private Institution inst; 
  85.  
  86.   /* Database Root same as name of the bank/Institution */
  87.  
  88.   private String bankName; 
  89.  
  90.   /* ObjectStore.initialize will be attached to this thread */
  91.  
  92.   private Thread mainThread; 
  93.  
  94.   private Database db;
  95.  
  96.   /**
  97.    * Initialize the Teller before starting the thread with teller name,
  98.    * bank name and mainThread.
  99.    * @param name The name of the Teller.
  100.    * @param bankName Name of the bank/database Root object to access.
  101.    * @mainThread pointer to the parent thread.
  102.    */
  103.   public Teller(String name, String bankName, Thread mainThread) {
  104.       super(name);
  105.       this.name = name;
  106.       this.mainThread = mainThread;
  107.       this.bankName = bankName;
  108.   }
  109.  
  110.   /**
  111.    * the thread starts and ends here
  112.    */
  113.   public void run() {
  114.     /* Thread related initializations */
  115.  
  116.     init();
  117.  
  118.     /* 
  119.      * Activities to be perfomed by this thread are read form a flat file
  120.      * called actionData.txt in the current directory.
  121.      * This file looks like this :
  122.      * 1-6(Teller name) 8(Activity) 10-18(ssn)
  123.      * Activity can be 1 = READ, 2= ADD, 3= UPDATE, 4= REMOVE
  124.      */
  125.  
  126.     BufferedReader in = null;
  127.     String actionLine;
  128.     try {
  129.       in = new BufferedReader(new FileReader("actionData.txt"), 60);
  130.     } catch(FileNotFoundException e) {
  131.       System.out.println("actionData.txt file not found");
  132.       return;
  133.     } catch(IOException e) {
  134.       System.out.println("I/O error for actionData.txt file");
  135.       return;
  136.     }
  137.     
  138.     try {
  139.       while((actionLine = in.readLine())!=null) {
  140.         /* Parse the line read from the file */
  141.  
  142.         String forName = actionLine.substring(0, 7);
  143.         int action = new Integer(actionLine.substring(8,9)).intValue();
  144.     String ssn = actionLine.substring(10, 18);
  145.  
  146.         /* if name of the current teller is same as teller in the file then
  147.          * perform activity mentioned in the file 
  148.          */
  149.  
  150.         if (name.equals(forName)) {
  151.       switch(action) {
  152.             case READ : /* Read Data */
  153.               performReads(ssn);
  154.               break;
  155.             case ADD : /* Add Data */
  156.             case UPDATE : /* update Data */
  157.             case REMOVE : /* Remove Data */
  158.               performUpdates(action, ssn);
  159.           break;
  160.             default :
  161.           System.out.println("Illegal Operation");
  162.           }
  163.         }
  164.       }
  165.     } catch (IOException e) {
  166.       System.out.println("Error reading ActionData.txt");
  167.     }
  168.     System.out.println("END THREAD :" + name);
  169.   }
  170.  
  171.   /**
  172.    * Thread related initialization.
  173.    */
  174.   private void init() {
  175.     try {
  176.       /* attach ObjectStore initialization to main thread */
  177.  
  178.       ObjectStore.initialize(mainThread);
  179.       db = Database.open(Banking.dbName, ObjectStore.OPEN_UPDATE);
  180.     } catch (ObjectStoreException e) {
  181.       System.out.println(e.getMessage());
  182.       stop();
  183.     }
  184.   }
  185.  
  186.   /**
  187.    * Get the root object and perform update activity for the Bank.
  188.    * @param flg activity could be ADD, UPDATE or REMOVE account.
  189.    * @param ssn for which this activity is performed.
  190.    */
  191.   private void performUpdates(int flg, String ssn) {
  192.     synchronized(db) {
  193.       Banking.t = Transaction.begin(ObjectStore.UPDATE);
  194.       inst = (Institution)db.getRoot(bankName);
  195.       switch(flg) {
  196.     case ADD: 
  197.       String actName = "New" + ssn;
  198.           inst.addAccount(name, actName, 36, ssn, "Savings", 1000);
  199.       break;
  200.     case REMOVE: 
  201.           inst.removeAccount(name, ssn);
  202.       break;
  203.     case UPDATE: 
  204.           inst.updateAccount(name, ssn, "DEPOSIT", 1000);
  205.       break;
  206.         }
  207.         Banking.t.commit();
  208.       }
  209.    }
  210.  
  211.   /**
  212.    * Get the root object and read Account from the Database.
  213.    * @param ssn for which Account object is needed.
  214.    */
  215.  
  216.   private void performReads(String ssn) {
  217.     synchronized(db) {
  218.       Banking.t = Transaction.begin(ObjectStore.READONLY);
  219.       inst = (Institution)db.getRoot(bankName);
  220.  
  221.       /* get the Account to be read from the institution */
  222.  
  223.       Account act = inst.getAccount(ssn);
  224.       if (act == null)
  225.         System.out.println(name + " Account for ssn:" + ssn + " does not exist");
  226.       else
  227.         act.displayAccountInfo(name);
  228.       Banking.t.commit();
  229.     }
  230.   }
  231.  
  232. }
  233.  
  234.