Write a program that models an employee. An employee has an employee number, a n
ID: 3589283 • 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
//Java Program for Employee Mode without inheritance concept
import java.util.Scanner;
class Employee
{
public int empno;
///method to set employee number
void setEmployeeNumber(int eno)
{
empno=eno;
}
///method to get employee number
void getEmployeeNumber()
{
System.out.println("Employee Number : "+empno);
}
}
class Name
{
public String fname,lname;
///method to set employee NAme
void setEmployeeName(String first,String last)
{
fname=first;
lname=last;
}
///method to get employee Name
void getEmployeeName()
{
System.out.println("Employee Name : "+fname+" "+lname);
}
}
class Address
{
public String street,city,state;
public int zip;
///method to set employee Address
void setEmployeeAddress(String st,String c,String stat,int zipcode)
{
street=st;
city=c;
state=stat;
zip=zipcode;
}
///method to get employee Address
void getEmployeeAddress()
{
System.out.println("Employee Address : "+street+","+city+","+state+","+zip);
}
}
class EmpDate
{
public int day,month,year;
///method to set employee joing date
void setDate(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
///method to get employee joining date
void getDate()
{
System.out.println("Employee Joining Date : "+day+"-"+month+"-"+year);
}
}
//main class
public class DriverClass{
public static void main(String []args)
{
int n=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of employee data to enter");
n =sc.nextInt();
Employee[] E=new Employee[n];
Name[] N=new Name[n];
Address[] A=new Address[n];
EmpDate[] D=new EmpDate[n];
//Input Part to entere all employee detials
for(int i=0;i<n;i++)
{
E[i] = new Employee();
N[i] = new Name();
A[i] = new Address();
D[i] = new EmpDate();
System.out.println("Enter Employee"+(i+1)+"Details");
System.out.println("Enter Emp ID");
int eno =sc.nextInt();
E[i].setEmployeeNumber(eno);
System.out.println("Enter fname & lname");
String fn =sc.nextLine();
String ln =sc.nextLine();
N[i].setEmployeeName(fn,ln);
System.out.println("Enter Address as given order");
String s =sc.nextLine();
String c =sc.nextLine();
String state =sc.nextLine();
int zcode=sc.nextInt();
A[i].setEmployeeAddress(s,c,state,zcode);
System.out.println("Enter Emp Date(ex. month 1-12, day 1-31, year > 1900 and < 2020)");
int dd =sc.nextInt();
int mm =sc.nextInt();
int yy =sc.nextInt();
D[i].setDate(dd,mm,yy);
}
//Output Part to display all entered employee detials
for(int i=0;i<n;i++)
{
System.out.println("Diplaying Employee"+(i+1)+"Details");
E[i].getEmployeeNumber();
N[i].getEmployeeName();
A[i].getEmployeeAddress();
D[i].getDate();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.