Write a program that models an employee. An employee has an employee number, a n
ID: 3589372 • Letter: W
Question
Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day 1-31, year > 1900 and < 2020). Issue appropriate error messages when incorrect data is entered. Create an Employee, a Name, an Address, and a Date classes in your solution. You may use the Date class from the lectures or define your own. The Java-supplied Date class cannot be used. Provide appropriate class constructors, getter methods, setter methods, toString() and any other methods you think are necessary. To keep things simple, your classes don’t need to do any editing of data. The classes should not do any direct user input/output. All user input/output will be done in the main method. Your program should prompt the user to enter data for several employees,store the data in an array of type Employee, and then display the data. Be sure to comment your code and use the course code formatting style.
Explanation / Answer
MyDate.java
public class MyDate {
//Declaring instance variables
private int year;
private int month;
private int day;
//Parameterized constructor
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
//getters and setters
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return month + "/" + year + "/" + day;
}
}
________________
Employee.java
public class Employee {
// Declaring variables
private int employeeNumber;
private String name;
private MyDate hireDate;
private String address;
//Parameterized constructor
public Employee(int employeeNumber, String name, MyDate hireDate,
String address) {
super();
this.employeeNumber = employeeNumber;
this.name = name;
this.hireDate = hireDate;
this.address = address;
}
//getters and setters
public int getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(int employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyDate getHireDate() {
return hireDate;
}
public void setHireDate(MyDate hireDate) {
this.hireDate = hireDate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Employee Number=" + employeeNumber + " Name=" + name + " Hire Date=" + hireDate + " Address=" + address;
}
}
__________________
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Declaring variables
String name, address;
int employeeNO;
int mon, day, year, size;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("How many Employees data you want to enter :");
size = sc.nextInt();
//Creating an Array of Employee class
Employee emp[] = new Employee[size];
//Getting the Employee info
for (int i = 0; i < size; i++) {
//Getting the input entered by the user
System.out.println("Employee#" + (i + 1) + ":");
sc.nextLine();
System.out.println("Enter Full name :");
name = sc.nextLine();
System.out.println("Enter Employee No :");
employeeNO = sc.nextInt();
sc.nextLine();
System.out.println("Enter Address :");
address = sc.nextLine();
while (true) {
System.out.print("Enter Month :");
mon = sc.nextInt();
if (mon < 1 || mon > 12) {
System.out.println("** Invalid.Must be between 1-12 **");
continue;
} else
break;
}
while (true) {
System.out.print("Enter Day :");
day = sc.nextInt();
if (day < 1 || day > 31) {
System.out.println("** Invalid.Must be between 1-31 **");
continue;
} else
break;
}
while (true) {
System.out.print("Enter Year :");
year = sc.nextInt();
if (year < 1900 || year > 2020) {
System.out.println("** Invalid.Must be between 1900-2020 **");
continue;
} else
break;
}
MyDate md = new MyDate(year, mon, day);
//Creating an Employee class object and populate them into an array
emp[i] = new Employee(employeeNO, name, md, address);
}
//Displaying each employee info
System.out.println("Dsipalying the Employees Info");
for (int i = 0; i < size; i++) {
System.out.println("Employee#" + (i + 1) + ":");
System.out.println(emp[i].toString());
}
}
}
______________________
Output:
How many Employees data you want to enter :2
Employee#1:
Enter Full name :
Kane Williams
Enter Employee No :
1234
Enter Address :
21 Park street,Mumbai,MH,54667
Enter Month :12
Enter Day :30
Enter Year :2009
Employee#2:
Enter Full name :
Sachin Tendulakar
Enter Employee No :
2345
Enter Address :
32,Baandra,Mumbai,MH,56767
Enter Month :4
Enter Day :5
Enter Year :2015
Dsipalying the Employees Info
Employee#1:
Employee Number=1234
Name=Kane Williams
Hire Date=12/2009/30
Address=21 Park street,Mumbai,MH,54667
Employee#2:
Employee Number=2345
Name=Sachin Tendulakar
Hire Date=4/2015/5
Address=32,Baandra,Mumbai,MH,56767
___________________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.