ATMApplet.java
上传用户:ljt780218
上传日期:2022-07-30
资源大小:110k
文件大小:2k
源码类别:

金融证券系统

开发平台:

Java

  1. // <html><head><title>ATM Simulation - applet</title></head><body><h2>ATM Simulation - applet</h2><pre>
  2. /*
  3.  * Example ATM simulation - file ATMApplet.java
  4.  *
  5.  * This file contains the main program for the ATM simulation - applet version
  6.  *
  7.  * Copyright (c) 1997 - Russell C. Bjork
  8.  *
  9.  */
  10.  
  11. import java.applet.Applet;
  12. import java.awt.*;
  13. import atm.ATM;
  14. import atm.Bank;
  15. import atm.util.Money;
  16. public class ATMApplet extends Applet implements Runnable
  17.   {
  18.     // Applet initialization.
  19.     // Create the ATM and simulated bank, plus the GUI - using this as its
  20.     // container, plus a thread to run the simulation (which executes the run()
  21.     // method of this class.)
  22.     
  23.     public void init()
  24.       { 
  25.         _theBank = new Bank();
  26.         _theATM = new ATM(ATM_NUMBER, ATM_LOCATION, _theBank, this);
  27.         
  28.         // If we are running in a frame we can get to, then set its title bar
  29.         // to our title
  30.         
  31.         Component c = this;
  32.         while (c.getParent() != null) c = c.getParent();
  33.         if (c instanceof Frame)
  34.           { ((Frame) c).setTitle("ATM number " + ATM_NUMBER + " at " + ATM_LOCATION);
  35.             ((Frame) c).setResizable(false);
  36.           }
  37.           
  38.         _theThread = new Thread(this);
  39.         _theThread.start();
  40.       }
  41.     
  42.     // stop() and start() are called as the applet is scrolled on and off the
  43.     // screen.  Simply suspend and resume the thread.
  44.     
  45.     public void start()
  46.       { _theThread.resume();
  47.       }
  48.       
  49.     public void stop()
  50.       { _theThread.suspend();
  51.       }
  52.    
  53.  
  54.     // This method is run by the thread.  Since there is no provision for an
  55.     // applet to terminate itself, we let the operator turn the machine on and
  56.     // off as often as desired.
  57.     
  58.     public void run()
  59.       {
  60.         while (true)
  61.           { Money initialCash = _theATM.startupOperation();
  62.             _theATM.serviceCustomers(initialCash);
  63.           }
  64.       }
  65.     private Bank _theBank;
  66.     private ATM _theATM;
  67.     private Thread _theThread;
  68.     
  69.     // Private constants
  70.     
  71.     private static final int ATM_NUMBER = 42;
  72.     private static final String ATM_LOCATION = "GORDON COLLEGE";      
  73.   }
  74. //</pre></body></html>