**JAVA LANGUAGE**
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.
SOLUTION-
I have solve the problem in Java code with comments and screenshot
for easy understanding :)
CODE-
//java code
import java.util.Scanner;
//class Mydata
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;
}
}
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 + "\nName=" + name +
"\nHire Date=" + hireDate + "\nAddress=" + address;
}
}
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());
}
}
}
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------
Get Answers For Free
Most questions answered within 1 hours.