Home > Back-end >  Dear bosses, this how to write
Dear bosses, this how to write

Time:11-01

Write a class, the class inside a ArrayList object, used to store employee class objects, the class has a staff number, employee name, employee wages three properties, please give the company class design three methods: 1) add employees, 2) the output all the employee information, 3) sorting method, the ArrayList object according to the wages of the employees from low to high order printing,

CodePudding user response:

refer to the original poster m0_46384278 response:
write a class, the class inside a ArrayList object, used to store employee class objects, the class has a staff number, employee name, employee wages three properties, please give the company class design three methods: 1) adding staff, 2) the output all the employee information, 3) sorting method, the ArrayList object according to the wages of the employees from low to high order printout,


This learn to write their own, basic
 
Public class Employee {
Private String Id;
private String name;
Private float salary;

Public Employee (String id, String name, float salary) {
Id=Id;
this.name=name;
This. Salary=salary;
}

Public String getId () {
Return the Id;
}

Public void setId (String id) {
Id=Id;
}

Public String getName () {
return name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

Public float getSalary () {
Return the salary;
}

Public void setSalary (float salary) {
This. Salary=salary;
}

@ Override
Public String toString () {
Return the Employee {" + "
"Id=" + Id +
"And name='" + name +' \ ' '+
", salary="+ salary +
'} ';
}

Public class Company {
ArrayList List=new ArrayList<> (a);
Void the Add (the Employee the Employee) {
List. The add (employee);
}
Void the print () {
For (the Employee I: list) {
System.out.println(i);
}
}
Void the sort () {
The Collections. The sort (list, new Comparator () {
@ Override
Public int the compare (Employee o1, o2) Employee {
Return (int) (o1. GetSalary () - o2. GetSalary ());
}
});
For (the Employee I: list) {
System.out.println(i);
}
}
}



CodePudding user response:

 package emp2; 

import java.util.ArrayList;
The import java.util.Com parator;
import java.util.List;

Public class Company {
List Emp=new ArrayList (a);

The public Company () {
}
Public static void main (String [] args) {
Company com=new Company ();
Com. Addemp (new Employees (4, "Allen", 8000));
Com. Addemp (new Employees (5000) 5, "Iverson,");
Com. Addemp (new Employees (6, "Kevin", 10000));
Com. Addemp (new Employees (9000) 7, "Jordan,");

The sort ();
Com. Print ();

}

Private void sort () {

An emp. Sort (new Comparator () {

@ Override
Public int the compare (Employees Employees o1, o2) {
Return (int) (o1. GetSalary () - o2. GetSalary ());
}

});
}
Private void print () {
For (Employees Employees: emp) {
System. The out. Println (employees. The toString ());
}
}
Private void addemp Employees (e) {
An emp. The add (e);

}
}

The class Employees {
Private int id;
private String name;
Private double salary;

Public Employees () {
super();
}

Public Employees (int id, String name, double salary) {
super();
this.id=id;
this.name=name;
This. Salary=salary;
}

Public double getSalary () {
Return the salary;
}

Public void setSalary (double salary) {
This. Salary=salary;
}

@ Override
Public String toString () {
Return "Employees [id=" + id + "name=" + name + ", salary="+ salary +"] ".
}




}

CodePudding user response:

Let employees class implements Comparable Interface, and then realize the compareTo () method, so there is no need to realize the Comparator in the main method, is also a way of thinking,
  • Related