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

Pinned 5 Codes
Posted 5 Coding Articles

Send A Message
View Coders Profile
Language Java
Expires Never
Length 5,271 Characters (170 Lines)
Password no password
  1. /******************************************
  2. *Name: Gleb Sorokosh
  3. *Date: April 05, 2008
  4. *Program: A test CLASS for a friend class
  5. *******************************************/
  6. import java.util.*;
  7. public class FriendInterface{
  8.           static int logicalSize = 0;
  9.  
  10.           public static void main (String [] args){
  11.  
  12.                     Scanner input = new Scanner(System.in);
  13.  
  14.                     //array of friend objects
  15.                     Friend [] database = new Friend[20];
  16.  
  17.                     System.out.println("Welcome to Gleb\'s version of friend\'s database, please select the following :");
  18.                     System.out.println("Warning, everything is case sensitive!");
  19.                     for(;;){
  20.                               System.out.println("\n1: Add a friend");
  21.                               System.out.println("2: Display friends by last name");
  22.                               System.out.println("3: Display friends by first name");
  23.                               System.out.println("4: Find a friend");
  24.                               System.out.println("5: Delete a friend");
  25.                               System.out.println("6: Exit");
  26.                               int choice = input.nextInt();
  27.                               if (choice == 6) break;
  28.  
  29.                               //different possibilities of the option selection of a user
  30.                               switch (choice){
  31.                                         case 1:
  32.                                                   if (logicalSize >= database.length){
  33.                                                             System.out.println("Your circle of friends is full!");
  34.                                                             break;
  35.                                                   }
  36.  
  37.                                                   System.out.println("Enter First Name:");
  38.                                                   String fN = input.next();
  39.                                                   System.out.println("Enter Last Name:");
  40.                                                   String lN = input.next();
  41.                                                   System.out.println("Enter Phone number:");
  42.                                                   String phone = input.next();
  43.                                                   System.out.println("Enter E-Mail:");
  44.                                                   String email = input.next();
  45.  
  46.                                                   database[logicalSize] = new Friend(fN, lN, phone, email);
  47.  
  48.                                                   logicalSize++;
  49.                                                   break;
  50.  
  51.                                         case 2:
  52.                                                   sortByLastName(database);
  53.                                                   for(int i=0 ; i<logicalSize ; i++){
  54.                                                             System.out.println(database[i].joinedDataLastName());
  55.                                                   }
  56.                                                   break;
  57.  
  58.                                         case 3:
  59.                                                   sortByFirstName(database);
  60.                                                   for(int i=0 ; i<logicalSize ; i++){
  61.                                                             System.out.println(database[i].joinedDataFirstName());
  62.                                                   }
  63.                                                   break;
  64.  
  65.                                         case 4:
  66.                                                   //String lN, fN, phone, email;
  67.                                                   System.out.println("Enter last name of a friend you want to find:");
  68.                                                   lN = input.next();
  69.                                                   System.out.println("Enter first name of a friend you want to find:");
  70.                                                   fN = input.next();
  71.                                                   System.out.println("Enter Phone number:");
  72.                                                   phone = input.next();
  73.                                                   System.out.println("Enter E-Mail:");
  74.                                                   email = input.next();
  75.  
  76.                                                   Friend f = new Friend(fN, lN, phone, email);
  77.                                                   int position = findFriend (database, 0, logicalSize-1, f);
  78.                                                   if (position == -1){
  79.                                                             System.out.println("Sorry, your given friend was not found! Ty again.");
  80.                                                             break;
  81.                                                   }
  82.                                                   System.out.println("Your friend is #"+ (position+1) +" in your list of friends");
  83.  
  84.                                                   break;
  85.  
  86.                                         case 5:
  87.                                                   System.out.println("Here is the list of friends you have so far...");
  88.  
  89.                                                   int count=1;
  90.                                                                                                     //for every Friend in the friend database array,
  91.                                                   for(int cell=0 ; cell < logicalSize ; cell++){ //output joinedDataLastName() member function in a Friend class
  92.                                                             System.out.println(count +": "+ database[cell].joinedDataLastName());
  93.                                                             count++;
  94.                                                   }
  95.                                                   System.out.println("\nPick the one you want to delete:\n");
  96.                                                   count = input.nextInt();// this is a location of a Friend (plus one) the user wants to delete
  97.                                                   count--;//decremented because arrays start at position zero
  98.  
  99.                                                   //Shift items up by one position, and decrement logical Size
  100.                                                   for(int i = count; i < logicalSize-1 ; i++)
  101.                                                             database[i] = database[i+1];
  102.  
  103.                                                   logicalSize--;
  104.                                                   break;
  105.  
  106.                                         default:
  107.                                                   System.out.println("Oops, wrong choice, please try again");
  108.  
  109.                               }
  110.                     }
  111.           }
  112.  
  113.  
  114.           private static void sortByFirstName(Friend [] fDatabase){
  115.  
  116.                     int count = 0;
  117.                     boolean change = true;
  118.                     while ( (count < fDatabase.length - 1) && change){
  119.                               change = false;
  120.                               count++;
  121.                               for (int i = 0 ; i < fDatabase.length - count ; i++){
  122.  
  123.                                         if ((fDatabase[i].getFN().compareTo(fDatabase[i+1].getFN())) > 0){
  124.                                                   swap(fDatabase, i, i+1);
  125.                                                   change = true;
  126.                                         }
  127.                               }
  128.                     }
  129.           }
  130.  
  131.           private static void sortByLastName(Friend [] fDatabase){
  132.  
  133.                     int count = 0;
  134.                     boolean change = true;
  135.                     while ( (count < fDatabase.length - 1) && change){
  136.                               change = false;
  137.                               count++;
  138.                               for (int i = 0 ; i < fDatabase.length - count ; i++){
  139.  
  140.                                         if ((fDatabase[i].getLN().compareTo(fDatabase[i+1].getLN())) > 0){
  141.                                                   swap(fDatabase, i, i+1);
  142.                                                   change = true;
  143.                                         }
  144.                               }
  145.                     }
  146.           }
  147.  
  148.           private static void swap ( Friend [] fDatabase, int x, int y ) {
  149.                     Friend temp = fDatabase [x];
  150.                     fDatabase [x] = fDatabase [y];
  151.                     fDatabase [y] = temp;
  152.           }
  153.  
  154.           //following method uses binary search algorithm
  155.           private static int findFriend(Friend [] fDatabase, int left, int right, Friend goal){
  156.  
  157.                     int midpoint = (left+right)/2;
  158.  
  159.                     if (left > right)
  160.                               return -1; // indicates that friend was not found
  161.  
  162.                     if(( (Comparable) fDatabase[midpoint]).compareTo( (Comparable) goal) == 0)
  163.                               return midpoint;
  164.  
  165.                     if(( (Comparable) fDatabase[midpoint]).compareTo( (Comparable) goal) > 0)
  166.                               return findFriend(fDatabase, left, midpoint, goal);
  167.  
  168.                     return findFriend(fDatabase, midpoint, right, goal)          ;
  169.           }
  170. }
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