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

Carl South
PLUS
Carl South
Courses Plus Student 866 Points

Java Basics Challenge question for normalizeDiscountCode

I'm sure I am making this more difficult than it needs to be (story of my life) but I cannot figure out the question below.

In the normalizeDiscountCode verify that only letters or the $ character are used. If any other character is used, throw a IllegalArgumentException with the message Invalid discount code.

Below is my code where I tried to create a charcter array of the string discountCode then loop through checking to see if they were characters. Clearly its wrong. I don't even know how to check for '$' yet.

private String normalizeDiscountCode(String discountCode){ for (char c : discountCode.toCharArray()){
if (!discountCode.contains(c)){
throw new IllegalArgumentException("Invalid discount code"); } } discountCode = discountCode.toUpperCase(); return discountCode; }

Thanks for your help!! Carl

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

  private String normalizeDiscountCode(String discountCode){
    for (char c : discountCode.toCharArray()){  
    if (!discountCode.contains(c)){         
          throw new IllegalArgumentException("Invalid discount code"); 
      }
    }
    discountCode = discountCode.toUpperCase();
    return discountCode;
  }

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

  }
}
Benjamin Orimoloye
Benjamin Orimoloye
23,328 Points

Hi Carl, I think you are overthinking the solution. Discount codes are usually a set of alphabets like "DISCNTTODAT". What you are trying to do is change this string to uppercase in case the user enters "discnttoday". All you need to do in your function is :

private String normalizeDiscountCode(String discountCode) {
    return discountCode.toUpperCase();
  }

To check for match, you use the matches() function with a regular expressions pattern like this :

public void applyDiscountCode(String discountCode) {
    if (!discountCode.matches("[a-zA-Z$]+")) {
      throw new IllegalArgumentException("Invalid discount code.");
    }
    this.discountCode = normalizeDiscountCode(discountCode);
  }

Hope this helps

1 Answer

andren
andren
28,558 Points

Your answer is actually far closer to the solution than you seem to think. The condition of your if statement is incorrect, but if you figured out the right condition then your code would work. Looping though the discountCode as a char array is very much the intended way of solving this challenge.

I'll first give you some hints that might help you out, if you can't solve it after that then just post a comment and I'll provide the solution to you. Your code is close enough that I think you should give it another try though.

  1. There is a method on the Character class called isLetter which comes quite in handy when solving this challenge. It is called like this Character.isLetter(x) where x is the character you want to verify is a letter. The method returns true if the char is a letter, and false if it is not.

  2. You need to check two things within your if statement, so you should have two conditions. If you are checking if the char is not a letter and not $ then both of the conditions should be true for the if statement to run. So the && operator should be used.

  3. Checking if the letter is not a $ is far simpler than you probably think, try to think of the simplest imaginable way of checking if one thing is not equal to another, and then use that.

Carl South
Carl South
Courses Plus Student 866 Points

Your hints definitely helped and I got it to work! Thanks very much Andren!

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

}

andren
andren
28,558 Points

Good to hear. Your code was already pretty close to complete, and this is a challenge a lot of people struggle with. Usually even more so than you did, so I'm glad you managed to find the solution.

andren
andren
28,558 Points

Khaleel Yusuf: If you combine Carl's original code and the code he posted after my hints then you get this:

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

  public Order(String itemName, int priceInCents) {
    this.itemName = itemName;
    this.priceInCents = priceInCents;
  }

  private String normalizeDiscountCode(String discountCode){
    for (char c : discountCode.toCharArray()){  
      if (!Character.isLetter(c) && c != '$'){         
        throw new IllegalArgumentException("Invalid discount code"); 
      }
    }
    discountCode = discountCode.toUpperCase();
    return discountCode;
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

  public void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
  }
}

Which is the complete solution to the task.