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 trialLucian Vrabiescu
735 PointsWhy doesn't it work
I've commented some code becouse I'm not sure if it's right, either way it doesn't work and I dont know why
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) {
// try {
discountCode = normalizeDiscountCode(discountCode);
// } catch (IllegalArgumentException iae) {
//System.out.printf("%s. Please try again. %n",
// iae.getMessage());
//}
this.discountCode = discountCode;
}
private String normalizeDiscountCode(String discountCode){
discountCode = discountCode.toUpperCase();
boolean validCode = true;
for (char c : dicountCode.toCharArray()) {
if (! c.isLetter() && c != '$') {
throw new IllegalArgumentException("Invalid discount code");
}
}
if (validCode) {
return discountCode;
}
}
}
2 Answers
Steve Hunter
57,712 PointsHi Lucian,
You've pretty much got that right. However, you want to throw the error if c
is not a letter AND is not a '$'.
At the moment, using ||
, if you pass in a '$' as c
, the error will get thrown because !Character.isLetter('$')
evaluates to true
and you only need one side of the ||
to evaluate to true
for the error to be thrown.
Check your comparison logic.
Steve.
Steve Hunter
57,712 PointsHi there,
Your applyDiscountCode
method looks OK but you can shorten it a little:
public void applyDiscountCode(String discountCode) {
this.discountCode = normalizeDiscountCode(discountCode);
}
Now, in your normalizeDiscountCode
method, you have validCode
which never changes, and is unnecessary. You've done most of the code that's required. Start with the for
loop then, once that's completed, return the uppercase code. You'll need to spell discountCode
correctly in there too .
Also, isLetter()
is a class method of Character
; you can't call it on c
. Try Character.isLetter(c)
instead.
Remove the if
test; it isn't needed. That should work fine.
Let me know how you get on.
Steve.
Lucian Vrabiescu
735 PointsHi Steve!
I followed your tips and it still doesn't work, what's the problem?
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){
for (char c : discountCode.toCharArray()) {
if (! Character.isLetter(c) || c != '$') {
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
}
Thanks for your help!
Luci.