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

Thandolwethu Bhebhe
Thandolwethu Bhebhe
1,417 Points

why is my discountCode seemingly not being changed to upper case??

Hi guys Im having problems with this exercise here... its not working as it should, I think, dont know what im doing wrong (LAST TWO METHODS)

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

///////////////////////// this is how i picture it...

public void applyDiscountCode(String discountCode) { // 1 i call this method and pass in a lowerCase z normalizeDiscountCode(discountCode); //2 the normalize method underneath is called, still using z and a parameter this.discountCode = discountCode; }

private String normalizeDiscountCode(String discountCode){//3 this method now see its been called using 'z' ok and just . //carries on the next step

 discountCode.toUpperCase();//4 turn the state of the value of discountCode to upperCase in this case i imagine z        . .                                                      //becomes Z
return discountCode; //5 now the capital Z get returned... I tried to turn my return to an upper case like this: return ..  . . . . .                                        //discountCode.toUpperCase(); but that doest work either


                             isnt this the correct login behind this, am i thinking the wrong way about it??

I just want to know now so that i can maybe change my way of thinking to try and makes this easier for myself if its wrong

} }

this is the error i get: Bummer! Hmmm, I called order.applyDiscountCode("abc") and I expected order.getDiscountCode to equal "ABC", but it was "abc"

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) {
    normalizeDiscountCode(discountCode);
    this.discountCode = discountCode;
  }

  private String normalizeDiscountCode(String discountCode){

    return discountCode.toUpperCase();
  }
}
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"
    }

  }
}

2 Answers

andren
andren
28,558 Points

The problem is that the normalizeDiscountCode method only returns an uppercase version of the discountCode, it does not modify the discountCode parameter. And since it only returns a new value you have to store that value back into the discountCode variable if you actually want it to change. Currently you just call the function and then do nothing with the value that is returned. And then set the field variable discountCode to be equal to the discountCode parameter that was passed in. If you changed your applyDiscountCode method to this:

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

Where you store the result of the normalizeDiscountCode method in the field variable discountCode instead then your code will work.

Thandolwethu Bhebhe
Thandolwethu Bhebhe
1,417 Points

makes a whole lot of sense Andren. I never grasped the reason behind returning values, i was always wondering where they go when we return them. This now taught me that they go nowhere... theyll just be floating around in the class until you use them for something, right?

andren
andren
28,558 Points

Correct, if you use a method that returns a value but do not assign that value to something (or use it somewhere that expects a value of the same type) then the returned value is essentially just thrown away.