java - How can I Enhance my AddressBook addEntry() method? -
i have address book program [1] adds entry [2] delete entry [3] update/edit entry [4] view entry , [5] view specific entry..
entries stored in array entry[]
like:
entry[counter] = new addressbookentry();
it runs thing is... want there checking if user's input name used or if user doesn't typed when ask "enter name: "
here's addentry()
method:
public void addentry() { entry[counter] = new addressbookentry(); entry[counter].setname(joptionpane.showinputdialog("enter name: ")); entry[counter].setadd(joptionpane.showinputdialog("enter add: ")); entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: ")); entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: ")); counter++; }
it allows user input entry likes... no checking if name used , if user leave "enter name: " blank still allows user proceed "enter add.: "
here's complete code in main program:
import javax.swing.joptionpane; import javax.swing.jtextarea; public class addressbook { private addressbookentry entry[]; private int counter; private string sname; private int notfound = 0; public static void main(string[] args) { addressbook = new addressbook(); a.entry = new addressbookentry[100]; int option = 0; try { while (option != 5) { string content = "choose option\n\n" + "[1] add entry\n" + "[2] delete entry\n" + "[3] update entry\n" + "[4] view entries\n" + "[5] view specific entry\n" + "[6] exit"; option = integer.parseint(joptionpane.showinputdialog(content)); switch (option) { case 1: a.addentry(); break; case 2: a.deleteentry(); break; case 3: a.editentry(); break; case 4: a.viewall(); break; case 5: a.searchentry(); break; case 6: system.exit(1); break; default: joptionpane.showmessagedialog(null, "invalid choice!"); } } }catch(numberformatexception e){ joptionpane.showmessagedialog(null, "please choose number in displayed menu"); } } public void addentry() { entry[counter] = new addressbookentry(); entry[counter].setname(joptionpane.showinputdialog("enter name: ")); entry[counter].setadd(joptionpane.showinputdialog("enter add: ")); entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: ")); entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: ")); counter++; } public void viewall() { string addtext = " name\taddress\tphone no.\te-mail add\n\n"; int nonnull = 0; (int = 0; < entry.length; i++) { if (entry[i] != null) { addtext = addtext + entry[i].getinfo() + "\n"; nonnull++; } if (nonnull == counter) { break; } } joptionpane.showmessagedialog(null, new jtextarea(addtext)); } public void searchentry() { sname = joptionpane.showinputdialog("enter name find: "); searchmethod(); } public void searchmethod() { (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { joptionpane.showmessagedialog(null, entry[i].getinfo2()); notfound = 0; break; } else { notfound++; } } if (notfound != 0) { joptionpane.showmessagedialog(null, "name not found!"); } } public void editentry() { sname = joptionpane.showinputdialog("enter name edit: "); (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { entry[i] = new addressbookentry(); entry[i].setname(joptionpane.showinputdialog("enter new name: ")); entry[i].setadd(joptionpane.showinputdialog("enter new add: ")); entry[i].setphoneno(joptionpane.showinputdialog("enter new phone no.: ")); entry[i].setemail(joptionpane.showinputdialog("enter new e-mail: ")); notfound = 0; break; } else { notfound++; } } if (notfound != 0) { joptionpane.showmessagedialog(null, "name not found!"); } } public void deleteentry() { sname = joptionpane.showinputdialog("enter name delete: "); (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { joptionpane.showmessagedialog(null, "found!"); entry[i] = null; break; } } } }
hope can can me new in java , don't add in code checking.
* should add in addentry()
method check if name used or no name entered? * need condition in searchmethod()
?
an address book tends thing given name, address, , other information.
so falls under general pattern have key (the name) , value (the other information)
when ever see this, want think of using map
so, i'd recommend instead of putting addressbookentry's in array, use map this
map<string, addressbookentry> addressbook = new hashmap<string, addressbookentry>();
then when want add new entry do
addressbook.put("john", new addressbookentry());
the nice thing map can have 1 entry same person.
so don't have worry happens if put john in book twice. allow 1 in there.
Comments
Post a Comment