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

Maan Azzam
Maan Azzam
2,239 Points

Oops! It looks like Task 1 is no longer passing.

I have no idea what am doing wrong... everything seems to be correct but it keeps taking me back to task 1.

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

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

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

  private String normalizeDiscountCode(String discountCode){

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

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

}

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Maan

Here is where your main problem lies:

public void applyDiscountCode(String discountCode) {
     discountCode = normalizeDiscountCode(discountCode);//<-- this is the main problem
  }

The main problem is you did not use this.discountCode in your applyDisountCode(String discountCode). Please remember that this.discountCode and discountCode which passed to the applyDiscountCode method are two different variables.

Here is how I wrote my code to pass the challenge. I hope this can give you some reference. I use the same IF statement style as yours:

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

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

I hope this can help you a little

Maan Azzam
Maan Azzam
2,239 Points

Thanks for the help, it was indeed "This.disccount" that was missing.