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

Xuanzheng Lin
Xuanzheng Lin
2,466 Points

Code is failing

No idea why the code just failed lol.

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

  // cut out class code for brevity - srh

  public void applyDiscountCode(String discountCode) {
    String want;
    try {
      want = normalizeDiscountCode(discountCode);
      this.discountCode = want;
    } catch (IllegalArgumentException iae) {
      System.out.print(iae.getMessage());
    }
  }

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

2 Answers

Say we have a character 'a'. That is NOT '$'. So, an OR would throw the error as a != '$' evaluates to true. Using an OR we only need one side to be true for the error to be thrown.

Send in a '1' and this is not a letter so !isLetter('1') is true (not-false) and 1 != '$' is also true. So, comparing with && will pick this up and throw the error. That's correct.

Same with '$'. It's not a letter so !isLetter('$') is true and '$' != '$' is false. Again, this would evaluate to true with an OR which would throw the error, when it shouldn't.

Make sense?

Steve.

my head hurts now!

Hi there,

You've got some extra code in there that's not needed. You don't need the try/catch block to pass this challenge.

In the normalizeDiscountCode method, you've correctly used a for:in loop to iterate over the code, converted to a char array. You've not done the comparison correctly, though. I think you need && not ||.

You can also tidy up your returned value by omitting the extra variable. Just return the string with .toUpperCase() applied directly to it:

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

I hope that helps,

Steve.

Xuanzheng Lin
Xuanzheng Lin
2,466 Points

Thanks bro. U r totally right. But can u explain u need && instead of ||?