CouponUtil { public static long parseDuration(String text) { long result = 0; int numIdx = 0; for (int i = 0; i < text.length(); i++) { char at = text.charAt(i); if (at == 'd' || at == 'w' || at == 'm' || at == 'h') { String ns = text.substring(numIdx, i); numIdx = i + 1; if (ns.isEmpty()) { continue; } int n = Integer.parseInt(ns); switch (at) { case 'd': result += TimeUnit.DAYS.toMillis(n); break; case 'w': result += TimeUnit.DAYS.toMillis(n * 7); break; case 'h': result += TimeUnit.HOURS.toMillis(n); break; case 'm': result += TimeUnit.MINUTES.toMillis(n); break; } } else if (!Character.isDigit(at)) { throw new IllegalArgumentException("Character " + at + " in position " + (i + 1) + " not valid"); } } return result; }  static long parseDuration(String text); static String generateCode(); static Coupon parseArguments(String[] args);  }
@Test(expected = IllegalArgumentException.class) public void parseDurationInvalidFormat() { Assert.assertEquals(TimeUnit.DAYS.toMillis(7), CouponUtil.parseDuration("xyz")); }
@Test public void parseDurationEmptyDuration() { Assert.assertEquals(0, CouponUtil.parseDuration("")); }
@Test public void parseDurationEmptyDuration2() { Assert.assertEquals(0, CouponUtil.parseDuration("dwhm")); }
@Test public void parseDurationPartialDuration() { Assert.assertEquals(TimeUnit.DAYS.toMillis(8), CouponUtil.parseDuration("w7d24hm")); }