I have a written code on a simple address book which allows addnew,view single entry and view all names. I have added the delete functionality to allow one to enter name to delete its entry but I get Error:
cannot find symbol
return addressBookEntry.delete();
symbol:method delete()
location:variable addressBookEntry of type AddressBookEntry
1 error
I am using a windows console to run it. Kindly check out my code below for the delete.
(Pay particular attention at the AddressBookDelegateImpl class in the public AddressBookEntry deleteAddressBookEntry( String name )
throws AddressBookDelegateException { method)
package com.jjpeople.addressbook.gui;
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.ShowAddressActionArgument;
import com.jjpeople.addressbook.actionargument.DeleteAddressActionArgument;
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;
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 );
}
}
private void DeleteAddress() 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 delete the address details of:" );
sb.append( LINE_SEPARATOR ).append( LINE_SEPARATOR );
print( sb.toString() );
String name = readInput();
DeleteAddressActionArgument deleteAddressActionArgument =
new DeleteAddressActionArgument( name );
try {
ActionResult actionResult =
controller.execute( deleteAddressActionArgument );
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" );
}
}
/**
* 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" ) ) {
DeleteAddress();
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();
}
}
}
}
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 reads addresses from the address book.
* <p>
* The AddressBookDelegateImpl is responsible for retrieving the addresses.
* <p>
* AddressBookDelegateImpl serializes and deserializes AddressBookEntryImpl
* to and from files respectively.
*
* @author JDickerson
* Created on 4 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 controller to set in this action
*/
public DeleteAddressAction( Controller controller ) {
super( controller );
initialize();
}
/* (non-Javadoc)
* @see com.jjpeople.serviceworker.action.AbstractAction#execute()
*/
public void execute() throws ActionException {
DeleteAddressActionArgument deleteAddressArgument =
( DeleteAddressActionArgument )actionArgument;
String name = deleteAddressArgument.getName();
try {
AddressBookEntry addressBookEntry =
addressBookDelegate.deleteAddressBookEntry( name );
}
catch( AddressBookDelegateException e ) {
throw new ActionException(
"Could not retrieve Address book entry for, " + name, e );
}
}
}
package com.jjpeople.addressbook.businessdelegate;
import java.io.File;
import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
/**
* This application uses a Service to Worker pattern which is located in the
* package: com.jjpeople.serviceworker
* <p>
* This class models saving and retrieving address book detaiils using
* the name of a the person to which the address details relate.
* <p>
* The object, AddressBookEntry, is serialized and deserialized from file
*
* @author JDickerson
* Created on 5 Aug 2008
*/
public class AddressBookDelegateImpl implements AddressBookDelegate {
private File userHomeDir;
private File addressBookDirectory;
/**
* Initializes this delegate.
* <p>
* Creates a directory for this Address Book application in the user's
* home directory if it does not already exist
*
* @throws AddressBookDelegateException
*/
private void initialize() throws AddressBookDelegateException {
String userHomeDirPath = System.getProperty( "user.home" );
userHomeDir = new File( userHomeDirPath );
addressBookDirectory = new File( userHomeDir, "ADDRESS_BOOK" );
if ( ! addressBookDirectory.exists() ) {
if ( ! addressBookDirectory.mkdir() ) {
throw new AddressBookDelegateException(
"Could not make directory, " +
addressBookDirectory.getAbsolutePath() );
}
}
}
/**
* Replaces Spaces with underscores
*
* @param string the string to replace the spaces with underscores
*
* @return the
*/
private String replaceSpacesWithUnderScores( String string ) {
return string.replaceAll( " ", "_" ).toLowerCase();
}
/**
* Constructor
*/
public AddressBookDelegateImpl() throws AddressBookDelegateException{
initialize();
}
/* (non-Javadoc)
* @see com.jjpeople.addressbook.businessdelegates.
* AddressBookDelegate#getAddressBook(java.lang.String)
*/
public AddressBookEntry getAddressBookEntry( String name )
throws AddressBookDelegateException {
String nameWithUnderscores = replaceSpacesWithUnderScores( name );
File fileToUnserialize =
new File( addressBookDirectory, nameWithUnderscores );
if ( ! fileToUnserialize.exists() ) {
return null;
}
try {
InputStream inputStream = new FileInputStream( fileToUnserialize );
ObjectInputStream objectInputStream
= new ObjectInputStream( inputStream );
AddressBookEntry addressBookEntry =
( AddressBookEntry )objectInputStream.readObject();
return addressBookEntry;
}
catch( FileNotFoundException e ) {
throw new AddressBookDelegateException(
"Could not find the following file to deserialize, " +
fileToUnserialize.getAbsolutePath(), e );
}
catch( IOException e ) {
throw new AddressBookDelegateException(
"Could not read inputStream of file, " +
fileToUnserialize.getAbsolutePath(), e );
}
catch( ClassNotFoundException e ) {
throw new AddressBookDelegateException(
"Could not find class, " + AddressBookEntry.class +
" to desrialize file, " +
fileToUnserialize.getAbsolutePath(), e );
}
}
public AddressBookEntry deleteAddressBookEntry( String name )
throws AddressBookDelegateException {
String nameWithUnderscores = replaceSpacesWithUnderScores( name );
File fileToDelete =
new File( addressBookDirectory, nameWithUnderscores );
if ( ! fileToDelete.exists() ) {
return null;
}
try {
InputStream inputStream = new FileInputStream( fileToDelete );
ObjectInputStream objectInputStream
= new ObjectInputStream( inputStream );
AddressBookEntry addressBookEntry =
( AddressBookEntry )objectInputStream.readObject();
return addressBookEntry.delete();
}
catch( FileNotFoundException e ) {
throw new AddressBookDelegateException(
"Could not find the following file to deserialize, " +
fileToDelete.getAbsolutePath(), e );
}
catch( IOException e ) {
throw new AddressBookDelegateException(
"Could not read inputStream of file, " +
fileToDelete.getAbsolutePath(), e );
}
catch( ClassNotFoundException e ) {
throw new AddressBookDelegateException(
"Could not find class, " + AddressBookEntry.class +
" to desrialize file, " +
fileToDelete.getAbsolutePath(), e );
}
}
/* (non-Javadoc)
* @see com.jjpeople.addressbook.businessdelegates.
* AddressBookDelegate#saveAddressBookEntry(
* com.jjpeople.addressbook.businessdelegates.
* AddressBookEntry )
*/
public void saveAddressBookEntry( AddressBookEntry addressBookEntry )
throws AddressBookDelegateException {
String nameWithUnderscores =
replaceSpacesWithUnderScores( addressBookEntry.getName() );
File fileToSerialize =
new File( addressBookDirectory, nameWithUnderscores );
if ( fileToSerialize.exists() ) {
if ( fileToSerialize.delete() ) {
throw new AddressBookDelegateException(
"Was trying to save file, " +
fileToSerialize.getAbsolutePath() + " and found " +
"file already existed. Tried to delete it " +
"but failed." );
}
}
else {
try {
OutputStream outputStream =
new FileOutputStream( fileToSerialize );
ObjectOutputStream objectOutputStream =
new ObjectOutputStream( outputStream );
objectOutputStream.writeObject( addressBookEntry );
}
catch( FileNotFoundException e ) {
throw new AddressBookDelegateException(
"Could not find file, " +
fileToSerialize.getAbsolutePath() +
" to serialize to", e );
}
catch( IOException e ) {
throw new AddressBookDelegateException(
"Could not serialize addressBookEntry to " +
"file, " + fileToSerialize.getAbsolutePath(), e );
}
}
}
/* (non-Javadoc)
* @see com.jjpeople.addressbook.businessdelegate.
* AddressBookDelegate#retrieveListOfAddressBookNames()
*/
public String[] retrieveListOfAddressBookNames() {
String[] addressBookNames = addressBookDirectory.list();
String[] formattedAddressBookNames =
new String[ addressBookNames.length ];
String unFormattedName;
StringTokenizer tokenizer;
String token;
String formattedName = "";
for( int i=0; i<addressBookNames.length; i++ ) {
// convert e.g. "john_dickerson" to "john dickerson"
unFormattedName = addressBookNames[ i ].replaceAll( "_", " " );
tokenizer = new StringTokenizer( unFormattedName, " " );
// convert "john dickerson" to "John Dickerson"
while ( tokenizer.hasMoreTokens() ) {
token = tokenizer.nextToken();
formattedName =
formattedName + " " +
Character.toUpperCase( token.charAt( 0 ) ) +
token.substring( 1 );
}
formattedAddressBookNames[ i ] = formattedName.trim();
formattedName = "";
}
return formattedAddressBookNames;
}
}
Kindly assist.