home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-02-11 | 7.0 KB | 260 lines |
- /* ****************************************************************
- ** @(#)RoloApi.java 1.3 0
- **
- ** Copyright 1997 Sun Microsystems, Inc. All Rights Reserved
- **
- ** ****************************************************************
- */
-
- import java.io.*;
- import java.util.Vector;
- import java.util.StringTokenizer;
-
- public class RoloApi {
-
- protected SortedList sl;
- public RoloEntry recurrent;
- public int current;
- public File filename;
- public boolean saved;
-
- private Vector search;
-
- public RoloApi() {
- sl = new SortedList();
- filename=null;
- saved=false;
- current= -1;
- recurrent=null;
- }
-
- public void open(File file) {
- sl = new SortedList();
- if (sl.load(file, RoloEntry.roloobj)==true) {
- filename=file;
- saved=true;
- current=0;
- updateCurrent();
- }
- }
-
- public void updateCurrent() {
- recurrent=getEntry(current);
- }
-
- public void save() {
- sl.save(filename);
- }
-
- public RoloEntry getEntry(int key) {
- RoloEntry rolo;
- if (key== -1 || key>=getSize())
- return new RoloEntry();
- rolo = (RoloEntry)sl.getItem(key);
- return rolo;
- }
-
- public int addEntry( String name, String address1, String address2,
- String phone, String email, String other) {
- recurrent=new RoloEntry( name, address1, address2, phone, email, other);
- return putEntry( recurrent );
- }
-
- public int putEntry(RoloEntry rolo) {
- RoloEntry r=(RoloEntry)rolo.clone();
- return sl.putItem(r.name, new ListItem(r.name,r));
- }
-
- public void showEntry(int i) {
- current=i;
- updateCurrent();
- }
-
- public void removeEntry(int key) {
- sl.removeItem(key);
- }
-
- public int getSize() {
- return sl.size();
- }
-
- public String[] getAllKeys() {
- String[] keys;
- keys = sl.getKeys();
- return keys;
- }
-
- private void addparam(int sel,String contains) {
- int i;
- String s;
- StringTokenizer st=new StringTokenizer( contains );
- while (st.hasMoreTokens()) {
- s=st.nextToken().toLowerCase();
- if (sel==0) {
- for(i=0;i<6;i++)
- ((Vector)search.elementAt(i)).addElement(s);
- } else {
- ((Vector)search.elementAt(sel-1)).addElement(s);
- }
- }
- }
-
- public Vector searchIt(int cat, String searchStr) {
- int ientry,isearch;
- String match;
- boolean found;
- Vector foundindex=new Vector();
-
- search=new Vector(6);
- for(int i=0;i<7;i++) {
- search.addElement(new Vector());
- }
-
- addparam(cat,searchStr);
- RoloEntry entry;
- for (ientry=0;ientry<getSize();ientry++) {
- found=false;
- entry=getEntry( ientry );
-
- for (isearch=0;isearch<((Vector)search.elementAt(0)).size();isearch++) {
- match=(String)((Vector)search.elementAt(0)).elementAt(isearch);
- if (entry.name.toLowerCase().indexOf(match)!=(-1)) {
- found=true;
- break;
- }
- }
-
- if (found==false) {
- for (isearch=0;isearch<((Vector)search.elementAt(1)).size();isearch++) {
- match=(String)((Vector)search.elementAt(1)).elementAt(isearch);
- if (entry.address1.toLowerCase().indexOf(match)!=(-1)) {
- found=true;
- break;
- }
- }
- }
-
- if (found==false) {
- for (isearch=0;isearch<((Vector)search.elementAt(2)).size();isearch++) {
- match=(String)((Vector)search.elementAt(2)).elementAt(isearch);
- if (entry.address2.toLowerCase().indexOf(match)!=(-1)) {
- found=true;
- break;
- }
- }
- }
- if (found==false) {
- for (isearch=0;isearch<((Vector)search.elementAt(3)).size();isearch++) {
- match=(String)((Vector)search.elementAt(3)).elementAt(isearch);
- if (entry.phone.toLowerCase().indexOf(match)!=(-1)) {
- found=true;
- break;
- }
- }
- }
-
- if (found==false) {
- for (isearch=0;isearch<((Vector)search.elementAt(4)).size();isearch++) {
- match=(String)((Vector)search.elementAt(4)).elementAt(isearch);
- if (entry.email.toLowerCase().indexOf(match)!=(-1)) {
- found=true;
- break;
- }
- }
- }
- if (found==false) {
- for (isearch=0;isearch<((Vector)search.elementAt(5)).size();isearch++) {
- match=(String)((Vector)search.elementAt(5)).elementAt(isearch);
- if (entry.other.toLowerCase().indexOf(match)!=(-1)) {
- found=true;
- break;
- }
- }
- }
- if (found==true) {
- foundindex.addElement(new Integer(ientry));
- }
- }
- return foundindex;
- }
- /**
- * Converts the salary from String to double. If not a valid String
- * InvalidSalary exception is thrown. Rules: Must have only [$,.0-9]
- * characters in string. $ Must be 1st character. , must have at
- * at least 1 character to its left and 3 characters to its right.
- * . must have exactly 2 digits to its right.
- * @param salary the salary as a String
- * @returns the salary in double format
- * @exception InvalidSalary if the string cannot be converted to
- * a number
- */
-
- public double processSalary (String salary) throws InvalidSalary {
- char ch;
- int tmp;
- // check for invalid string
- if (salary==null)
- throw new InvalidSalary("invalid string was null or empty");
- // check for empty string
- if (salary.length()==0)
- return 0;
-
- // remove all trailing and leading spaces
- String total=salary;
- StringTokenizer st=new StringTokenizer(total);
- salary=st.nextToken();
- if (st.hasMoreTokens())
- throw new InvalidSalary("invalid salary has spaces separating numbers");
-
- // check for invalid characters anything besides $,.[0-9]
- for (int i=0;i<salary.length();i++){
- tmp=salary.charAt(i);
- if (tmp!='$' && tmp!='.' && tmp!=',' && (tmp<'0' || tmp>'9'))
- throw new InvalidSalary("invalid salary contains characters other than [$,.0-9] '"+salary+"'");
- }
- // check for $
- if (salary.indexOf('$')!= -1 && salary.indexOf('$')!= 0) {
- throw new InvalidSalary("invalid salary the $ is not at the beginning");
- }
- if (salary.indexOf('$')==0) {
- salary=salary.substring(1);
- }
- // remove commas
- while (salary.indexOf(',')!= -1) {
- String temp=salary;
- int itmp=salary.indexOf(',');
- salary=temp.substring(0,itmp)+temp.substring(itmp+1);
- }
- // verify only 1 . and only 2 decimal places may follow
- if (salary.indexOf('.')!= -1) {
- int loc=salary.indexOf('.');
- if (salary.indexOf('.',loc+1) != -1)
- throw new InvalidSalary("must only have 1 '.'");
- if (loc!=salary.length()-3)
- throw new InvalidSalary("must have cents if use decimal place");
- }
-
- double ret;
- try {
- ret=new Double(salary).doubleValue();
- } catch (NumberFormatException e) {
- throw new InvalidSalary("Invalid NumberFormat");
- }
- return ret;
- }
- public static void main(String args[]) {
- RoloApi r=new RoloApi();
- String s;
- double result=0;
- for (int i=0;i<args.length;i++) {
- try {
- result=r.processSalary(args[0]);
- }
- catch (InvalidSalary e) {
- System.out.println(e);
- }
- System.out.println("result="+result);
- }
- }
- }
-