Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Delivering the MVP Applying a Discount Code

Adam Shockley
Adam Shockley
4,548 Points

Exception error

Error is pointing to my exception on line 32

Order.java
public class Order {
  private String itemName;
  private int priceInCents;
  private String discountCode;

  public Order(String itemName, int priceInCents) {
    this.itemName = itemName;
    this.priceInCents = priceInCents;
  }

  private String normalizeDiscountCode(String discountCode) {
    return discountCode.toUpperCase();
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

  public void applyDiscountCode(String discountCode) {
    for (int i = 0; i < discountCode.length(); i++) {
        char check = discountCode.charAt(i);

      if (! Character.isLetter(check) || check != '$') {
        throw new IllegalArgumentException("Invalid discount code.");
      }
    }
    this.discountCode = normalizeDiscountCode(discountCode);
  }
}

3 Answers

Hi Adam,

The question says In the normalizeDiscountCode verify .... Your code is in a different method. I'd also look at the enhanced for loop to make that code easier.

Have a go at that, and let me know how you get on.

Steve.

Adam Shockley
Adam Shockley
4,548 Points

I moved it to the correct method and switched to an enhanced. Still receiving the error. Any ideas?

 public class Order {
  private String itemName;
  private int priceInCents;
  private String discountCode;

  public Order(String itemName, int priceInCents) {
    this.itemName = itemName;
    this.priceInCents = priceInCents;
  }

  private String normalizeDiscountCode (String discountCode) {
    for (char check : discountCode.toCharArray()) {
        if (! Character.isLetter(check) || check != '$') {
        throw new IllegalArgumentException("Invalid discount code");
      }
    }
    return discountCode.toUpperCase();
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

  public void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
  }
}
Adam Shockley
Adam Shockley
4,548 Points

Got it! The 'or' should have been an 'and'. The error was not at all helpful though.

Cheers.

Good work! :+1: