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

Kenneth Emmanuel Kouadio Ziguy
PLUS
Kenneth Emmanuel Kouadio Ziguy
Courses Plus Student 1,125 Points

I am not able to complete the code challenge

I keep recieving this error message : Oops! It looks like Task 1 is no longer passing. and I do not know how to get pass it while my code seems to be logically correct. I really do not know what to do anymore. please Help! NB: I wrote my code in the normalizeDiscountCode() method and used it in applyDiscountCode() method

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;
  }
  private String normalizeDiscountCode(String discountCode){
     for (char characterCheck: discountCode.toCharArray()){
     if(! Character.isLetter(characterCheck) || characterCheck !='$'){
          throw new IllegalArgumentException("Invalid discount code");
       }
     }
    return discountCode.toUpperCase();
  }

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

  }
}

3 Answers

Seth Kroger
Seth Kroger
56,414 Points

You're having a little trouble wrapping your brain around inverting the condition. That's okay, it can be a little tricky. You want it to pass if the character is a letter or the character is a dollar sign. But your if statement is looking for a failure, the inverse of the condition. This means it can't be either, or another way of putting it the character is both not a letter and not a dollar sign. (Basically you always flip the and/or as well as the is/not).

Anders Björkland
Anders Björkland
7,481 Points

Hi Kenneth,
This is a tricky one. Task 1 is not the problem, and your code looks logical enough. Where the challenge lies here is in the normalizeDiscountCode-method. You want it to return a String in uppercase as long as it is letters and/or $-sign. So if a String contains something that is not a letter or a $-sign the method should throw an error. So try to go through the method. What would happen if you call the method with "abc$"? You want it to not throw an error, but is that how you have set up your method?