using System; using System.Collections; using System.Text; namespace babyApp { /* the AddressBook * Ok here is where we interact with the AddrCollection. We will addOne, addMulti * deleteOne, deleteMulti, burnTheBook, modifyOne, and view one of the * addresses in the AddrBook. */ class AddrBook : AddrCollection { /* var to hold the collection of Addr we will work with * * @visible private * @type AddrBook */ private AddrBook book; /* constructor * make the address book */ public AddrBook() { AddrBook book = new AddrBook(); } /* Add one address to the book. * Currently implemented as an array where the data is held in specific * elements of the array. Current format of the array required: * * [0] => name * [1] => address * [2] => phone number * [3] => website address * * @TODO make this more flexable * @param string[] * @return bool */ public bool Add(string[] addr) { // if the name isn't empty if (addr[0] != "") { Addr entry = new Addr(addr[0], addr[1], addr[2], addr[3]); book.Add(entry); return true; } else { return false; } } /* Add multiple addresses to the book * Takes in an ArrayList and as long as there is a name * provided will add the values to the book. The ArrayList * for now will have to be in a fixed format so I can work * with it. Current format needed: * * [0] => array ( // addr 1 to be added * [0] => name, * [1] => address, * [2] => phone, * [3] => website, * ), * [1] => array ( // addr 2 to be added * [0] => name, * [1] => address, * [2] => phone, * [3] => website, * ) * * Will look to see if a name is provided - if it is will insert the addr data * otherwise will move on to next entry * * @TODO make this more flexable * @param Arraylist * @return bool - if at least one entry was added it will return true - otherwise false * * * **** Watch to make sure the Addr conversion works correctly */ public bool addMulti(ArrayList list) { bool oneAdded = false; Addr addr = new Addr(); for (int i = 0; i < list.Count; i++) { addr = (Addr)list[i]; if (addr.name != "" ) { book.Add(addr); oneAdded = true; } } return oneAdded; } /* Delete one address from the book * Delete the Addr entry for the name provided. * * @param string name * @return void * @calls _search */ public void deleteOne(string name) { int index = _search(name); if (index != -1 && index < book.Count) { book.Remove(book[index]); } } /* Delete multiple entries in the AddrBook * pass in an array of names and their entries in the * AddrBook will be removed. * * @param string[] names * @return void */ public void deleteMulti(string[] names) { foreach (string name in names) { int index = _search(name); if (index > 0 && index < book.Count) { book.Remove(book[index]); } } } /* Burn the Book * While I don't normally agree with people burning books for * any reason - it just seemed like a fun way to clear the * AddrBook of all it's entries. * * @param AddrBook - the address book you want to empty of entries * @return void */ public void burnTheBook(AddrBook burn) { book.Clear(); } /* Modify the entry for the given name * This will allow you update the entries in the * AddrBook in case someone moves, changes their phone * number and what not since life rarely stands still. * * @TODO make this more flexable * @param string name - the name you will be editing * @param string[] values - a fixed format array containing * the values that are to be edited. * For values that you do not wish * to change - just leave an empty * string in that position. * Format of the array: * [0] => address, * [1] => phone, * [2] => website * * @return bool - true if entry edited, false otherwise */ public bool modifyOne(string name, string[] values) { int index = _search(name); if (index > 0 && index < book.Count) { book[index].address = (values[0] != "") ? values[0] : book[index].address; book[index].phone = (values[1] != "") ? values[1] : book[index].phone; book[index].website = (values[2] != "") ? values[2] : book[index].website; return true; } return false; } /* View only one of the entries * Using the name of the entry - go find it and return that * Addr entry to the user for viewing. * * @param string name * @return Addr */ public Addr viewOne(string name) { int index = _search(name); return book[index]; } /* go find the entry * A private function used to find the entry in the * AddrBook for a given name and return it's index in * the AddrCollection. * * @param string name to search for * @return returns the index of the entry */ private int _search(string name) { for (int index = 0; index < book.Count; index++) { if (book[index].name == name) { return index; } } return -1; } } }