import java.util.*;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;
public class contacts
{
private static final int MAX_RECORDS = 20;
private static String lastName[] = new String[MAX_RECORDS];
private static String firstName[] = new String[MAX_RECORDS];
private static String telNumber[] = new String[MAX_RECORDS];
private static String emailAddress[] = new String[MAX_RECORDS];
private static Scanner data_input = new Scanner(System.in);
public static int read_in_file(String file_name)
{
Scanner read_in;
Scanner line;
int record_count = 0;
String record;
try
{
read_in = new Scanner(new File(file_name));
// read in one line at a time
read_in.useDelimiter(System.getProperty("line.separator"));
while (read_in.hasNext())
{
// Test to see if there are too many records in the file
if (record_count == MAX_RECORDS)
{
System.out.printf("Only %d records allowed in file", MAX_RECORDS);
System.exit(0);
}
// read in record
record = new String(read_in.next());
// Split the record up into its fields and store in
// appropriate arrays
line = new Scanner(record);
line.useDelimiter("\\s*,,\\s*");
lastName[record_count] = line.next();
firstName[record_count] = line.next();
telNumber[record_count] = line.next();
emailAddress[record_count] = line.next();
// Increment record count
record_count++;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return record_count;
}
public static void write_out_file(int no_of_records, String filename)
{
PrintStream write_out;
int counter;
try
{
// Create new file
write_out = new PrintStream(new File(filename));
// Output all reacords
for(counter = 0; counter < no_of_records; counter++)
{
// Output a record
write_out.print(lastName[counter]);
write_out.print(",,");
write_out.print(firstName[counter]);
write_out.print(",,");
write_out.print(telNumber[counter]);
write_out.print(",,");
write_out.println(emailAddress[counter]);
}
// Close file
write_out.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
// Your 'Methods' go here
public static void main(String[] args)
{
// Declare Variables
int user_choice;
boolean quit = false;
Scanner dataInput = new Scanner(System.in);
int number_of_records, records, n, sort;
String filename, first_name, last_name, enter, tel_num, email_addr;
// Get filename and read in file
System.out.print("Enter the masterfile file name: ");
filename = data_input.next();
number_of_records = read_in_file(filename);
// Your 'main' code goes here
//display menu
do
{
System.out.printf("%n%nMenu%n%n");
System.out.println("1. Search records (use * to list all)");
System.out.println("2. Insert record");
System.out.println("3. Delete record");
System.out.println("4. Quit with saving");
System.out.println("5. Quit without saving");
//get users choice
user_choice = data_input.nextInt();
//process choice
switch(user_choice)
{
case 1:
{
System.out.print("Please Enter Last Name: ");
last_name = dataInput.next();
System.out.print("Do You with to Enter First Name? (Y/N): ");
enter = dataInput.next();
if (enter.equalsIgnoreCase("N"))
{
for (int i = 0; i < number_of_records; i++)
{
if (last_name.compareTo(lastName[i]) == 0)
//Arrays.sort(lastName[i]);
{
System.out.printf("%n%s%n", lastName[i] );
System.out.printf("%s%n", firstName[i] );
System.out.printf("%s%n", telNumber[i] );
System.out.printf("%s%n", emailAddress[i] );
}
}
}
if (enter.equalsIgnoreCase("Y"))
{
System.out.print("Please Enter First Name: ");
first_name = dataInput.next();
for (int i = 0; i < number_of_records; i++)
{
if ((last_name.compareTo(lastName[i]) == 0) && (first_name.compareTo(firstName[i]) == 0))
{
System.out.printf("%n%s%n", lastName[i] );
System.out.printf("%s%n", firstName[i] );
System.out.printf("%s%n", telNumber[i] );
System.out.printf("%s%n", emailAddress[i] );
}
}
}
if (last_name.compareTo("*") == 0)
{
for (int i = 0; i < number_of_records; i++)
//There cannot be more then a 20 data records in the file
{
if (lastName[i] != last_name)
{
System.out.printf("%n%s%n", lastName[i] );
System.out.printf("%s%n", firstName[i] );
System.out.printf("%s%n", telNumber[i] );
System.out.printf("%s%n", emailAddress[i] );
}
}
}
}
break;
case 2:
{
System.out.print("Please enter the last name of the contact: ");
last_name = dataInput.next();
System.out.print("Please enter the first name of the contact: ");
first_name = dataInput.next();
System.out.print("Please enter the telephone number of the contact: ");
tel_num = dataInput.next();
System.out.print("Please enter the email address of the contact: ");
email_addr = dataInput.next();
int z = 0;
for (int i=0; i < number_of_records; i++)
{
if ((last_name.compareTo(lastName[i])) < i && (last_name.compareTo(lastName[i]) > i))
{
z = i;
}
}
for (int i = number_of_records; i > z; i--)
{
lastName[i] = lastName[i-1];
}
lastName[z+1] = last_name;
firstName[z+1] = first_name;
telNumber[z+1] = tel_num;
emailAddress[z+1] = email_addr;
}
break;
case 3:
{
System.out.print("Please enter the last name of the contact: ");
last_name = dataInput.next();
int z = 0;
for (int i=0; i < number_of_records; i++)
{
if ((last_name.compareTo(lastName[i])) < i && (last_name.compareTo(lastName[i]) > i))
{
z = i;
}
}
for (int i = z; i < number_of_records; i++)
{
lastName[i] = lastName[i+1];
}
// Get new filename and write out the file
System.out.print("Please Enter New Masterfile File Name: ");
filename = dataInput.next();
write_out_file(number_of_records, filename);
System.out.print("File Save In Progress. Please Wait...");
for (int r = 0; r < 35000000; ++ r) System.currentTimeMillis ();
{
System.out.print("..");
}
for (int r = 1000000; r < 10000000; ++ r) System.currentTimeMillis ();
{
System.out.print("............");
}
System.out.printf("%nGoodbye!");
}
break;
case 4:
{
// Get new filename and write out the file
System.out.print("Please Enter New Masterfile File Name: ");
filename = dataInput.next();
write_out_file(number_of_records, filename);
System.out.print("File Save In Progress. Please Wait...");
for (int r = 0; r < 35000000; ++ r) System.currentTimeMillis ();
{
System.out.print("..");
}
for (int r = 1000000; r < 10000000; ++ r) System.currentTimeMillis ();
{
System.out.print("............");
}
System.out.printf("%nGoodbye!");
}
break;
case 5:
quit = true;
break;
default:
System.out.println("Invalid menu choice, please try again");
}
} while(!quit);
}
}