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 trial

Java Java Objects Delivering the MVP Applying a Discount Code

Should work, but doesnt?

If anyone can have a look I would be grateful. The code runs perfect on Eclipse, but here I get a bummer message.

Order.java
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){

  for (char letter : discountCode.toCharArray()){
   if(! Character.isLetter(letter)){
     throw new IllegalArgumentException("A letter is required");
  }
  }

    return discountCode.toUpperCase();
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

  public void applyDiscountCode(String discountCode) {
   String result =normalizeDiscountCode(discountCode);
    this.discountCode = result;

  }
}
Example.java
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){

  for (char letter : discountCode.toCharArray()){
   if(! Character.isLetter(letter) && letter!='$'){
     throw new IllegalArgumentException("A letter is required");
  }
  }  
    return discountCode.toUpperCase();
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

  public void applyDiscountCode(String discountCode) {
  this.discountCode = normalizeDiscountCode(discountCode);


  }
}

1 Answer

andren
andren
28,558 Points

The code is right but you have placed it in the wrong file. There are two files in the challenge workspace Order.java and Example.java. The code is meant to be placed in the Order.java file but you have placed the finished code in Example.java.

If you copy all the code from the Example.java file and paste it over all the code in the Order.java file then you should be able to finish the challenge.