다음은 상태 입력이 유효한지 확인하는 전자 제품의 생성자입니다. 내 main()
에서 그러나클래스로 잘못된 입력 처리
public Electronics(String name, double price, int quantity, double weight, boolean frag, String s)
{
super("Electronics",name,price,quantity,weight);
fragile=frag;
s=s.toUpperCase();
if(checkState(s)==true)
{
state=s;
}
else
{
out.println("ERROR - not a valid state abbreviation");
}
}
, 나는 이런 일이 :
public List<Item> shoppingCart = new ArrayList<Item>();
temp= new Electronics(name,price,quantity,weight, fragile, state);
...
shoppingCart.add(temp);
그래서 국가의 약어가 유효하지 않은 경우에도, (그냥 상태가 유효하지 않음을 출력합니다)를하지만, 객체입니다 여전히 ArrayList에 추가됩니다. 상태 약어가 올바르지 않은 경우 추가를 중단하려면 어떻게해야합니까?
: 주요 방법
: thecheckState()에 대한 호출이 true를 반환하는 경우 검사
방법 오류 메시지를 인쇄하는 대신 IllegalArgumentException을 던져 주시겠습니까? 그렇게하면 목록에 추가되는 것을 방지하고 상태가없는 개체가 만들어지지 않도록 방지 할 수 있습니다. – pabrantes