public class Friend {
private String firstName;
private String lastName;
private String phoneNumber;
private String addr;
public Friend(String fn, String ln, String phone, String a)
{
firstName = fn;
lastName = ln;
phoneNumber = phone;
addr = a;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getPhoneNumber()
{
return phoneNumber;
}
public String getAddr()
{
return addr;
}
public String toString()
{
return "Name: "+firstName+" "+lastName+"\nPhone Number: "+phoneNumber+"\nEmail: "+addr;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class FriendDatabase {
public static final int MAX_FRIENDS=20;
Friend [] database = new Friend[MAX_FRIENDS];
int logicalSz;
public FriendDatabase()
{
logicalSz=0;
}
public void printFriends()
{
for (int cntIndex=0; cntIndex < logicalSz; cntIndex++)
{
System.out.println( database[cntIndex] );
System.out.println( "------------" );
}
}
public void sortByLast()
{
for (int cntIndex=1; cntIndex < logicalSz; cntIndex++)
{
boolean stillLooking = true;
Friend itmToInsert = database[cntIndex];
int walkBackIndex = cntIndex-1;
while (walkBackIndex >= 0 && stillLooking)
{
if ( itmToInsert.getLastName().compareTo(database[walkBackIndex].getLastName()) < 0 )
{
database[walkBackIndex+1] = database[walkBackIndex];
walkBackIndex--;
}
else
{
stillLooking = false;
}
database[walkBackIndex+1] = itmToInsert;
}
}
}
public void sortByFirst()
{
for (int cntIndex=1; cntIndex < logicalSz; cntIndex++)
{
boolean stillLooking = true;
Friend itmToInsert = database[cntIndex];
int walkBackIndex = cntIndex-1;
while (walkBackIndex >= 0 && stillLooking)
{
if ( itmToInsert.getFirstName().compareTo(database[walkBackIndex].getFirstName()) < 0 )
{
database[walkBackIndex+1] = database[walkBackIndex];
walkBackIndex--;
}
else
{
stillLooking = false;
}
database[walkBackIndex+1] = itmToInsert;
}
}
}
public void findAndPrint(String lastNameToken)
{
int index = find(lastNameToken);
if (index == -1)
{
System.out.println("Could not find friend!");
}
else
{
System.out.println( database[index] );
}
}
public int find(String lastNameToken)
{
sortByLast();
int left=0;
int right=logicalSz-1;
while (left <= right)
{
int mid = (left+right)/2;
if ( database[mid].getLastName().equals(lastNameToken) )
{
return mid;
}
else if ( database[mid].getLastName().compareTo(lastNameToken) > 0)
{
right = mid-1;
}
else
{
left = mid+1;
}
}
return -1;
}
public boolean removeFriend(String lastNameToken)
{
if (logicalSz <= 0)
{
return false;
}
int deleteIndex = find(lastNameToken);
if (deleteIndex == -1)
{
return false;
}
for (int cntIndex=deleteIndex; cntIndex < logicalSz; cntIndex++)
{
database[cntIndex] = database[cntIndex+1];
}
logicalSz--;
return true;
}
private int findInsertionPoint(String lastName)
{
sortByLast();
for (int cntIndex=0; cntIndex < logicalSz; cntIndex++)
{
if ( database[cntIndex].getLastName().equals(lastName) )
{
return cntIndex;
}
}
return 0;
}
public boolean insertFriend(String fn, String ln, String phone, String addr)
{
if (logicalSz == MAX_FRIENDS)
{
return false;
}
else
{
int insertionPnt = findInsertionPoint(ln);
for (int cntIndex=logicalSz; cntIndex > insertionPnt; cntIndex--)
{
database[cntIndex] = database[cntIndex-1];
}
database[insertionPnt] = new Friend(fn, ln, phone, addr);
logicalSz++;
return true;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.*;
public class FriendInterface {
private static void dispMenu()
{
System.out.println("\n\n~~~~~~~~~~~~~~~~~~Menu~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("1) 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) Quit");
System.out.print("> ");
}
public static void main(String [] args)
{
FriendDatabase friends = new FriendDatabase();
friends.insertFriend("Oleksi", "Derkatch", "416", "s");
friends.insertFriend("Aleksi", "Zerkatch", "416", "s");
friends.insertFriend("Pleksi", "Cerkatch", "416", "s");
int choice=0;
while (choice!=6)
{
Scanner in = new Scanner(System.in);
dispMenu();
choice = in.nextInt();
in.nextLine();
switch(choice)
{
case 1:
String firstName;
String lastName;
String phone;
String addr;
System.out.print("First Name: ");
firstName = in.nextLine();
System.out.print("Last Name: ");
lastName = in.nextLine();
System.out.print("Phone Number ");
phone = in.nextLine();
System.out.print("Email: ");
addr = in.nextLine();
if ( friends.insertFriend(firstName, lastName, phone, addr) )
{
System.out.println("Friend added successfully!");
}
else
{
System.out.println("Error. There is no more room to add another friend");
System.out.println("This program can currently only handle "+FriendDatabase.MAX_FRIENDS+" friends.");
}
break;
case 2:
friends.sortByLast();
friends.printFriends();
break;
case 3:
friends.sortByFirst();
friends.printFriends();
break;
case 4:
String searchToken;
System.out.print("Enter the last name of the friend to search for: ");
searchToken = in.nextLine();
friends.findAndPrint(searchToken);
break;
case 5:
String deleteToken;
System.out.print("Enter the last name of the friend to delete: ");
deleteToken = in.nextLine();
if ( !friends.removeFriend(deleteToken) )
{
System.out.println("Could not find friend!");
}
else
{
System.out.println("Friend deleted!");
}
break;
case 6:
System.out.println("Good-bye!");
break;
default:
System.out.println("Invalid Choice. Enter 1 to 6 only!");
}
}
}
}