Probably easiest if I just quote,

5) Assignment Entity, A class that models an association between an Employee and a Project Each project usually has several employees working on it at any one time, but there may be none at times. Each employee must work on at least one project, but may work part time on other projects. Each Assignment have their own independent start date and stop date, but the dates must lie within the overall start and stop dates for the projects concerned.

I have my Employee and Project class.
My test class fills out info for both of these classes.
names, project start/end dates ect.

How would I go about making the assignment class?
Apprecaite it.

package test_class;

// will be extended to both consultant and manager
public class Employee {

    private String name;
    private int age;
    private char sex;
    private double salary;
    private String job_title;

    // Accessors and Mutators

    // name
    public void setName(String nameIn)
    {
        name = nameIn;
    }
    public String getName()
    {
        return name;
    }

    // age
    public void setAge(int ageIn)
    {
        age = ageIn;
    }
    public int getAge()
    {
        return age;
    }

    // sex
    public void setSex(char sexIn)
    {
        sex = sexIn;
    }
    public char getSex()
    {
        return sex;
    }

    // salary
    public void setSalary(double salaryIn)
    {
        salary = salaryIn;
    }
    public double getSalary()
    {
        return salary;
    }

    // job title
    public void setJob_title(String jobIn)
    {
        job_title = jobIn;
    }
    public String getJob_title()
    {
        return job_title;
    }

    // salary increment
    public void increment(double salaryIn) {
        salary = salary*1.1;
    }
    
    // promotion
    public void promote(String jobIn) {
        job_title = jobIn;
    }
}
package test_class;

public class Project {

    // Load variables
    private String projectName;
    private String projectManager;
    private String projectStartDate;
    private String projectStopDate;

    // Accessors and Mutators

    // projectName
    public void setProjectName(String projectNameIn)
    {
        projectName = projectNameIn;
    }
    public String getProjectName()
    {
        return projectName;
    }

    // projectManager
    public void setProjectManager(String projectManagerIn)
    {
        projectManager = projectManagerIn;
    }
    public String getProjectManager()
    {
        return projectManager;
    }

    // projectStartDate
    public void setProjectStartDate(String projectStartDateIn)
    {
        projectStartDate = projectStartDateIn;
    }
    public String getProjectStartDate()
    {
        return projectStartDate;
    }

    // projectStopDate
    public void setProjectStopDate(String projectStopDateIn)
    {
        projectStopDate = projectStopDateIn;
    }
    public String getProjectStopDate()
    {
        return projectStopDate;
    }
}

Recommended Answers

All 10 Replies

Your Project class should have an ArrayList (or some data structure) to hold Employees in it. That way you will know what Employees are working on what project. As far as the Assignment class, I don't really understand what your teacher has in mind for what an Assignment actually is. Ok so it has a start and stop date. . so what? What else does an Assignment have? What exactly is an Assignment in this context?

I'm kind of lost on it too, but I believe there should be methods like,
employeeHasProject()
projectsAvailable()

dont really know any others it would need.

Also, how can I load the employee into the arrayList?
Thanks.

When you create a new Employee, you would just say something like

Employee emp = new Employee();
yourArrayList.add(emp);

See the javadoc for arraylist. If your Assignment class needs to be able to list the Projects that are available, then the Assignment class should probably have an ArrayList of Projects. If you need to be able to list what Projects an Employee is involved in, assuming your Project class has an ArrayList of Employees you can use the following:

//Assuming projectsList is an ArrayList of your Projects and someEmployee is
//an Employee Object..
for (Project proj: projectsList){
if (proj.employeesList.contains(someEmployee)){
System.out.println("The employee is part of the project " + proj.projectName);
}
}

One more thing: in order to search for an Employee in any type of Collection (i.e. an ArrayList, etc) you must override the equals() method.

So I have an error when I try to implement it this way. Why cant I add the emp?

