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 trialWilliam Mariadason
1,393 PointsWhy isn't task one passing?
I didn't change anything from task one to two, but task one isn't passing. Did I set up the exception wrong?
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 c : discountCode.toCharArray()){
if(!Character.isLetter(c) || c != '$'){
throw new IllegalArgumentException("Invalid discount code");
}
}
return discountCode.toUpperCase();
}
}
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
andren
28,558 PointsThere is an error in your solution, but it is very, very close to correct. I would recommend taking a close look at your if statement, remember that the exception should be thrown if the char is not a letter and it is not the $ symbol. Your if statement does something close yet somewhat different that changes it's behavior quite a bit.
If you are completely stuck and still can't find the error then I have posted the solution here.
William Mariadason
1,393 PointsWilliam Mariadason
1,393 PointsI figured that it should be && instead of || out of your wording, but why is this. Both make logical sence in my mind.
EDIT: I thought about it a bit more and figured it out. Thank you.
andren
28,558 Pointsandren
28,558 PointsYeah I intentionally worded my comment to be quite a hint, and don't worry too much about struggling a bit with figuring out when || and && is appropriate that's pretty normal. When I first started out it took some serious mental gymnastic to figure out what each of them would actually end up doing practically speaking. Once you have used them a while it starts to become a bit more of a second nature thing. But it is definitively a bit of a brain twister in the beginning.
Though I will say that one thing to keep in mind is that they are pretty much "mutually exclusive" in the sense that you will very rarely have an if statement where you could use either one and get the same behavior, they result in very different types of checks being performed so it really is quite important to think it through whenever you use one of them.
It's also worth remembering that in Java || and && have a "feature" called short-circuiting, basically once the && operator encounters a False condition Java will ignore the rest of the conditions and skip the if statement immediately, the opposite is true for || with True, this means that often times only one of the conditions you specify will actually get tested at all when using || and &&. Keeping that in mind can make it a bit easier to remember how the operators will act, at least it did for me.