Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.campusdual;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Utils {
private Utils(){}
public static final Random random = new Random();
public static final DecimalFormat dF = new DecimalFormat("#.##");
/**
* Method to initialize input data.
* @return input Buffered string data.
*/
public static String init() {
String buffer = "";
InputStreamReader stream = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(stream);
try {
buffer = reader.readLine();
} catch (Exception e) {
System.out.append("Invalid data.");
}
return buffer;
}
/**
* Method to get an integer from input data.
* @return integer entered by the user.
*/
public static int integer() {
return Utils.integer(null);
}
/**
* Method to get an integer from input data with a custom message.
* @param message custom message for the user.
* @return integer entered by the user.
*/
public static int integer(String message) {
if (message != null) {
System.out.print(message);
}
try{
return Integer.parseInt(Utils.init());
} catch (NumberFormatException e){
System.out.println("The value is not an integer.");
return integer(message);
}
}
/**
* Method to get a real number from input data.
* @return real number entered by the user.
*/
public static double real() {
return Utils.real(null);
}
/**
* Method to get a real number from input data with a custom message.
* @param message custom message for the user.
* @return real number entered by the user.
*/
public static double real(String message) {
if (message != null) {
System.out.print(message);
}
try{
return Double.parseDouble(Utils.init());
} catch (NumberFormatException e){
System.out.println("Value is not a real number.");
return real(message);
}
}
/**
* Method to get a text string from input data.
* @return text string entered by the user.
*/
public static String string() {
return Utils.string(null);
}
/**
* Method to get a text string from input data with a custom message.
* @param message custom message for the user.
* @return text string entered by the user.
*/
public static String string(String message) {
if (message != null) {
System.out.print(message);
}
return Utils.init();
}
/**
* Method to get a character from input data.
* @return character entered by the user.
*/
public static char character() {
return Utils.character(null);
}
/**
* Method to get a character from input data with a custom message.
* @param message custom message for the user.
* @return character entered by the user.
*/
public static char character(String message) {
if (message != null) {
System.out.print(message);
}
String valor = Utils.init();
return valor.charAt(0);
}
/**
* Formats the {@link Double} value passed as a parameter with the pattern #.##
*
* @param d
* the value to be transformed
* @return The formatted double
*/
public static String formatLocalNumber(double d) {
return dF.format(d);
}
/**
* Retrieves a random number selected from the values passed as parameters.
*
* @param min
* The lower limit of the range
* @param max
* The upper limit of the range
* @return A random number within the selected range.
*/
public static int getRandomNumberInRange(int min, int max) {
max++;
return random.nextInt(max - min) + min;
}
/**
* Displays a list with indices and allows it to show a waiting message.
*
* @param list
* The list to be displayed
* @param wait
* true to wait after displaying the list,
* false otherwise.
*/
public static void showFromList(List list, boolean wait) {
Utils.showFromList(list, wait, null);
}
/**
* Displays a list with indices, allows showing a waiting message, and excludes
* the elements from the first list that are present in the second list.
*
* @param list
* The list to be displayed
* @param wait
* true to wait after displaying the list,
* false otherwise.
* @param excludeElements
* Excludes the elements that exist in this list from the list
* passed as a parameter.
*/
public static void showFromList(List list, boolean wait, List excludeElements) {
StringBuilder builder = new StringBuilder();
List