public static Employee readEmployee() {
        Employee emp = new Employee();
        ArrayList<String> empArrayList = new ArrayList<String>();
        empArrayList.add(emp);

Line 41

package test_class;

// Import Scanner
import java.util.*;

public class main {

    public static void main(String[] args) {
        // call readEmployee to gather employee info
        Employee emp = readEmployee();

        // call readProject to gather project info
        Project pro = readProject();

        // call readConsultant to gather consultant info
        Consultant con = readConsultant();

        // call readConsultant to gather consultant info
        Manager man = readManager();
        System.out.println("============ END OF INPUT ============");

        // formating
        System.out.println("");
        System.out.println("============ BEGIN OUTPUT ============");

        // call writeEmployee to print out employee info
        System.out.println("\n============   Employee   ============");
        writeEmployee(emp);
        System.out.println("\n============    Project   ============");
        writeProject(pro);
        System.out.println("\n============  Consultant  ============");
        writeConsultant(con);
        System.out.println("\n============    Manager   ============");
       writeManager(man);
    }

    // read employee info
    public static Employee readEmployee() {
        Employee emp = new Employee();
        ArrayList<String> empArrayList = new ArrayList<String>();
        empArrayList.add(emp);

        
        Scanner scan = new Scanner(System.in);
        try {
            // collect and assign employee name
            System.out.println("Enter Employee name.");
            emp.setName(scan.nextLine());
                        
            // collect and assign employee age
            System.out.println("\nEnter Employee age.");
            emp.setAge(scan.nextInt());

            // collect and assign employee sex
            System.out.println("\nEnter Employee sex.");

            // since there is no nextChar use array to collect & assign first letter
            String aid = scan.next();
            char[] charArr = aid.toCharArray();
            emp.setSex(charArr[0]);

            // collect and assign employee salary
            System.out.println("\nEnter Employee salary.");
            emp.setSalary(scan.nextDouble());

            // collect and assign employee job title
            System.out.println("\nEnter Employee job title.");
            emp.setJob_title(scan.nextLine());
        }
        catch (InputMismatchException ime) {
            System.out.println("Input mismatch, please enter the correct datatype.");
        }

        return emp;
    }

    // read project info
    public static Project readProject() {
        Project pro = new Project();
        Scanner scan = new Scanner(System.in);
        try {
            // collect and assign project manager
            System.out.println("\nEnter Project manager.");
            pro.setProjectManager(scan.nextLine());

            // collect and assign project name
            System.out.println("\nEnter Project name.");
            pro.setProjectName(scan.nextLine());

            // collect and assign project start date
            System.out.println("\nEnter Project start date.");
            pro.setProjectStartDate(scan.nextLine());

            // collect and assign project stop date
            System.out.println("\nEnter Project stop date.");
            pro.setProjectStopDate(scan.nextLine());
        }
        catch (InputMismatchException ime) {
            System.out.println("Input mismatch, please enter the correct datatype.");
        }

        return pro;
    }

    // read consultant info
    public static Consultant readConsultant() {
        Consultant con = new Consultant();
        Scanner scan = new Scanner(System.in);
        try {
            // collect and assign consultant name using inheritance
            System.out.println("\nEnter consultant name.");
            con.setName(scan.nextLine());

            // collect and assign consultant age using inheritance
            System.out.println("\nEnter consultant age.");
            con.setAge(scan.nextInt());

            // since there is no nextChar use array to collect & assign first letter with inheritance
            System.out.println("\nEnter consultant sex.");
            String aid = scan.next();
            char[] charArr = aid.toCharArray();
            con.setSex(charArr[0]);

            // collect and assign consultant salary using inheritance
            System.out.println("\nEnter consultant salary.");
            con.setSalary(scan.nextInt());

            // collect and assign consultant job title using inheritance
            System.out.println("\nEnter consultant job title.");
            con.setJob_title(scan.nextLine());

            // collect and assign consultant start date
            System.out.println("\nEnter consultant start date.");
            con.setContractStart(scan.nextLine());

            // collect and assign consultant contract length
            System.out.println("\nEnter consultant contract length.");
            con.setContractLength(scan.nextLine());
        }
        catch (InputMismatchException ime) {
            System.out.println("Input mismatch, please enter the correct datatype.");
        }
        return con;
    }

    public static Manager readManager() {
        Manager man = new Manager();
        Scanner scan = new Scanner(System.in);
        try {
            // collect and assign manager name using inheritance
            System.out.println("\nEnter manager name.");
            man.setName(scan.nextLine());

            // collect and assign manager age using inheritance
            System.out.println("\nEnter manager age.");
            man.setAge(scan.nextInt());

            // since there is no nextChar use array to collect & assign first letter with inheritance
            System.out.println("\nEnter manager sex.");
            String aid = scan.next();
            char[] charArr = aid.toCharArray();
            man.setSex(charArr[0]);

            // collect and assign manager salary using inheritance
            System.out.println("\nEnter manager salary.");
            man.setSalary(scan.nextInt());

            // collect and assign manager job title using inheritance
            System.out.println("\nEnter manager job title.");
            man.setJob_title(scan.nextLine());

            // collect and assign manager contract length
            System.out.println("\nEnter manager contract length.");
            man.setManagerLocation(scan.nextLine());
        }
        catch (InputMismatchException ime) {
            System.out.println("Input mismatch, please enter the correct datatype.");
        }
        return man;
    }

    // employee format
    public static void writeEmployee(Employee emp) {

        System.out.println("Name: " + emp.getName());
        System.out.println("Age: " + emp.getAge());
        System.out.println("Sex: " + emp.getSex());
        System.out.println("Salary: " + emp.getSalary());
        System.out.println("Job: " + emp.getJob_title());
    }

    // project format
    public static void writeProject(Project pro) {

        System.out.println("Name: " + pro.getProjectManager());
        System.out.println("Project: " + pro.getProjectName());
        System.out.println("Start: " + pro.getProjectStartDate());
        System.out.println("Stop: " + pro.getProjectStopDate());
    }

    // consultant format
    public static void writeConsultant(Consultant con) {
        System.out.println("Name: " + con.getName());
        System.out.println("Age: " + con.getAge());
        System.out.println("Sex: " + con.getSex());
        System.out.println("Salary: " + con.getSalary());
        System.out.println("Job: " + con.getJob_title());
        System.out.println("Start: " + con.getContractStart());
        System.out.println("Length: " + con.getContractLength());
    }

    // manager format
    public static void writeManager(Manager man) {
        System.out.println("Name: " + man.getName());
        System.out.println("Age: " + man.getAge());
        System.out.println("Sex: " + man.getSex());
        System.out.println("Salary: " + man.getSalary());
        System.out.println("Job: " + man.getJob_title());
        System.out.println("Start: " + man.getManagerLocation());
    }
}

Because you said

ArrayList<String>

instead of

ArrayList<Employee>

. Your code won't work anyway, though, because your ArrayList is in the wrong place. Your ArrayList of Employees should be an instance variable in your Project class. That means it should look something like this:

public class Project{
//Variable Declarations..
ArrayList<Employee> empArrayList = new ArrayList<Employee>();
//Methods go down here..
}

This is in the project class

Error on line 6, why cant I add the emp?

ArrayList<Employee> empArrayList = new ArrayList<Employee>();       

    // Add employee to arraylist
    public void setEmployee(ArrayList Employee)
    {
        empArrayList.add(emp);
    }
    
    public ArrayList getEmployee()
    {
        return empArrayList;
    }

Because your method definition is wrong.

public void setEmployee(ArrayList Employee)

You defined your setEmployee method as taking an ArrayList Object, not as taking an Employee Object. It should be

public void addEmployee(Employee emp)

And notice I changed the name from set to add, because what you're doing is adding an Employee to the ArrayList, not setting the ArrayList. You should also read through this tutorial because your current assignment is going to be nearly impossible for you to get done unless you read about the topics in that tutorial.

Alright :) I'll take a look, marking it solved for now. Isn't even due for a while so i'm probably not suppose to know everything quite yet.

Thanks for the help.

No problem, just let me know if you need any more help.

I tried to do the same program in my way but I am not able to input values for employee's name, employee's job title, project name, project managers' title manager's job title and consultant job title. It skips and goes to next line without letting the user to enter these following information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.