|
| 1 | +package chapter_10; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | + |
| 5 | +public class AddressBook { |
| 6 | + private static final int DEFAULT_SIZE = 25; |
| 7 | + private Person[] entry; |
| 8 | + private int count; |
| 9 | + |
| 10 | + public AddressBook(){ |
| 11 | + this(DEFAULT_SIZE); |
| 12 | + } |
| 13 | + |
| 14 | + |
| 15 | + public void expand(){ |
| 16 | + //create a new array whose size is 150% of the current array |
| 17 | + int newLength = (int) (1.5 * entry.length); |
| 18 | + Person[] temp = new Person[newLength]; |
| 19 | + |
| 20 | + //copy the data to the new array |
| 21 | + |
| 22 | + for(int i = 0 ; i < entry.length ; i++) { |
| 23 | + temp[i] = entry[i]; |
| 24 | + } |
| 25 | + |
| 26 | + //set the variable entry to point to the new array |
| 27 | + entry = temp; |
| 28 | + |
| 29 | + System.out.println("Inside the method enlarge."); //TEMP |
| 30 | + |
| 31 | + System.out.println("Size of a new array : " + entry.length); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + public void add (Person newPerson) { |
| 36 | + assert count >= 0 && |
| 37 | + count <= entry.length; |
| 38 | + |
| 39 | + if(count == entry.length) { |
| 40 | + expand(); |
| 41 | + } |
| 42 | + |
| 43 | + entry[count] = newPerson; |
| 44 | + count++; |
| 45 | + } |
| 46 | + |
| 47 | + public AddressBook(int size){ |
| 48 | + count = 0; |
| 49 | + if(size <= 0) { |
| 50 | + throw new IllegalArgumentException("Size must be positive."); |
| 51 | + } |
| 52 | + entry = new Person[size]; |
| 53 | + |
| 54 | + System.out.println("Array of " + size + " is created."); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public Person searchName (String searchName) { |
| 59 | + Person foundPerson; |
| 60 | + int loc = 0 ; |
| 61 | + |
| 62 | + assert count >= 0 && count<= entry.length; |
| 63 | + |
| 64 | + while(loc < count && !searchName.equals(entry[loc].getName())) { |
| 65 | + loc++; |
| 66 | + }; |
| 67 | + |
| 68 | + if(loc == count) { |
| 69 | + foundPerson = null; |
| 70 | + }else { |
| 71 | + foundPerson = entry[loc]; |
| 72 | + } |
| 73 | + |
| 74 | + return foundPerson; |
| 75 | + } |
| 76 | + |
| 77 | + public Person searchAge (int searchAge) { |
| 78 | + Person foundPerson; |
| 79 | + int loc = 0 ; |
| 80 | + |
| 81 | + assert count >= 0 && count<= entry.length; |
| 82 | + |
| 83 | + while(loc < count && searchAge != entry[loc].getAge()) { |
| 84 | + loc++; |
| 85 | + }; |
| 86 | + |
| 87 | + if(loc == count) { |
| 88 | + foundPerson = null; |
| 89 | + }else { |
| 90 | + foundPerson = entry[loc]; |
| 91 | + } |
| 92 | + |
| 93 | + return foundPerson; |
| 94 | + } |
| 95 | + |
| 96 | + public Person searchGender (char searchGender) { |
| 97 | + Person foundPerson; |
| 98 | + int loc = 0 ; |
| 99 | + |
| 100 | + assert count >= 0 && count<= entry.length; |
| 101 | + |
| 102 | + while(loc < count && searchGender != entry[loc].getGender()) { |
| 103 | + loc++; |
| 104 | + }; |
| 105 | + |
| 106 | + if(loc == count) { |
| 107 | + foundPerson = null; |
| 108 | + }else { |
| 109 | + foundPerson = entry[loc]; |
| 110 | + } |
| 111 | + |
| 112 | + return foundPerson; |
| 113 | + } |
| 114 | + |
| 115 | + // exercise 14 |
| 116 | + public ArrayList<Person> searchMultiple(String searchName) { |
| 117 | + ArrayList<Person> foundPerson = new ArrayList<Person>(); |
| 118 | + |
| 119 | + assert count >= 0 && count < entry.length; |
| 120 | + |
| 121 | + for (int i = 0 ; i< entry.length ; i++) { |
| 122 | + if(searchName.equals(entry[i].getName())) { |
| 123 | + foundPerson.add(entry[i]); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return foundPerson; |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +} |
0 commit comments