1,105,331 Community Members

Help with java project

Member Avatar
zangster421k
Newbie Poster
1 post since Feb 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a project with these requirements:

Create an array based phone directory which can load name and numbers of all the poeple in the phone book (which is an array) and can change numbers of existing entries, add new entries, delete existing entries and save any new changes to the array

Design of some classes:

design of DirectoryEntry class:
DATA FIELD                  ATTRIBUTE
private String name     Name of the individual represented in the entry
private String number   Phone number for the individual


CONSTRUCTOR
public DirectoryEntry(String name, String number)
BEHAVIOR(of above)
Creates a new DirectoryEntry with the specified name and number


METHOD
1. public String getName()
2. public String getNumber()
3. public void setNumber(String number)
BEHAVIOR(of above)
1. Retrieves name
2. Retrieves number
3. Sets the number to the specified value


Methods Declared in interface PhoneDirectory:
METHOD
1. public void loadData(String sourceName)
2. public String addOrChangeEntry(String name, String number)
3. public String lookupEntry(String name)
4. public String removeEntry(String name)
5. public void save()
BEHAVIOR
1. Loads the data from the data file whose name is given by  sourceName
2. Changes the number associated with the given name to the new value, or adds a new entry with this name and number
3. Searches the directory for the given name
4. Removes the entry with the specified name from the directory and returns that person’s number or null if not in the directory
5. Saves the data if anything new is added or changed

Can someone help me finish writing the code so that this program will work?

