Hi everyone, I'm taking a intro to programming in Java class at school, and we have an assignment requiring us to take the already provided hsa.Phonebook class from Ready To Program Java, and upgrading it with more methods. I've got a good amount so far, but unfortunately when the version I am about to post is compiled and executed, the lookUp function isn't quite coded correctly.
I'll let you all look at it for yourself, and tell me what you think.
The Custom_Phonebook class (I.E. The main user program)
// The "Custom_PhoneBook" class. import java.awt.*; import hsa.Console; import hsa.Stdin; public class Custom_PhoneBook { static Console c; // The output console public static void main (String[] args) { String userName; //Sets userName as a String Variable String entryName = (""); //Sets entryName as a String Variable with the value of blank String firstName = (""); //Sets firstName as a String Variable with the value of blank String lastName = (""); //Sets lastName as a String Variable with the value of blank String phoneNumber = (""); //Sets phoneNumber as a String Variable with the value of blank String address = (""); //Sets address as a String Variable with the value of blank String email = (""); //Sets email as a String Variable with the value of blank boolean loop = true; //Greets user, asks for user's name System.out.print ("Welcome to your Personal Phonebook! What's your name? "); userName = Stdin.readString (); MyPhoneBook pb = new MyPhoneBook (); String[] temp; String[] [] data; while (loop = true) { System.out.println ("1. New Entry"); System.out.println ("2. Edit Number"); System.out.println ("3. Look Up Name"); System.out.println ("4. Delete Entry"); System.out.println ("5. Quit Program"); System.out.println (""); System.out.print ("Alright then, " + (userName) + ". Please choose a option from the menu above. "); int menuChoice = Stdin.readInt (); switch (menuChoice) { case 1: System.out.println (""); System.out.println ("You chose to make a new entry."); System.out.print ("Please input the first name for your new entry. "); entryName = Stdin.readString (); temp = pb.lookUp (entryName); if (!temp[0].equals ("")) { System.out.println ("Sorry, you've already put a contact under that first name."); System.out.println (""); } else { firstName = entryName; System.out.print ("Please input the last name for your new entry. "); entryName = Stdin.readString (); temp = pb.lookUp (entryName); if (!temp[1].equals ("")) { System.out.println ("Sorry, you've already put a contact under that last name."); System.out.println (""); } else { lastName = entryName; System.out.print ("Please input the number for your new entry. "); phoneNumber = Stdin.readString (); System.out.print ("Please input the address for your new entry. "); address = Stdin.readString (); System.out.print ("Please input the email address for your new entry. "); email = Stdin.readString (); pb.add (firstName, lastName, phoneNumber, address, email); System.out.println (""); } } break; case 2: System.out.println (""); System.out.println ("You chose to edit a number."); System.out.println ("Please input the name of the person who's number "); System.out.print ("you are editing. "); entryName = Stdin.readString (); temp = pb.lookUp (entryName); pb.remove (firstName, lastName, phoneNumber, address, email); System.out.print ("Please input the first name for your new entry. "); firstName = Stdin.readString (); System.out.print ("Please input the last name for your new entry. "); lastName = Stdin.readString (); System.out.print ("Please input the new number. "); phoneNumber = Stdin.readString (); System.out.print ("Please input the address for your new entry. "); address = Stdin.readString (); System.out.print ("Please input the email address for your new entry. "); email = Stdin.readString (); pb.add (firstName, lastName, phoneNumber, address, email); System.out.println (""); break; case 3: System.out.println (""); System.out.println ("You chose to look up a name in your Phonebook."); System.out.println ("Please input the name of the person you've "); System.out.print ("entered, and use proper capitilization. "); entryName = Stdin.readString (); temp = pb.lookUp (entryName); System.out.println (""); System.out.println ("Name: " + (temp [0]) + " " + (temp [1]) + ""); System.out.println ("Number: " + (temp [2]) + " Address: " + (temp [3]) + " email: " + (temp [4]) + ""); System.out.println (""); break; case 4: System.out.println (""); System.out.println ("You chose to delete an entry."); System.out.print ("Please input the name of the user you want to delete. "); entryName = Stdin.readString (); temp = pb.lookUp (entryName); pb.remove (firstName, lastName, phoneNumber, address, email); System.out.println (""); break; case 5: System.exit (0); break; } } // Place your program here. 'c' is the output console } // main method } // Custom_PhoneBook class
The MyPhoneBook class (I.E. Where all the upgraded methods are.)
public class MyPhoneBook { int numEntries; String data[] []; String temp[]; String entryName; public MyPhoneBook () { numEntries = 0; data = new String [100] [5]; temp = new String [5]; temp [0] = ""; temp [1] = ""; } // PhoneBook constructor public void add (String firstName, String lastName, String phoneNumber, String address, String email) { data [numEntries] [0] = firstName; data [numEntries] [1] = lastName; data [numEntries] [2] = phoneNumber; data [numEntries] [3] = address; data [numEntries] [4] = email; numEntries++; } // add method public String[] lookUp (String entryName) { for (int cnt = 0 ; cnt < numEntries ; cnt++) { if (!data [cnt] [0].equals ("")) { temp [0] = data [cnt] [0]; temp [1] = data [cnt] [1]; temp [2] = data [cnt] [2]; temp [3] = data [cnt] [3]; temp [4] = data [cnt] [4]; return temp; } } return temp; } // lookUp method // Delete the entry public void remove (String firstName, String lastName, String phoneNumber, String address, String email) { for (int cnt = 0 ; cnt < numEntries ; cnt++) { if (data [cnt] [0].equals (data [cnt] [0])) { data [cnt] [0] = (""); data [cnt] [1] = (""); data [cnt] [2] = (""); data [cnt] [3] = (""); data [cnt] [4] = (""); numEntries--; } } } // remove method /* public void removeAll (String firstName, String lastName, String phoneNumber, String address, String email) { for (int cnt = 0 ; cnt < numEntries ; cnt++) //Loops following code while the count is less than the number of Contacts in the Phonebook { if (data [cnt] [0].equals (data [cnt] [0])) { data [cnt] [0] = (""); data [cnt] [1] = (""); data [cnt] [2] = (""); data [cnt] [3] = (""); data [cnt] [4] = (""); numEntries--; } } // removeAll method */ } // MyPhoneBook class
If anyone can spot what I'm doing wrong here and push me in the right direction, it would be a big help. Thanks!
- Zero