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
    }
}
user1234567890
  • 271
  • 1
  • 4
  • 18

2 Answers2

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

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Thank you. I've looked at the post of Implementing the Java Comparator, but it doesn't really help me.. – user1234567890 Oct 11 '17 at 01:31
  • well try searching for more resources as to how to implement Comparator e.g. https://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/ esp. *4. Sort an Object with Comparator* – Scary Wombat Oct 11 '17 at 01:33
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;
    }
}
Jeremiah Stillings
  • 743
  • 1
  • 7
  • 19