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

mark steinberg
mark steinberg
1,144 Points

I'm stuck -I can't see how to progress this challenge - or if my approach is correct...

The first step here is to check the incoming discountCode as validation requires. The only approach I can think of is to use a for loop to pass through each of the characters in the string and check they are correct - and use a boolean to pick up if it is valid or not. Then the idea is to throw the exception is the boolean is false. But! So many compiler errors. And I'm not sure if my if logic works. And is this the right approach anyway! thanks for help, mark

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

  private String normalizeDiscountCode(String discountCode) {
    this.discountCode = discountCode.toUpperCase();
    int discountCodeLength = this.discountCode.length();
    boolean validCode = false;
    for(int i=0;i<discountCodeLength;i++) {
      char c = this.discountCode.charAt(i);
      if ((c < 'A' || c > 'Z') && (c != '$')) {
         validCode = false;
      } else {
        validCode = true;
      }

    }
  }
  }

  public void applyDiscountCode(String discountCode) {
    this.discountCode = normalizeDiscountCode(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"
    }

  }
}

2 Answers

Boban Talevski
Boban Talevski
24,793 Points

Hi mark,

You should not be assigning the passed parameter discountCode to the member variable/field discountCode right away inside normalizeDiscountCode. That's the job of the applyDiscountCode method. The only thing you need to do inside normalizeDiscountCode is to actually validate that parameter discountCode, and if it's valid, return its uppercase version. If it isn't valid, throw an exception

private String normalizeDiscountCode(String discountCode) {
    this.discountCode = discountCode.toUpperCase(); // you assign it here which defeats the purpose of applyDiscountCode 
    int discountCodeLength = this.discountCode.length(); // and you are using that member variable throughout the method which isn't causing compiler errors, but is not what should be done and is partly why the task is failing
    boolean validCode = false;
    for(int i=0;i<discountCodeLength;i++) {
      char c = this.discountCode.charAt(i);
      if ((c < 'A' || c > 'Z') && (c != '$')) {
         validCode = false;
      } else {
        validCode = true;
      }

    } // this is an excess curly brace, should be deleted
    // you are missing a return statement at this point, the method should return a String
  }

Your validation looks ok though, you got that part right. But instead of using a boolean variable for validation purposes, you should do it like this: if the validation doesn't pass, you should throw an exception, otherwise, move on and simply return the upper case version of the String discountCode which was passed as a parameter.

  private String normalizeDiscountCode(String discountCode) {
    discountCode = discountCode.toUpperCase(); // making the passed parameter upper case, not the field
    for(int i = 0; i < discountCode.length(); i++) {
      char c = discountCode.charAt(i);
      if ((c < 'A' || c > 'Z') && (c != '$')) {
         throw new IllegalArgumentException("Invalid discount code");
      } 
    }
    return discountCode;
  }

Hope it makes more sense.

mark steinberg
mark steinberg
1,144 Points

Many thanks indeed Boban. Very helpful.

(sorry about the delay in replying to you)

mark

Daniel Marin
Daniel Marin
8,021 Points

Hi Mark, I'm doing a regular expression to match a-z lower and upper A-Z or the $ sign if these are not found we throw in IllegalArgumentException as mentioned then we return to uppercase. So this is what I wrote for the normalizeDiscountCode method

    private String normalizeDiscountCode(String discountCode) {
        if(!discountCode.matches("[a-zA-Z|$]+")) {
            throw new IllegalArgumentException();
        }

        return discountCode.toUpperCase();
    }