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

Paul Vickers
Paul Vickers
3,380 Points

I'm stuck on applying discount code 2/2

here's my code:

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) { this.discountCode = normalizeDiscountCode(discountCode); }

private String normalizeDiscountCode(String discountCode) { for(char letter: discountCode.toCharArray()){ //Here we need to turn Code into char array if (! Character.isLetter(letter) && ! Character.isLetter('$')) { throw new IllegalArgumentException("Invalid discount code"); } } return discountCode.toUpperCase(); }

}

Hi Paul. Could you provide a link to the challenge please? Also, to format your code, just surround it with 3 backticks (```), 3 before, 3 after and write java after the top ones (on the same line, no space) if you want to see the Java syntax highlighted (colours). Thanks

3 Answers

Hi Paul. You're almost there! The problem is in the second part of your if expression:

if (! Character.isLetter(letter) && ! Character.isLetter('$'))

Right now, you're checking whether $ is not a letter (which is always true). However, we want to check that letter is not a letter and is not $. You got the first part right. For the second, you just need to write:

letter != '$'

Hope that helps :)

Paul Vickers
Paul Vickers
3,380 Points

https://teamtreehouse.com/library/java-objects-2/delivering-the-mvp/applying-a-discount-code

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) { 
this.discountCode = normalizeDiscountCode(discountCode);
 }

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

}

Thanks :)

Paul Vickers
Paul Vickers
3,380 Points

thank you, that helps a lot, at least I am someway to understanding it. Thank you Lauren.

You're welcome Paul :)