Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have most of it done, I just need help fixing some things. import java.util.Scanner; public class Main { private String[] database;

imageimage

I have most of it done, I just need help fixing some things. 

import java.util.Scanner;    public class Main {      private String[] database;      private Scanner input;        public Main() {          database = new String[0];          input = new Scanner(System.in);      }        public String[] add(String info) {            int newSpace = database.length + 1;            String[] newArr = new String[newSpace];            for (int i = 0; i < database.length; i++)              newArr[i] = database[i];            newArr[newSpace - 1] = info;            return newArr;        }        public int search(final String name) {                    int position = -1;                       for (int i = 0; i < database.length; i++) {                            String nameTrim = database[i];              nameTrim = nameTrim.substring(0, nameTrim.indexOf(" "));                                               if (nameTrim.compareTo(name) == 0) {                                position = i;                  break;              }          }                    return position;             }        public void displayAll() {            for (int i = 0; i < database.length; i++) {                System.out.println(database[i] + "\n");            }        }        public boolean remove(final String name) {          int pos = search(name);          if (pos >= 0) {              String[] temp = new String[database.length - 1];              System.arraycopy(database, 0, temp, 0, pos);              System.arraycopy(database, pos + 1, temp, pos, database.length - pos - 1);              database = temp;              return true;          }          return false;      }        public void displayMenu() {          System.out.println("\n\n\n1) Add");          System.out.println("2) Delete");          System.out.println("3) Search");          System.out.println("4) Display All");          System.out.println("5) Exit\n");      }        public int getChoice() {          int choice = 4;// default          boolean done = false;          while (!done) {              System.out.print("choice? ");              try {                  choice = input.nextInt();              } catch (Exception e) {              }              if (choice > 0 && choice <= 5)                  done = true;              else                  System.out.println("\nYour choice is incorrect, please try again");          }          return choice;      }        public void addPerson() {          String name = "";          String phone = "";          try {              System.out.print("Enter the persons name ");              name = input.next();              System.out.print("\nEnter the persons phone number ");              phone = input.next();              System.out.println("");          } catch (Exception e) {          }          database = add(name + " " + phone);      }        public void deletePerson() {          String name = "";          try {              System.out.print("Enter the persons first name ");              name = input.next();              System.out.println("");          } catch (Exception e) {          }          if (!remove(name))              System.out.println("Could not delete " + name);          else              System.out.println(name + " was deleted successfully");      }        public void findPerson() {          String name = "";          try {              System.out.print("Enter the persons first name ");              name = input.next();              System.out.println("");          } catch (Exception e) {          }          int pos = search(name);                             if (pos >= 0) {              System.out.println(database[pos]);          } else {              System.out.println("No such person");          }      }        public void run() {          int choice = 0;          do {              displayMenu();              choice = getChoice();              switch (choice) {              case 1:                  addPerson();                  break;              case 2:                  deletePerson();                  break;              case 3:                  findPerson();                  break;              case 4:                  displayAll();              default:                  // should not get here              }            } while (choice != 5);      }        public static void main(String[] args) {          new Main().run();      }  }

Description: You are to add the necessary features and class to complete the AddressBook class code provided. See the java file Main.java. The given class must: Add a person to a "database" (an array) Search the database for a person given the name. Delete a person in the database given the name of the person. Display all the people (on the console) in the database using the following format (not in italics): Name Bob Sue Phone Number 451-1234 723-5832 Currently the class contains methods to display the menu (displayMenu()), get the users choice (getChoice()), delete a person from the database (remove()), and a main method to test the features. There are also the UI methods addperson() (which gets the person info to add and calls the add() method), deletePerson() (which gets the name of the person to delete and calls the remove() method), and findPerson() (which gets the name of the person to find and calls the search() method). The method run() runs the UI methods. You will need to provide a search method (which is required by the remove() method), add a person to the database (add()) and displayAll() method to display the records. You will also need to add to findPerson() method your display code of the person found using the same format as displayAll() to display the single record. search(String name) - given the name of the person to find, returns the position of that entry in the address book (database) OR returns -1 indicating not found. add() - given a new person's data (name and phone number) adds it to the address book (database) displayAll() - display all the records in the address book (database) using the described format. Use a spacing of 20 chars for the name and 15 for the phone number and left justify it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Global Strategy

Authors: Mike W. Peng

5th Edition

0357512367, 978-0357512364

More Books

Students also viewed these Programming questions