Arraylist检查CustomerID是否在Rent ArrayList中

时间:2019-04-04 18:12:01

标签: java

我目前有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>();

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

如果您为对象覆盖equals方法,则可以在ArrayList实例上使用contains

相关问题