The Application should be able to add an entry , view an entry and delete and entry.. All other functions are working properly but my applica tion cannot delete.. Please assit where i shuld place my delete method
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;
/**
* 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;
String name=deleteAddressActionArgument.getName();
try {
AddressBookEntry addressBookEntry =
addressBookDelegate.deleteAddressBookEntry( name );
}
catch( AddressBookDelegateException e ) {
throw new ActionException( "Could not delete Address Book Entry,= " +
name, e );
}
}
}
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;
/**
* Constructor
*/
public DeleteAddressActionArgument(String name) {
super();
this.name=name;
setActionCommand( DELETE_ADDRESS_ACTION );
}
/**
* 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;
}
}
package com.jjpeople.addressbook.gui;
import java.io.File;
import java.io.*;
import com.jjpeople.addressbook.action.actionresult.ShowAddressActionResult;
import com.jjpeople.addressbook.action.actionresult.ShowAllNamesInAddressBookActionResult;
import com.jjpeople.addressbook.actionargument.AddAddressActionArgument;
import com.jjpeople.addressbook.actionargument.DeleteAddressActionArgument;
import com.jjpeople.addressbook.actionargument.ShowAddressActionArgument;
import com.jjpeople.addressbook.actionargument.ShowAllNamesInAddressBookActionArgument;
import com.jjpeople.addressbook.businessdelegate.AddressBookEntry;
import com.jjpeople.addressbook.businessdelegate.AddressBookEntryImpl;
import com.jjpeople.serviceworker.action.actionresult.ActionResult;
import com.jjpeople.serviceworker.controller.Controller;
import com.jjpeople.serviceworker.controller.ControllerException;
import com.jjpeople.serviceworker.gui.AbstractCommandlineGui;
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 class models the Gui.
*
* @author JDickerson
* Created on 4 Aug 2008
*/
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 );
try {
controller.execute( deleteAddressActionArgument );
}
catch( ControllerException e ) {
throw new GuiException( "Could not delete address", e );
}
catch (NullPointerException e){
throw new GuiException( "The Entry Does Not Exist", e );
}
}
/**private boolean removeAddressBookEntry(AddressBookEntry addressBookEntry ) {
if ( addressBookEntry != null ) {
File deleted=new File(addressBookEntry,name);
return deleted.delete();
}
else {
print( "Could not find entry" );
}
}
private void endApplication() throws GuiException {
System.exit(0);
}*/
/**
* 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();
}
}
}
}