I previously wrote an entire successful program composed of 7 classes(Date
, Address
, Time
, Customer
, Course
, InClassCourse
, OnLineCourse
), an interface (Invoice
) and of course the test class (CustomerTest
). The interface Invoice
has a method createInvoice
which is implemented in the Customer
class. In the test case, I created 3 new customers, added their respective courses, and then calculated their charges depending on the numbers of courses they are enrolled in, the type of course they are enrolled in and whether the class is an InClassCourse
or OnLineCourse
, and finally print out the information a dialogue box. The list of customers and courses are kept in two separate array lists:
ArrayList<Customer> customerList = new ArrayList<Customer>();
ArrayList<Course> courseList = new ArrayList<Course>();
Using an enhanced for loop, I polymorphically walked through the customerList
and created the invoice for each customer.
I have now written an additional class CreateFiles
, containing a new list of customers and courses, that writes customer data to a file customers.txt and course data to file courses.txt. CreateFiles
has a method writeCustomers
and writeCourses
.
I am new to exceptions and still learning. To modify my program I would like to add a user-specified exception called CustomerNotEnrolledException
. The createInvoice
method in the Customer
class will throw CustomerNotEnrolledException
if the customer does not have any courses on his list (several of my customers are not enrolled in any courses), and then handle this exception in my test class.
My question is how do I write the statement in the try block to check to see if a customer is enrolled in any courses, and if not eliminated them. I need to do this because after I have eliminated the non-enrolled customers, I will add methods readCustomers
, readCourses
, and generateInvoice
in the Test case and use them to read the customers.txt file and courses.txt file to create customers and add them to customerList
, as well as, create courses, which would be added to their respective customers.
I already created an exception class called CustomerNotEnrolledException
that extends exception:
public class CustomerNotEnrolledException extends Exception
{
public CustomerNotEnrolledException()
{
super("Customer not enrolled");
}
and my original createInvoice method looks like this:
public String createInvoice()
{
double total;
if (cType==CustomerType.STUDENT)
total = 25;
else if (cType==CustomerType.FACULTY)
total = 50;
else if (cType==CustomerType.GOVERNMENT)
total = 35;
else
total = 0;
for(Course course:this.courseList)
{
total += course.calculateCharge(this.cType);
}
return (String.format("%s\t\t%d\t\t$%.2f", this.name,
this.accountNumber,total));
}