-2

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.

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

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

The two arrays that I have declared earlier are:

ArrayList<Course> courseList = new ArrayList<Course>();
ArrayList<Customer> customerList = new ArrayList<Customer>();
Barranka
  • 20,547
  • 13
  • 65
  • 83
Aaron Gabriel
  • 37
  • 1
  • 1
  • 2
  • Not clear at all. Exceptions are handled by try catch not a for statement. How does a customer have a course they are not enrolled for, if it's in their courses list. Presumably there's an other list of courses with customers that have been enrolled? – Tony Hopkinson Feb 28 '15 at 23:38
  • Taken from [How to ask](http://stackoverflow.com/help/how-to-ask): *"Now that you're ready to ask your question, take a deep breath and read through it from start to finish. Pretend you're seeing it for the first time: **does it make sense**?"* You have to write your question in a way other people can help you. As it stands right now, your question does not provide enough information about the problem: How are `Course` and `Customer` related? Does a `Customer` has a list of `Course`s, or does a `Course` contains a list of `Customers`? There must be a way to check how objects are related – Barranka Feb 28 '15 at 23:47
  • Yes I have a new list of customers and courses. Each customers have a list of classes in which they are enrolled. If a customer isn't enrolled in any courses, I would like to throw and exception and handle it in the test class. Can you help me please? – Aaron Gabriel Feb 28 '15 at 23:48

1 Answers1

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

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

 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 */);
        }

    }

}
alainlompo
  • 4,414
  • 4
  • 32
  • 41