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

szabolcs lenkovits
szabolcs lenkovits
1,059 Points

I got stuck on this exercise

Hello guys, I got stuck here I am not sure what di I do wrong it seems good to me but I guess I messed up somewhere, please help!!! Thanks in advance

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

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

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


  }

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

}
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 there,

You've done the difficult bit right - the conditional looks correct to me (I'll check in a minute).

However, the purpose of the method is to perform that conditional and either throw an exception or return the upper case version of the code received as a parameter. The other method sets the member variable; that was done in the first task.

So, after the for loop, return the upper case code; nothing else. These two lines:

this.discountCode = letter;
return this.discountCode.toUpperCase();

need amending. The first one is redundant. letter is used in the applyDiscountCode method to set the member variable, not in this method.

The second line is almost correct; but leave off this as the other method has the responsibility to set that and return the upper case version of letter.

I hope that helps,

Steve.

You've also used two names for the incoming code - the method signature receives a variable called letter; that's the whole code, not just a letter, but we'll go with it.

Then, when you convert this to a char array to loop over it, you've switched to calling it discountCode. Make those two the same thing and make the amendments, as above, and your code is fine.

szabolcs lenkovits
szabolcs lenkovits
1,059 Points

that's great thank you for your help!!!!!