/*
     * @author JDickerson
     * Created on 4 Aug 2008
     */
    package com.jjpeople.addressbook.gui;

    import com.jjpeople.addressbook.lineformatter.LineFormatter;
    import com.jjpeople.serviceworker.gui.GuiException;

    /**
     * This application uses a Service to Worker pattern which is located in the
     * package: com.jjpeople.serviceworker
     * <p>
     * This interface models the Gui.
     *
     * @author JDickerson
     * Created on 7 Aug 2008
     */
    public interface AddressBookGui extends LineFormatter {

        /**
         * This method starts the Gui and brings up the menus
         *
         * @throws GuiException
         */
        public void start() throws GuiException;


        /**
         * This method shows the menus
         *
         * @throws GuiException
         */
        public void showMenu() throws GuiException;
    }


    public class AddressBookGuiImpl extends AbstractCommandlineGui
        implements AddressBookGui {



        /**
         * If the Add Addresses option is chosen this method is executed
         *
         * @throws GuiException
         */
        private void showAddAddress() throws GuiException {

            AddressBookEntry addressBookEntry = new AddressBookEntryImpl();

            StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
            sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
            sb.append( DIVIDER ).append( LINE_SEPARATOR );
            sb.append( "Address Book: Adding Entry" ).append( LINE_SEPARATOR );
            sb.append( DIVIDER ).append( LINE_SEPARATOR );
            sb.append( LINE_SEPARATOR );
            sb.append( "Please enter the following details:" );
            sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
            sb.append( "Name : " );

            print( sb.toString() );
            addressBookEntry.setName( readInput() );

            print( "Mobile Number : " );
            addressBookEntry.setMobileNumber( readInput() );

            print( "Landline Number : " );
            addressBookEntry.setLandlineNumber( readInput() );

            print( "First line Address : " );
            addressBookEntry.setFirstLineAddress( readInput() );

            print( "Second line Address : " );
            addressBookEntry.setSecondLineAddress( readInput() );

            print( "Town or City : " );
            addressBookEntry.setTownOrCity( readInput() );

            print( "Postcode : " );
            addressBookEntry.setPostcode( readInput() );

            print( "Country : " );
            addressBookEntry.setCountry( readInput() );

            AddAddressActionArgument addAddressActionArgument =
                new AddAddressActionArgument();

            addAddressActionArgument.setAddressBookEntry( addressBookEntry );

            try {
                controller.execute( addAddressActionArgument );
            }
            catch( ControllerException e ) {

                throw new GuiException( "Could not add address", e );
            }
        }


        /**
         * If the View Address option is found this method is called
         */
        private void showViewAddress() throws GuiException{

            StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
            sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
            sb.append( DIVIDER ).append( LINE_SEPARATOR );
            sb.append( "Address Book" ).append( LINE_SEPARATOR );
            sb.append( DIVIDER ).append( LINE_SEPARATOR );
            sb.append( LINE_SEPARATOR );

            sb.append( "Please enter the name of the person you wish " +
                    "to view the address details of:" );

            sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );

            print( sb.toString() );

            String name = readInput();

            ShowAddressActionArgument showAddressActionArgument =
                new ShowAddressActionArgument( name );

            try {
                ActionResult actionResult =
                    controller.execute( showAddressActionArgument );

                ShowAddressActionResult showAddressActionResult
                    = ( ShowAddressActionResult )actionResult;

                AddressBookEntry addressBookEntry =
                        showAddressActionResult.getAddressBookEntry();

                renderAddressBookEntry( addressBookEntry );
            }
            catch( ControllerException e ) {

                throw new GuiException( "Could not retrieve address entry", e );
            }
        }






        /**
         * Renders an address book entry to the Gui
         *
         * @param addressBookEntry
         */
        private void renderAddressBookEntry(
                AddressBookEntry addressBookEntry ) {

            print( LINE_SEPARATOR );

            if ( addressBookEntry != null ) {

                print( addressBookEntry.toString() );
            }
            else {

                print( "Could not find entry" );
            }
        }


        /**
         * If the Delete Address option is found this method is called
         */
        private void  showDeleteAddress() throws GuiException{

            AddressBookEntry addressBookEntry = new AddressBookEntryImpl(); 

            StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
            sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
            sb.append( DIVIDER ).append( LINE_SEPARATOR );
            sb.append( "Address Book" ).append( LINE_SEPARATOR );
            sb.append( DIVIDER ).append( LINE_SEPARATOR );
            sb.append( LINE_SEPARATOR );

            sb.append( "Please enter the name of the person you wish " +
            "to delete the address details of:" );

            sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );

            print( sb.toString() );

            String name = readInput();

            DeleteAddressActionArgument deleteAddressActionArgument =
            new DeleteAddressActionArgument(name );

            deleteAddressActionArgument.setAddressBookEntry( addressBookEntry );

            try {
                controller.execute( deleteAddressActionArgument );
            }
            catch( ControllerException e ) {

                throw new GuiException( "Could not delete address", e );
            }
             }

        /**
         * Shows all names in the address book
         */
        private void showViewAllNamesInAddressBook() throws GuiException {

            ShowAllNamesInAddressBookActionArgument
                showAllNamesInAddressBookActionArgument =
                      new ShowAllNamesInAddressBookActionArgument();

            try {
                ActionResult actionResult =
                    controller.execute( showAllNamesInAddressBookActionArgument );

                ShowAllNamesInAddressBookActionResult
                    showAllNamesInAddressBookActionResult
                        = ( ShowAllNamesInAddressBookActionResult )actionResult;

                String[] addressBookNames =
                    showAllNamesInAddressBookActionResult.getAddressBookNames();

                renderAllAddressBookNames( addressBookNames );
            }
            catch( ControllerException e ) {

                throw new GuiException(
                        "Could not show all address book name", e );
            }
        }



        /**
         * Renders all adress book names
         *
         * @param addressBookNames addressBookNames to render
         */
        private void renderAllAddressBookNames( String[] addressBookNames ) {

            StringBuffer sb = new StringBuffer();

            for ( String name : addressBookNames ) {
                sb.append( name ).append( LINE_SEPARATOR );
            }

            print( sb.toString() );
        }


        /**
         * Constructor
         */
        public AddressBookGuiImpl( Controller controller ) {

            super( controller );
        }


        /* (non-Javadoc)
         * @see com.jjpeople.addressbook.gui.AddressBookGui#start()
         */
        public void start() throws GuiException {

            showMenu();
        }


        /* (non-Javadoc)
         * @see com.jjpeople.addressbook.gui.AddressBookGui#showMenu()
         */
        public void showMenu() throws GuiException {

            String entryNumber = "-1";

            while ( true ) {

                if ( entryNumber.equals( "1" ) ) {

                    showAddAddress();
                    entryNumber = "-1";
                }
                else if ( entryNumber.equals( "2" ) ) {

                    showViewAddress();
                    entryNumber = "-1";
                }
                else if ( entryNumber.equals( "3" ) ) {

                    showViewAllNamesInAddressBook();
                    entryNumber = "-1";
                }
                else if ( entryNumber.equals( "4" ) ) {

                    showDeleteAddress();
                    entryNumber = "-1";
                }
                else {

                    StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
                    sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
                    sb.append( DIVIDER ).append( LINE_SEPARATOR );
                    sb.append( "Address Book" ).append( LINE_SEPARATOR );
                    sb.append( DIVIDER ).append( LINE_SEPARATOR );
                    sb.append( LINE_SEPARATOR );

                    sb.append( "1. Add Address " ).append( LINE_SEPARATOR );
                    sb.append( "2. View Address of one person" );
                    sb.append( LINE_SEPARATOR );
                    sb.append( "3. View all Names in Address book" );
                    sb.append( LINE_SEPARATOR );

                    sb.append( "4. Delete Address" );
                    sb.append( LINE_SEPARATOR );

                    sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );

                    sb.append( "Please enter a number of the action " +
                            "you wish to perform" );

                    print( sb.toString() );

                    entryNumber = readInput();
                }
            }
        }
    }


    /*
     * @author JDickerson
     * Created on 5 Aug 2008
     */
    package com.jjpeople.addressbook.action;


    import com.jjpeople.addressbook.action.actionresult.ShowAddressActionResult;
    import com.jjpeople.addressbook.actionargument.ShowAddressActionArgument;
    import com.jjpeople.addressbook.actionargument.DeleteAddressActionArgument;
    import com.jjpeople.addressbook.businessdelegate.AddressBookDelegate;
    import com.jjpeople.addressbook.businessdelegate.AddressBookDelegateException;
    import com.jjpeople.addressbook.businessdelegate.AddressBookDelegateImpl;
    import com.jjpeople.addressbook.businessdelegate.AddressBookEntry;
    import com.jjpeople.serviceworker.action.AbstractAction;
    import com.jjpeople.serviceworker.action.ActionException;
    import com.jjpeople.serviceworker.controller.Controller;

    /**
     * This application uses a Service to Worker pattern which is located in the
     * package: com.jjpeople.serviceworker
     * <p>
     * This action adds addresses to the address book. The AddressBookDelegateImpl
     * is responsible for persisting the addresses.
     * <p>
     * AddressBookDelegateImpl serializes and deserializes AddressBookEntryImpl
     * to and from files respectively.
     *
     * @author JDickerson
     * Created on 5 Aug 2008
     */
    public class DeleteAddressAction extends AbstractAction {

        private AddressBookDelegate addressBookDelegate;
        String name;


        /**
         * Initializes AddressBookDelegateImpl. The initialisation includes
         * creating a directory un der the user's home directory for this
         * application if it does not exist already
         */
        private void initialize() {

            try {
                addressBookDelegate = new AddressBookDelegateImpl();
            }
            catch( AddressBookDelegateException e ) {

                throw new RuntimeException(
                        "Fatal Exception initializing Address Book", e );
            }
        }


        /**
         * Constructor
         *
         * @param controller the controller to set in thisn action
         */
        public DeleteAddressAction( Controller controller ) {

            super( controller );
            initialize();
        }


        /* (non-Javadoc)
         * @see com.jjpeople.serviceworker.action.AbstractAction#execute()
         */

        public void execute() throws ActionException {


            DeleteAddressActionArgument deleteAddressActionArgument =
                ( DeleteAddressActionArgument )actionArgument;

            AddressBookEntry addressBookEntry =
                deleteAddressActionArgument.getAddressBookEntry();
                deleteAddressActionArgument.delete(name);

           /** try {
                addressBookDelegate.deleteAddressBookEntry( addressBookEntry );
            }
            catch( AddressBookDelegateException e ) {

                throw new ActionException( "Could not delete Address Book Entry, " +
                        addressBookEntry.toString(), e );
            }*/
        }
    }


      /*
     * @author JDickerson
     * Created on 5 Aug 2008
     */
    package com.jjpeople.addressbook.actionargument;

    import com.jjpeople.addressbook.action.ActionConstants;
    import com.jjpeople.addressbook.businessdelegate.AddressBookEntry;
    import com.jjpeople.serviceworker.action.actionargument.AbstractActionArgument;

    /**
     * This application uses a Service to Worker pattern which is located in the
     * package: com.jjpeople.serviceworker
     * <p>
     * Before calling the execute method on the AddAddressAction, the controller,
     * AbstractController sets this class in the action. This class encapsulates
     * the arguments required to run the processing in the execute method of the
     * AddAddressAction.
     *
     * @author JDickerson
     * Created on 5 Aug 2008
     */
    public class DeleteAddressActionArgument extends AbstractActionArgument
        implements ActionConstants {


       String name;
        AddressBookEntry addressBookEntry;
        int counter;


        /**
         * Constructor
         */
        public DeleteAddressActionArgument(String name) {

          super();
          this.name=name;

            this.setActionCommand( DELETE_ADDRESS_ACTION );
        }

        /**
         * Sets the AddressBookEntry
         *
         * @param addressBookEntry the addressBookEntry to set in this class
         */
        public void setAddressBookEntry( AddressBookEntry addressBookEntry ) {

            this.addressBookEntry = addressBookEntry;
        }


        /**
         * Gets the AddressBookEntry
         *
         * @return AddressBookEntry
         */
        public AddressBookEntry getAddressBookEntry() {

            return this.addressBookEntry;
        }

     /**
      * Gets the name of the person we want to delete the address details
      * for
      *
      * @return the name of the person we want to delete the address details
      * for
      */
        public String getName(){
         return name;
        }

       public AddressBookEntry delete(String name) {

        if(name == null) return null;

        for (int i = 0; i <  counter; i++) {
            // make sure we have an entry
             // we know name is not null 
           if (addressBookEntry != null && name.equals(addressBookEntry.getName())) {
                // null out the deleted entry 
                addressBookEntry = null;
                 break; // If you know you have unique names, you can leave the for loop now 
            } // end if 
        } // end for i
      return this.addressBookEntry;
    }
    }

