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

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

What's wrong with my code #2

After I submit code, I get error like this:

java.lang.IllegalArgumentException: Invalid discount code at Order.normalizeDiscountCode(Order.java:11) at Order.applyDiscountCode(Order.java:38) at JavaTester.run(JavaTester.java:89) at JavaTester.main(JavaTester.java:43)

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

  private String normalizeDiscountCode(String discountCode) {

    boolean isDollar = false;
    for(char letter : discountCode.toCharArray()){
      if(!discountCode.matches("[a-zA-Z]") && !(letter == '$')){
      throw new IllegalArgumentException("Invalid discount code");
      }
    }


    return discountCode.toUpperCase();
  }

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

  }
}

1 Answer

Simon Coates
Simon Coates
28,694 Points

I don't know if your regular expression is correct. However, testing the complete string for every character is monumentally repetitive. A standard solution uses the Character class's static isLetter method.

    private String normalizeDiscountCode(String discountCode) {

    //boolean isDollar = false;
    for(char letter : discountCode.toCharArray()){
      if(!Character.isLetter(letter) && letter != '$'){
        throw new IllegalArgumentException("Invalid discount code");
      }
    }
    return discountCode.toUpperCase();
  }

I don't know what i'm doing with regex/pattern matching, but it I took a guess. Maybe something like:

    private String normalizeDiscountCode(String discountCode) {

      if(!discountCode.matches("[a-zA-Z$]*")){
          throw new IllegalArgumentException("Invalid discount code");
      }
    return discountCode.toUpperCase();
  }
Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Well, I have already used the first option with isLetter to pass through the exercise, but I still don't know what I did wrong with defining regex the way I did.

Simon Coates
Simon Coates
28,694 Points

I think the matches method is trying to work out if the complete string matches the pattern. My regex [a-zA-Z$]* means that i'm testing for zero or more instances of the characters in the []. If that describes the complete string, i get a match.

Simon Coates
Simon Coates
28,694 Points

your regex works if you are only operating on the character, rather than the complete discount code. eg.

  private String normalizeDiscountCode(String discountCode) {

    boolean isDollar = false;
    for(char letter : discountCode.toCharArray()){
      if(!(""+letter).matches("[a-zA-Z]") && !(letter == '$')){
          throw new IllegalArgumentException("Invalid discount code");
      }
    }
    return discountCode.toUpperCase();
  }

However, this is equivalent to the Character.isLetter approach.