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 trialMUHAMMAD talha arshad
1,272 PointsIs there a bug in Verification?
It appears there is a bug in verification of first Exercise, Task 2.
My code is attached. I also verified through Jshell giving same arguments as appears in Error thrown "Bummer: Hmmm...I ran order.applyDiscount("h1!") and I expected it to throw an IllegalArgumentException, but it didn't" . In Jshell, I have verified he same code and it works. What seems to be the issue?
public class Order {
private String itemName;
private int priceInCents;
private String discountCode;
public Order(String itemName, int priceInCents) {
this.itemName = itemName;
this.priceInCents = priceInCents;
}
private String normalizeDiscountCode (String discountCode) {
discountCode = discountCode.toUpperCase();
for (char letter: discountCode.toCharArray()) {
if (!Character.isLetter(letter) && letter != '$') {
throw new IllegalArgumentException("Invalid Discount Code");
}
}
return discountCode;
}
public String getItemName() {
return itemName;
}
public int getPriceInCents() {
return priceInCents;
}
public String getDiscountCode() {
return discountCode;
}
public void applyDiscountCode(String discountCode) {
try {
this.discountCode = normalizeDiscountCode(discountCode);
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
}
}
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
Jennifer Nordell
Treehouse TeacherHi there, MUHAMMAD talha arshad ! I received your request for assistance and apologize for my late reply. I am somewhat surprised that others have not helped either and can only thank you for your patience. When I try your code, it fails as written. But I feel like it shouldn't. However, when I simply cut out ande paste your normalizeDiscountCode()
method and put it as the last bit of code in the file, it passes the challenge. In short, move your code to the bottom of the file and it should work.
Hope this helps!
MUHAMMAD talha arshad
1,272 PointsHey Jennifer, Thanks for the reply. As we say Better late than never )). However, I did try pasting method normalizeDiscountCode() at the end of the file, and infact tried many other such permutations as well, but it still gives the same error. I even tried starting all over again. I am actually curious now to find out why is this happening so? And why did it pass for you when you pasted it at the end of the code - seems illogical, don't you think? and why is it still not working for me? Jennifer Nordell
MUHAMMAD talha arshad
1,272 Points@Anyone! Please can you sort this out. I am unable to move ahead without this issue being resolved.
Jennifer Nordell
Treehouse TeacherHi there, MUHAMMAD talha arshad ! I'm sorry you haven't gotten this fixed yet, You said that you've tried it after moving the function to the end of the file. Can you copy the current code that you're trying here? See the Markdown Cheatsheet at the bottom of the "Add an Answer" box for tips on how to post your code here.
MUHAMMAD talha arshad
1,272 PointsHi, PFB the code as requested:
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) {
try {
this.discountCode = normalizeDiscountCode(discountCode);
} catch(IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
}
private String normalizeDiscountCode (String discountCode) {
discountCode = discountCode.toUpperCase();
for (char letter: discountCode.toCharArray()) {
if (!Character.isLetter(letter) && letter != '$') {
throw new IllegalArgumentException("Invalid Discount Code");
}
}
return discountCode;
}
}
Jennifer Nordell
Treehouse TeacherHi again, MUHAMMAD talha arshad ! I see the problem now, although this did pass for me before. My guess is that I only copied in your normalizeDiscountCode
method and did the applyDiscountCode
method myself. Because the problem lies in the latter method. You went way overboard here and did things the challenge did not ask you to do. The only thing the applyDiscountCode
method is supposed to do is call the normalizeDiscountCode
method. Your applyDiscountCode
should not have a try
or catch
or anything fancy Take a look:
// This is what I changed
public void applyDiscountCode(String discountCode) {
this.discountCode = normalizeDiscountCode(discountCode);
}
// This is your code
private String normalizeDiscountCode (String discountCode) {
discountCode = discountCode.toUpperCase();
for (char letter: discountCode.toCharArray()) {
if (!Character.isLetter(letter) && letter != '$') {
throw new IllegalArgumentException("Invalid Discount Code");
}
}
return discountCode;
}
Hope it gets sorted now!
MUHAMMAD talha arshad
1,272 Pointswa ALLAH u akbar.....yes it works :D. thanks! But shouldn't it throw an exception even if we are catching it?
MUHAMMAD talha arshad
1,272 PointsMUHAMMAD talha arshad
1,272 PointsDears @toWhomItMayConcern, How soon should I expect an answer