Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,439 members, 7,815,998 topics. Date: Thursday, 02 May 2024 at 11:08 PM

Java Peeps Hook Me Up With A Solution Pls - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Java Peeps Hook Me Up With A Solution Pls (1810 Views)

Are You Tired Of Marketing Your Business? Here Is A Solution / Java Programers:java Assignment: Can Someone Help Provide A Solution To This / Java Proramers:java Assignment: Can Someone Help Provide A Solution To This (2) (3) (4)

(1) (Reply) (Go Down)

Java Peeps Hook Me Up With A Solution Pls by Hardballer(m): 11:47pm On May 23, 2008
Write a complete address book maintenance application. The user of the program has three options: add new person, delete a person and modify the data of a person.
You have to decide how to allow the user to enter the values for a new person, and the other options.
You must use files in order to save and read the data.

Use the AddressBook class provided below.

the address book class
/*
Introduction to OOP with Java 4th Ed, McGraw-Hill

Wu/Otani

Chapter 10 Sample Program: Address Book Maintenance

File: AddressBook.java
*/

/**
* This class is designed to manage an address book that contains
* Person objects. The user can specify the size of the address book
* when it is created. If no size is specified, then the default size
* is set to 25 Person objects.
*
* @author Dr. Caffeine
*
*/
class AddressBook { //Step 4: Implement the delete method


//--------------------------------
// Data Members
//--------------------------------

/**
* Default size of the array
*/
private static final int DEFAULT_SIZE = 25;

/**
* Constant for signaling an unsuccessful search
*/
private static final int NOT_FOUND = -1;

/**
* The array of Person objects
*/
private Person[] entry;

/**
* The number of elements in the <code>entry</code> array,
* which is also the position to add the next Person object
*/
private int count;

//--------------------------------
// Constructors
//--------------------------------

/**
* Default constructor.
* Creates an address book of size 25.
*/
public AddressBook( ) {
this( DEFAULT_SIZE );
}


/**
* Creates an address book with the designated size.
*
* @param size the size of this address book.
*/
public AddressBook( int size ){
if (size <= 0 ) {
throw new IllegalArgumentException("Size must be positive."wink;
}

entry = new Person[size];

//System.out.println("Array of "+ size + " is created."wink; //TEMP
}


//-------------------------------------------------
// Public Methods:
//
// void add ( Person )
// void delete ( String )
// Person search ( String )
//
//------------------------------------------------

/**
* Adds a new Person to this address book.
* If the overflow occurs, the array size
* is increased by 50 percent.
*
* @param newPerson a new Person object to add
*/
public void add( Person newPerson ) {

assert count >= 0 && count <= entry.length;

if (count == entry.length) { //no more space left,
enlarge( ); //create a new larger array
}

//at this point, entry refers to a new larger array
entry[count] = newPerson;
count++;
}


/**
* Deletes the Person whose name is 'searchName'.
*
* @param searchName the name of a Person to delete
*
* @return true if removed successfully; false otherwise
*/
public boolean delete( String searchName )
{
boolean status;
int loc;

loc = findIndex( searchName );

if (loc == NOT_FOUND) {
status = false;
} else { //found, pack the hole

entry[loc] = entry[count-1];

status = true;
count--; //decrement count,
//since we now have one less element
assert count >= 0 && count <= entry.length;
}

return status;
}

/**
* Searches this address book for a Person
* whose name is <code>searchName</code>.
*
* @param searchName the name to search
*
* @return a Person object if found; otherwise null
*/
public Person search( String searchName ) {
Person foundPerson;
int loc = 0;

while ( loc < count &&
!searchName.equals( entry[loc].getName() ) ) {
loc++;
}

if (loc == count) {

foundPerson = null;
} else {

foundPerson = entry[loc];
}

return foundPerson;
}

//-------------------------------------------------
// Private Methods:
//
// void enlarge ( )
//
//------------------------------------------------

/**
* Enlarges the size of <code>entry</code> array to
* eliminate the overflow condition. The new array
* is 50 percent larger than the current array.
*/
private void enlarge( ) {
//create a new array whose size is 150% of
//the current array
int newLength = (int) (1.5 * entry.length);
Person[] temp = new Person[newLength];

//now copy the data to the new array
for (int i = 0; i < entry.length; i++) {
temp[i] = entry[i];
}

//finally set the variable entry to point to the new array
entry = temp;

System.out.println("Inside the method enlarge"wink; //TEMP
System.out.println("Size of a new array: " + entry.length); //TEMP
}

/**
* Finds the index in the array where <code>searchName</code>
* is the name of a person to locate.
*
* @param searchName the name of person to find
*
* @return the index of the found Person in the array; NOT_FOUND
* if the searched person is not found
*/
private int findIndex( String searchName ) {
int loc = 0;

while ( loc < count &&
!searchName.equals( entry[loc].getName() ) ) {
loc++;
}

if (loc == count) {

loc = NOT_FOUND;
}

return loc;
}
}
Re: Java Peeps Hook Me Up With A Solution Pls by sbucareer(f): 9:34am On May 24, 2008

I don't really know what you are trying to accomplished with this limited code. Besides, the Object Person is not included for us to see their private and public fields and methods.

Also, assert and count field/object where are they from?  However, if you are trying to architect an Address Book artefact you would need these Objects or Classes

1. Entry
2. Person
3. AddressBook

I don't have time to draw UML diagram for you but I guess you can do that, right? Entry Object will hold all the the following methods:

1. Edit function(s)
2. Add function(s)
3. Remove function(s)
4. Search function(s)
5. Retrieve function(s)
6. Store function(s)

Now, for every entry into the Entry Object there must be one Person Object, you can check for duplicate entry and throw exceptions. For every AddressBook, there maybe several entries, therefore, you have M:1 relationship.


Remember, since you are going to store the file on the hard drive, you are going to create a text file called addressBook.txt Use the I/O routine i.e



    try{
   
        PrintWriter out = new PrintWriter(new FileWriter("adrressBook.txt"wink);
        }catch(Exception e){}





To read the file use


    try{
   
        PrintReader in = new PrintReader(new FileReader("adrressBook.txt"wink);
        }catch(Exception e){e.printStackTrace()}


These routines should be in you Entry object routine like save contact

Re: Java Peeps Hook Me Up With A Solution Pls by sbucareer(f): 10:18am On May 24, 2008

In the Entry class don't use ArraryList or Array because you have to worry about the growth and size of the array. If your requirements allows you to change the array, I would advice you to change it to a Vector Object.

Because Vector grows and shrink by it self. However, looking at your routine enlarge(), you have taken care of that growth. But how about shrink? You do not want an empty entry record still in you memory.


// The "Person" class.
import java.io.*;

public class Person extends Object
{
  private String firstName="First name";
  private String lastName ="Last name";
  private String telephoneNumber ="Telephone number";
 
  public Person (){
   
   }//End default Constructor
   
   public Person (String firstName, String lastName,
                            String telephoneNumber)throws Exception{
          if ( ( firstName == null ) ||
               ( lastName  == null ) ||
               ( telephoneNumber == null )) throw new Exception
                    ("You must enter "+this.firstName+ " "+
                      this.lastName+ " "+ this.telephoneNumber );
           this.firstName = firstName;
           this.lastName  = lastName;
           this.telephoneNumber = telephoneNumber;
    }//End alternative constructor
   
   
    //Setters
    public void setFirstName(String firstName){this.firstName = firstName;}
    public void setlastName(String lastName){this.lastName = lastName;}
    public void setTelephoneNumber(String telephoneNumber){this.telephoneNumber = telephoneNumber;}
   
   
    //Getters
    public String getFirstName(){return this.firstName;}
    public String getLastName(){return this.lastName;}   
    public String getTelephoneNumber(){return this.telephoneNumber;}   
} // FileExample class


Re: Java Peeps Hook Me Up With A Solution Pls by Hardballer(m): 11:11am On May 25, 2008
i really appereciate the help mane but the teach is askin for like an address book maintenance application  the adress book class already exists the app is just to update the adress book i.e add new person, delete a person and modify the data of a person he doesnt want the person class its alredy in the adress book code and dude specified that we must use files to save and read the data . will appreciate the help thnxs again
Re: Java Peeps Hook Me Up With A Solution Pls by sbucareer(f): 2:32pm On May 27, 2008

Here is some code I have managed to architect for you in managing your Phone Book application software. I have not implemented the routines for:

1. Add
2. Delete
3. Modify

I believe you can do that yourself. I do not want to do your coursework for you. Good luck






Code

// The "PhoneBook" class.
package Menus;

import java.util.*;

public class PhoneBook extends Object{

    private static char INVALID_OPTION = 'q';
    private static char answer;
    private static BasicMenu menu;
    private static Vector list;
    private static String options[] = {"Add new person",
                                 "Delete new person",
                                 "Modify person details"};
   
    public static void main (String[] args){

    list = new Vector();
    menu = new BasicMenu("Phone Book Application",
                         options,
                         "Please make a selection"wink;
                         
                         
     while ( answer != INVALID_OPTION){
   
        answer = menu.offerMenuAsChar();
   
     switch( answer ){
     
        case 'A':
            addPerson();
            break;
        case 'B':
            deletePerson();
            break;
        case 'C':
            modifyPerson();
            break;
     }//End switch
     }//End while
    }//End main
     
      public static void addPerson (){
      }//End addPerson
     
      public static void deletePerson(){
      }//End deletePerson
     
      public static void modifyPerson(){
      }//End modifyPerson

} // PhoneBook class






// Filename Menus/BasicMenu.java.
// Providing an initial Interactive Menu.
//


package Menus;

public class BasicMenu extends Object {

private String  theTitle;
private String  theOptions[];
private String  thePrompt = "Please enter your choice :";
private char    minimumOption;
private char    maximumOption;

private java.io.DataInputStream keyboard =
                new java.io.DataInputStream(  System.in);


   public BasicMenu( String title,
                     String options[],
                     String prompt) {
                                         
   int thisString;                     

     if ( title.length() > 0 ) {
        theTitle = new String( title);
     } // End if.
     
     theOptions = new String[ options.length];
     for ( thisString = 0;
           thisString < options.length;
           thisString++                  ) {
        theOptions[ thisString] = new String( options[ thisString]); 
     } // End for.     
                                                   
     if ( prompt.length() > 0) {
        thePrompt = new String( prompt);
     } // End if.                                               
  } // End BasicMenu   
 

  public char offerMenuAsChar() {
 
  char validatedResponse;
 
     this.showMenu();
     validatedResponse = getValidatedMenuChoice();
     return validatedResponse;
  } // End offerMenu
 
 
  public int offerMenuAsInt() {
 
  char responseAsChar  = offerMenuAsChar();

       return (responseAsChar - 'A') + 1;
  } // End offerMenu


  private void showMenu( ){
   
  int  thisString;
  char thisOption = 'A';                     

     setMinimumOption( thisOption);

     showTitle();
     
     for ( thisString = 0;
           thisString < theOptions.length;
           thisString++  ) {
         showMenuLine( thisOption,  thisString);
         thisOption++;
     } // End for 
     setMaximumOption( --thisOption);     
  } // End showMenu 
 

  protected void showTitle(){ 
     if ( theTitle != null ) {
           System.out.println( "\t" + theTitle + "\n"wink;
     }   
  } // end fun showMenuOption.


  protected void showMenuLine( char menulabel, int menuText){ 
     System.out.println( menulabel + ".   " + theOptions[ menuText]);   
  } // end fun showMenuLine.


  protected void setMinimumOption( char setTo ){
     minimumOption = setTo;
  } // end fun setMinimumOption.

 
  protected void setMaximumOption( char setTo ){
     maximumOption = setTo;
  } // end fun setMinimumOption.
   

  protected char getValidatedMenuChoice(){

  String  fromKeyboard      = new String( ""wink;
  char    possibleResponse  = ' ';
  boolean isNotGoodResponse = true; 
 
     System.out.print( "\n" + thePrompt + " "wink;
     System.out.flush();
     
     while ( isNotGoodResponse ) {
        try {
           // possibleResponse = keyboard.readChar();
           fromKeyboard =  new String( keyboard.readLine());
           if ( fromKeyboard.length() > 0 ) {
              possibleResponse  = fromKeyboard.charAt( 0);
           } else {
              possibleResponse  = ' ';
           }
        } catch( java.io.IOException exception) {
           // do something
        } // End try/catch.

        possibleResponse = Character.toUpperCase( possibleResponse);
           
        isNotGoodResponse = ( (possibleResponse < minimumOption) ||
                              (possibleResponse > maximumOption) );

        if ( isNotGoodResponse ) {
           System.out.println( "\n Please enter a response between " +
                               minimumOption + " and " + 
                               maximumOption + "."wink;
           System.out.print( "\n" + thePrompt + " "wink;
           System.out.flush();                                 
        } // End if                 
     } // End while.
     return possibleResponse;   
  } // End fun getValidatedMenuChoice .
 
} // End BasicMenu

Re: Java Peeps Hook Me Up With A Solution Pls by sbucareer(f): 3:50pm On May 27, 2008

I have just finished implementing add new person routine for you. I hope you can do the rest.



// The "PhoneBook" class.
package Menus;

import java.util.*;

public class PhoneBook extends Object{

    private static char INVALID_OPTION = 'q';
    private static char answer;
    private static BasicMenu menu;
    private static Person person;
    private static Vector list;
    private static String options[] = {"Add new person",
                                 "Delete new person",
                                 "Modify person details"};
   
    public static void main (String[] args){

    list = new Vector();
    menu = new BasicMenu("Phone Book Application",
                         options,
                         "Please make a selection"wink;
                         
                         
     while ( answer != INVALID_OPTION){
   
        answer = menu.offerMenuAsChar();
   
     switch( answer ){
     
        case 'A':
            addPerson();
            break;
        case 'B':
            deletePerson();
            break;
        case 'C':
            modifyPerson();
            break;
     }//End switch
     }//End while
    }//End main
     
      public static void addPerson (){
     
      String fname = null;
      String lname = null;
      String tele = null;
      Person person;
      String options[] = {"Enter your First name: ",
                         "Enter your Last name: ",
                         "Enter your telephone number: "};
      BasicMenu theBasicMenu;
     
        try{
     
        java.io.DataInputStream keyboard =
                new java.io.DataInputStream(  System.in);
       
        theBasicMenu = new BasicMenu("Add a person to the phone book list",
                                      options,
                                      "Enter your choice: "wink;
       
         while ( answer != INVALID_OPTION){
   
            char choice = theBasicMenu.offerMenuAsChar();
           
            switch( choice ){
     
                case 'A':
                    System.out.println("Enter your first name: "wink;
                    fname = keyboard.readLine().trim();
                    break;
                case 'B':
                    System.out.println("Enter your last name: "wink;
                    lname = keyboard.readLine().trim();
                    break;
                case 'C':
                    System.out.println("Enter your telephone number: "wink;
                    tele = keyboard.readLine().trim();
                    break;
            }//End switch
        }//End while
       
       
        }catch ( Exception e){
            //e.getStackTrace();
        }//End try/catch
person = new Person(fname, lname, tele);
list.add( person);
      }//End addPerson
     

      public static void deletePerson(){
      }//End deletePerson
     
      public static void modifyPerson(){
      }//End modifyPerson
} // PhoneBook class
Re: Java Peeps Hook Me Up With A Solution Pls by Hardballer(m): 7:10pm On May 28, 2008
ay mane thnx for the help ill clean up the rest
Re: Java Peeps Hook Me Up With A Solution Pls by sbucareer(f): 8:38pm On May 28, 2008

I hope this helps you with the rest of the assignment. Incase you want to get a particularly person from the Vector list, do this



for (int index = 0; index < list.size(); index++){
   if (list.elementAt(index) instanceof person.getTelephone() )
        Do something
}


Remember person will be the instance of the object you are looking for in the list. Not very efficient but you can come up with clever algorithm

You can add another method in Person class for enquiry i.e,


private String getTelephone(String tele){
  if ( this.telephoneNumber == tele )
        return this.telephoneNumber;
}//End getTelephoneNumber

(1) (Reply)

Help! I Want To Learn Python Programming / Help! I Can't Download This Video / I Am An Application Penetration Tester/ethical Hacker

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 53
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.