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 trialDavid Player
1,183 PointsI tried to add validation for normlizeDiscountCode method, but it kept said that it couldn't pass the test
I wasn't sure what to do about this task since I added the restriction for not allowing non-letter and any symbols.
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 void applyDiscountCode(String discountCode) {
this.discountCode = normalizeDiscountCode(discountCode);
}
private String normalizeDiscountCode(String discountCode){
this.discountCode = discountCode;
for (char letter: discountCode.toCharArray()){
if( !Character.isLetter(letter) || letter != '$'){
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
}
1 Answer
Steve Hunter
57,712 PointsHi David,
You're pretty much there.
First, don't set this.discountCode
inside normaliseDiscountCode
you're already doing that in applyDiscountCode
. And you might want to try comparing the characters with &&
and not ||
.
Give that a whirl and let me know how you get on.
Steve.
David Player
1,183 PointsDavid Player
1,183 PointsIt's working great. Thank you!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsCool!