home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / examples / namedb / RoloApi.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  7.0 KB  |  260 lines

  1. /* ****************************************************************
  2. ** @(#)RoloApi.java    1.3 0
  3. **
  4. ** Copyright 1997 Sun Microsystems, Inc. All Rights Reserved
  5. **
  6. ** ****************************************************************
  7. */
  8.  
  9. import java.io.*;
  10. import java.util.Vector;
  11. import java.util.StringTokenizer;
  12.  
  13. public class RoloApi {
  14.  
  15.   protected SortedList sl;
  16.   public RoloEntry recurrent;
  17.   public int current;
  18.   public File filename;
  19.   public boolean saved;
  20.  
  21.   private Vector search;
  22.  
  23.   public RoloApi() {
  24.     sl = new SortedList();
  25.     filename=null;
  26.     saved=false;
  27.     current= -1;
  28.     recurrent=null;
  29.   }
  30.  
  31.   public void open(File file) {
  32.     sl = new SortedList();
  33.     if (sl.load(file, RoloEntry.roloobj)==true) {
  34.       filename=file;
  35.       saved=true;
  36.       current=0;
  37.       updateCurrent();
  38.     }
  39.   }
  40.  
  41.   public void updateCurrent() {
  42.     recurrent=getEntry(current);
  43.   }
  44.  
  45.   public void save() {
  46.     sl.save(filename);
  47.   }
  48.  
  49.   public RoloEntry getEntry(int key) {
  50.     RoloEntry rolo;
  51.     if (key== -1 || key>=getSize())
  52.       return new RoloEntry();
  53.     rolo = (RoloEntry)sl.getItem(key);
  54.     return rolo;
  55.   }
  56.  
  57.   public int addEntry( String name, String address1, String address2,
  58.             String phone, String email, String other) {
  59.     recurrent=new RoloEntry( name, address1, address2, phone, email, other);
  60.     return putEntry( recurrent );
  61.   }
  62.  
  63.   public int putEntry(RoloEntry rolo) {
  64.     RoloEntry r=(RoloEntry)rolo.clone();
  65.     return sl.putItem(r.name, new ListItem(r.name,r));
  66.   }
  67.  
  68.   public void showEntry(int i) {
  69.     current=i;
  70.     updateCurrent();
  71.   }
  72.  
  73.   public void removeEntry(int key) {
  74.       sl.removeItem(key);
  75.   }
  76.  
  77.   public int getSize() {
  78.     return sl.size();
  79.   }
  80.  
  81.   public String[] getAllKeys() {
  82.     String[] keys;
  83.     keys = sl.getKeys();
  84.     return keys;
  85.   }
  86.  
  87.   private void addparam(int sel,String contains) {
  88.     int i;
  89.     String s;
  90.     StringTokenizer st=new StringTokenizer( contains );
  91.     while (st.hasMoreTokens()) {
  92.       s=st.nextToken().toLowerCase();
  93.       if (sel==0) {
  94.     for(i=0;i<6;i++)
  95.       ((Vector)search.elementAt(i)).addElement(s);
  96.       } else {
  97.     ((Vector)search.elementAt(sel-1)).addElement(s);
  98.       }
  99.     }
  100.   }
  101.  
  102.   public Vector searchIt(int cat, String searchStr) {
  103.     int ientry,isearch;
  104.     String match;
  105.     boolean found;
  106.     Vector foundindex=new Vector();
  107.  
  108.     search=new Vector(6);
  109.     for(int i=0;i<7;i++) {
  110.       search.addElement(new Vector());
  111.     }
  112.  
  113.     addparam(cat,searchStr);
  114.     RoloEntry entry;
  115.     for (ientry=0;ientry<getSize();ientry++) {
  116.       found=false;
  117.       entry=getEntry( ientry );
  118.  
  119.       for (isearch=0;isearch<((Vector)search.elementAt(0)).size();isearch++) {
  120.     match=(String)((Vector)search.elementAt(0)).elementAt(isearch);
  121.     if (entry.name.toLowerCase().indexOf(match)!=(-1)) {
  122.       found=true;
  123.       break;
  124.     }
  125.       }
  126.  
  127.       if (found==false) {
  128.     for (isearch=0;isearch<((Vector)search.elementAt(1)).size();isearch++) {
  129.       match=(String)((Vector)search.elementAt(1)).elementAt(isearch);
  130.       if (entry.address1.toLowerCase().indexOf(match)!=(-1)) {
  131.         found=true;
  132.         break;
  133.       }
  134.     }
  135.       }
  136.  
  137.       if (found==false) {
  138.     for (isearch=0;isearch<((Vector)search.elementAt(2)).size();isearch++) {
  139.       match=(String)((Vector)search.elementAt(2)).elementAt(isearch);
  140.       if (entry.address2.toLowerCase().indexOf(match)!=(-1)) {
  141.         found=true;
  142.         break;
  143.       }
  144.     }
  145.       }
  146.       if (found==false) {
  147.     for (isearch=0;isearch<((Vector)search.elementAt(3)).size();isearch++) {
  148.       match=(String)((Vector)search.elementAt(3)).elementAt(isearch);
  149.       if (entry.phone.toLowerCase().indexOf(match)!=(-1)) {
  150.         found=true;
  151.         break;
  152.       }
  153.     }
  154.       }
  155.  
  156.       if (found==false) {
  157.     for (isearch=0;isearch<((Vector)search.elementAt(4)).size();isearch++) {
  158.       match=(String)((Vector)search.elementAt(4)).elementAt(isearch);
  159.       if (entry.email.toLowerCase().indexOf(match)!=(-1)) {
  160.         found=true;
  161.         break;
  162.       }
  163.     }
  164.       }
  165.       if (found==false) {
  166.     for (isearch=0;isearch<((Vector)search.elementAt(5)).size();isearch++) {
  167.       match=(String)((Vector)search.elementAt(5)).elementAt(isearch);
  168.       if (entry.other.toLowerCase().indexOf(match)!=(-1)) {
  169.         found=true;
  170.         break;
  171.       }
  172.     }
  173.       }
  174.       if (found==true) {
  175.     foundindex.addElement(new Integer(ientry));
  176.       }
  177.     }
  178.     return foundindex;
  179.  }
  180.   /**
  181.    * Converts the salary from String to double.  If not a valid String
  182.    * InvalidSalary exception is thrown.  Rules:  Must have only [$,.0-9]
  183.    * characters in string.  $ Must be 1st character.  , must have at
  184.    * at least 1 character to its left and 3 characters to its right.
  185.    * . must have exactly 2 digits to its right.
  186.    * @param salary  the salary as a String
  187.    * @returns the salary in double format
  188.    * @exception InvalidSalary  if the string cannot be converted to
  189.    * a number
  190.    */
  191.  
  192.   public double processSalary (String salary) throws InvalidSalary {
  193.     char ch;
  194.     int tmp;
  195.     // check for invalid string
  196.     if (salary==null)
  197.       throw new InvalidSalary("invalid string was null or empty");
  198.     // check for empty string
  199.     if (salary.length()==0)
  200.       return 0;
  201.  
  202.     // remove all trailing and leading spaces
  203.     String total=salary;
  204.     StringTokenizer st=new StringTokenizer(total);
  205.     salary=st.nextToken();
  206.     if (st.hasMoreTokens())
  207.       throw new InvalidSalary("invalid salary has spaces separating numbers");
  208.  
  209.     // check for invalid characters anything besides $,.[0-9]
  210.     for (int i=0;i<salary.length();i++){
  211.       tmp=salary.charAt(i);
  212.       if (tmp!='$' && tmp!='.' && tmp!=',' && (tmp<'0' || tmp>'9'))
  213.     throw new InvalidSalary("invalid salary contains characters other than [$,.0-9] '"+salary+"'");
  214.     }
  215.     // check for $
  216.     if (salary.indexOf('$')!= -1 && salary.indexOf('$')!= 0) {
  217.       throw new InvalidSalary("invalid salary the $ is not at the beginning");
  218.     }
  219.     if (salary.indexOf('$')==0) {
  220.       salary=salary.substring(1);
  221.     }
  222.     // remove commas
  223.     while (salary.indexOf(',')!= -1) {
  224.       String temp=salary;
  225.       int itmp=salary.indexOf(',');
  226.       salary=temp.substring(0,itmp)+temp.substring(itmp+1);
  227.     }
  228.     // verify only 1 . and only 2 decimal places may follow
  229.     if (salary.indexOf('.')!= -1) {
  230.       int loc=salary.indexOf('.');
  231.       if (salary.indexOf('.',loc+1) != -1)
  232.     throw new InvalidSalary("must only have 1 '.'");
  233.       if (loc!=salary.length()-3)
  234.     throw new InvalidSalary("must have cents if use decimal place");
  235.     }
  236.  
  237.     double ret;
  238.     try {
  239.       ret=new Double(salary).doubleValue();
  240.     } catch (NumberFormatException e) {
  241.       throw new InvalidSalary("Invalid NumberFormat");
  242.     }
  243.     return ret;
  244.   }
  245.   public static void main(String args[]) {
  246.     RoloApi r=new RoloApi();
  247.     String s;
  248.     double result=0;
  249.     for (int i=0;i<args.length;i++) {
  250.       try {
  251.     result=r.processSalary(args[0]);
  252.       }
  253.       catch (InvalidSalary e) {
  254.     System.out.println(e);
  255.       }
  256.       System.out.println("result="+result);
  257.     }
  258.   }
  259. }
  260.