Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.
4. Add the new person to the end of the array.
5. Assign the new array to the old reference:
people = tempPeople.
6. Repeat for EmployeeProgram.
here is thr existing code.
date class
public class Date {
// variables
private int year;
private int month;
private int day;
// constructor
public Date(int theYear, int theMonth, int theDay)
{
setYear(theYear);
setMonth(theMonth);
setDay(theDay);
}
public int getYear() { // getters for variables
return year;
}
public int getMonth() { // getters for variables
return month;
}
public int getDay() { // getters for variables
return day;
}
// setters
public void setYear(int theYear)
{
year = theYear;
}
public void setMonth(int theMonth)
{
// only allow months between 1 and 12
if(theMonth>=1 && theMonth<=12)
month = theMonth;
else
month = 1;
}
public void setDay(int theDay)
{
// variables
// find out how many days of the month there should be
int[] days = {0, 31, 28,31,30,31,30,31,31,30,31,30,31};
// correct for leap years
if( month==2 && theDay==29 && (year%400==0 ||
(year % 4==0 && year % 100 != 0 )))
{
day = theDay;
}
// make sure the day is a valid number for the month
else if( theDay<1 || theday> days[month] ){
day = 1;
}
else
{
day = theDay;
}
}
public String toString()
{
String result = "";
result = year+"/"+month+"/"+day;
return result;
}
}.
employee
public class Employee extends Person {
//variables
int employeeNo;
private float monthlySalary;
private Date hireDate;
//constructors
public Employee(String itsLastName, String itsFirstName,
char itsMiddleInit, Date itsBirthDate, int itsEmployeeNo,
float itsMonthlySalary, Date itsHireDate) {
super(itsLastName, itsFirstName, itsMiddleInit, itsBirthDate);
// super.setLastName(itsLastName); othee=r way to use super
employeeNo = itsEmployeeNo;
monthlySalary = itsMonthlySalary;
hireDate = itsHireDate;
}
public Employee() {} ;
public void setEmployeeNo(int itsEmployeeNo) {
employeeNo = itsEmployeeNo;
}
public void setMonthlySalary(float itsMonthlySalary) {
monthlySalary = itsMonthlySalary;
}
public void setHireDate(Date itsHireDate) {
hireDate = itsHireDate;
}
//getters
public int getEmployeeNo() {
return employeeNo;
}
public float getMonthlySalary() {
return monthlySalary;
}
public Date getHireDate() {
return hireDate;
}
public String toString() {
String result = "";
result = "Employee No.:" + employeeNo + " Name:" + getFullName()
+ " Birth Date:" + getBirthDate().toString()
+ " Monthly Salary:" + monthlySalary;
return result;
}
}
person
public abstract class Person {
//variables
private String lastName;
private String firstName;
private char middleInit;
private Date birthDate;
//Constructors
public Person(){};
public Person(String itsLastName, String itsFirstName,
char itsMiddleInit, Date itsBirthDate){
lastName = itsLastName;
firstName = itsFirstName;
middleInit = itsMiddleInit;
birthDate = itsBirthDate;
}
//getters
public String getLastName(){ return lastName; }
public String getFirstName(){ return firstName; }
public char getMiddleInit(){ return middleInit; }
public Date getBirthDate(){ return birthDate; }
//setters
public void setLastName(String itsLastName){ lastName = itsLastName; }
public void setFirstName(String itsFirstName){ firstName = itsFirstName; }
public void setMiddleInit(char itsMiddleInit){ middleInit = itsMiddleInit; }
public void setBirthDate(Date itsBirthDate){ birthDate = itsBirthDate; }
//returns full name in "[firstname], [lastname] [middleinit]." format
public String getFullName()
{
String result = "";
result = lastName+", "+firstName+" "+middleInit+".";
return getLastName() +" "+ getFirstName();
}
public abstract String toString();
}
employe program
import java.util.Scanner; // Import the Scanner class
/**
*
* @author Instructor
*/
public class EmployeeProgram {
//variables
public Scanner input;
public Employee[] employees;
public EmployeeProgram() {
input = new Scanner(System.in);
}
// initialize the employees array
public void getData() {
employees = new Employee[5];
employees[0] = new Employee("Smith", "John", 'T', new Date(1980, 9, 30),
100101, 2789.0f, new Date(2000, 5, 22));
employees[1] = new Employee("Chan", "Deanna", 'A', new Date(1991, 7, 11),
100102, 1580.0f, new Date(2001, 6, 22));
employees[2] = new Employee("Ahmad", "Zeeshan", 'F', new Date(1985, 12, 14),
100103, 3400.0f, new Date(2002, 5, 10));
employees[3] = new Employee("Prasad", "Ari", 'A', new Date(1999, 11, 23),
100104, 2560.0f, new Date(2005, 1, 31));
employees[4] = new Employee("Sky", "Chad", 'J', new Date(1989, 2, 01),
100105, 2164.0f, new Date(2010, 12, 9));
}
// display the menu and return the users selection
public int showMenu() {
int result = 0;
System.out.println("1. Display All Employees");
System.out.println("2. Diplay Info for Employee by List Number");
System.out.println("3. Display Info for Employee by Employee No.");
System.out.println("4. Edit Info for Employee by Employee No.");
System.out.println("5. Enter a new Person");
System.out.println("6. Exit");
result = GetValidateMenuOption("Enter Selection", 1, 5);
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;
case 4:
menuOption4();
break;
}
}
// "1. Display All Employees"
public void menuOption1() {
// loop through the list of employees
for (int i = 0; i < employees.length; i++) {
//display the users info
System.out.println(employees[i].getEmployeeNo() + ": "
+ employees[i].getFullName());
}
}
// "2. Diplay Info for Employee by List Number"
public void menuOption2() {
//variables
int employeeListNo = 0;
//get persons number
employeeListNo = GetValidateMenuOption("Enter the List Number:", 1, employees.length);
// display the persons info
System.out.println("Employee Data");
System.out.println("-----------------------");
System.out.println(employees[employeeListNo - 1].toString());
}
// "3. Display Info for Employee by Employee No."
public void menuOption3() {
//variables
int employeeNumberToFind = 0;
int employeeListNo = 0;
//get employee number
System.out.println("Enter the Employee No.: ");
employeeNumberToFind = input.nextInt();
//loop through the employee list to see if a user exists with this employeeNo
boolean employeeFound = false;
//loop through the employees to search for the employeeNo
for (int i = 0; i < employees.length; i++) {
if (employees[i].getEmployeeNo() == employeeNumberToFind) {
//if you have found the employee, save the array number of
//that employee
employeeFound = true;
employeeListNo = i;
}
}
if (employeeFound == true) {
// display the persons info
System.out.println("Employee Data");
System.out.println("-----------------------");
System.out.println(employees[employeeListNo].toString());
} else {
System.out.println("Employee Not Found");
}
}
// "4. Edit Info for Employee by Employee No."
public void menuOption4() {
//variables
int menuChoice = 0;
int employeeNumberToFind = 0;
int employeeListNo = 0;
boolean employeeFound = false;
int newYear = 0;
int newMonth = 0;
int newDay = 0;
//get employee number
System.out.println("Enter the Employee No.: ");
employeeNumberToFind = input.nextInt();
//loop through the employee list to see if a user exists with this employeeNo
for (int i = 0; i < employees.length; i++) {
if (employees[i].getEmployeeNo() == employeeNumberToFind) {
//if you have found the employee, save the array number of
//that employee
employeeFound = true;
employeeListNo = i;
}
}
if (employeeFound == true) {
// ask user which piece of info they want to edit
System.out.println("1. Employee No.");
System.out.println("2. First Name");
System.out.println("3. Last Name");
System.out.println("4. Middle Initial");
System.out.println("5. Monthly Salary");
System.out.println("6. Birth Date");
System.out.println("7. Hire Date");
System.out.println("8. Cancel");
menuChoice = GetValidateMenuOption("Enter Selection", 1, 8);
switch (menuChoice) {
//"1. Employee No."
case 1:
System.out.println("Enter New Employee No.:");
employees[employeeListNo].setEmployeeNo(input.nextInt());
break;
//"2. First Name"
case 2:
System.out.println("Enter New First Name:");
employees[employeeListNo].setFirstName(input.next());
break;
//"3. Last Name"
case 3:
System.out.println("Enter New Last Name:");
employees[employeeListNo].setLastName(input.next());
break;
//"4. Middle Initial"
case 4:
System.out.println("Enter New Middle Initial:");
employees[employeeListNo].setMiddleInit(input.next().charAt(0));
break;
//"5. Monthly Salary"
case 5:
System.out.println("Enter New Monthly Salary:");
employees[employeeListNo].setMonthlySalary(input.nextFloat());
break;
//"6. Birth Date"
case 6:
newYear = 0;
newMonth = 0;
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();
employees[employeeListNo].setBirthDate(new Date(newYear, newMonth, newDay));
break;
//"7. Hire Date"
case 7:
newYear = 0;
newMonth = 0;
newDay = 0;
System.out.println("Enter New Hire Year:");
newYear = input.nextInt();
System.out.println("Enter New Hire Month:");
newMonth = input.nextInt();
System.out.println("Enter New Hire Day:");
newDay = input.nextInt();
employees[employeeListNo].setHireDate(new Date(newYear, newMonth, newDay));
break;
}
} else {
System.out.println("Employee Not Found");
}
}
public int GetValidateMenuOption(String menuPrompt, int lowerBound, int upperBound) {
int result = 0;
do {
System.out.println(menuPrompt);
result = input.nextInt();
if (result < lowerBound || result > upperBound) {
System.out.println("Not a valid option.");
System.out.println("=======================");
}
} while (result < lowerBound || result > upperBound);
return result;
}
//menuChoice = GetValidateMenuOption("Enter Selection",1,5);
public static void main(String[] args) {
int userInput = 0;
EmployeeProgram mainProgram = new EmployeeProgram();
FileHandler fileHandler = new FileHandler();
//mainProgram.getData();
mainProgram.employees = (Employee[]) fileHandler.getDataFromFile("233PersonTestData.txt");
do {
System.out.println("=======================");
userInput = mainProgram.showMenu();
System.out.println("=======================");
mainProgram.executeChoice(userInput);
} while (userInput != 4);
fileHandler.SaveAll(mainProgram.employees);
}
}

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

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

How was their resistance overcome?

Answered: 1 week ago

Question

Analyse the process of new product of development.

Answered: 1 week ago

Question

Define Trade Mark.

Answered: 1 week ago