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

金融证券系统

开发平台:

Java

  1. // <html><head><title>ATM Simulation Implementation - the Card Reader</title></head><body><h2>ATM Simulation Implementation - the Card Reader</h2><pre>
  2. /*
  3.  * Example ATM simulation - file CardReader.java
  4.  *
  5.  * This file implements the class that manages the ATM's Card Reader
  6.  *
  7.  * Copyright (c) 1997 - Russell C. Bjork
  8.  *
  9.  */
  10. package atm.atmparts;
  11. import java.awt.*;
  12. //</pre><hr><h3>Class CardReader</h3><pre>
  13. public class CardReader extends Button
  14.   {
  15.     //</pre><hr><pre>
  16.     public CardReader()
  17.       { super("Click to insert Card");
  18.         _status = NO_CARD;
  19.         _originalBounds = null;  // Must get this after GUI is all laid out
  20.       }
  21.       
  22.     //</pre><hr><pre>
  23.     public void ejectCard()
  24.       { // Animate card coming out of machine
  25.       
  26.         setLabel("Ejecting card");
  27.         Rectangle currentBounds = 
  28.            new Rectangle(_originalBounds.x + _originalBounds.width / 2,
  29.                          _originalBounds.y + _originalBounds.height / 2,
  30.                          _originalBounds.width / _originalBounds.height, 1);
  31.         show();
  32.         
  33.         while (currentBounds.height <= _originalBounds.height &&
  34.                currentBounds.width <= _originalBounds.width)
  35.           { reshape(currentBounds.x, currentBounds.y,
  36.                     currentBounds.width, currentBounds.height);
  37.             repaint();
  38.             try 
  39.               { Thread.sleep(100); } 
  40.             catch (InterruptedException e) 
  41.               { }
  42.             currentBounds.height += 1;
  43.             currentBounds.width = 
  44.               (_originalBounds.width * currentBounds.height) / _originalBounds.height;
  45.             currentBounds.x =
  46.               _originalBounds.x + (_originalBounds.width - currentBounds.width) / 2;
  47.             currentBounds.y =
  48.               _originalBounds.y + (_originalBounds.height - currentBounds.height) / 2;
  49.           }
  50.           
  51.         hide();
  52.                   
  53.         _status = NO_CARD; 
  54.       }
  55.     
  56.     //</pre><hr><pre>
  57.     public void retainCard()
  58.       { _status = NO_CARD;
  59.         try
  60.           { Thread.sleep(10 * 1000); }
  61.         catch (InterruptedException e)
  62.           { } 
  63.       }
  64.                 
  65.     //</pre><hr><pre>
  66.     // These are the values that can be returned by checkForCardInserted()
  67.     
  68.     public static final int NO_CARD = 0;
  69.     public static final int UNREADABLE_CARD = 1;
  70.     public static final int CARD_HAS_BEEN_READ = 2;
  71.     
  72.     //</pre><hr><pre>
  73.     public synchronized int checkForCardInserted()
  74.       { 
  75.         // Wait until user clicks the "Click To Insert Card" button, or operator
  76.         // Turns the machine off.  (The timeout in the wait ensures that we will
  77.         // return periodically so that ATM can recheck the switch status.)
  78.       
  79.         if (_originalBounds == null)
  80.             _originalBounds = bounds();
  81.         else
  82.             reshape(_originalBounds.x, _originalBounds.y,
  83.                     _originalBounds.width, _originalBounds.height);
  84.         setLabel("Click to insert card");
  85.         show();
  86.         repaint();
  87.         requestFocus();
  88.                                 
  89.         try
  90.           { wait(1000); }
  91.         catch (InterruptedException e)
  92.           { }
  93.         if (_status == NO_CARD)   // This happens if we timeout
  94.           { hide();
  95.             return NO_CARD;
  96.           }
  97.              
  98.         // Animate card going into the machine
  99.         Rectangle currentBounds =
  100.            new Rectangle(_originalBounds.x, _originalBounds.y,
  101.                          _originalBounds.width, _originalBounds.height);
  102.                          
  103.         while (currentBounds.width > 0 && currentBounds.height > 0)
  104.           { reshape(currentBounds.x, currentBounds.y,
  105.                     currentBounds.width, currentBounds.height);
  106.             repaint();
  107.             try 
  108.               { Thread.sleep(100); } 
  109.             catch (InterruptedException e) 
  110.               { }
  111.             currentBounds.height -= 1;
  112.             currentBounds.width = 
  113.               (_originalBounds.width * currentBounds.height) / _originalBounds.height;
  114.             currentBounds.x =
  115.               _originalBounds.x + (_originalBounds.width - currentBounds.width) / 2;
  116.             currentBounds.y =
  117.               _originalBounds.y + (_originalBounds.height - currentBounds.height) / 2;
  118.           }
  119.           
  120.         hide();  
  121.         // Since we don't have a magnetic stripe reader (or a literal card!),
  122.         // pop up a dialog and ask user to enter the card number.  
  123.         
  124.         QuestionDialog cardNumberDialog = 
  125.             new QuestionDialog("Enter card number:", this);
  126.         String answer = cardNumberDialog.answer();
  127.         
  128.         // Extract the number typed.  Typing a non-number simulates an unreadable
  129.         // card.
  130.         
  131.         if (answer == null)
  132.              _status = UNREADABLE_CARD;
  133.         else
  134.           { try
  135.               { _cardNumberRead = Integer.parseInt(answer); 
  136.                 _status = CARD_HAS_BEEN_READ;
  137.               }
  138.             catch (NumberFormatException e)
  139.               { _status = UNREADABLE_CARD;
  140.               }
  141.            }
  142.         return _status;
  143.       }
  144.     //</pre><hr><pre>
  145.     public int cardNumber()
  146.       { return _cardNumberRead;
  147.       }
  148.     
  149.     //</pre><hr><pre>
  150.     // Instance variables
  151.     
  152.     private int _status;
  153.     private int _cardNumberRead;    
  154.     //</pre><hr><pre>
  155.     /* The following method and field are needed for the GUI */
  156.     
  157.     public synchronized boolean action(Event e, Object arg)
  158.       { if (e.target == this)
  159.           { _status = CARD_HAS_BEEN_READ;
  160.             notify();
  161.             return true;
  162.           }
  163.         else
  164.             return false;
  165.       }
  166.       
  167.     private Rectangle _originalBounds;
  168.   }
  169. //</pre></body></html>