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

Jacob Ludwig
Jacob Ludwig
2,065 Points

Task 2 to create Illegal Argument Exception, but once created, states task 1 is no longer passing, but due to IAE thrown

Copied and pasted code from task 2 into task 1 and it states IAE thrown. Which was the goal of task 2...

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 letter : discountCode.toCharArray()) {
     if (!Character.isLetter(letter) || letter != '$') {
       throw new IllegalArgumentException("Invalid Discount Code");
     }
    }
    this.discountCode = discountCode.toUpperCase();
    return this.discountCode;
  }

  public String applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
    return this.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

Not sure why you would need to copy and paste code from task two into task one, but, if we follow the error that you're receiving, we can find a way forward.

"Bummer! java.lang.IllegalArgumentException: Invalid Discount Code (Look around Order.java line 26)"

so, we know that the code challenge is not accepting the exception being thrown on line 26. If you comment out that exception, then the code should run for task one.

private String normalizeDiscountCode(String discountCode) {
     for (char letter : discountCode.toCharArray()) {
     if (!Character.isLetter(letter) || letter != '$') {
       //commented out the below code to pass task one
       //throw new IllegalArgumentException("Invalid Discount Code");
     }
    }
    this.discountCode = discountCode.toUpperCase();
    return this.discountCode;
  }

Once you pass task one, you'll need to examine the logic used in the if statement of normalizeDiscountCode(). It is currently throwing an exception if the char is not a letter OR if the char is not a $ symbol. Currently, only one of these conditions needs to be met for the exception to be thrown. Is that really the behavior we want?

Good luck!