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

What am I doing wrong?

I need to convert an entire string into uppercase and I can't figure out where the mistake in my code is or if I'm completely misinterpreting the instructions.

The instructions are:

Let's use your normalization skills to normalize a discount code for a Shopping Cart application. All codes should be upper cased, no matter what the user enters

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.

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 main(String [] args){
    String normalizeDiscountCode ;
      System.out.println(applyDiscountCode.toUpperCase());
    return this.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"
    }

  }
}

11 Answers

It's asking us to create a new private method called normalizeDiscountCode. So we can add the following into the Order class:

private String normalizeDiscountCode(String discountCode) {
    return discountCode.toUpperCase();
}

Then you can call this private method from the applyDiscountCode method.

You don't need a main method in the Order.java file.

Thank you, I had to change to this.discountCode = normalizeDiscountCode(discountCode); in the public method above to get it to run after implementing your code. This helped a lot!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi stormy! I was on my way to answer you, but I see codebyamir has helped you already. :thumbsup: Glad you got it working! :sparkles:

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Hi Thandolwethu Bhebhe try this it will work. (Uphendule umbuzo wami ukubana ubuya eZim kumbe SA )

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


  private String normalizeDiscountCode(String discountCode) {
    return discountCode.toUpperCase();
}
}
Thandolwethu Bhebhe
Thandolwethu Bhebhe
1,417 Points

Christopher Mlalazi... thanks man. Managed to check it out, I'm from SA man. How about you

Sunny Wood
Sunny Wood
1,368 Points

Thanks for the solution Christopher Mlalazi!

I still don't get it, why did we have to change this part this.discountCode = normalizeDiscountCode(discountCode):

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

It took me a while to figure out I haven't done this step, so I couldn't finish the challenge. I would like to understand why we needed to do this. Thanks a lot!

yahh this one works

said shah
said shah
5,454 Points
    public void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
  }

  private String normalizeDiscountCode(String letter){
    this.discountCode = letter;
    return this.discountCode.toUpperCase();
  }

Thats what your answer should look like, you have to call it from the applyDiscountCode which means you should set the method to pass the discount code string to the new method, and after that you should make a new method as stated in the instructions that accepts an argument which is then passed into this.discountCode (which is the global variable) and finally it should return the discountCode converted to all upper case letters.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi said! Please refrain from posting "spoiler" answers with no explanation on how or why the code works. Feel free to update your answer to include an explanation. Also, review the Markdown Cheatsheet at the bottom of the "Add an Answer" section for instructions on how to post code to the forums.

Sunny Wood
Sunny Wood
1,368 Points

After trying 3 different solutions, I still haven't managed to complete this challenge.. Please help! I tried the following:

1) Tried the solution that was posted here:

private String normalizeDiscountCode(String discountCode) {
    return discountCode.toUpperCase();

ERROR: Make sure you call your new private method from the applyDiscountCode method. Make sure you set the this.discountCode = normalizeDiscountCode(discountCode)

2) Added: this.discountCode = normalizeDiscountCode(discountCode)

private String normalizeDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(discountCode);
    return discountCode.toUpperCase();

ERROR: Make sure you call your new private method from the applyDiscountCode method. Make sure you set the this.discountCode = normalizeDiscountCode(discountCode)

3) Put return discountCode.toUpperCase(); out of the curly brackets

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

ERROR: There is a compiler error. Please click on preview to view your syntax errors!

./Order.java:30: error: illegal start of type return discountCode.toUpperCase(); ^ ./Order.java:30: error: ';' expected return discountCode.toUpperCase(); ^ ./Order.java:30: error: illegal start of type return discountCode.toUpperCase(); ^ ./Order.java:30: error: missing method body, or declare abstract return discountCode.toUpperCase(); ^ Note: JavaTester.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 4 errors

Sean M
Sean M
7,344 Points

Step 1:

  • Create new private method named normalizeDiscountCode.
  • Takes the discount code that is passed into the method
  • Returns the uppercase version

private String normalizeDiscountCode(String discountCode) { return discountCode.toUpperCase(); }

Step 2:

  • Call it from the current applyDiscountCode method
  • Set this.discountCode to the result.

In other words, use the already established applyDiscountCode method, and set the this.discountCode equal to the normalizeDiscountCode by using the discountCode.

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

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

private String normalizeDiscountCode(String discountCode) { return discountCode.toUpperCase();

} }

private String normalizeDiscountCode(String discountCode){ return discountCode.toUpperCase();

}

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

Answer posted.

Thandolwethu Bhebhe
Thandolwethu Bhebhe
1,417 Points

hi Chris, im having problems with mine here... its not working as it should, dont know what im doing wrong

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

} }