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

normalizeDiscountCode $@ving$ testcase working

I'm trying to run the code and it keeps saying that IllegalArgumentException should be called when the input: $@ving$ is put in. Why and how is $@ving$ even working? Isn't my for loop iterating through all the letters in the String. Then how is the '@' being allowed through? Is the @ symbol being considered as a letter? Thanks to anyone that answers this!

Order.java
public class Order {
  private String itemName;
  private int priceInCents;
  private String discountCode;

  private String normalizeDiscountCode (String dCode){
    discountCode = dCode.toUpperCase();
    char[] cArr = discountCode.toCharArray();
    for(char c : cArr) {
     if(!Character.isLetter(c) && discountCode.indexOf('$') == -1) { 
       throw new IllegalArgumentException("Invalid discount code");
     }
    }
    return 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;
    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"
    }

  }
}

1 Answer

Gergely Horvath
PLUS
Gergely Horvath
Courses Plus Student 8,207 Points

There are two problems with your code: 1) in your normalizeDiscountCode method you overwrite the content of the instance variable discountCode

private String normalizeDiscountCode (String dCode){
  discountCode = dCode.toUpperCase(); // <-- this line

Declare your variable in the scope of normalizeDiscountCode method:

private String normalizeDiscountCode (String dCode){
  String discountCode = dCode.toUpperCase(); // <-- this line

2) while looping through characters of discountCode you use indexOf method that is a loop also. So there is an unnecessary loop in your loop. Use simple checking of equality here:

c == '$'

Then you have to think of the truth table, and find out which bitwise operator can be helpful here.

One solution would be using your code:

private static String normalizeDiscountCode2 (String dCode) {
  String discountCode = dCode.toUpperCase();
  char[] cArr = discountCode.toCharArray();
  for(char c : cArr) {
    if(!Character.isLetter(c) ^ c == '$') { 
      throw new IllegalArgumentException("Invalid discount code");
    }
  }
  return discountCode;    
}

My solution:
```Java
private String normalizeDiscountCode(String discountCode) {
  boolean b = true;
  int i = 0;

  while (b && i < discountCode.length()) {
    char c = discountCode.charAt(i);
    b &= Character.isLetter(discountCode.charAt(i)) ^ c == '$';
    i++;
   }

  if(!b) throw new IllegalArgumentException("Invalid discount code");

  return discountCode.toUpperCase();
}

Maybe it's not the most elegant one, but it works, and it doesn't break a for loop.

Gergely Horvath
Gergely Horvath
Courses Plus Student 8,207 Points

Sorry, i forgot to close a code block before the "My solution" line. And i can't edit my answer. Why isn't there at least a preview function? Also i could change "discountCode.charAt(i)" to "c" in my while loop.

Gergely Horvath
Gergely Horvath
Courses Plus Student 8,207 Points

Oh there is a preview function, i just failed to see the button in the bottom right corner. :(