Question
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 (result
Add a menu option: Enter new Person. This will need to:
- Get a new person from the keyboard.
- Create a new array, 1 cell bigger than the current array (call it tempPeople).
- Use ArrayCopy to copy the old data to the new array.
Add the new person to the end of the array.
- Assign the new array to the old reference:
people = tempPeople.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started