gpt4 book ai didi

java - Arraylist 检查 CustomerID 是否在 Rent ArrayList 中

转载 作者:行者123 更新时间:2023-12-02 10:02:39 26 4
gpt4 key购买 nike

我目前有 2 个 Arraylist,其中包含 CustomerBike 构造函数数据。我成功地输入并租用 Arraylist 使用:

rent.add(new Rent(customers.get(1),bike.get(1)));

如何检查 CustomerID 是否已在 Arraylist 租赁中,并且会给出错误,提示客户已在租赁?

public Rent(Customer customer, Bike bike) {

this.customers = customer;
this.bike = bike;
}

public Customer(int CustID, String CustFName, String CustLName) {

this.CustID = CustID;
this.CustFName = CustFName;
this.CustLName = CustLName;
}

public Bike(int BikeID, String BikeType, int PricePerDay,booleanisAvailable) {

this.BikeID = BikeID;
this.BikeType = BikeType;
this.PricePerDay = PricePerDay;
this.isAvailable = isAvailable;
}

protected ArrayList<Customer> customers = new ArrayList<Customer();
protected ArrayList<Bike> bike = new ArrayList<Bike>();
protected ArrayList<Rent> rent = new ArrayList<Rent>();

最佳答案

假设您正在寻找具有给定 custId 的客户,您可以执行以下操作:

public boolean isAlreadyRenting(Customer cust) {
return rent.stream().anyMatch(r -> r.getCustomer().getCustId() == cust.getCustId());
}

关于java - Arraylist 检查 CustomerID 是否在 Rent ArrayList 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55522496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com