Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Delivering the MVP Applying a Discount Code

Please help with my code.

Applying discount code, I don't really understand what is wrong with my code.

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;
  }

  public String getItemName() {
    return itemName;
  }

  public int getPriceInCents() {
    return priceInCents;
  }

  public String getDiscountCode() {
    return discountCode;
  }

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

  public String normalizeDiscountCode(){
    String discountCode = applyDiscountCode(String discountCode).toUpperCase();
    this.discountCode = discountCode;
    return discountCode;
  }
}

1 Answer

dodders
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
dodders
Python Development Techdegree Graduate 38,431 Points

You need to edit the normalizeDiscountCode method to accept a string (the code to be normalized).

You should call the normalizeDiscountCode from within the applyDiscountCode method.

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) {
    // send discount code to normalizeDiscountCode and assign the result to this.discountCode
    this.discountCode = normalizeDiscountCode(discountCode);
  }

  private String normalizeDiscountCode(String discountCode){
    // receive a discount code and pass back the UPPERCASE version
    return discountCode.toUpperCase();
  }
}