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

Carlos Mota
Carlos Mota
2,596 Points

My method in DiscountCode question for validating is not working

I'm on the problem where you need to normalize discountCode and my method seems to work fine outside of the treehouse test environment, but it does not seem to be working in it any help greatly appreciated.

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 = normalizeDiscountCode(discountCode); 
        } 
    catch(IllegalArgumentException iae){
      System.out.printf("%s", iae.getMessage());
    }
  }

  private String normalizeDiscountCode(String discountCode){
    if(!discountCode.contains("$") || discountCode.matches("[a-zA-Z]")){
      throw new IllegalArgumentException("Invalid discount code");
    }
    return discountCode = discountCode.toUpperCase();
  }
}
Carlos Mota
Carlos Mota
2,596 Points

This is a small example I made and it ran fine

class Playground {
    private String discountCodeValidation(String discountCode){
        boolean regex = discountCode.matches("^[a-zA-Z]+$");
        if(!discountCode.contains("$") && !regex){
            throw new IllegalArgumentException("Invalid discount code");
        }
        System.out.println(discountCode.toUpperCase() + ": passes validation test");
        return discountCode.toUpperCase();
    }
    public static void main(String[ ] args) {
        Playground playGround = new Playground();
        try{
            playGround.discountCodeValidation("h");
        } catch(IllegalArgumentException iae){
            System.out.printf("%s", iae.getMessage());
        }

    }
}

1 Answer

Carlos Mota
Carlos Mota
2,596 Points

Lol, solved my own problem but it would be cool to see other peoples implementation, Thanks! ?

class Playground {
    private String discountCodeValidation(String discountCode){
        boolean regex = discountCode.matches("^[a-zA-Z$]+$");
        if(!regex){
            throw new IllegalArgumentException("Invalid discount code");
        }
        System.out.println(discountCode.toUpperCase() + ": passes validation test");
        return discountCode.toUpperCase();
    }
    public static void main(String[ ] args) {
        Playground playGround = new Playground();
        try{
            playGround.discountCodeValidation("hello$");
        } catch(IllegalArgumentException iae){
            System.out.printf("%s", iae.getMessage());
        }

    }
}