Build a simple PhoneBook program.

Entry.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Entry
  4. {
  5. String phoneNumber;
  6. List<String> entryArray = new ArrayList <String>();
  7. public Entry(String phoneNumber){
  8. entryArray.add(0,phoneNumber);
  9. }
  10. public String getPhoneNumber(){
  11. StringBuilder phoneNumber = new StringBuilder();
  12. if(entryArray.size()>1){
  13. for(int i = 0; i < entryArray.size()-1; i++){
  14. phoneNumber.append(entryArray.get(i) + ", ");
  15. }
  16. }
  17. phoneNumber.append(entryArray.get(entryArray.size()-1));
  18. return phoneNumber.toString();
  19. }
  20. public boolean hasPhoneNumber(String phoneNumber){
  21. boolean hasPhoneNumber = false;
  22. for(String number:entryArray){
  23. if(phoneNumber.equals(number)){
  24. hasPhoneNumber = true;
  25. break;
  26. }
  27. }
  28. return hasPhoneNumber;
  29. }
  30. public void addPhoneNumber(String phoneNumber){
  31. entryArray.add(phoneNumber);
  32. }
  33. public boolean entryArrayContainsNumber(String phonenumber){
  34. boolean containsNumber = false;
  35. if(entryArray.contains(phonenumber)){
  36. containsNumber = true;
  37. }
  38. return containsNumber;
  39. }
  40. public int getNumberOfRecords(){
  41. return entryArray.size();
  42. }
  43. public void removePhoneNumber(String phoneNumber){
  44. for(String number:entryArray){
  45. if(phoneNumber.equals(number)){
  46. entryArray.remove(number);
  47. break;
  48. }
  49. }
  50. }
  51. }