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 / Banking1.java < prev    next >
Encoding:
Java Source  |  1997-04-23  |  5.0 KB  |  194 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 two threads 
  7.  * One thread computes total number of children under 10 having amount more
  8.  * than 10000 in the bank.
  9.  * Second thread computes total number of poeple having more than $10000 and
  10.  * total of such amount.
  11.  *
  12.  * The Database is created in the Load object and the name of the Database
  13.  * is a command line parameter. Command line for this program is :
  14.  *    java -DtellerDB=g:\testDB\teller.odb COM.odi.demo.threads.Banking1
  15.  * if the Database already exists with this name then the Database is not created.
  16.  * The Roots in the Database start from Institution/Bank . Institution Object
  17.  * contains a vector of Account holders which can be browsed through the 
  18.  * OSVector class.
  19.  */
  20.  
  21. import COM.odi.*;
  22. import java.util.Enumeration;
  23. import COM.odi.util.OSVector;
  24.  
  25.  
  26. public class Banking1 {
  27.   /* Activities */
  28.   /* Sum up children <= 10 and amount >=10000 in the account */
  29.   public static final int CHILD = 1;
  30.  
  31.   /* compute number of Account holders having amount >= 10000 and sum of this
  32.      amount.
  33.    */
  34.   public static final int ACT10 = 2;
  35.  
  36.   /* Database name */
  37.   public static String dbName;
  38.  
  39.   public static void main(String args[]) {
  40.  
  41.     dbName = System.getProperty("tellerDB");
  42.     ObjectStore.initialize(null, null);
  43.  
  44.     if (dbName == null)
  45.       dbName = "teller.odb";
  46.  
  47.     /* populate Database */
  48.  
  49.     new Load(dbName);
  50.  
  51.     Database db = Database.open(dbName, ObjectStore.OPEN_READONLY);
  52.     Transaction t = Transaction.begin(ObjectStore.READONLY);
  53.     Compute t1 = new Compute("Sum", "Bank1", Thread.currentThread(), CHILD);
  54.     Compute t2 = new Compute("Children", "Bank1", Thread.currentThread(), ACT10);
  55.  
  56.     /* start threads */
  57.  
  58.     t1.start();
  59.     t2.start();
  60.  
  61.     /* wait for completion of all threads then commit the transaction */
  62.  
  63.     try {
  64.       t1.join();
  65.       t2.join();
  66.     } catch (InterruptedException e1) { }
  67.     t.commit();
  68.     db.close();
  69.     ObjectStore.shutdown(true);
  70.     System.exit(0);
  71.   }
  72.  
  73. };
  74.  
  75. /* the thread class */
  76.  
  77. class Compute extends Thread {
  78.   /* name of the thread */
  79.  
  80.   private String name; 
  81.  
  82.   private Institution inst; 
  83.  
  84.   /* name of the bank same as database root name */
  85.  
  86.   private String bankName; 
  87.  
  88.   /* ObjectStore.initialize will be attached to this thread */
  89.  
  90.   private Thread mainThread;
  91.  
  92.   /* Database Object */
  93.  
  94.   private Database db;
  95.  
  96.   /* action to be taken in this thread */
  97.  
  98.   private int action;
  99.  
  100.   /**
  101.    * Initialize the Compute object before starting the thread.
  102.    * @param name The name of the thread.
  103.    * @param bankName Name of the bank/database Root object to access.
  104.    * @mainThread pointer to the parent thread.
  105.    */
  106.   public Compute(String name, String bankName, Thread mainThread, int action) {
  107.     super(name);
  108.     this.name = name;
  109.     this.mainThread = mainThread;
  110.     this.bankName = bankName;
  111.     this.action = action;
  112.   }
  113.  
  114.   /**
  115.    * The thread starts and ends here.
  116.    */
  117.   public void run() {
  118.     
  119.     init();
  120.  
  121.     /* depending on name the Thread performs different activity */
  122.  
  123.     if (action == Banking1.ACT10) {
  124.       accountsMoreThan10k();
  125.     } else if (action == Banking1.CHILD) {
  126.       childrenUnder10AndMoreThan10k();
  127.     }
  128.       System.out.println("END THREAD :" + name);
  129.    }
  130.  
  131.   /**
  132.    * Thread related initialization.
  133.    */
  134.   private void init() {
  135.     try {
  136.       /* attach ObjectStore initialization to main thread */
  137.       ObjectStore.initialize(mainThread);
  138.       db = Database.open(Banking1.dbName, ObjectStore.OPEN_READONLY);
  139.     } catch (ObjectStoreException e) {
  140.       System.out.println(e.getMessage());
  141.       stop();
  142.     }
  143.   }
  144.  
  145.   /**
  146.    * Find total number of accounts with more than 10K in their accounts,
  147.    * sum this amount and display each account maching the criteria
  148.    */
  149.   private void accountsMoreThan10k() {
  150.     int numberOfAccounts = 0;
  151.     float totalAmount = 0F;
  152.     float amt;
  153.  
  154.     inst = (Institution)db.getRoot(bankName);
  155.  
  156.     /* get the Account to be read from the institution */
  157.     Enumeration e = inst.getAccountEnum();
  158.     Account act;
  159.     while( e.hasMoreElements()) {
  160.       act = (Account)e.nextElement();
  161.       if((amt = act.getBalAmount()) >= 10000) {
  162.     totalAmount += amt;
  163.     numberOfAccounts += 1;
  164.     act.displayAccountInfo(name);
  165.       }
  166.     }
  167.     System.out.println("Number Of Accounts having More than $10K " + numberOfAccounts);
  168.     System.out.println("Sum of amount in Accounts having More than $10K " + totalAmount);
  169.   }
  170.  
  171.   /**
  172.    * Find total number of accounts with more than 10K in their accounts
  173.    * and age under 10 and display each account maching the criteria.
  174.    */
  175.   private void childrenUnder10AndMoreThan10k() {
  176.     int numberOfChildren = 0;
  177.  
  178.     inst = (Institution)db.getRoot(bankName);
  179.  
  180.     Enumeration e = inst.getAccountEnum();
  181.     Account act;
  182.     while( e.hasMoreElements()) {
  183.       act = (Account)e.nextElement();
  184.       if((act.getAge() < 10) && (act.getBalAmount() >= 10000)) {
  185.     act.displayAccountInfo(name);
  186.     numberOfChildren += 1;
  187.       }
  188.     }
  189.     System.out.println("Number Of children having More than $10K and age less than 10 " + numberOfChildren);
  190.   }
  191.  
  192. }
  193.  
  194.