package cse308.swift.tool; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.sql.Timestamp; /** * SwiftToolBox is a utility that aids in mundane tasks such as conversions. * To obtain a toolbox, it must be requested statically as it is a singleton. * * @author Kenny Law * */ public class SwiftToolBox { /** * Instance of this SwiftToolBox singleton */ private static SwiftToolBox instance; /** * Money formatter (possible round off error) */ NumberFormat moneyFormatter = new DecimalFormat("#.00"); //NOTE POSSIBLE ROUND OFF AMBIGUITY private SwiftToolBox () {} public static SwiftToolBox getInstance() { if (instance == null) { instance = new SwiftToolBox(); } return instance; } /** * Returns a timestamp given a properly formatted string. * @author Chun Li * @param datetime is the string to be converted to a Timestamp * @return The timestamp representative of the string: Timestamp * @throws ParseException If the string is not parseable */ public Timestamp getTimeStampFromString(String datetime) throws ParseException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); java.util.Date parsedDate = null; parsedDate = dateFormat.parse(datetime); Timestamp timestamp = new Timestamp(parsedDate.getTime()); return timestamp; } /** * Returns a double properly money formatted * @param amount The amount of money as a double * @return A double formatted for currency: double */ public double moneyFormatt(double amount) { return Double.parseDouble(moneyFormatter.format(amount)); } /** * Returns a double properly money formatted * @param amount The amount of money as a string * @return A double formatted for currency: double * @throws NumberFormatException If the string is not completely numeric */ public double moneyFormatt(String amount) throws NumberFormatException { return Double.parseDouble(moneyFormatter.format(Double.parseDouble(amount))); } }