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

Herman Vicens
Herman Vicens
12,540 Points

"Looks like Task 1 is no longer passing" error.

the first discountCode is abc and gets changed to ABS which is correct.

The second discountCode I'm getting is a dEF which I don't know where is comming from because it is suposed to be $ale. Any ideas?

To make matters worst, after I execute task 2, the error I get is that Task 1 is no longer passing error and I have to start again with task 1 in order to return to 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;
  }

  private String normalizeDiscountCode(String discountCode) {
    char[] charArray = discountCode.toUpperCase().toCharArray();
    //char[] charArray = new this.discountCode.toCharArray();
    for ( int i=0 ; i<charArray.length ; i++) {

      if (charArray[i] >= 'A' && charArray[i] <= 'Z' && charArray[i] != '$' ) {
          System.out.println("Condition is true...continue  Char["+i+"] = "+charArray[i]+" DiscountCode = "+discountCode);
          continue; 
      } else {
           System.out.println("Condition is false...throw error   Char["+i+"] = "+charArray[i]+" DiscountCode = "+discountCode);
           throw new IllegalArgumentException("invalid discount code.");
        }
    }

    this.discountCode = discountCode;
    System.out.println("this.discountCode "+this.discountCode+" DiscountCode = "+discountCode); 
    return this.discountCode;

  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    System.out.println("LLego a getdiscountcode  "+discountCode);
    return discountCode;
  }

  public void applyDiscountCode(String discountCode) {
     this.discountCode = normalizeDiscountCode(discountCode);
     System.out.println("En Apply order  this.  "+this.discountCode+"  no 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
    System.out.println("In Example with first apply and get ");

    order.applyDiscountCode("$ale");
    System.out.println("In Example with second apply ");
    order.getDiscountCode(); // $ALE
    System.out.println("In Example with second get ");


    try {
      // This will throw an exception because it contains numbers
      System.out.println("LLego a primer try ");
      order.applyDiscountCode("ABC123");

    } catch (IllegalArgumentException iae) {
      System.out.println("LLego a segundo catch ");
      System.out.println(iae.getMessage());  // Prints "Invalid discount code"
    }
    try {
      // This will throw as well, because it contains a symbol.
      System.out.println("LLego al segundo try ");
      order.applyDiscountCode("w@w");
    }catch (IllegalArgumentException iae) {
      System.out.println("LLego a segundo catch ");
      System.out.println(iae.getMessage());  // Prints "Invalid discount code"
    }

  }
}

2 Answers

Hi Herman,

Have a read through this as that might help.

Plus, remove all the println output lines. The challenge isn't expecting all that output so it may dislike those unrequired outputs.

Have a look through this too.

Let me know how you get on.

Steve.

Herman Vicens
Herman Vicens
12,540 Points

Thanks again Steve! I found that I wasn't giving the THIS. variable the value of the transforemed String. Also changed the If from and && != '$' to a || == '$' and it worked.

Thanks.

Hi Herman,

What did your final code look like? Let's just check that's all nice and tight.

Steve.