简体   繁体   English

从客户列表中返回客户

[英]Returning a customer from an arraylist of customers

So I have these two methods: The first one is to go through the arraylist of customers and return a value c which is the customer whose ID of type String matches with one of the customers within the ArrayList . 因此,我有两种方法:第一种方法是遍历客户的arraylist并返回值c ,该值是其String类型的ID与ArrayList一个客户匹配的客户。

private Customer findCustomer(String id){
    Customer c;
    for(Customer customer : customers){
        if(customer.getID().equals(id)){
            c = customer;
            return c;
        }
    }
    return null;
}

Then I have the second one which when someone access this method in my GUI of a make-shift movie rental program and passes in the movie, the day they are renting, and the id of the customer 然后是第二个,当有人在我的临时电影租借程序的GUI中访问此方法并传递电影,租借的日期和客户ID时,

public void movieRented(Movie m, Date rented, String id){
    m.setDateRented(rented);
    Customer c = findCustomer(id);
    c.addMovie(m);
    m.setIntStock(false);
}

I am getting an error message surrounding these two methods, and I just wanted to make sure that they at least look correct. 我收到有关这两种方法的错误消息,我只是想确保它们至少看起来正确。

Take care that you are returning null, so you can have a NullPointerException 请注意,您将返回null,因此您可以拥有NullPointerException

private Customer findCustomer(String id){
        Customer c;
        for(Customer customer : customers){
            if(customer.getID().equals(id)){
                c = customer;
                return c;
            }
        }
        return null;
    }

You may consider improving your method 您可以考虑改善方法

private Customer findCustomer(String id){
            Customer c=null;
            for(Customer customer : customers){
                if(customer.getID().equals(id)){
                    c = customer;
                    break;
                }
            }
            return c;
}

Or better now , using custom exceptions 或者现在更好,使用自定义异常

 private Customer findCustomer(String id) throws NoFoundCustomerException{
                Customer c=null;
                for(Customer customer : customers){
                    if(customer.getID().equals(id)){
                        c = customer;
                        break;
                    }
                }
                if(c == null){
                 throw new NoFoundCustomerException();
                }

                return c;
}

And in client code you can do something like this: 在客户端代码中,您可以执行以下操作:

public void movieRented(Movie m, Date rented, String id){
    try{
    m.setDateRented(rented);
    Customer c = findCustomer(id);
    c.addMovie(m);
    m.setIntStock(false);
   }catch(NotFoundedCustomerException e){
     JOptionPane.showMessage(null,"Customer doesn't exist");
   }
}

And your exception would look like this 而且您的异常看起来像这样

public class NotFoundedCustomerException extends Exception{

 public NotFoundedCustomerException(){
        super();
 }

 public NotFoundedCustomerException(String message){
        super(message);
 }
  .
  .
  .
}

I'm not sure if this is right but there could be a spelling mistake in this line: 我不确定这是否正确,但是这一行可能存在拼写错误:

m.setIntStock(false);

It looks like is should be 看起来应该是

m.setInStock(false);

It also seems that you could be having trouble if the customer is not found you return null. 如果没有找到客户,您似乎会遇到麻烦,您返回null。 If you are not sure why this is happening I would suggest some simple println statements. 如果您不确定为什么会这样,我建议您使用一些简单的println语句。 For example: 例如:

System.out.println("customer id = " + customer.getId() + " id= " + id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM