JAVA - 使用功能界面进行计算

时间:2017-07-19 21:11:04

标签: java functional-interface

所以......这是我在Stack上的第一篇文章,我也是Java的新手(完全是编程)。我试图创建一个简单的命令行应用程序,用于计算员工和#39;利润取决于产生的收入。已经做到了但是我现在正在学习功能界面和lambdas,我想尝试使用它们。在下面你可以找到代码。

package BalanceCounter;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Number of employees: ");
    int empCounter = input.nextInt();
    System.out.println();

    List<Employee> employees = new ArrayList<>(empCounter);
    for (int i = 0; i < empCounter; i++) {
        employees.add(new Employee());
    }

    for (Employee employee : employees) {
        System.out.println("Employee: " + employee.getEmpName()
                + "\nBase salary: " + employee.getEmpBaseSalary()
                + "\nProfit from income: " + employee.getEmpBonus()
                + "\n");
    }
}
}

这里是注释块中带有getEmpBonus()方法的Employee类。那是我尝试使用功能界面的地方。

package BalanceCounter;

import java.util.Scanner;

class Employee {
private String empName;
private double empBaseSalary;
private double empIncome;
private double empBonus;


Employee() {
    Scanner input = new Scanner(System.in);

    System.out.print("Employees name: ");
    setEmpName(input.nextLine());

    System.out.print("Type basic salary: ");
    setEmpSalary(input.nextDouble());

    System.out.print("Generated income: ");
    setEmpIncome(input.nextDouble());
    setEmpBonus();

    System.out.println();
}

/*
 * Here are all the setters
 */

private void setEmpName(String empName) {
    this.empName = empName;
}

private void setEmpSalary(double empBaseSalary) {
    this.empBaseSalary = empBaseSalary;
}

private void setEmpIncome(double empIncome) {
    this.empIncome = empIncome;
}

private void setEmpBonus() {
    if (getEmpIncome() <= 10000)
        empBonus = (getEmpIncome() * 3) / 100;
    else if (getEmpIncome() > 10000 && getEmpIncome() <= 20000)
        empBonus = (getEmpIncome() * 2) / 100;
    else empBonus = (getEmpIncome() * 1) / 100;
}


/*
 * Time for the getters
 */

String getEmpName() {
    return empName;
}

double getEmpBaseSalary() {
    return empBaseSalary;
}

private double getEmpIncome() {
    return empIncome;
}

double getEmpBonus() {
    return empBonus;
}

/*
 * double getEmpBonus(Calculation calculation) {
 * return empBonus = calculation.calculate(this.empBonus);
 * }
 */

}

最后一件事是计算界面。

package BalanceCounter;


public interface Calculation {
    double calculate(double arg);
}

class CalcBonus implements Calculation{
    public double calculate(double empBonus) {
        return empBonus;
    }
}

很抱歉很长的帖子但是想给你我所有的信息。 如果你在我的代码中看到一些错误和坏习惯 - 请告诉我。亲切的问候。

2 个答案:

答案 0 :(得分:0)

此界面:

public interface Calculation {
    double calculate(double arg);
}

DoubleUnaryOperator

具有相同的合同
@FunctionalInterface
public interface DoubleUnaryOperator {
    double applyAsDouble(double operand);
   ...
}

所以你真的不需要为此引入一个新界面 你可以使用内置的 但是如果你想真正强调方法的名称和/或参数。

作为一般方法,请在创建之前查看内置功能接口是否与您的要求不匹配。

假设您现在取消注释方法,并且想要使用此方法:

  double getEmpBonus(DoubleUnaryOperator calculation) {
     return empBonus = calculation.applyAsDouble(this.empBonus);
  }

如果你想传递一个加倍奖金的实现,你必须创建一个子类或匿名类:

class DoubleBonus implements DoubleUnaryOperator {
    public double applyAsDouble(double operand) {
        return operand * 2;
    }
}

lambda的想法是你可以使用lambda表达式内联子类 你可以这样调用方法:

double doubledBonus = getEmpBonus(empBonus->empBonus*2);

您不再需要子类或匿名类。

答案 1 :(得分:0)

package training;
public class Calculator {
    interface IntegerMath {
        int operation(int a, int b);
    }
    interface RealMath {
        double operation(double a, double b);
    }
    public int operateBinary(int a, int b, IntegerMath op) {
        return op.operation(a, b);
    }
    public double operateBinary(int a, int b, RealMath op) {
        return op.operation(a, b);
    }
    public static void main(String... args) {
        Calculator cal = new Calculator();
        IntegerMath addition = (a, b) -> a + b;
        IntegerMath subtraction = (a, b) -> a - b;
        IntegerMath multiplication = (a, b) -> a * b;
        RealMath division = (a, b) -> a / b;
                System.out.println("Add: 40 + 22 = " +
                cal.operateBinary(40, 22, addition));
        System.out.println("Sub: 120 - 80 = " +
                cal.operateBinary(120, 80, subtraction));
        System.out.println("Mul: 12 * 12 = " +
                cal.operateBinary(12, 12, multiplication));
        System.out.println("Div: 478 / 12 = " +
                cal.operateBinary(478, 12, division));
    }
}