mercredi 28 mars 2018

Why does my code ignore the Exception?

My code should make entering anything other than a letter or a '$' in discountCode String result in throwing an exception however this doesn't happen. The user can type anything and still not receive an exception message. Any help is much appreciated, thanks.

private String normalizeDiscountCode(String discountCode) {
  String upperDiscountCode = discountCode.toUpperCase();

    for (char i : upperDiscountCode.toCharArray()) {
      if (Character.isLetter(i) || i == '$') {
        return upperDiscountCode;
      } else {
        throw new IllegalArgumentException("Invalid discount code");
        }
    }
    return upperDiscountCode;
  }

  public void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
  }
}

Aucun commentaire:

Enregistrer un commentaire