1 16 package org.getahead.dwrdemo.people; 17 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.Random ; 21 import java.util.Set ; 22 23 import org.directwebremoting.util.Logger; 24 25 29 public class People 30 { 31 34 public People() 35 { 36 log.debug("Generating a new set of random people"); 37 for (int i = 0; i < 5; i++) 38 { 39 people.add(getRandomPerson()); 40 } 41 } 42 43 47 public Set getAllPeople() 48 { 49 return people; 50 } 51 52 56 public void setPerson(Person person) 57 { 58 log.debug("Adding person: " + person); 59 if (person.getId() == -1) 60 { 61 person.setId(getNextId()); 62 } 63 64 people.remove(person); 65 people.add(person); 66 } 67 68 72 public void deletePerson(Person person) 73 { 74 log.debug("Removing person: " + person); 75 people.remove(person); 76 debug(); 77 } 78 79 82 private Set people = new HashSet (); 83 84 88 private Person getRandomPerson() 89 { 90 Person person = new Person(); 91 person.setId(getNextId()); 92 93 String firstname = FIRSTNAMES[random.nextInt(FIRSTNAMES.length)]; 94 String surname = SURNAMES[random.nextInt(SURNAMES.length)]; 95 person.setName(firstname + " " + surname); 96 97 String housenum = (random.nextInt(99) + 1) + " "; 98 String road1 = ROADS1[random.nextInt(ROADS1.length)]; 99 String road2 = ROADS2[random.nextInt(ROADS2.length)]; 100 String town = TOWNS[random.nextInt(TOWNS.length)]; 101 String address = housenum + road1 + " " + road2 + ", " + town; 102 person.setAddress(address); 103 104 float salary = Math.round(10 + 90 * random.nextFloat()) * 1000; 105 person.setSalary(salary); 106 107 return person; 108 } 109 110 113 protected void debug() 114 { 115 for (Iterator it = people.iterator(); it.hasNext();) 116 { 117 Person person = (Person) it.next(); 118 log.debug(person.toString()); 119 } 120 } 121 122 126 private static synchronized int getNextId() 127 { 128 return nextId++; 129 } 130 131 134 private static int nextId = 1; 135 136 private Random random = new Random (); 137 138 private static final String [] FIRSTNAMES = 139 { 140 "Fred", "Jim", "Shiela", "Jack", "Betty", "Jacob", "Martha", "Kelly", 141 "Luke", "Matt", "Gemma", "Joe", "Ben", "Jessie", "Leanne", "Becky", 142 }; 143 144 private static final String [] SURNAMES = 145 { 146 "Sutcliffe", "MacDonald", "Duckworth", "Smith", "Wisner", "Iversen", 147 "Nield", "Turton", "Trelfer", "Wilson", "Johnson", "Cowan", "Daniels", 148 }; 149 150 private static final String [] ROADS1 = 151 { 152 "Green", "Red", "Yellow", "Brown", "Blue", "Black", "White", 153 }; 154 155 private static final String [] ROADS2 = 156 { 157 "Close", "Drive", "Street", "Avenue", "Crescent", "Road", "Place", 158 }; 159 160 private static final String [] TOWNS = 161 { 162 "Birmingham", "Kettering", "Paris", "San Francisco", "New York", 163 "San Mateo", "Barcelona", 164 }; 165 166 169 private static final Logger log = Logger.getLogger(People.class); 170 } 171
| Popular Tags
|