1 57 58 package inout.wsiftypes; 59 60 import inout.wsifservice.Inout; 61 import java.util.Date ; 62 import java.util.Hashtable ; 63 import java.util.Iterator ; 64 65 import util.AddressUtility; 66 67 import addressbook.wsiftypes.Address; 68 import addressbook.wsiftypes.Phone; 69 70 74 public class InoutImpl implements Inout { 75 private Hashtable name2AddressTable = new Hashtable (); 76 77 public InoutImpl() { 78 addEntry( 79 "John B. Good", 80 new Address( 81 123, 82 "Main Street", 83 "Anytown", 84 "NY", 85 12345, 86 new Phone(123, "456", "7890"))); 87 addEntry( 88 "Bob Q. Public", 89 new Address( 90 456, 91 "North Whatever", 92 "Notown", 93 "ME", 94 12424, 95 new Phone(987, "444", "5566"))); 96 } 97 98 public void addEntry(String name, Address address) { 99 if (name != null && address != null) 100 name2AddressTable.put(name, address); 101 } 102 103 public void addEntry(String firstName, String lastName, Address address) { 104 if (firstName != null && lastName != null && address != null) 105 name2AddressTable.put(firstName + " " + lastName, address); 106 } 107 108 public Address getAddressFromName(String name) 109 throws IllegalArgumentException { 110 if (name == null) 111 return null; 112 return getAddressFromName(new Mutablestring(name)); 113 } 114 115 public Address getAddressFromName(Mutablestring name) 116 throws IllegalArgumentException { 117 if (name == null) 118 return null; 119 120 String found = null; 121 int star = name.toString().indexOf("*"); 122 if (star != -1) { 123 String trimmed = name.toString().substring(0, star); 124 Iterator it = name2AddressTable.keySet().iterator(); 125 while (it.hasNext()) { 126 String key = (String ) it.next(); 127 if (key.startsWith(trimmed)) { 128 found = key; 129 break; 130 } 131 } 132 133 if (found == null) 134 throw new IllegalArgumentException ( 135 "Couldn't find " + name + " trimmed=" + trimmed); 136 } else 137 found = name.toString(); 138 139 return (Address) name2AddressTable.get(found); 140 } 141 142 public boolean getAddressFromName(Mutablestring name, Address addr) 143 throws IllegalArgumentException { 144 Address newAddr = getAddressFromName(name); 145 if (newAddr == null) 146 return false; 147 AddressUtility addrUtil = new AddressUtility(newAddr); 148 addrUtil.copy(addr); 149 return true; 150 } 151 152 public int addNumbers(int[] nums) { 153 int result = 0; 154 for (int i = 0; i < nums.length; i++) 155 result += nums[i]; 156 return result; 157 } 158 159 public Date getDate() { 160 return new Date (); 161 } 162 163 public String whoami(String s) { 164 return new String ("String"); 165 } 166 public String whoami(float f) { 167 return new String ("float"); 168 } 169 public String whoami(int i) { 170 return new String ("int"); 171 } 172 public String whoami(Address a) { 173 return new String ("Address"); 174 } 175 } 176 | Popular Tags |