Here's what I have so far.
I have one class Directory.java and another class DirectoryWithObjectDesign.java
I need help editing my code that when executed, it will be prompted by 3 commands. Find, Delete, and Add. If asked to find, the user will input find then("stringhere"), then the program will search the .txt file to see if the name is in the file. The Add command will add the name a user inputs to the .txt file if it is not in the .txt file, and finally the delete command will delete a string in the .txt file, if it is in there. I need help adding this to my code.
Directory.Java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class Directory {
//String array directory for holding directory items
final int maxDirectorySize = 1024;
String directory[] = new String[maxDirectorySize];
int directorySize = 0;
File directoryFile = null;
Scanner directoryDataIn = null;
Directory() {
directoryFile = new File("directory.txt");
try {
directoryDataIn = new Scanner("directory.txt");
}
catch (FileNotFoundException e) {
System.out.printf("File %s not found, exiting!",
directoryFile);
System.exit(0);
//set up directoryFile to read from directoryFileName for reading
//Load data from the file into the array directory
}
// Loading Directory
while (directoryDataIn.hasNext())
directory[directorySize++] = directoryDataIn.nextLine();
}
public boolean inDirectory(/*String name*/) {
//(fill in)
//returns true if name is in directory and false otherwise
}
public boolean add(/*String name*/) {
//(fill in)
// add to directory if directory is not full
// directory size is increased by 1
// returns true if successful; false o.w
}
//returns true if successful in adding name to directory; false otherwise
public boolean delete(//enter string name here//); {
//(fill in)
// if name is in directory, remove it and shift
// other entries to use freed space; directory size
// is reduced by 1
// returns true if successful; false o.w
//returns true if successful in deleting name; false otherwise
}
public void closeDirectory() {
directoryDataIn.close();
// close explicitly before writing
PrintStream directoryDataOut = null;
// now open the directory data file for writing
try {
directoryDataOut = new PrintStream(directoryFile);
}
catch (FileNotFoundException e) {
System.out.println("File %s not found, exiting!");
System.exit(0);
}
// write updated directory back to file
for (int i = 0; i < directorySize; i++)
directoryDataOut.println(directory[i]);
directoryDataOut.close();
}
}
//sets up the directoryFile for writing (after closing it)
//write updated data to the directoryFile and close it
DirectoryWithObjectDesign.Java
public class DirectoryWithObjectDesign {
public static void main(String[] args) {
String directoryDataFile = "directory.txt";
Directory d = new Directory();
//Create/initialize the directory object
//Tell the user the system is ready and waiting to execute commands
while (stdin.hasNext()) {
String command = stdin.next().trim();
// using trim to get rid of leading whitespace
String name = stdin.nextLine().trim();
// using trim to get rid of leading + trailing whitespace
if (command.equalsIgnoreCase("find")) {
if (d.inDirectory(name))
System.out.println(name + " is in the directory");
else
System.out.println(name + " is NOT in the directory");
} else if (command.equalsIgnoreCase("add")) {
if (d.add(name))
System.out.println(name + " added");
else
System.out.println(name + " already in directory");
} else if (command.equalsIgnoreCase("delete")) {
if (d.delete(name))
System.out.println(name + " deleted");
else
System.out.println(name + " NOT in directory");
} else {
System.out.println("bad command, try again");
}
//read the command
//execute the user command by calling the appropriate method of the
//directory object and giving the user appropriate feedback
}
//close the directory
}
}
Here's the data in my directory.txt file:
Joan
Mike
Jim
Serena
Barry
Cristian
Vincent
Chengjun
Sunil