2

In my Main class I have this piece of code:

UUID uniqueID;

public void createEmployee(){       
    uniqueID = UUID.randomUUID(); 
    // ...
}

In my class Corporation there's a method called promoteEmployee, which should receive the uniqueID as parameter. Is that possible, and when yes, how?

public void promoteEmployee(uniqueID){
   // it doesn't recognize uniqueID as argument
}

I also have the method sortEmployees, which sorts the ArrayList alphabetically, and if two names are equal, the employee with a higher salary should be printed out first. It sorts the List alphabetically, but doesn't check if the salary is bigger. What do I need to change?

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

public void sortEmployees(){
    Collections.sort(employees, (p1, p2) -> p1.name.compareTo(p2.name));
    for(Employee employee: employees){
        Comparator.comparing(object -> employee.name).thenComparingDouble(object -> employee.grossSalary);
        System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: "+employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
        System.out.println(""); // just an empty line
    }
}

2 Answers 2

1

Change the method to be valid java code

public void promoteEmployee(UUID uniqueID){

but as it even seems to be a field, why pass the value at all?

As for sorting see Implementing Java Comparator

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I've looked at the post of Implementing the Java Comparator, but it doesn't really help me..
well try searching for more resources as to how to implement Comparator e.g. mkyong.com/java/… esp. 4. Sort an Object with Comparator
1

one passes variables from one class to another's methods by using the classname.method(arg) syntax.

    public class JavaTeachMe2018

{
    //variable in other class to be passed as a method argument
    public static int startID = 0;

    public static void main(String[] args)
    {
        // we are passing startID to anouther class's method
        String[] currentEmployees = Corporation.createEmployee(startID);
        System.out.println("Welcome " + currentEmployees[1] + " to the company as employee number " + currentEmployees[0]);
    }
}// end class teachme

here is the second class

 import java.util.Scanner;
public class Corporation
{

    public static int createId(int startID)
    {
            // create unique id
            int uniqueID = startID + 1;
            return uniqueID;
    }
    public static String[] createEmployee(int startID)
    {

        // assign a variable to the return of the createId call
        int employeeNumber = createId(startID);
        System.out.println("Your assigned employee number is " + employeeNumber);
        // get employee name
        Scanner stdin = new Scanner(System.in);
        System.out.print(" Enter Your Name : ");
        String employeeName = stdin.nextLine();
        String employees[] = {Integer.toString(employeeNumber), employeeName};
        return employees;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.