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

Carlo Antonio Bilbao
Carlo Antonio Bilbao
24,113 Points

Java Applying Discount Code Challenge, Not sure why code isnt passing the first take anymore. Looks good to me.

Passed first task, coded the second task, then 1st task will no longer pass. Not sure why.

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

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

  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);
  }
}
Example.java
public class Example {

  public static void main(String[] args) {
    // This is here just for example use cases.

    Order order = new Order(
            "Yoda PEZ Dispenser",
            600);

    // These are valid.  They are letters and the $ character only
    order.applyDiscountCode("abc");
    order.getDiscountCode(); // ABC

    order.applyDiscountCode("$ale");
    order.getDiscountCode(); // $ALE


    try {
      // This will throw an exception because it contains numbers
      order.applyDiscountCode("ABC123");
    } catch (IllegalArgumentException iae) {
      System.out.println(iae.getMessage());  // Prints "Invalid discount code"
    }
    try {
      // This will throw as well, because it contains a symbol.
      order.applyDiscountCode("w@w");
    }catch (IllegalArgumentException iae) {
      System.out.println(iae.getMessage());  // Prints "Invalid discount code"
    }

  }
}

2 Answers

andren
andren
28,558 Points

Your code is close to correct but you have made a mistake in your if statement, mind you it is a very common mistake.

The issue is the logic of your if statement. The if statement should execute (and therefore throw an exception) only if the character is not a letter and not the $ symbol. Notice the use of the word and in that sentence. In your if statement you use the || or operator, the way the or operator works is that if any of the conditions is true it will return true. Meaning that if the character is either not a letter, or not the $ symbol it will trigger an exception. Since something can't be both a letter and the $ symbol at the same time one of those conditions will always be true, which means that the if statement will always execute even if you pass it a valid character.

One of the ways around this is to use the && and operator instead. Using that the if statement will only execute if the character is not a letter and not the $ symbol which is the behavior you are actually looking for.

Michael Santos
Michael Santos
835 Points

Thank you. Had the same mistake put an || instead of an &&. Appreciate it.

Carlo Antonio Bilbao
Carlo Antonio Bilbao
24,113 Points

Thanks! I stared at it for an hour and couldn't get it. But once you explained it, it clicked right away. Thanks!