2

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));    
    }
user987339
  • 10,519
  • 8
  • 40
  • 45
  • Instead of describing our code, post the relevant parts of it, and post the method you would like to write with its javadoc describing what it should do. – JB Nizet Mar 01 '15 at 18:51

2 Answers2

1

First, modify your method createInvoice() as follows:

public String createInvoice() throws CustomerNotEnrolledException {

    if ((this.courseList == null) || (this.courseList.isEmpty())) {
        throw new CustomerNotEnrolledException("Customer does not have any course");
    }

    // rest of your method goes here
}

Then, in every class that invokes the createInvoice() method, you must surround the invocation with a try block that catches the CustomerNotEnrolledException:

Customer customer = ...; // get the customer from some place
try {

    // some code here

    customer.createInvoice();

    // more code here

} catch (CustomerNotEnrolledException e) {
    // handle CustomerNotEnrolledException here, maybe show error message?
    System.out.println("Exception creating invoice for customer " + customer.getName() + ": " + e.getMessage());
}

The idea is that createInvoice() throws a CustomerNotEnrolledException if it doesn't have any course in his course list. This is just a guessing of what should happen, since I don't know the problem in advance. Moreover, I believe it's your job to implement the logic properly.

As the createInvoice() method throws a checked exception, in this case CustomerNotEnrolledException, the method that calls createInvoice() must handle the CustomerNotEnrolledException exception, either with a try/catch block as I've shown in the example, or by declaring that the method throws CustomerNotEnrolledException in its signature (like the createInvoice() method does).

fps
  • 33,623
  • 8
  • 55
  • 110
  • Will the try block be where an attempt to enter a customer would be? – Anastacia Kong Mar 01 '15 at 22:23
  • @AnastaciaKong Not necessarily, the try block must surround the call to `createInvoice()`. You can do the surrounding in the very same method that calls `createInvoice()` or do it in the method that calls the method that calls it, or in the method that calls the method that calls the method that calls it, etc. In an extreme case, you can have a try block in your `public static void main()` method, and in the most extreme case of all, you can declare that your `public static void main()` method throws `CustomerNorEnrolledException`. – fps Mar 01 '15 at 22:43
  • the problem I am having understanding here is when you say that the try block must surround the call to createInvoice. I have one call to createInvoice in my original test case which serves the purpose of retrieving each customers calculated invoice. The line of code looks like this : for(Customer c:customerList) { output += c.createInvoice() + "\n"; System.out.println(c.toString()); System.out.println(c.getCourseList() + "\n"); } JOptionPane.showMessageDialog(null, output); – Anastacia Kong Mar 01 '15 at 23:36
  • OK, if that's what you're supposed to do, surround all your test case's code with a try block and catch the `CustomerNotEnrolledException` to handle it properly (depends on what you have to do when this exceptional situation occurs). Don't forget to make your methods declare that they throw this exception, so that the exception can reach your main method. – fps Mar 01 '15 at 23:38
  • Ok I forget to tell you one more thing. Im sorry but I'm modifying this program in parts(#1,#2 & #3). #2 was where added the exception and threw it to the main to be handled, but I just looked at the last part of #3 and missed a fundamental part. I am supposed to create a method called generateInvoice in the test case which I did. This method polymorphically creates the invoice to display in my dialog box. The generateInvoice method handles the CustomerNotEnrolled Exception. When this exception is raised it prints a message to the system prompt. – Anastacia Kong Mar 01 '15 at 23:55
  • So you're done! Within `generateInvoice()`, surround its lines with a try block, and in the catch block display the error message. – fps Mar 01 '15 at 23:58
0

I believe you can extend 'Exception' into a newly created class that looks a little like this.

public class CustomerNotEnrolledException extends Exception {

      public CustomerNotEnrolledException(String message) {
           super(message);
      }

      public CustomerNotEnrolledException(String message, Throwable throwable) {
           super(message, throwable);
      }

}

Then import this exception into the class you intend on using it in.

Coleton
  • 19
  • 1
  • 3
  • Thanks, I already created this class. I just need to know how to modify my method createInvoice or if I need to create a new method in the main to check to see if the customers are enrolled in any classes. – Anastacia Kong Mar 01 '15 at 19:07
  • Could you provide your createInvoice method and main so that I can see what you mean? You'll just want to use this exception like any other(try - catch). – Coleton Mar 01 '15 at 19:17
  • Edit your initial post and add this code to it in a code block for readability please. – Coleton Mar 01 '15 at 19:38
  • Ok I edited it, and placed the question I am trying to get across in bold. Please tell me if you understand better. – Anastacia Kong Mar 01 '15 at 19:58