This is what i have now:
(If you don't feel like reading it here i have also zipped the code i have below in the file proj1.zip)

import java.io.*;


/** This is an implementation of the KWList interface that uses
an array to store the data.
*/


public class KWArrayList implements KWList {


/** The current size of the ArrayList (number of directory entries) */
private int size = 0;


/** The array to contain the directory data */
private Object[] list;


/** Constructor -- initialize <code>list</code>.
*/
public KWArrayList(int capacity) { }


/** Returns the number of entries in <code>list</code>.
*/
public int size() { return 0; }


/** Searches for <code>target</code>.
Returns the index of the first occurrence, if found;
otherwise returns -1.
*/
public int indexOf(Object target) { return 0; }


/** Return a reference to the element at position
<code>index</code>.
*/
public Object get(int index) { return null; }


/** Set the element at position
<code>index</code> to <code>anEntry</code>.
Return the old value.
*/
public Object set(int index, Object anEntry) { return null; }


/** Adds a reference to <code>anEntry</code> at index (size) of the
<code>list</code>; if necessary, the (list) is made larger.
Always returns <code>true<code> once completed.
*/
public boolean add(Object anEntry) { return true; }


/** Returns and removes the item at position <code>index</code>
(in the range [0, size-1]).
*/
public Object remove(int index) { return null; }


public String toString( ) { return ""; }


} // end class



import java.io.*;


/** This is an implementation of the KWList interface that uses
an array to store the data.
@author Koffman & Wolfgang; modified by Noonan
*/


public interface KWList {


/** Returns the number of entries in <code>list</code>.
*/
public abstract int size();


/** Searches for <code>target</code>.
Returns the index of the first occurrence, if found;
otherwise returns -1.
*/
public abstract int indexOf(Object target);


/** Return a reference to the element at posiiton
<code>index</code>.
*/
public abstract Object get(int index);


/** Set the element at posiiton
<code>index</code> to <code>anEntry</code>.
Return the old value.
*/
public abstract Object set(int index, Object anEntry);


/** Adds a reference to <code>anEntry</code> at index _c(size) of the
<code>list</code>; if necessary, the _c(list) is made larger.
Always returns <code>true<code>.
*/
public abstract boolean add(Object anEntry);


/** Returns and removes the item at position <code>index</code>
(in the range 0...size-1).
*/
public abstract Object remove(int index);


} // end class



public class PDConsoleApplication {
public static void main(String args[]) {
// Check to see that there is a command line argument.



if (args.length == 0) {
System.err.println("You must provide the name of the file"
+ " that contains the phone directory.");
System.exit(1);


}
// Create a PhoneDirectory object.
PhoneDirectory phoneDirectory = new ArrayBasedPD();
// Load the phone directory from the file.
phoneDirectory.loadData(args[0]);
// Create a PDUserInterface object.
PDUserInterface phoneDirectoryInterface = new PDConsoleUI();
// Process user commands.
phoneDirectoryInterface.processCommands(phoneDirectory);
}
}



public class PDConsoleUI
implements PDUserInterface {


/** A reference to the PhoneDirectory object to be processed.
Globally available to the command-processing methods.
*/
private PhoneDirectory theDirectory = null;
/** Buffered reader to read from the input console. */
private BufferedReader in = null;


// Constructor
/** Default constructor. */
public PDConsoleUI() {
in = new BufferedReader(new InputStreamReader(System.in));
}


// Methods


/** Method to display the command choices and process user
commands.
pre:  The directory exists and has been loaded with data.
post: The directory is updated based on user commands.
@param thePhoneDirectory A reference to the PhoneDirectory
to be processed
*/


public void processCommands(PhoneDirectory thePhoneDirectory) {
String[] commands = {
"Add/Change Entry",
"Look Up Entry",
"Remove Entry",
"Save Directory",
"Exit"};


theDirectory = thePhoneDirectory;
int choice = 0;
try {
do {
for (int i = 0; i < commands.length; i++) {
System.out.println("Select " + i + ": " + commands);
}
String line = in.readLine();
if (line != null)
choice = Integer.parseInt(line);
else
choice = commands.length - 1;
switch (choice) {
case 0:
doAddChangeEntry();
break;
case 1:
doLookupEntry();
break;
case 2:
doRemoveEntry();
break;
case 3:
doSave();
break;
case 4:
doSave();
break;
}
}
while (choice < commands.length - 1);
System.exit(0);
}
catch (IOException ex) {
System.err.println
("IO Exception while reading from System.in");
System.exit(1);
}
catch (NumberFormatException ex){
System.out.println("Invalid command: " + ex.getMessage());
System.out.println("Exiting...");
System.exit(1);
}
}


/** Method to add or change an entry.
pre:  The directory exists and has been loaded with data.
post: A new name is added, or the value for the name is
changed, modified is set to true.
@throws IOException - if an IO error occurs
*/
private void doAddChangeEntry() throws IOException {
// Request the name.
System.out.println("Enter name");
String newName = null;
newName = in.readLine();
if (newName == null) {
return;
}
// Request the number.
System.out.println("Enter number");
String newNumber = null;
newNumber = in.readLine();
if (newNumber == null) {
return;
}
// Insert/change name-number.
String oldNumber =
(theDirectory.addOrChangeEntry(newName, newNumber));
String message = null;
if (oldNumber == null) { // New entry.
message = newName + " was added to the directory"
+ "\nNew number: " + newNumber;
}
else { // Changed entry.
message = "Number for " + newName + " was changed"
+ "\nOld number: " + oldNumber
+ "\nNew number: " + newNumber;
}
// Display confirmation message.
System.out.println(message);
}


/** Method to look up an entry.
pre:  The directory has been loaded with data.
post: No changes made to the directory.
@throws IOException - If an IO error occurs
*/
private void doLookupEntry() throws IOException {
// Request the name.
System.out.println("Enter name");
String theName = null;
theName = in.readLine();
if (theName == null) {
return; // Dialog was cancelled.
}
// Look up the name.
String theNumber = theDirectory.lookupEntry(theName);
String message = null;
if (theNumber != null) { // Name was found.
message = "The number for " + theName + " is " + theNumber;
}
else { // Name was not found.
message = theName + " is not listed in the directory";
}
// Display the result.
System.out.println(message);
}


/** Method to remove an entry.
pre:  The directory has been loaded with data.
post: The requested name is removed, modifed is set to true.
@throws IOException - If there is an IO Error
*/
private void doRemoveEntry() throws IOException {
// Request the name.
System.out.println("Enter name");
String theName = in.readLine();
if (theName == null) {
return; // Dialog was cancelled.
} //end if
//Look up the name.
String removeName = theDirectory.removeEntry(theName);
String message = null;
if (removeName != null) { // Name was found.
message = "The entry for " + theName + " was deleted.";
} // end if
else { // Name not found.
message = "The entry for " + theName + " was not found.";
} // end else
// Display result.
System.out.println(message);
}


/** Method to save the directory to the data file.
pre:  The directory has been loaded with data.
post: The current contents of the directory have been saved
to the data file.
*/
private void doSave() {
theDirectory.save();
}
}


import java.io.*;
import java.util.Scanner;
public interface PhoneDirectory {


/** Load the data file containing the directory, or
establish a connection with the data source.
@param sourceName The name of the file (data source)
with the phone directory entries
*/
void loadData(String sourceName);


/** Add an entry or change an existing entry.
@param name The name of the person being added or changed
@param number The new number to be assigned
@return The old number or, if a new entry, null
*/
String addOrChangeEntry(String name, String number);


/** Look up an entry.
@param name The name of the person to look up
@return The number or null if name is not in the directory
*/
String lookupEntry(String name);


/** Remove an entry from the directory.
@param name The name of the person to be removed
@return The current number. If not in directory, null is
returned
*/
String removeEntry(String name);


/** Method to save the directory.
pre:  The directory has been loaded with data.
post: Contents of directory written to Standard Output in
the form of name-number pairs on adjacent lines
modified is reset to false.
*/
void save();
}


public interface PDUserInterface {
/** Abstract method that processes user's commands.
@param thePhoneDirectory The PhoneDirectory object that
contains the data to be displayed and/or changed
*/


void processCommands(PhoneDirectory thePhoneDirectory);
} // end interface PDUserInterface


public class ArrayBasedPD implements PhoneDirectory {


// Data Fields


/** The initial capacity of the array */
private static final int INITIAL_CAPACITY = 10;


/** The array to contain the directory data */
private KWArrayList list = new KWArrayList(INITIAL_CAPACITY);


/** The data file that contains the directory data */
private String sourceName = null;


/** Boolean flag to indicate whether the directory was
modified since it was either loaded or saved. */
private boolean modified = false;


/** Method to load the data file.
pre:  The directory storage has been created and it is empty.
If the file exists, it consists of name-number pairs
on adjacent lines.
post: The data from the file is loaded into the directory.
@param sourceName The name of the data file
*/
public void loadData(String sourceName) {
// Remember the source name.
this.sourceName = sourceName;
try {
// Create a BufferedReader for the file.
BufferedReader in = new BufferedReader(
new FileReader(sourceName));
String name;
String number;


// Read each name and number and add the entry to the array.
while ( (name = in.readLine()) != null) {
// Read name and number from successive lines.
if ( (number = in.readLine()) == null) {
break; // No number read, exit loop.
} // end if
// Add an entry for this name and number.
list.add(new DirectoryEntry(name, number));
} // end while
// Close the file.
in.close();
} // end try
catch (FileNotFoundException ex) {
// Do nothing - no data to load.
return;
} // end catch
catch (IOException ex) {
System.err.println("Load of directory failed.");
ex.printStackTrace();
System.exit(1);
} // end catch
} // end loadData(String sourceName)


/** Add an entry or change an existing entry.
@param name The name of the person being added or changed
@param number The new number to be assigned
@return The old number or, if a new entry, null
*/
public String addOrChangeEntry(String name, String number) {
String oldNumber = null;
int index = list.indexOf(new DirectoryEntry(name, ""));
if (index > -1) {
DirectoryEntry entry =
(DirectoryEntry)(list.set(index,
new DirectoryEntry(name, number)));
oldNumber = entry.getNumber();
} // end if
else {
list.add(new DirectoryEntry(name, number));
} // end else
modified = true;
return oldNumber;
} // end addOrChangeEntry(String name, String number)


/** Look up an entry.
@param name The name of the person
@return The number. If not in the directory, null is returned
*/
public String lookupEntry(String name) {
int index = list.indexOf(new DirectoryEntry(name, ""));
if (index > -1) {
return ((DirectoryEntry)list.get(index)).getNumber();
} // end if
else {
return null;
} // end else
} // end lookupEntry(String name)


/** Remove an entry from the directory.
@param name The name of the person to be removed
@return The current number. If not in directory, null is
returned
*/
public String removeEntry(String name) {
/**** EXERCISE ****/
return null;
} // end removeEntry(String name)


/** Method to save the directory.
pre:  The directory has been loaded with data.
post: Contents of directory written back to the file in the
form of name-number pairs on adjacent lines.
modified is reset to false.
*/
public void save() {
//Prints the directory to Standard output instead of
//saving to a file. Implemented in this manner for ease of grading.
System.out.println("---------- SAVE -----------"); //radutt
System.out.println(list);
modified = false;
} // end save()


/** Add an entry to the directory.
@param name The name of the new person
@param number The number of the new person
*/
private void add(String name, String number) {
list.add(new DirectoryEntry(name, number));
} // end add(String name, String number)


} // end class ArrayBasedPD



public class DirectoryEntry {


private String name;
private String number;



public DirectoryEntry(String Name, String Number)
{


}


public String getName()
{


}


public void setNumber(String number)
{


}


public String getNumber()
{


}


public equals()
{


}


}
Attachments proj1.zip (15.14KB)
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Any code posted should be placed between [code] [/code] tags so that formatting and indentation is preserved.

Please ask specific questions. "Can someone help me finish writing the code so that this program will work?" is not a specific question. No one wants to complete your homework just for the sheer fun of it. If you don't understand how to implement a particular section, explain your thoughts on that section, perhaps some pseudo code, and what you have tried.

Member Avatar
rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 134 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 0 [?]
 
0
 

Sure someone can help you finish your code. But we won't do it for you. Adding to Ezzaral's suggestion, start with a small piece and get that where you need it to be. Breaking it down to the smallest possible bit you need to get something accomplished. If that's just getting one line of code to start then one line it is. When you get stuck, bounce that off of people. Then you really get help AND you learn in the process.

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

And to complete rapture's suggestion, try writing and TESTING small pieces of.
Break down he assignment into small pieces of code and test them individually.
You have written lots of stuff and you didn't explain what is the problem.
I have noticed that you read and write from files.
>Create separate classes that read and write to files. Test them using some dummy values. Create some objects with values and try to read and write. When that is done,
> Try in a different class to have methods that do what you described at the List you implemented, When that is done,
>Combine the above classes so that the List is written and read from the file.
> Then after the above work and tested, write the main method

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article