Guest User

Using a while loop to solve "Applying a Discount Code"

a guest
Jan 3rd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. private String normalizeDiscountCode(String discountCode) {
  2.     int index = 0; // create variable to hold the loop count / index
  3.     while(index < discountCode.length()) { // loop until the index is less than the discountCode length
  4.       char code = discountCode.charAt(index); // use the index to pull out the appropriate char
  5.       if(!Character.isLetter(code) || code != '$' ){
  6.           throw new IllegalArgumentException("Invalid discount");
  7.       }
  8.       index++; // increase the index so that during the next loop we pull out the next character, and to make sure the loop eventually ends.
  9.     }
  10.     discountCode = discountCode.toUpperCase();
  11.     return discountCode;
  12. }
Advertisement
Add Comment
Please, Sign In to add comment