Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class PersonProgram { //variables public Scanner input; public Person[] people; public PersonProgram() { input = new Scanner(System.in); } // initialize the people array public

public class PersonProgram { //variables public Scanner input; public Person[] people; public PersonProgram() { input = new Scanner(System.in); } // initialize the people array public void getData() { people = new Person[5]; people[0] = new Person("Smith", "John", 'T', new Date(1980,9,30) ); people[1] = new Person("Chan", "Deanna", 'A', new Date(1991,7,11) ); people[2] = new Person("Ahmad", "Zeeshan", 'F', new Date(1985,12,14) ); people[3] = new Person("Prasad", "Ari", 'A', new Date(1999,11,23) ); people[4] = new Person("Sky", "Chad", 'J', new Date(1989,2,01) ); } // display the menu and return the users selection public int showMenu() { int result = 0;

System.out.println("1. Display all names"); System.out.println("2. Diplay info for Person by number"); System.out.println("3. Edit information for person by number"); System.out.println("4. Exit");

result = GetValidateMenuOption("Enter Selection",1,4); return result; } // call the relevant method related to the choice selected public void executeChoice(int choice) { switch(choice) { case 1: menuOption1(); break; case 2: menuOption2(); break; case 3: menuOption3(); break; } } // "1. Display all names" public void menuOption1() { // loop through the list of people for(int i = 0; i < people.length; i++) { //display the users info System.out.println(people[i].getFullName()); } } // "2. Diplay info for Person by number" public void menuOption2() { //variables int personNumber = 0; //get persons number personNumber = GetValidateMenuOption("Enter the Person's number:",1,people.length); // display the persons info System.out.println("Person Data"); System.out.println("Full Name: " + people[personNumber-1].getFullName()); System.out.println("Birth Date: " + people[personNumber-1].getBirthDate() ); } // "3. Edit information for person by number" public void menuOption3() { //variables int personNumber = 0; int menuChoice = 0;

//get persons number personNumber = GetValidateMenuOption("Enter the Person's number:",1,people.length); // ask user which piece of info they want to edit System.out.println("1. First Name"); System.out.println("2. Last Name"); System.out.println("3. Middle Initial"); System.out.println("4. Birth Date"); System.out.println("5. Cancel");

menuChoice = GetValidateMenuOption("Enter Selection",1,5);

switch(menuChoice) { //"1. First Name" case 1: System.out.println("Enter New First Name:"); people[personNumber-1].setFirstName(input.next()); break; //"2. Last Name" case 2: System.out.println("Enter New Last Name:"); people[personNumber-1].setLastName(input.next()); break; //"3. Middle Initial" case 3: System.out.println("Enter New Middle Initial:"); people[personNumber-1].setMiddleInit(input.next().charAt(0)); break; //"4. Birth Date" case 4: int newYear = 0; int newMonth = 0; int newDay = 0;

System.out.println("Enter New Birth Year:"); newYear = input.nextInt();

System.out.println("Enter New Birth Month:"); newMonth = input.nextInt();

System.out.println("Enter New Birth Day:"); newDay = input.nextInt();

people[personNumber-1].setBirthDate(new Date(newYear, newMonth, newDay)); break; } } public int GetValidateMenuOption(String menuPrompt, int lowerBound, int upperBound) { int result = 0; do { System.out.println(menuPrompt); result = input.nextInt(); if (resultupperBound) { System.out.println("Not a valid option."); System.out.println("======================="); } }while(resultupperBound); return result; } //menuChoice = GetValidateMenuOption("Enter Selection",1,5); public static void main(String[] args) { //Initialize variables int userInput = 0; PersonProgram mainProgram = new PersonProgram(); PersonFileHandler fileHandler = new PersonFileHandler(); //mainProgram.getData(); mainProgram.people = fileHandler.getDataFromFile("233PersonTestData.txt"); do { System.out.println("======================="); userInput = mainProgram.showMenu(); System.out.println("======================="); mainProgram.executeChoice(userInput); }while(userInput != 4); fileHandler.SaveAll(mainProgram.people); } }

Add a menu option: Enter new Person. This will need to:

  1. Get a new person from the keyboard.
  2. Create a new array, 1 cell bigger than the current array (call it tempPeople).
  3. Use ArrayCopy to copy the old data to the new array.

Add the new person to the end of the array.

  1. Assign the new array to the old reference:

people = tempPeople.

(ignore other java classes used i just need answer for this question ) Thank You :)

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_2

Step: 3

blur-text-image_3

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

Microsoft Visual Basic 2008 Comprehensive Concepts And Techniques

Authors: Gary B. Shelly, Corinne Hoisington

1st Edition

1423927168, 978-1423927167

More Books

Students also viewed these Databases questions