Random Number Generator:
public String randomNumberGenerator(int length) throws Exception{
Random random = new Random();
char[] digits = new char[length];
digits[0] = (char) (random.nextInt(9) + '1');
for (int i = 1; i < length; i++) {
digits[i] = (char) (random.nextInt(10) + '0');
}
long st = Long.parseLong(new String(digits));
String uniqueID = String.valueOf(st);
return uniqueID;
}
Random String Generator:
public String randomStringGenerator(int length) throws Exception {
// Chose a Character random from this String
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvxyz";
// create StringBuffer size of AlphaNumericString
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (AlphaNumericString.length() * Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
Random Alpha Numeric Generator:
public String randomAlphaNumericGenerator(int length) throws Exception {
// chose a Character random from this String
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "abcdefghijklmnopqrstuvxyz";
// create StringBuffer size of AlphaNumericString
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int index = (int) (AlphaNumericString.length() * Math.random());
// add Character one by one in end of sb
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
Random Date Generator:
Enter Start and End Year.
public static int randomInBetween(int start, int end) {
return start + (int) Math.round(Math.random() * (end - start));
}
public String randomDateGenerator(int startYear, int endYear) throws Exception {
int day = createRandomIntBetween(1, 28);
int month = createRandomIntBetween(1, 12);
int year = createRandomIntBetween(startYear, endYear);
return LocalDate.of(year, month, day).toString();
}
Calculate The Discount Price:
public String calculateDiscount(String Percentage, String Amount) {
Amount= Amount.replace(",", ""); BigDecimal price = new BigDecimal(Amount);
BigDecimal discount = new BigDecimal(Percentage);
BigDecimal ONE_HUNDRED = new BigDecimal(100);
BigDecimal discountValue = price.multiply(discount).divide(ONE_HUNDRED);
discountValue = price.subtract(discountValue);
discountValue = discountValue.setScale(2, BigDecimal.ROUND_HALF_UP);
double value = Double.parseDouble(discountValue.toString());
String afterDiscount = NumberFormat.getCurrencyInstance().format(value);
// To remove prefix $ symbol
afterDiscount = afterDiscount.substring(1);
System.out.println("Amount after discount: " + afterDiscount);
return afterDiscount;
}
No comments:
Post a Comment