The application should be able to add address, view address,view address of all persons and lastly delete an address of an individual..
All other functions are working properly but the application cannot delete an address please Asssit...Whea shud i implement the delete method??

/*
 * @author JDickerson
 * Created on 4 Aug 2008
 */
package com.jjpeople.addressbook.gui;

import com.jjpeople.addressbook.lineformatter.LineFormatter;
import com.jjpeople.serviceworker.gui.GuiException;

/**
 * This application uses a Service to Worker pattern which is located in the
 * package: com.jjpeople.serviceworker
 * <p>
 * This interface models the Gui.
 *
 * @author JDickerson
 * Created on 7 Aug 2008
 */
public interface AddressBookGui extends LineFormatter {

    /**
     * This method starts the Gui and brings up the menus
     *
     * @throws GuiException
     */
    public void start() throws GuiException;


    /**
     * This method shows the menus
     *
     * @throws GuiException
     */
    public void showMenu() throws GuiException;
}


public class AddressBookGuiImpl extends AbstractCommandlineGui
    implements AddressBookGui {



    /**
     * If the Add Addresses option is chosen this method is executed
     *
     * @throws GuiException
     */
    private void showAddAddress() throws GuiException {

        AddressBookEntry addressBookEntry = new AddressBookEntryImpl();

        StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
        sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
        sb.append( DIVIDER ).append( LINE_SEPARATOR );
        sb.append( "Address Book: Adding Entry" ).append( LINE_SEPARATOR );
        sb.append( DIVIDER ).append( LINE_SEPARATOR );
        sb.append( LINE_SEPARATOR );
        sb.append( "Please enter the following details:" );
        sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
        sb.append( "Name : " );

        print( sb.toString() );
        addressBookEntry.setName( readInput() );

        print( "Mobile Number : " );
        addressBookEntry.setMobileNumber( readInput() );

        print( "Landline Number : " );
        addressBookEntry.setLandlineNumber( readInput() );

        print( "First line Address : " );
        addressBookEntry.setFirstLineAddress( readInput() );

        print( "Second line Address : " );
        addressBookEntry.setSecondLineAddress( readInput() );

        print( "Town or City : " );
        addressBookEntry.setTownOrCity( readInput() );

        print( "Postcode : " );
        addressBookEntry.setPostcode( readInput() );

        print( "Country : " );
        addressBookEntry.setCountry( readInput() );

        AddAddressActionArgument addAddressActionArgument =
            new AddAddressActionArgument();

        addAddressActionArgument.setAddressBookEntry( addressBookEntry );

        try {
            controller.execute( addAddressActionArgument );
        }
        catch( ControllerException e ) {

            throw new GuiException( "Could not add address", e );
        }
    }


    /**
     * If the View Address option is found this method is called
     */
    private void showViewAddress() throws GuiException{

        StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
        sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
        sb.append( DIVIDER ).append( LINE_SEPARATOR );
        sb.append( "Address Book" ).append( LINE_SEPARATOR );
        sb.append( DIVIDER ).append( LINE_SEPARATOR );
        sb.append( LINE_SEPARATOR );

        sb.append( "Please enter the name of the person you wish " +
                "to view the address details of:" );

        sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );

        print( sb.toString() );

        String name = readInput();

        ShowAddressActionArgument showAddressActionArgument =
            new ShowAddressActionArgument( name );

        try {
            ActionResult actionResult =
                controller.execute( showAddressActionArgument );

            ShowAddressActionResult showAddressActionResult
                = ( ShowAddressActionResult )actionResult;

            AddressBookEntry addressBookEntry =
                    showAddressActionResult.getAddressBookEntry();

            renderAddressBookEntry( addressBookEntry );
        }
        catch( ControllerException e ) {

            throw new GuiException( "Could not retrieve address entry", e );
        }
    }






    /**
     * Renders an address book entry to the Gui
     *
     * @param addressBookEntry
     */
    private void renderAddressBookEntry(
            AddressBookEntry addressBookEntry ) {

        print( LINE_SEPARATOR );

        if ( addressBookEntry != null ) {

            print( addressBookEntry.toString() );
        }
        else {

            print( "Could not find entry" );
        }
    }


    /**
     * If the Delete Address option is found this method is called
     */
    private void  showDeleteAddress() throws GuiException{

        AddressBookEntry addressBookEntry = new AddressBookEntryImpl(); 

        StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
        sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
        sb.append( DIVIDER ).append( LINE_SEPARATOR );
        sb.append( "Address Book" ).append( LINE_SEPARATOR );
        sb.append( DIVIDER ).append( LINE_SEPARATOR );
        sb.append( LINE_SEPARATOR );

        sb.append( "Please enter the name of the person you wish " +
        "to delete the address details of:" );

        sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );

        print( sb.toString() );

        String name = readInput();

        DeleteAddressActionArgument deleteAddressActionArgument =
        new DeleteAddressActionArgument(name );

        deleteAddressActionArgument.setAddressBookEntry( addressBookEntry );

        try {
            controller.execute( deleteAddressActionArgument );
        }
        catch( ControllerException e ) {

            throw new GuiException( "Could not delete address", e );
        }
         }

    /**
     * Shows all names in the address book
     */
    private void showViewAllNamesInAddressBook() throws GuiException {

        ShowAllNamesInAddressBookActionArgument
            showAllNamesInAddressBookActionArgument =
                  new ShowAllNamesInAddressBookActionArgument();

        try {
            ActionResult actionResult =
                controller.execute( showAllNamesInAddressBookActionArgument );

            ShowAllNamesInAddressBookActionResult
                showAllNamesInAddressBookActionResult
                    = ( ShowAllNamesInAddressBookActionResult )actionResult;

            String[] addressBookNames =
                showAllNamesInAddressBookActionResult.getAddressBookNames();

            renderAllAddressBookNames( addressBookNames );
        }
        catch( ControllerException e ) {

            throw new GuiException(
                    "Could not show all address book name", e );
        }
    }



    /**
     * Renders all adress book names
     *
     * @param addressBookNames addressBookNames to render
     */
    private void renderAllAddressBookNames( String[] addressBookNames ) {

        StringBuffer sb = new StringBuffer();

        for ( String name : addressBookNames ) {
            sb.append( name ).append( LINE_SEPARATOR );
        }

        print( sb.toString() );
    }


    /**
     * Constructor
     */
    public AddressBookGuiImpl( Controller controller ) {

        super( controller );
    }


    /* (non-Javadoc)
     * @see com.jjpeople.addressbook.gui.AddressBookGui#start()
     */
    public void start() throws GuiException {

        showMenu();
    }


    /* (non-Javadoc)
     * @see com.jjpeople.addressbook.gui.AddressBookGui#showMenu()
     */
    public void showMenu() throws GuiException {

        String entryNumber = "-1";

        while ( true ) {

            if ( entryNumber.equals( "1" ) ) {

                showAddAddress();
                entryNumber = "-1";
            }
            else if ( entryNumber.equals( "2" ) ) {

                showViewAddress();
                entryNumber = "-1";
            }
            else if ( entryNumber.equals( "3" ) ) {

                showViewAllNamesInAddressBook();
                entryNumber = "-1";
            }
            else if ( entryNumber.equals( "4" ) ) {

                showDeleteAddress();
                entryNumber = "-1";
            }
            else {

                StringBuffer sb = new StringBuffer( LINE_SEPARATOR );
                sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
                sb.append( DIVIDER ).append( LINE_SEPARATOR );
                sb.append( "Address Book" ).append( LINE_SEPARATOR );
                sb.append( DIVIDER ).append( LINE_SEPARATOR );
                sb.append( LINE_SEPARATOR );

                sb.append( "1. Add Address " ).append( LINE_SEPARATOR );
                sb.append( "2. View Address of one person" );
                sb.append( LINE_SEPARATOR );
                sb.append( "3. View all Names in Address book" );
                sb.append( LINE_SEPARATOR );

                sb.append( "4. Delete Address" );
                sb.append( LINE_SEPARATOR );

                sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );

                sb.append( "Please enter a number of the action " +
                        "you wish to perform" );

                print( sb.toString() );

                entryNumber = readInput();
            }
        }
    }
}

Recommended Answers

All 3 Replies

Can you explain why you posted 850+ lines of code?

The application is required to Add an addresss, view an address, delete address. How do i implement a delete function and where?? All other functionalities are working..please assit

800 lines are too many for me. Can you make a smaller program (<200 lines) that shows the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.