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

Hyun Yoo
Hyun Yoo
975 Points

Why is my code not working?

Thank you.

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;
  }
  private String normalizeDiscountcode() {
    toUpperCase(String discountCode) = this.discountCode;
    this.discountCode = discountCode;
    return discountCode;
  }
}
Example.java
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

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Hyun...

Looks like there are many revisions in your code needs to be done:

public void applyDiscountCode(String discountCode) {
    this.discountCode = discountCode;
  }
  private String normalizeDiscountcode() {
    toUpperCase(String discountCode) = this.discountCode;
    this.discountCode = discountCode;
    return discountCode;
  }

First please take some time to review the String toUpperCase() method in the java doc here. As you can see the usage of the method is supposed to be: discountCode.toUpperCase()

Next is on the way you call an object. There are many revision to be done also. First you need to call the normalizeDiscountCode from the applyDiscountCode method by using this.discountCode = normalizeDiscountCode(discountCode) this is the correct way to call a method and pass an argument a String called discountCode.

Then in the receiving end which is the normalizeDiscountCode method you need to declare that it must receive a String argument called dsicountCode. This is how I code to pass the 1st task of the challenge:

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

  private String normalizeDiscountCode(String discountCode) {

    return discountCode.toUpperCase();
  }

There are not too many lines of code as you can see, thus please try to grasp the logic behind the code by reviewing my explanation above and read the java documentation about String. I hope this can help a little