home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / EmployeeList.java < prev    next >
Text File  |  1997-08-23  |  2KB  |  84 lines

  1. public class EmployeeList
  2. {
  3.     LinkList fir = null;
  4.     LinkList cur = null;
  5.  
  6.     public void add(Employee e)
  7.     {
  8.         if (cur == null) fir = new LinkList(e);
  9.         else fir = fir.cons(e);
  10.         cur = fir;
  11.     }
  12.  
  13.     /**
  14.      * Sort the employee list on one of the three fields: name, section, or SS#
  15.      */
  16.  
  17.     public void sort(int fld)
  18.     {
  19.         // Use a bubble sort, for simplicity
  20.  
  21.         LinkList e = fir;
  22.         while (e != null)
  23.         {
  24.             LinkList ee = e.rest();
  25.             while (ee != null)
  26.             {
  27.                 switch (fld)
  28.                 {
  29.                 case Employee.NAME_FLD:
  30.                     if
  31.                     (
  32.                             (((Employee)(e.value())).name).
  33.                             isGreaterThan
  34.                             ((((Employee)(ee.value()))).name)
  35.                     )
  36.                         LinkList.swapValues(e, ee);
  37.                     break;
  38.                 case Employee.AGE_FLD:
  39.                     if
  40.                     (
  41.                             (((Employee)(e.value())).section).
  42.                             isGreaterThan
  43.                             ((((Employee)(ee.value()))).section)
  44.                     )
  45.                         LinkList.swapValues(e, ee);
  46.                     break;
  47.                 case Employee.SSN_FLD:
  48.                     if
  49.                     (
  50.                             (((Employee)(e.value())).ssn).
  51.                             isGreaterThan
  52.                             ((((Employee)(ee.value()))).ssn)
  53.                     )
  54.                         LinkList.swapValues(e, ee);
  55.                     break;
  56.                 }
  57.                 ee = ee.rest();
  58.             }
  59.             e = e.rest();
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * Return the first employee in the list
  65.      */
  66.  
  67.     public Employee first()
  68.     {
  69.         cur = fir;
  70.         if (cur == null) return null; else return (Employee)(cur.value());
  71.     }
  72.  
  73.     /**
  74.      * Return the next employee in the list
  75.      */
  76.  
  77.     public Employee next()
  78.     {
  79.         if (cur == null) return null; else cur = cur.rest();
  80.         if (cur == null) return null; else return (Employee)(cur.value());
  81.     }
  82. }
  83.  
  84.