gpt4 book ai didi

java - 检查数组列表中的空字段

转载 作者:行者123 更新时间:2023-12-01 14:59:57 25 4
gpt4 key购买 nike

我可以在这里找到很多关于数组列表是否可以等于 null 的问答,这以它自己的方式很有帮助,但是如果数组列表中的任何字段为空,我找不到抛出错误的答案。当我将对象添加到数组列表时,如果用户尝试传入任何空值,我想抛出异常。这是代码:

    void addInvoiceItem(Item item, Integer quantity, double discount) throws Exception {    
for (InvoiceItem thing: invoiceList) {
if (thing == null) {
throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
}
else {
invoiceList.add(thing);
thing.setItemQuantity(quantity);
thing.setItemDiscount(discount);
System.out.println(invoiceList);
}
}
}

这是 Item 类:

final class Item {

String itemDescription;
double itemPrice;
Integer itemSKU;

Item (String description, double price, Integer sku) {
this.itemDescription = description;
this.itemPrice = price;
this.itemSKU = sku;
}

}

以下测试方法让我知道我肯定遗漏了一些东西。一种是测试有效的 InvoiceItem,另一种是测试无效的 InvoiceItem(包含空值):

public class InvoiceTest {
//create the static values to be used
//for InvoiceItem
String goodDescription = "wheel";
double goodPrice = 500.00;
Integer goodSku = 0002;
Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);

String emptyDescription = null;
double emptyPrice = 0;
Integer emptySku = 0;
Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);

Integer itemQuantity = 0;
double itemDiscount = 0.05;

@Test
public void invalidItemAddTest() {
Invoice badInvoice = new Invoice(null);
try {
badInvoice.addInvoiceItem(badInvoiceItem, itemQuantity, itemDiscount);
System.out.println(badInvoice);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void validItemAddTest() {
Invoice goodInvoice = new Invoice(null);
try {
goodInvoice.addInvoiceItem(goodInvoiceItem, itemQuantity, itemDiscount);
System.out.println(goodInvoice);
} catch (Exception e) {
e.printStackTrace();
}
}

预先感谢您的所有帮助

编辑添加内容:所以梅尔的答案是我的起点,我做了一些补充,让它按照我需要的方式工作。我的添加方法现在如下所示:

    void addInvoiceItem(Item item, Integer quantity, double discount) {
if(item == null || quantity == 0 || discount == 0) {
throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
} else {
InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
invoiceList.add(invoice);
}
}

我的测试方法如下所示:

public class InvoiceTest {
//create the static values to be used
//for InvoiceItem
String goodDescription = "wheel";
double goodPrice = 500.00;
int goodSku = 0002;
Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);

String emptyDescription = null;
double emptyPrice = 0;
int emptySku = 0;
Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);

int badItemQuantity = 0;
double badItemDiscount = 0;
int goodItemQuantity = 1;
double goodItemDiscount = 0.05;


/**
* @Before - initialize what we need for the test
* @throws Exception
*/
@Before
public void setUp() {
//things needed for testInvalidItemAdd()


}

/**
* @throws Exception
* @Test - confirm you cannot add an item that is null
*/
@Test
public void invalidItemAddTest() {
boolean exceptionThrown = false;
Invoice badInvoice = new Invoice(null);
try {
badInvoice.addInvoiceItem(badInvoiceItem, badItemQuantity, badItemDiscount);
System.out.println(badInvoice);
} catch (Exception e) {
e.printStackTrace();
exceptionThrown = true;
}
assertTrue(exceptionThrown);
}

@Test
public void validItemAddTest() {
boolean exceptionThrown = false;
Invoice goodInvoice = new Invoice(null);
try {
goodInvoice.addInvoiceItem(goodInvoiceItem, goodItemQuantity, goodItemDiscount);
System.out.println(goodInvoice);
} catch (Exception e) {
e.printStackTrace();
exceptionThrown = true;
}
assertFalse(exceptionThrown);
}

最佳答案

你的添加例程有点不对劲。它尝试使用列表中已有的项目而不是创建新项目。试试这个:

void addInvoiceItem(Item item, Integer quantity, double discount) {
if(
item == null ||
quantity == null ||
item.sku == null ||
item.description == null
) {
throw new NullPointerException();
}
InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
invoiceList.add(invoice);
}

另请参阅 Google 库中的 checkNotNull( ),以减少输入量。

在 InvoiceItem 构造函数中而不是在加法器中检查 null 可能会很有用,除非您希望在其他地方允许 null。

关于java - 检查数组列表中的空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812427/

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