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

Hudson Kim
Hudson Kim
2,965 Points

why is task 1 failing. There is no way to see what the failure is due to.

I got task one working earlier but added new code to do some validation before returning the upper case input. Not sure why task 1 is failing now?

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;
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

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

  private String normalizeDiscountCode (String input) {
    String upper_input = input.toUpperCase();
    for(int i = 0; i < upper_input.length(); i++) {
       char place = upper_input.charAt(i);
       if(place != '$' || !Character.isLetter(place)) {
         throw new IllegalArgumentException("Invalid discount code!");
       }
    }
    System.out.println("input: "+input);
    System.out.println("input_upper: "+upper_input);
    return upper_input;
  }
}

3 Answers

You want to negate the check for isLetter() like you did in your first post. You had:

if(place != '$' && !Character.isLetter(place))

I changed OR to AND. Use that. :+1: :wink:

Steve.

P.S. Have a look at an enhanced for loop - way easier way to access an iterable.

Hi there,

Check your logic. You want to throw the exception if the character is not a letter and not a dollar sign. Using OR throws the exception for the dollar sign as it is not a letter.

Steve.

Hudson Kim
Hudson Kim
2,965 Points

I fixed it but still task one is failing! Here's the code:

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 String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

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

  private String normalizeDiscountCode (String input) {
    for(int i = 0; i < input.length(); i++) {
       char place = input.charAt(i);   
       if(place != '$' && Character.isLetter(place)) {
          throw new IllegalArgumentException("Invalid discount code.");
      }
    }
    return input.toUpperCase();       
  }
}