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

Rafał Stasiak
Rafał Stasiak
3,763 Points

In the normalizeDiscountCode verify that only letters or the $ character are used.

Hello,

Please look on my code. All time I get syntax errors.

Why cant i store code.toCharArray in Char S

Char s = code.toCharArray

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 code){
    char s = code.toCharArray;
    if(Character.isLetter(s) = -1 || code.indexOF('$')= -1) {
    throw new IllegalArgumentException("Invalid discount code");
    }
  return code.toUpperCase();
  }
}
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"
    }

  }
}

4 Answers

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

Well there are a few issues with your code.

1) When you call normalizeDiscountCode() in the the applyDiscountCode method, the parenthesis need to be against the method call.

2) You are creating a character array of the discount code which is a valid way of completing this challenge but toCharArray is a method so you need to add parenthesis behind the call. Also you pass this variable into Character.isLetter() which only takes a single char not a char[] and also you are comparing a char[] to a single '$'. To fix this, you need to loop over each character in the char array and then complete the logic in the for loop by comparing each individual char.

3) Character.isLetter() and the use of the == operator both return a boolean value which means -1 isn't a correct value to compare to so the value needs to be false or 0. If you do all this it will work, and this is what I got if you need to see what I'm talking about:

private String normalizeDiscountCode(String discountCode) {
    for(char c : discountCode.toCharArray()) {
      if (!(Character.isLetter(c) || c == '$')) {
        throw new IllegalArgumentException("Invalid discount code");
      }
    }
    return discountCode.toUpperCase();
  }
Rafał Stasiak
Rafał Stasiak
3,763 Points

Hello Daniel,

May I ask about (!(Character.isLetter(c) || c == '$'))

!() means if code in parenthesis is false? Frankly speaking I firstly saw such use of "!"

Daniel Turato
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

So the ! is a NOT operator which means it will reverse the logic inside it's parenthesis. So !(true) = false and !(false) = true. So (!(Character.isLetter(c) || c == '$')) in natural language is:

If the character isn't a letter or isn't equal to $ then...

You can read more on the not operator here in the docs : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html (under unary operators)

Rafał Stasiak
Rafał Stasiak
3,763 Points

Secondly,

Please show on code example what you mean by; 1) When you call normalizeDiscountCode() in the the applyDiscountCode method, the parenthesis need to be against the method call.

Daniel Turato
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

So in your code you do this:

normalizeDiscountCode (discountCode);

where you should be doing this:

normalizeDiscountCode(discountCode);

As you can see, the parenthesis are right next to the method name and not with a space between it.

Rafał Stasiak
Rafał Stasiak
3,763 Points

Thanks Daniel, now It is clear to me :D