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 trialDamjan Vlaic
19,244 Points---JAVA---
can someone help me with this if statement in the last method pls :)
In the normalizeDiscountCode verify that only letters or the $ character are used. If any other character is used, throw a IllegalArgumentException with the message Invalid 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 void applyDiscountCode(String discountCode) {
this.discountCode = normalizeDiscountCode(discountCode);
}
private String normalizeDiscountCode(String discountCode) {
return discountCode.toUpperCase();
if (! discountCode.isLetter() || ! '$') {
throw new IllegalArgumentException("Invalid discount code.");
}
}
}
2 Answers
Steve Hunter
57,712 PointsHi there,
For starters, you have return
ed out of the method before applying the condition; make the return
statement the last line of the method.
Next, you need something to compare with !
- you need to break the discountCode
down into an array then iterate over that in a for
loop. At each loop, you test each character to see if it is/isn't a letter and is/isn't a dollar sign. Depending on that, throw the exception or not. Note that you have to stipulate both tests in full - one, you have correctly established, is using the isLetter()
method - however, this can't be called on a String
- it is a class method of the Character
class.
So, some steps:
- First - convert the incoming
discountCode
into an array of characters. - Second - loop over that array, one character at a time.
- Third - test each character to see if it is a letter (
isLetter()
will help here); test it against for being a dollar sign - watch your logic!
Depending on the logic used, throw the exception or return the uppercase discountCode
.
Let me know how you get on.
Steve.
P.S. You can also use a regular expression to solve this in fewer steps.
Steve Hunter
57,712 PointsTwo issues there that I can see, and I'm on my phone about to go to sleep.
First, as in my earlier post, the isLetter
method is a class method of the Character
class. Read up on how to use it.
Second, your logic in your test is wrong. You want see if the letter is not a letter AND is not a dollar sign. The negation makes an OR unworkable. Think that through.
Steve.
Damjan Vlaic
19,244 Pointsi have solved it. I wrote ! Character.isLetter(letter) && letter != '$' tnx for helping me steve ;)
Steve Hunter
57,712 PointsGood work!
Damjan Vlaic
19,244 PointsDamjan Vlaic
19,244 Pointseverything you told me helped me a lot but still trying to write that if statement correctly, can you help me with it pls :)
here's my code (method)