Java Java Objects Delivering the MVP Applying a Discount Code

Troy Childs
PRO
Troy Childs
Pro Student 1,781 Points

Stuck at this point on the Java project

trying to understand what im doing wrong here....

For this first task, create a new private method named normalizeDiscountCode. It should take the discount code that is passed into the method and return the uppercase version. Call it from the current applyDiscountCode method and set this.discountCode to the result.

this is what i came up with...

private String normalizeDiscountCode(String discountCode){
    Order order = new Order("bascketball",100);
    order.applyDiscountCode("hooper");
    order.getDiscountCode();
    discountCode.toUpperCase();
    return this.discountCode;    
  }

What im i going wrong here?

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 void applyDiscountCode(String discountCode) {
    this.discountCode = discountCode; // call normalizeDiscountCode here
  }
  private String normalizeDiscountCode(String discountCode){
    Order order = new Order("bascketball",100);
    order.applyDiscountCode("hooper");
    order.getDiscountCode();
    discountCode.toUpperCase(); // return this!
    return this.discountCode;    
  }
}

3 Answers

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 55,710 Points

Hi Troy,

The challenge mentions nothing about an Order. There are two tasks. One, is to call the normalizeDiscountCode method from within applyDiscountCode; you've done that. Second, the new normalizeDiscountCode method should return the uppercase version of the string it receives.

The applyDiscountCode method has the responsibility of managing the member variable this.discountCode. Don't amend that in normalizeDiscountCode - that method just receives a string and returns it, in uppercase form. That's it's single responsibility (at this stage!).

So, you've got applyDiscountCode done - excellent. Now, inside normaliseDiscountCode you want one line of code which just returns the uppercase version of the string it received.

Steve.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 55,710 Points

Hi Troy,

You have two tasks here.

First, in applyDiscountCode you want to set this.discountCode to the returned value coming back from normalizeDiscountCode.

Create the normalizeDiscountCode method - you've done that fine. Inside it, you have the second task. Just return the upper case version of the argument passed in. You've called that discountCode so, just convert that to upper case and return it from the method.

Make sense?

Let me know how you get on.

Steve.

Steve Hunter
Steve Hunter
Treehouse Moderator 55,710 Points

I added a couple of comments in your code to assist.

Troy Childs
PRO
Troy Childs
Pro Student 1,781 Points

Hi Steve, i have still been working on this and it not working. this is would i'm doing now.

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

  private String normalizeDiscountCode(String discountCode){
    Order order = new Order("bascketball",100);
    order.applyDiscountCode("hooper");
    order.getDiscountCode();
    return this.discountCode.toUpperCase();  
  }