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

melissa zhang
melissa zhang
760 Points

In the normalizeDiscountCode verify that only letters or the $ character are used. If any other character is used, throw

Hi, I can't figure out how to code this question. Can anyone help and show me the answer? many thanks.

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){
    String symbol = "$";

    if(discountCode.isLetter(discountCode) ||discountCode !=symbol){
      throw new IllegalArgumentException("Invalid discount code");
    }
    return discountCode.toUpperCase();
  }

  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

Hi Melissa! You have to iterate through the discountCode string, and to be able to do that, you should first convert it to a char array, using the String class's toCharArray() method. Now you should use a for loop and do the check for every character. Be aware that the isLetter method is not defined in the String class, but it's a static method in the Character class, and accepts a char type argument (so you should call it on the class directly, like Character.isLetter(myCharacter)).

Also note: the first part of the condition check should actually check for falseyness (should be inverted with a !), since we're throwing the exception if our character is not a letter (or $ sign). Because of this, instead of || we should use &&, since we're checking if neither is true.

Hello, Sarantis! First of all: you can edit your post/comment and format your code with syntax highlighting like me and Melissa, check the "Markdown Cheatsheet" link below the textbox.

Almost every hint is there in the answer I've given to Melissa:

  • you want to do the checks for the characters in the discountCode string, not the string itself, so you should iterate it through (it should be converted to a char[] first with the .toCharArray() method of the String class); in fact, the isLetter() method accepts a character, it doesn't work for a string argument
  • moreover, isLetter() is a static method of the Character class, so you shouldn't call it on the character in question, but on the Character class itself (and give it your character as argument)

try to finish it yourself first, but...

SPOILER

here is the solution (for reference):

    private String normalizeDiscountCode(String discountCode) {
        for (char character : discountCode.toCharArray()) {            
            if (!Character.isLetter(character) && character != '$') {  // note that '$' is between single quotes, since it should be a char, not a string
                throw new IllegalArgumentException("Invalid discount code.");
            }
        }
        return discountCode.toUpperCase();