ArrayList
개의 제품이 이미 초기화되었습니다. Product
생성자이다객체 속성으로 계산하도록 ArrayList 반복
public Product(String number, String type, double rentalDeposit, double rentalPrice, double lateFee, double buyPrice, int maxDuration)
type
을 열거하여 결정
protected enum productType {Basket, BabySeat, Helmet, Headlight, Bell};
난 열거 대한 toString
방법을 사용하여 전달 type
. 나는 등, 내가 가진 (shop.getInventory()
에 의해 주어진)를 ArrayList<Product>
을 반복하고 type
Basket
, BabySeat
, Helmet
의 얼마나 많은 즉, 거기에 각 type
의 얼마나 많은 계산이 필요
Product
클래스는 getType()
방법이있다 문자열을 반환합니다.
for (Product.productType product : Product.productType.values()) {
int occurences = Collections.frequency(shop.getInventory(), product.toString());
}
나는 Collections.frequency
를 사용하여 시도했다, 그러나 그것은 0
를 반환 유지하고 나는 이유를 모르겠어요.
if
문장을 사용하지 않고 반복하고이 금액을 찾는 다른 방법이 있습니까?
'shop.getInventory()'를 다음과 같이 바꾸어보십시오. shop.getInventory(). stream(). map (제품 -> product.productType') .collect (Collectors.toList())'(get for-loop 제거 - 필요하지 않음). – alfasin