I have tried to start and this is all I have so far! The instructions are below,
ID: 3660094 • Letter: I
Question
I have tried to start and this is all I have so far! The instructions are below, I did the first part but it still needs a little more fixing and I also Need help on the second part.Thank you!
public class Employee {
private String fName;
private String mName;
private String lName;
private String ID;
private String title;
private String startDate;
public Employee (String fName1, String mName1, String lName1, String ID1, String title1, String startDate1) {
fName = fName1;
mName = mName1;
lName = lName1;
ID = ID1;
title = title1;
startDate = startDate1;
}
private String getfName() {
return fName;
}
private String getmName() {
return mName;
}
private String getlName() {
return lName;
}
public void setName (String Name1, String Name2, String Name3) {
fName = Name1;
mName = Name2;
lName = Name3;
}
private String getID() {
return ID;
}
public void setID (String employeeID) {
ID = employeeID;
}
private String gettitle() {
return title;
}
public void settitle (String employeeTitle) {
title = employeeTitle;
}
private String getstartDate() {
return startDate;
}
public void setstartDate (String employeeStartDate) {
startDate = employeeStartDate;
}
public void print () {
System.out.println(fName + mName + lName);
System.out.println(ID);
System.out.println(title);
System.out.println(startDate);
}
}
Part I: (50 pts) Create a Employee class with the following:
Explanation / Answer
Ok I did all of part I for you since part 2 made this question way too long for just 350 karma. You had lots of mistakes so read my comments in the program. I also made a Driver class to test out to make sure all this works so you can use it. To test just include another java file named "Driver", copy my Driver class here, and run it in the same directory as the "Employee" java file.
The Driver class:
public class Driver
{
public static void main(String[] args)
{
Employee bob = new Employee();
bob.setNames("bob"); //Testing bad input
bob.setNames("Saradomin Zamorak Armadyl Bandos"); //Testing bad input
bob.setNames("Bob Guam Megadolmer"); //Correct input
bob.setID("123456789"); //Testing bad input
bob.setID("avc-45-6789"); //Testing bad input
bob.setID("123-45-6789"); //Correct input
bob.setTitle("Pizza Delivery Boy"); //Testing bad input
bob.setTitle("Overlord"); //Correct input
bob.setstartDate("12/11/12"); //You'll need to check for correctness late
System.out.println(); //Just blank line so its easier to read
System.out.println(bob.getName());
System.out.println(bob.getID());
System.out.println(bob.getTitle());
System.out.println(bob.getstartDate());
System.out.println(); //Just blank line so its easier to read
System.out.println(bob); //Calls toString() method
}
}
The Employee class:
public class Employee
{
private String fName;
private String mName;
private String lName;
private String ID;
private String title;
private String startDate;
private String presetTitle1;
private String presetTitle2;
private String presetTitle3;
public Employee() //Default constructor should set everything to null except preset titles which you can change.
{
fName = null;
mName = null;
lName = null;
ID = null;
title = null;
startDate = null;
//Setting preset titles. You can of course change it.
presetTitle1="Grunt";
presetTitle2="Worker";
presetTitle3="Overlord";
}
//All get methods must be public
public String getName() //Only asked for one get name method
{
if (mName==null)
return fName+" "+lName;
else
return fName+" "+mName+" "+lName;
}
public void setNames(String Name) //Should only have 1 parameter
{
String[] temp = Name.split(" "); // Splits name into tokens and stores into an array based of the white space.
if (temp.length>3) //If more than 3 names
{
System.out.println("Contains more than four parts or too many white spaces!");
}
else if (temp.length<2) //If name does not include at least 1st and last
{
System.out.println("Does not contain at least first name seperated by a white space.");
}
else if (temp.length==2) //Input is correct and only 1st and last names were entered.
{
//Store first and last names
fName=temp[0];
lName=temp[1];
}
else if (temp.length==3) //First, middle, and last names
{
fName=temp[0];
mName=temp[1];
lName=temp[2];
}
}
public String getID()
{
return ID;
}
public void setID(String employeeID)
{
if (employeeID.length()==11) //Length must be exactly 11 counting the '-' and numbers
{
if (employeeID.charAt(3)=='-' && employeeID.charAt(6)=='-') //Makes sure of the '-' s
{
String temp="";
for (int i=0; i<11; i++) //Gathers integers
{
if (i!=3 && i!=6)
{
temp+=employeeID.charAt(i);
}
}
try //Attempt to see if temp only contains integers by trying to parse it
{
Integer.parseInt(temp);
//If successfully parsed wo error, format is correct.
ID=employeeID;
}
catch(Exception e)
{
System.out.println("Incorrect format");
}
}
}
else
{
System.out.println("Incorrect format");
}
}
public String getTitle()
{
return title;
}
public void setTitle(String employeeTitle)
{
//If the title currently trying to be input matches with a preset title then set it
if (employeeTitle.equals(presetTitle1)||employeeTitle.equals(presetTitle2)||employeeTitle.equals(presetTitle3))
title = employeeTitle;
else
System.out.println("Title does not match preset titles!");
}
public String getstartDate()
{
return startDate;
}
public void setstartDate(String employeeStartDate)
{
/* This is incorrect since you need to find out if the date is correct or not.
* There are many things on the internet that show how if you can determine whether or not a date is correct.
* I'm just being lazy here but I don't want to copy code.
*/
startDate = employeeStartDate;
}
public String toString()
{
if (mName==null)
return("Name: "+fName +" " + lName +" "+
"Title: "+title+" "+
"ID: "+ID+" "+
"Starting Date: "+startDate);
else
return("Name: "+fName+" " + mName+" " + lName +" "+
"Title: "+title+" "+
"ID: "+ID+" "+
"Starting Date: "+startDate);
}
}
The output:
Does not contain at least first name seperated by a white space.
Contains more than four parts or too many white spaces!
Incorrect format
Incorrect format
Title does not match preset titles!
Bob Guam Megadolmer
123-45-6789
Overlord
12/11/12
Name: Bob Guam Megadolmer
Title: Overlord
ID: 123-45-6789
Starting Date: 12/11/12