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

Gonzalo Torres del Fierro
PLUS
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

checking Iletter or '$' on discount code

please some help, iยดm very stuck on this, and getting mad.. please i do not want more hint just the code, bacuse i really try hard...

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

  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 discountCode){

   for (char letter : discountCode.toCharArray()){
     check1 = Character.isLetter(letter);
     check2 = letter =='$';
     if( !check1 || ! check2)
     {
       throw new IllegalArgumentException(letter + "Invalida discount Code");
     }
   }     

 return discountCode.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"
    }

  }
}
Craig Dennis
Craig Dennis
Treehouse Teacher

I think you are missing the type of check1 and check2....

andren
andren
28,558 Points

Craig Dennis: It might look like so at first glance but he actually has check1 and check2 declared as field variables at the top of the class, with types properly declared. It's certainly not the cleanest code I've ever seen but those variables are not at fault for his code not working.

Craig Dennis
Craig Dennis
Treehouse Teacher

Oh whoops I missed those up there. They do belong in the scope of the for loop Gonzalo Torres del Fierro .

1 Answer

andren
andren
28,558 Points

Answer:

private String normalizeDiscountCode(String discountCode){
   for (char letter : discountCode.toCharArray()){
     check1 = Character.isLetter(letter);
     check2 = letter =='$';
     if( !check1 && ! check2) // Both of the conditions should be true for an exception to be raised
     {
       throw new IllegalArgumentException(letter + "Invalida discount Code");
     }
   }     
 return discountCode.toUpperCase();
 }

You were very, very close. The only error was the logic in your if statement. By using the OR (||) operator your if statement executes if either of the conditions you specify is true, and since letters (which are valid) are not equal to the $ symbol, and the $ symbol is not a letter one of the conditions will always be true even for valid characters. By changing it to the && operator which will only run if both conditions are true you get the desired result.

This is an extremely common mistake that quite a few other students have made (multiple students just today in fact) so you don't need to feel bad about having a tough time with it.