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!

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

David Player
David Player
1,183 Points

I tried to add validation for normlizeDiscountCode method, but it kept said that it couldn't pass the test

I wasn't sure what to do about this task since I added the restriction for not allowing non-letter and any symbols.

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

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

  public void applyDiscountCode(String discountCode) {

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

1 Answer

Hi David,

You're pretty much there.

First, don't set this.discountCode inside normaliseDiscountCode you're already doing that in applyDiscountCode. And you might want to try comparing the characters with && and not ||.

Give that a whirl and let me know how you get on.


David Player
David Player
1,183 Points

It's working great. Thank you!

Cool! :+1: