操作 在 AbstractClasses/practice_1 项目的 java_basics 存储库中运行作业。该目录是空的,您需要在其中创建一个新项目并完成任务。
- 创建一个包含员工的 Company 类并实现以下方法:
雇用一名员工 - 雇用(雇员员工),雇用员工列表 - 雇用所有(收集雇主),解雇一名员工 - 解雇(员工员工),获取公司的收入值 - getIncome()。每个方法都不应该有静态修饰符,这将允许 Company 类的每个对象拥有自己的一组员工、自己的收入计算、解雇和雇用。根据应用程序的逻辑选择方法的参数和返回值。
- 创建两个返回指定长度(计数)的列表的方法。它们应包含按工资降序和升序排序的员工:
列表 getTopSalaryStaff(int count),列表 getLowestSalaryStaff(int count)。
- 使用薪资信息和薪资条件创建员工类别:
经理 - 工资由固定部分和为公司赚到的钱的 5% 形式的奖金组成。随机生成 115,000 到 140,000 卢布之间的公司收入。 TopManager - 工资由固定部分和奖金组成,如果公司收入超过1000万卢布,则奖金为工资的150%。操作员——工资仅由固定部分组成。每个员工类必须实现 Employee 接口。 Employee 接口必须声明一个返回员工工资的方法 - getMonthSalary()。
根据工资逻辑选择方法的参数和返回值。在接口中声明必要的方法。要演示和测试您的课程如何运作:
为公司创建和雇用:180 名操作员、80 名经理销售经理、10 名顶级经理。打印出公司薪资最高的 10-15 名名单。打印出公司内 30 名最低工资的清单。解雇 50% 的员工。打印出公司薪资最高的 10-15 名名单。打印出公司内 30 名最低工资的清单。
还没有说您需要编写一个工作比较器。老实说,我用代码查看了选项,但我仍然找不到如何创建比较器,以便根据 TopManager 类中的条件授予奖金 if ( company.getCompanyRevenue() > 10_000_000.0) ,并且与现在不同的是,尽管 10_000_000.0 中的情况存在,但奖金是根据公司收入 800000.0 授予的。给我一个引导。
- 请告诉我如何在这里正确提问,这是我的第一次经验。
- 请提供解决方案。
import java.util.*;
public class Company {
private double inCome = 0;
List <Employee> employees = new ArrayList<>();
public void hire (Employee employee) {
employees.add(employee);
}
public void hireAll (List <Employee> employees) {
for ( Employee e : employees) {
hire(e);
}
}
public void fire (Employee employee) {
employees.remove(employee);
}
public double getInCome () {
return inCome;
}
public List <Employee> getTopSalaryStaff (int count) {
employees.sort(new SortByTopSalaryStaff());
if (count < 0 && count > employees.size()) {
List <Employee> EmptyList = new ArrayList<>();
System.out.println("неверное количество");
return EmptyList;
}
return employees.subList(0, count);
}
public List <Employee> getLowestSalaryStaff (int count) {
employees.sort(new SortByLowestSalaryStaff());
if (count < 0 && count > employees.size()) {
List <Employee> EmptyList = new ArrayList<>();
System.out.println("неверное количество");
return EmptyList;
}
return employees.subList(0, count);
}
public double getCompanyRevenue () {
for (Employee e : employees) {
inCome = inCome + e.getRevenue();
}
return inCome;
}
}
public interface Employee {
double getMonthSalary ();
double getRevenue();
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Company company = new Company();
List <Employee> workers = new ArrayList<>();
Operator operator = new Operator();
//Manager manager = new Manager();
//TopManager topManager = new TopManager(company);
company.hire(operator);
for (int i = 0; i < 180; i++){
workers.add(new Operator());
}
for (int i = 0; i < 80; i++) {
workers.add(new Manager());
if ( i%8 == 0 ) {
workers.add(new TopManager(company));
}
}
company.hireAll(workers);
System.out.println();
System.out.println("Количество работников : " + company.employees.size());
System.out.println();
System.out.println("Доход компании : " + company.getCompanyRevenue());
System.out.println();
System.out.println("topSalaryStaff");
System.out.println();
List <Employee> topSalaryStaff = company.getTopSalaryStaff(12);
for (Employee e: topSalaryStaff) {
System.out.println(e.getMonthSalary());
}
System.out.println("--------------------------------------");
/*
System.out.println();
System.out.println("lowSalaryStaff");
System.out.println();
List <Employee> lowSalaryStaff = company.getLowestSalaryStaff(12);
for (Employee e: lowSalaryStaff) {
System.out.println(e.getMonthSalary());
}
System.out.println("--------------------------------------");
*/
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Company company = new Company();
List <Employee> workers = new ArrayList<>();
Operator operator = new Operator();
//Manager manager = new Manager();
//TopManager topManager = new TopManager(company);
company.hire(operator);
for (int i = 0; i < 180; i++){
workers.add(new Operator());
}
for (int i = 0; i < 80; i++) {
workers.add(new Manager());
if ( i%8 == 0 ) {
workers.add(new TopManager(company));
}
}
company.hireAll(workers);
System.out.println();
System.out.println("Количество работников : " + company.employees.size());
System.out.println();
System.out.println("Доход компании : " + company.getCompanyRevenue());
System.out.println();
System.out.println("topSalaryStaff");
System.out.println();
List <Employee> topSalaryStaff = company.getTopSalaryStaff(12);
for (Employee e: topSalaryStaff) {
System.out.println(e.getMonthSalary());
}
System.out.println("--------------------------------------");
/*
System.out.println();
System.out.println("lowSalaryStaff");
System.out.println();
List <Employee> lowSalaryStaff = company.getLowestSalaryStaff(12);
for (Employee e: lowSalaryStaff) {
System.out.println(e.getMonthSalary());
}
System.out.println("--------------------------------------");
*/
}
}
public class Operator implements Employee {
private double salary = 30_000;
final double income = 0;
@Override
public double getMonthSalary() {
return salary;
}
@Override
public double getRevenue() {
return income;
}
}
import java.util.Comparator;
public class SortByLowestSalaryStaff implements Comparator<Employee> {
@Override
public int compare(Employee employee1, Employee employee2) {
if (employee1.getMonthSalary() > employee2.getMonthSalary()) {
return 1;
}
if (employee1.getMonthSalary() == employee2.getMonthSalary()) {
return 0;
}
return -1;
}
}
import java.util.Comparator;
public class SortByTopSalaryStaff implements Comparator <Employee> {
@Override
public int compare(Employee employee1, Employee employee2) {
return Double.compare(employee2.getMonthSalary(), employee1.getMonthSalary());
}
/*
@Override
public int compare(Employee employee1, Employee employee2) {
if (employee1.getMonthSalary() < employee2.getMonthSalary()) {
return 1;
}
if (employee1.getMonthSalary() == employee2.getMonthSalary()) {
return 0;
}
return -1;
}
*/
}
import java.util.Random;
public class TopManager implements Employee{
private final Company company;
private final double salary;
private final double bonus = 1.5;
private final double earnedLimit = 10_000_000.0;
final double income = 0;
public TopManager (Company company) {
this.company = company;
this.salary = 900000; // + new Random().nextInt(40000);
}
@Override
public double getRevenue() {
return income;
}
@Override
public double getMonthSalary() {
//double companyIncome = company.getCompanyRevenue();
if ( company.getCompanyRevenue() > earnedLimit) {
return salary + salary * bonus;
}
return salary;
}
}
所提供代码中的主要错误是该方法中公司收入
getCompanyRevenue()的计算不正确,其中变量的值inCome(该名称应翻译为利润)未重置为零,因此,“利润”将随着每次增加而增加调用这个方法,也就是说,比如在对员工进行排序时,每个高层管理人员的收入与任何其他员工的收入进行比较时,都会调用这个方法,公司的“收入”就会暴涨。您可能应该缓存计算出的收入值以减少不必要的计算次数:
因此,当调用其他改变公司员工/人员的方法时,变量的值
income应该重置为null(仅在本教程的范围内,假设该程序是单线程的)。加法
可能有一个不带缓存的替代解决方案( getter 中的附加逻辑
getCompanyRevenue),但无论如何,您都需要实现一个单独的方法来计算总收入/利润,您应该记住调用该方法而不是右侧的setter例如,填充所有员工的集合后的时间。该类应该在排序之前
Main调用此方法:hireAll此外,在所提供的所有员工的代码中,从该方法返回的他们的“收入”
getRevenue等于零,因此,公司的收入将始终为零。为了检查高层管理人员累积奖金的可能性,运营商至少需要带来 56K 的收入以及其他输入数据。
另一个常见错误是使用浮点类型
double来存储需要固定精度(例如,精确到便士)的财务计算结果。另外,在所提供的代码中,您可以改进类
getTopSalaryStaff中方法的实现。getLowestSalaryStaffCompany首先,检查输入参数后进行排序
count。if (count < 0 && count > employees.size())其次,由于逻辑运算无效,该条件没有意义并且永远不会被满足&&,这意味着count它是大于零的负数。应该使用 OR 运算符||。此外,对于无效参数,抛出相应的类型异常是有意义的
IllegalArgumentException,而不是返回空列表。如果需要返回一个空列表,最好使用标准方法,Collections.emptyList而不是每次都创建一个空的可修改列表。第三,自 2014 年引入 Java 8 以来,不再需要单独的比较器类,而应该使用静态方法
Comparator::comparingDouble:第四,上述方法应使用 . 返回不可修改的列表
Collections.unmodifiableList。类似地,使用 Stream API 并定义两个静态比较器: