如何创建一个for语句来处理我的异常

时间:2022-06-13 14:30:22

I had previously created a program that contained a customerList that added courses to each customer via their courseList. Now I have to modify the program (having a new list of customers) to throw an exception called CustomerNotEnrolledException if one the customers are not enrolled in any course.

我之前创建了一个程序,其中包含一个customerList,它通过courseList向每个客户添加了课程。现在我必须修改程序(有一个新的客户列表),如果客户没有注册任何课程,则抛出一个名为CustomerNotEnrolledException的异常。

I throw the exception from a createInvoice method in my Customer class, and handle it in the test class. My question is:

我从Customer类中的createInvoice方法抛出异常,并在测试类中处理它。我的问题是:

How will I write a for loop to check for these courses within each customer.

我将如何编写for循环来检查每个客户中的这些课程。

The two arrays that I have declared earlier are:

我之前声明的两个数组是:

ArrayList<Course> courseList = new ArrayList<Course>();
ArrayList<Customer> customerList = new ArrayList<Customer>();

1 个解决方案

#1


0  

Depending on the way you conceived it, it could be something very simple You may check at construction that the calling code has provided a non empty list of courses or you can have a method that checks it. If the list of courses is null or empty then you throw the exception

根据你构思它的方式,它可能是非常简单的你可以在构造中检查调用代码提供了非空的课程列表,或者你可以有一个检查它的方法。如果课程列表为null或为空,则抛出异常

Example: In your createInvoice method you can call checkCourseEnrollment()before further processing

示例:在createInvoice方法中,您可以在进一步处理之前调用checkCourseEnrollment()

 public class Customer {
    private List<Course> courses;

    public Customer() {}
    public Customer(List<Course> courses) throws CustomerNotEnrolledException {

        // Check here if the constructor receives any course list
        // If not trigger the exception 

        if (null == courses || courses.size() == 0) {
            throw new CustomerNotEnrolledException(/* potential parameters here */);
        }

        // continue constructor initialization process here
        this.courses = courses;
    }

    public void checkCourseEnrollment() throws CustomerNotEnrolledException {

        if (null == this.courses || this.courses.size() == 0) {
            throw new CustomerNotEnrolledException(/* potential parameters here */);
        }

    }

}

#1


0  

Depending on the way you conceived it, it could be something very simple You may check at construction that the calling code has provided a non empty list of courses or you can have a method that checks it. If the list of courses is null or empty then you throw the exception

根据你构思它的方式,它可能是非常简单的你可以在构造中检查调用代码提供了非空的课程列表,或者你可以有一个检查它的方法。如果课程列表为null或为空,则抛出异常

Example: In your createInvoice method you can call checkCourseEnrollment()before further processing

示例:在createInvoice方法中,您可以在进一步处理之前调用checkCourseEnrollment()

 public class Customer {
    private List<Course> courses;

    public Customer() {}
    public Customer(List<Course> courses) throws CustomerNotEnrolledException {

        // Check here if the constructor receives any course list
        // If not trigger the exception 

        if (null == courses || courses.size() == 0) {
            throw new CustomerNotEnrolledException(/* potential parameters here */);
        }

        // continue constructor initialization process here
        this.courses = courses;
    }

    public void checkCourseEnrollment() throws CustomerNotEnrolledException {

        if (null == this.courses || this.courses.size() == 0) {
            throw new CustomerNotEnrolledException(/* potential parameters here */);
        }

    }

}