/******************************************
*Name: Gleb Sorokosh
*Date: April 05, 2008
*Program: A test CLASS for a friend class
*******************************************/
import java.util.*;
public class FriendInterface{
static int logicalSize = 0;
public static void main (String [] args){
Scanner input = new Scanner(System.in);
//array of friend objects
Friend [] database = new Friend[20];
System.out.println("Welcome to Gleb\'s version of friend\'s database, please select the following :");
System.out.println("Warning, everything is case sensitive!");
for(;;){
System.out.println("\n1: Add a friend");
System.out.println("2: Display friends by last name");
System.out.println("3: Display friends by first name");
System.out.println("4: Find a friend");
System.out.println("5: Delete a friend");
System.out.println("6: Exit");
int choice = input.nextInt();
if (choice == 6) break;
//different possibilities of the option selection of a user
switch (choice){
case 1:
if (logicalSize >= database.length){
System.out.println("Your circle of friends is full!");
break;
}
System.out.println("Enter First Name:");
String fN = input.next();
System.out.println("Enter Last Name:");
String lN = input.next();
System.out.println("Enter Phone number:");
String phone = input.next();
System.out.println("Enter E-Mail:");
String email = input.next();
database[logicalSize] = new Friend(fN, lN, phone, email);
logicalSize++;
break;
case 2:
sortByLastName(database);
for(int i=0 ; i<logicalSize ; i++){
System.out.println(database[i].joinedDataLastName());
}
break;
case 3:
sortByFirstName(database);
for(int i=0 ; i<logicalSize ; i++){
System.out.println(database[i].joinedDataFirstName());
}
break;
case 4:
//String lN, fN, phone, email;
System.out.println("Enter last name of a friend you want to find:");
lN = input.next();
System.out.println("Enter first name of a friend you want to find:");
fN = input.next();
System.out.println("Enter Phone number:");
phone = input.next();
System.out.println("Enter E-Mail:");
email = input.next();
Friend f = new Friend(fN, lN, phone, email);
int position = findFriend (database, 0, logicalSize-1, f);
if (position == -1){
System.out.println("Sorry, your given friend was not found! Ty again.");
break;
}
System.out.println("Your friend is #"+ (position+1) +" in your list of friends");
break;
case 5:
System.out.println("Here is the list of friends you have so far...");
int count=1;
//for every Friend in the friend database array,
for(int cell=0 ; cell < logicalSize ; cell++){ //output joinedDataLastName() member function in a Friend class
System.out.println(count +": "+ database[cell].joinedDataLastName());
count++;
}
System.out.println("\nPick the one you want to delete:\n");
count = input.nextInt();// this is a location of a Friend (plus one) the user wants to delete
count--;//decremented because arrays start at position zero
//Shift items up by one position, and decrement logical Size
for(int i = count; i < logicalSize-1 ; i++)
database[i] = database[i+1];
logicalSize--;
break;
default:
System.out.println("Oops, wrong choice, please try again");
}
}
}
private static void sortByFirstName(Friend [] fDatabase){
int count = 0;
boolean change = true;
while ( (count < fDatabase.length - 1) && change){
change = false;
count++;
for (int i = 0 ; i < fDatabase.length - count ; i++){
if ((fDatabase[i].getFN().compareTo(fDatabase[i+1].getFN())) > 0){
swap(fDatabase, i, i+1);
change = true;
}
}
}
}
private static void sortByLastName(Friend [] fDatabase){
int count = 0;
boolean change = true;
while ( (count < fDatabase.length - 1) && change){
change = false;
count++;
for (int i = 0 ; i < fDatabase.length - count ; i++){
if ((fDatabase[i].getLN().compareTo(fDatabase[i+1].getLN())) > 0){
swap(fDatabase, i, i+1);
change = true;
}
}
}
}
private static void swap ( Friend [] fDatabase, int x, int y ) {
Friend temp = fDatabase [x];
fDatabase [x] = fDatabase [y];
fDatabase [y] = temp;
}
//following method uses binary search algorithm
private static int findFriend(Friend [] fDatabase, int left, int right, Friend goal){
int midpoint = (left+right)/2;
if (left > right)
return -1; // indicates that friend was not found
if(( (Comparable) fDatabase[midpoint]).compareTo( (Comparable) goal) == 0)
return midpoint;
if(( (Comparable) fDatabase[midpoint]).compareTo( (Comparable) goal) > 0)
return findFriend(fDatabase, left, midpoint, goal);
return findFriend(fDatabase, midpoint, right, goal) ;
}
}