我有以下课程:
public class Insurance {
...
}
Run Code Online (Sandbox Code Playgroud)
public class Customer {
private List<Insurance> insurances;
public List<Insurance> getInsurances() {
return insurances;
}
...
}
Run Code Online (Sandbox Code Playgroud)
public class CustomerRegistry {
private List<Customer> customers;
...
}
Run Code Online (Sandbox Code Playgroud)
以及这个帮助方法,它将a减少List<Predicate<T>>
为单个Predicate<T>
:
public Predicate<T> reducePredicates(List<Predicate<T>> predicates) {
return predicates.stream()
.reduce(Predicate::and)
.orElse(p -> true);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获得与过滤器列表匹配的保险列表,这些过滤器属于与过滤器列表匹配的客户.如果不清楚,下面的代码将有希望澄清.
方法在CustomerRegistry
上面的类中.
public List<Insurance> findInsurances(List<Predicate<Customer>> cusPredicates,
List<Predicate<Insurance>> insPredicates) {
List<Insurance> matches = new LinkedList<>();
customers.stream()
.filter(reducePredicates(cusPredicates)
.forEach(cus -> cus.getInsurances()
.stream()
.filter(reducePredicates(insPredicates))
.forEach(cus -> matches.add(cus))) …
Run Code Online (Sandbox Code Playgroud) 所以我有以下课程..
public abstract class Insurance {
private int yearlyPremium;
...
}
Run Code Online (Sandbox Code Playgroud)
我想要一个字段来表示一个人支付保险的频率,可以是枚举,也可以是整数。
这是我对枚举的想法:
public enum PaymentFrequency {
MONTHLY("Monthly", 12),
QUARTERLY("Quarterly", 4),
SEMIANNUALLY("Semiannually", 2),
ANNUALLY("Annually", 1);
private String name;
private int paymentsPerYear;
PaymentFrequency(String name, int paymentsPerYear) {
this.name = name;
this.paymentsPerYear = paymentsPerYear;
}
public String getName() {
return name;
}
public int getPaymentsPerYear() {
return paymentsPerYear;
}
}
Run Code Online (Sandbox Code Playgroud)
枚举的另一个优点是我可以使用它的values
(静态) 和getName
方法为用户界面生成下拉菜单,这样如果我添加一个新的PaymentFrequency
,我将不必更改下拉菜单。
这是枚举的工作,还是我在这里过于复杂?该字段可以用一个简单的 int 表示,但缺点是我必须检查它是否是一个有效的数字,因为我只想要 1 到 12 之间的值(包含两者),这样12 % n == 0
.
我正在编写java编程并尝试测试它.在Class Card中,我有format()语句告诉它要返回什么.当我尝试在新创建的对象上使用此格式时,它会抛出卡中的format()无法应用于(卡)的错误.我不允许使用toString()语句作为赋值的一部分.请帮忙.以下是课程:
public class Card {
private String name;
public Card() {
name = " ";
}
public Card(String n) {
name = n;
}
public String getName() {
return name;
}
public boolean isExpired() {
return false;
}
public String format() {
return "Card holder: " + name;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试文件
import java.io.IOException;
public class Lab12Test {
public static void main (String [] args) throws IOException {
Card q = new Card("John");
System.out.println("Card Class: ");
System.out.println(Card.format(q) + "\n");
} …
Run Code Online (Sandbox Code Playgroud)