Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Delivering the MVP Applying a Discount Code

Urooba Urooba
Urooba Urooba
3,565 Points

unable to understand

i think the code is ok but it keeps on returning the error that the letters are not being uppercase

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) {
    try{
    this.discountCode = discountCode;

    this.discountCode=normalizeDiscountCode(discountCode);
    }catch(IllegalArgumentException iae)
    {
      System.out.println(iae.getMessage());
    }
  }
  private String normalizeDiscountCode(String result)
  {
    for (char letter: result.toCharArray())
    {
      if(!Character.isLetter(letter)&&letter!='$');{
      throw new IllegalArgumentException("Invalid discount code");
      }
    }
    result=result.toUpperCase();
    return result;
  }
}
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

andren
andren
28,538 Points

That error message is indeed a bit strange, I can't quite see why that is the error message produced, but I can see the issues with your code.

There are two issues:

  1. You use a semicolon ; on the line where you declare your if statement, that is incorrect and will cause the if statement to be terminated prematurely. Meaning that the code that follows it will be executed always, regardless of what the conditions for the if statement is.

  2. The challenge asks you to throw an exception but does not ask you to catch said exception yourself, and in fact doing so will break the challenge. The code checker that verifies your code expects it to throw it an exception when given certain input, and uses that to validate that the code is working correctly. If you catch the exception yourself then it won't reach the code checker that runs the code.

So if you remove the wrongly placed semicolon ; and remove the try/catch block from your code then your code will work.