Coder Profile - Show off your skills, get a coder profile.
 
 
 
code pin board Friends Download Source Code
Author Details Code Information
Cinjection ( Oleksi Derkatch )

Pinned 13 Codes
Posted 8 Coding Articles

Send A Message
View Coders Profile
Language Java
Expires Never
Length 6,885 Characters (317 Lines)
Password no password
Description

Enjoy
  1. public class Friend {
  2.  
  3.           private String firstName;
  4.           private String lastName;
  5.           private String phoneNumber;
  6.           private String addr;
  7.  
  8.           public Friend(String fn, String ln, String phone, String a)
  9.           {
  10.                     firstName = fn;
  11.                     lastName = ln;
  12.                     phoneNumber = phone;
  13.                     addr = a;
  14.           }
  15.  
  16.           public String getFirstName()
  17.           {
  18.                     return firstName;
  19.           }
  20.  
  21.           public String getLastName()
  22.           {
  23.                     return lastName;
  24.           }
  25.  
  26.           public String getPhoneNumber()
  27.           {
  28.                     return phoneNumber;
  29.           }
  30.  
  31.           public String getAddr()
  32.           {
  33.                     return addr;
  34.           }
  35.  
  36.           public String toString()
  37.           {
  38.                     return "Name: "+firstName+" "+lastName+"\nPhone Number: "+phoneNumber+"\nEmail: "+addr;
  39.           }
  40.  
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  43.  
  44. public class FriendDatabase {
  45.  
  46.           public static final int MAX_FRIENDS=20;
  47.  
  48.           Friend [] database = new Friend[MAX_FRIENDS];
  49.           int logicalSz;
  50.  
  51.           public FriendDatabase()
  52.           {
  53.                     logicalSz=0;
  54.           }
  55.  
  56.           public void printFriends()
  57.           {
  58.                     for (int cntIndex=0; cntIndex < logicalSz; cntIndex++)
  59.                     {
  60.                               System.out.println( database[cntIndex] );
  61.                               System.out.println( "------------" );
  62.                     }
  63.           }
  64.  
  65.           public void sortByLast()
  66.           {
  67.                     for (int cntIndex=1; cntIndex < logicalSz; cntIndex++)
  68.                     {
  69.                               boolean stillLooking = true;
  70.                               Friend itmToInsert = database[cntIndex];
  71.                               int walkBackIndex = cntIndex-1;
  72.  
  73.                               while (walkBackIndex >= 0 && stillLooking)
  74.                               {
  75.                                         if ( itmToInsert.getLastName().compareTo(database[walkBackIndex].getLastName()) < 0 )
  76.                                         {
  77.                                                   database[walkBackIndex+1] = database[walkBackIndex];
  78.                                                   walkBackIndex--;
  79.                                         }
  80.                                         else
  81.                                         {
  82.                                                   stillLooking = false;
  83.                                         }
  84.  
  85.                                         database[walkBackIndex+1] = itmToInsert;
  86.                               }
  87.                     }
  88.           }
  89.  
  90.           public void sortByFirst()
  91.           {
  92.                     for (int cntIndex=1; cntIndex < logicalSz; cntIndex++)
  93.                     {
  94.                               boolean stillLooking = true;
  95.                               Friend itmToInsert = database[cntIndex];
  96.                               int walkBackIndex = cntIndex-1;
  97.  
  98.                               while (walkBackIndex >= 0 && stillLooking)
  99.                               {
  100.                                         if ( itmToInsert.getFirstName().compareTo(database[walkBackIndex].getFirstName()) < 0 )
  101.                                         {
  102.                                                   database[walkBackIndex+1] = database[walkBackIndex];
  103.                                                   walkBackIndex--;
  104.                                         }
  105.                                         else
  106.                                         {
  107.                                                   stillLooking = false;
  108.                                         }
  109.  
  110.                                         database[walkBackIndex+1] = itmToInsert;
  111.                               }
  112.                     }
  113.           }
  114.  
  115.           public void findAndPrint(String lastNameToken)
  116.           {
  117.                     int index = find(lastNameToken);
  118.  
  119.                     if (index == -1)
  120.                     {
  121.                               System.out.println("Could not find friend!");
  122.                     }
  123.                     else
  124.                     {
  125.                               System.out.println( database[index] );
  126.                     }
  127.           }
  128.  
  129.           public int find(String lastNameToken)
  130.           {
  131.                     sortByLast();
  132.                     int left=0;
  133.                     int right=logicalSz-1;
  134.  
  135.                     while (left <= right)
  136.                     {
  137.                               int mid = (left+right)/2;
  138.  
  139.                               if ( database[mid].getLastName().equals(lastNameToken) )
  140.                               {
  141.                                         return mid;
  142.                               }
  143.                               else if ( database[mid].getLastName().compareTo(lastNameToken) > 0)
  144.                               {
  145.                                         right = mid-1;
  146.                               }
  147.                               else
  148.                               {
  149.                                         left = mid+1;
  150.                               }
  151.                     }
  152.                     return -1;
  153.           }
  154.  
  155.           public boolean removeFriend(String lastNameToken)
  156.           {
  157.                     if (logicalSz <= 0)
  158.                     {
  159.                               return false;
  160.                     }
  161.  
  162.                     int deleteIndex = find(lastNameToken);
  163.  
  164.                     if (deleteIndex == -1)
  165.                     {
  166.                               return false;
  167.                     }
  168.  
  169.                     for (int cntIndex=deleteIndex; cntIndex < logicalSz; cntIndex++)
  170.                     {
  171.                               database[cntIndex] = database[cntIndex+1];
  172.                     }
  173.                     logicalSz--;
  174.                     return true;
  175.           }
  176.  
  177.           private int findInsertionPoint(String lastName)
  178.           {
  179.                     sortByLast();
  180.                     for (int cntIndex=0; cntIndex < logicalSz; cntIndex++)
  181.                     {
  182.                                if ( database[cntIndex].getLastName().equals(lastName) )
  183.                                {
  184.                                          return cntIndex;
  185.                                }
  186.                     }
  187.                     return 0;
  188.           }
  189.  
  190.           public boolean insertFriend(String fn, String ln, String phone, String addr)
  191.           {
  192.                     if (logicalSz == MAX_FRIENDS)
  193.                     {
  194.                               return false;
  195.                     }
  196.                     else
  197.                     {
  198.                               int insertionPnt = findInsertionPoint(ln);
  199.  
  200.                               for (int cntIndex=logicalSz; cntIndex > insertionPnt; cntIndex--)
  201.                               {
  202.                                         database[cntIndex] = database[cntIndex-1];
  203.                               }
  204.  
  205.                               database[insertionPnt] = new Friend(fn, ln, phone, addr);
  206.                               logicalSz++;
  207.                               return true;
  208.                     }
  209.           }
  210. }
  211. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  212. import java.util.*;
  213.  
  214. public class FriendInterface {
  215.  
  216.           private static void dispMenu()
  217.           {
  218.                     System.out.println("\n\n~~~~~~~~~~~~~~~~~~Menu~~~~~~~~~~~~~~~~~~~~~~~~");
  219.                     System.out.println("1) Add a friend.");
  220.                     System.out.println("2) Display friends by last name.");
  221.                     System.out.println("3) Display friends by first name.");
  222.                     System.out.println("4) Find a friend.");
  223.                     System.out.println("5) Delete a friend.");
  224.                     System.out.println("6) Quit");
  225.                     System.out.print("> ");
  226.           }
  227.  
  228.           public static void main(String [] args)
  229.           {
  230.                     FriendDatabase friends = new FriendDatabase();
  231.  
  232.                     friends.insertFriend("Oleksi", "Derkatch", "416", "s");
  233.                     friends.insertFriend("Aleksi", "Zerkatch", "416", "s");
  234.                     friends.insertFriend("Pleksi", "Cerkatch", "416", "s");
  235.  
  236.                     int choice=0;
  237.                     while (choice!=6)
  238.                     {
  239.                               Scanner in = new Scanner(System.in);
  240.                               dispMenu();
  241.                               choice = in.nextInt();
  242.                               in.nextLine();
  243.  
  244.                               switch(choice)
  245.                               {
  246.                                         case 1:
  247.                                                   String firstName;
  248.                                                   String lastName;
  249.                                                   String phone;
  250.                                                   String addr;
  251.  
  252.                                                   System.out.print("First Name: ");
  253.                                                   firstName = in.nextLine();
  254.  
  255.                                                   System.out.print("Last Name: ");
  256.                                                   lastName = in.nextLine();
  257.  
  258.                                                   System.out.print("Phone Number ");
  259.                                                   phone = in.nextLine();
  260.  
  261.                                                   System.out.print("Email: ");
  262.                                                   addr = in.nextLine();
  263.  
  264.                                                   if ( friends.insertFriend(firstName, lastName, phone, addr) )
  265.                                                   {
  266.                                                             System.out.println("Friend added successfully!");
  267.                                                   }
  268.                                                   else
  269.                                                   {
  270.                                                             System.out.println("Error. There is no more room to add another friend");
  271.                                                             System.out.println("This program can currently only handle "+FriendDatabase.MAX_FRIENDS+" friends.");
  272.                                                   }
  273.                                                   break;
  274.  
  275.                                         case 2:
  276.                                                   friends.sortByLast();
  277.                                                   friends.printFriends();
  278.                                                   break;
  279.  
  280.                                         case 3:
  281.                                                   friends.sortByFirst();
  282.                                                   friends.printFriends();
  283.                                                   break;
  284.  
  285.                                         case 4:
  286.                                                   String searchToken;
  287.                                                   System.out.print("Enter the last name of the friend to search for: ");
  288.                                                   searchToken = in.nextLine();
  289.                                                   friends.findAndPrint(searchToken);
  290.                                                   break;
  291.  
  292.                                         case 5:
  293.                                                   String deleteToken;
  294.                                                   System.out.print("Enter the last name of the friend to delete: ");
  295.                                                   deleteToken = in.nextLine();
  296.                                                   if ( !friends.removeFriend(deleteToken) )
  297.                                                   {
  298.                                                             System.out.println("Could not find friend!");
  299.                                                   }
  300.                                                   else
  301.                                                   {
  302.                                                             System.out.println("Friend deleted!");
  303.                                                   }
  304.                                                   break;
  305.  
  306.                                         case 6:
  307.                                                   System.out.println("Good-bye!");
  308.                                                   break;
  309.  
  310.                                         default:
  311.                                                   System.out.println("Invalid Choice. Enter 1 to 6 only!");
  312.                               }
  313.                     }
  314.  
  315.           }
  316.  
  317. }
code pin board Back To Code Pin Board Post New Code
Please login to post comments.
Page 1 of 1
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved