gpt4 book ai didi

java - 如何编写语句来检查我的用户指定的异常 CustomerNotEnrolledException

转载 作者:行者123 更新时间:2023-11-30 11:07:42 26 4
gpt4 key购买 nike

我之前写过一个完整的成功程序,由 7 个类(DateAddressTimeCustomerCourseInClassCourseOnLineCourse)、接口(interface)(Invoice),当然还有测试类(CustomerTest). Invoice 接口(interface)有一个方法 createInvoice,它在 Customer 类中实现。在测试用例中,我创建了 3 个新客户,添加了他们各自的类(class),然后根据他们注册的类(class)数量、注册类(class)的类型以及类(class)是否为 InClassCourse 来计算他们的费用OnLineCourse,最后打印出一个对话框的信息。客户和类(class)列表保存在两个单独的数组列表中:

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

使用增强的 for 循环,我以多态方式遍历了 customerList 并为每个客户创建了发票。我现在已经编写了一个附加类 CreateFiles,其中包含一个新的客户和类(class)列表,它将客户数据写入文件 customers.txt 并将类(class)数据写入文件 类(class).txtCreateFiles 有一个方法 writeCustomerswriteCourses

我是异常的新手,仍在学习中。为了修改我的程序,我想添加一个名为 CustomerNotEnrolledException 的用户指定异常。 Customer 类中的 createInvoice 方法将抛出 CustomerNotEnrolledException 如果客户的列表中没有任何类(class)(我的几个客户没有注册在任何类(class)中),然后在我的测试课中处理这个异常。

我的问题是如何在 try block 中编写语句来检查客户是否注册了任何类(class),如果没有则将其淘汰。我需要这样做,因为在我之后已经排除了非注册客户,我将在测试用例中添加方法 readCustomersreadCoursesgenerateInvoice 并使用它们读取 < strong>customers.txt 文件和 courses.txt 文件来创建客户并将他们添加到 customerList,以及创建类(class),这些类(class)将被添加到他们各自的客户。

我已经创建了一个名为 CustomerNotEnrolledException 的异常类,它扩展了异常:

  public class CustomerNotEnrolledException extends Exception
{
public CustomerNotEnrolledException()
{
super("Customer not enrolled");
}

我原来的 createInvoice 方法是这样的:

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

最佳答案

首先,修改您的方法 createInvoice() 如下:

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
}

然后,在每个调用 createInvoice() 方法的类中,您必须用一个 try block 包围调用,该 block 捕获 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());
}

想法是,如果他的类(class)列表中没有任何类(class),createInvoice() 将抛出一个 CustomerNotEnrolledException。这只是对应该发生什么的猜测,因为我事先不知道问题所在。此外,我相信正确实现逻辑是您的工作。

由于 createInvoice() 方法抛出一个checked 异常,在本例中为 CustomerNotEnrolledException,即调用 createInvoice() 的方法 必须 处理 CustomerNotEnrolledException 异常,可以使用示例中所示的 try/catch block ,也可以通过声明该方法在其签名中抛出 CustomerNotEnrolledException(就像 createInvoice() 方法一样)。

关于java - 如何编写语句来检查我的用户指定的异常 CustomerNotEnrolledException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com