Hey, I created a phone directory program. But I was wondering if I could get some hints on how I could create a txt file and add, delete names and numbers on the txt file.
import java.util.*;
import java.lang.*;
import java.lang.Object;
import jpb.*;
public class PhoneDir {
private static LinkedList phone;
public static void main(String[] args) {
//Display commands
System.out.println("List of Commands:\n" +
" a - Show 'a'll records\n" +
" d - 'd'elete the current record\n" +
" f - change the 'f'irst name in current record\n" +
" l - change the 'l'ast name in current record\n" +
" n - add a 'n'ew record\n" +
" p - change the 'p'hone number in current record\n" +
" q - 'q'uit\n" +
" s - 's'elect a record from record list\n");
//Prompt user to enter a command
SimpleIO.prompt("Enter a command from the list above: ");
String command = (String) SimpleIO.readLine();
//Determine if command is legal.
if (command.equalsIgnoreCase("a"))
{
showAll();
}
else if (command.equalsIgnoreCase("d"))
{
deleteRecord();
}
else if (command.equalsIgnoreCase("f"))
{
changeFirst();
}
else if (command.equalsIgnoreCase("l"))
{
changeLast();
}
else if (command.equalsIgnoreCase("n"))
{
addNew();
}
else if (command.equalsIgnoreCase("p"))
{
changePhone();
}
else if (command.equalsIgnoreCase("q"))
{
quit();
}
else if (command.equalsIgnoreCase("s"))
{
selectRecord();
}
else
{
//Illegal command will display an error message.
System.out.println("Illegal command");
}
System.out.println();
}
//Shows all records in the directory
private static void showAll(){
try{
for (int i = 0; i < phone.size(); i++)
System.out.println(phone);
}
catch (Exception e) {
}
}
//Deletes the current record
private static void deleteRecord()
{
SimpleIO.prompt("Enter name to delete: ");
String key = SimpleIO.readLine();
for (int i = 0; i < phone.size(); i++)
{
PhoneDir curr = (PhoneDir) phone.get(i);
String name = curr.getName();
if (name.startsWith(key));
phone.remove((String) key);
}
}
//Changes the first name of the current record
private static void changeFirst()
{
SimpleIO.prompt("Enter name to change: ");
String key = SimpleIO.readLine();
for (int i = 0; i < phone.size(); i++)
{
PhoneDir curr = (PhoneDir) phone.get(i);
String FirstName = curr.getFirstName();
if (FirstName.startsWith(key))
System.out.println(curr.getFirstName() + " " + curr.getFirstName());
}
SimpleIO.prompt("Enter name change:");
phone.add((String) key);
}
//Change the last name of the current record
private static void changeLast()
{
SimpleIO.prompt("Enter name to change: ");
String key = SimpleIO.readLine();
for (int i = 0; i < phone.size(); i++)
{
PhoneDir curr = (PhoneDir) phone.get(i);
@SuppressWarnings("LocalVariableHidesMemberVariable")
String LastName = curr.getLastName().toLowerCase();
if (LastName.startsWith(key))
System.out.println(curr.getLastName() + " " + curr.getLastName());
}
SimpleIO.prompt("Enter name changes: ");
phone.add((String) key);
}
//Add a new record
private static void addNew()
{
try {
int number;
SimpleIO.prompt("Enter name: ");
String name = (String) SimpleIO.readLine();
System.out.print("Enter phone number: " );
Scanner input = new Scanner(System.in);
number = input.nextInt();
phone.add(name);
phone.add(number);
}
catch (Exception e){
}
}
//Change the phone number of the current record
private static void changePhone()
{
SimpleIO.prompt("Enter number to change: ");
String key = (String)SimpleIO.readLine();
for (int i = 0; i < phone.size(); i++)
{
PhoneDir curr = (PhoneDir) phone.get(i);
String name = (String) curr.getNumber().toLowerCase();
if (name.startsWith(key))
System.out.println(curr.getNumber() + " " + curr.getNumber());
phone.add((String) key);
}
}
//Quits the program
private static void quit()
{
System.exit(0);
}
//Selects a record from the record list
private static void selectRecord()
{
SimpleIO.prompt("Enter name to change: ");
String key = SimpleIO.readLine();
for (int i = 0; i < phone.size(); i++)
{
PhoneDir curr = (PhoneDir) phone.get(i);
String name = curr.getName().toLowerCase();
if (name.startsWith(key))
System.out.println(curr.getName() + " " + curr.getName());
}
}
private String FirstName;
private String LastName;
private String name;
private String number;
public String getFirstName()
{
return FirstName;
}
public String getLastName()
{
return LastName;
}
public String getName()
{
return name;
}
public String getNumber()
{
return number;
}
}