1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A Phone List Application coded in Java

Discussion in 'Other Languages' started by sfidirectory, Apr 8, 2011.

  1. sfidirectory

    sfidirectory Senior Member

    Joined:
    Mar 29, 2010
    Messages:
    899
    Likes Received:
    483
    Occupation:
    Web developer/BTC enthusiast
    Location:
    php artisan make:migration
    Home Page:
    Hi All,

    Here is an application that utilises Java's HashMap class to enable a user to enter a persons name and number (seperated by whitespace), removes the entry for a given name, returns the number for a given name, and prints out all the entrys in the HashMap (where the names and numbers are kept). If you are able to use the cmd line (preferably Linux) you can

    *add a number like this:
    Code:
    a bob 123-456-7890
    *remove a number for a given name like this:
    Code:
    r peter
    *get a number from a person already in the list:
    Code:
    g paul
    *print out the whole HashMap (all the name-number pairings) currently in the list:
    Code:
    p
    Here is the code if you want to give it a shot (remember to compile BOTH files first, AND put BOTH files in the SAME folder :)):

    THE MAIN CLASS:

    Code:
    /** File PhoneListApp.java 1/4/2011 */
    
    import java.util.*;
    
    /** The main class which utilise the methods and instances contained in
     *  the supporting class PhoneList.java.
     */
    public class PhoneListApp{
    
       private static String number;
       private static String name;
       private static PhoneList list;
       private static Scanner input;
    
       /** The main method of the application class. Prompts the user for
        *  some input which is needed for the application to run. "a" = add
        *  a name and number (eg. Paul 123-456-7890. "g" = get the number for
        *  a particular key (eg. g paul would return the matching value
        *  (number) to the key (paul). "p" prints out the current pairs
        *  of keys and values that are in the hash map. "r" removes a
        *  key-value pair, given the key's name entered.
        *
        * @param args a String array of arguements.
        */
       public static void main(String[] args){
          input = new Scanner(System.in);
          list = new PhoneList();
          while(input.hasNextLine()){
             handleArray(input.nextLine(), list);
          }
       }
    
       /** handleArray(): Handles the user's inputs and uses
        *  switch statements to determine what the user wants to
        *  do, given a preset of commands and associated methods
        *  from the support class.
        *
        * @param t User input for command (either a,g,r or p)
        * @param numList the phone number list which will be
        * used for this application.
        */
       public static void handleArray(String t, PhoneList numList){
          Scanner tokens = new Scanner(t);
          if(tokens.hasNext("[agrp]")){ // if next token is a, g, r or p
             char command = tokens.next().charAt(0);
             switch(command){
             case 'a':  // Add an entry for the given name and number
                if(tokens.hasNext()){
                   String name = tokens.next();
                   if(tokens.hasNext()){
                      String number = tokens.next();
                      list.addEntry(name, number);
                   }
                }
                break;
    
             case 'g': // Print out the number for a given
                // name (if it's in the list)
    
                if(tokens.hasNext()){
                   name = tokens.next();
                   if(list.contains(name)){
                      number = list.getNumber(name);
                      System.out.println(number);
                   }
                }
                 
                break;
    
             case 'r': // Remove the entry for a given name, if
                // it is in the list
                if(tokens.hasNext()){
                   name = tokens.next();
                   if(list.contains(name)){
                      list.remove(name);
                   }
                }
                 
                break;
    
             case 'p': // print out the list using a toString method.
                System.out.println(list);
    
    
                break;
             
             }
          
          }
       }
    }
    THE SUPPORTING CLASS:
    Code:
    /** File PhoneList.java: 29/3/2011 */
    
    import java.util.*;
    import java.util.HashMap;
    
    /** This supporting class contains the methods that are needed for
     *  the main application class (PhoneListApp) to function.
     */
    public class PhoneList{
    
       private Map<String,String> plist;
    
       /**Constructor which creates a new phonelist using hash mapping */
       public PhoneList(){
          plist = new HashMap<String,String>();
       }
    
       /** addEntry(): a void method that adds an entry to the HashMap,
        *  which consists of a persons name and their phone number.
        *
        * @param name The persons name.
        * @param number The persons phone number.
        */
       public void addEntry(String name, String number){
          String addName = name;
          String addNumber = number;
    
          plist.put(addName, addNumber);
       }
    
       /** getNumber(): A method that returns a persons
        *  phone number which is attributed to the persons
        *  name. The number is returned in String format.
        *
        * @param name The persons name.
        * @return result The persons phone number
        */
       public String getNumber(String name){
    
          System.err.println(name);
          
          String result = "";
          if(plist.containsKey(name)){
             result += plist.get(name);;
          }
          return result;
       }
    
       /** remove(): A void method that removes an entry for
        *  a given name (an entry is the name AND phone number).
        *
        * @param name The persons name for the entry you want
        * to remove.
        */
       public void remove(String name){
    
          if(plist.containsKey(name)){
             plist.remove(name);
          }
       }
    
       /** toString(): This method returns the list in a readable
        *  String format.
        *
        * @return theList the list of entries which are printed out.
        */
       public String toString(){
          return plist.toString();
          
       }
    
       /** size(): returns the size of the phone list "plist".
        *
        * @return theSize The size of the phone list plist.
        */
       public int size(){//NOT IMPLEMENTED YET
          int theSize = 0;
          theSize+= plist.size();
          return theSize;
          
       }
    
       /** contains(): a method that returns true if there is
        *  an entry for a given person's name.
        *
        * @return true If there is an entry. false If there isn't an entry.
        */
       public boolean contains(String name){
          if(plist.containsKey(name)){
             return true;
          }
          else{
             return false;
          }
       }
    }
    Let me know what you think :). If I have time at the end of the semester (or sooner, depending on assessments, assignments etc) I might code up a GUI for it.