Fixed currencies display, added help for currency rates keyword.

This commit is contained in:
Erik C. Thauvin 2015-08-29 12:27:18 -07:00
parent 0255fa537f
commit 80921e2aea
8 changed files with 670 additions and 860 deletions

View file

@ -98,7 +98,7 @@ public class CurrencyConverter implements Runnable
*/
public final void run()
{
if (Utils.isValidString(sender) && Utils.isValidString(query))
if (Utils.isValidString(sender))
{
if (EXCHANGE_RATES.isEmpty())
{
@ -140,71 +140,76 @@ public class CurrencyConverter implements Runnable
}
}
if (!EXCHANGE_RATES.isEmpty())
if (EXCHANGE_RATES.isEmpty())
{
if (query.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+"))
bot.getLogger().debug("The exchange rate table is empty.");
bot.send(sender, "Sorry, but the exchange rate table is empty.");
}
else if (Utils.isValidString(query))
{
if (!EXCHANGE_RATES.isEmpty())
{
final String[] cmds = query.split(" ");
if (cmds.length == 4)
if (query.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+"))
{
if (cmds[3].equals(cmds[1]) || cmds[0].equals("0"))
{
bot.send(sender, "You're kidding, right?");
}
else
{
try
{
final double amt = Double.parseDouble(cmds[0].replaceAll(",", ""));
final double from = Double.parseDouble(EXCHANGE_RATES.get(cmds[1].toUpperCase()));
final double to = Double.parseDouble(EXCHANGE_RATES.get(cmds[3].toUpperCase()));
final String[] cmds = query.split(" ");
bot.send(bot.getChannel(),
NumberFormat.getCurrencyInstance(Locale.US).format(amt).substring(1) + ' ' +
cmds[1].toUpperCase() + " = " +
NumberFormat.getCurrencyInstance(Locale.US).format((amt * to) / from)
.substring(1) + ' ' + cmds[3].toUpperCase());
}
catch (NullPointerException ignored)
if (cmds.length == 4)
{
if (cmds[3].equals(cmds[1]) || cmds[0].equals("0"))
{
bot.send(sender, "The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
bot.send(sender, "You're kidding, right?");
}
else
{
try
{
final double amt = Double.parseDouble(cmds[0].replaceAll(",", ""));
final double from = Double.parseDouble(EXCHANGE_RATES.get(cmds[1].toUpperCase()));
final double to = Double.parseDouble(EXCHANGE_RATES.get(cmds[3].toUpperCase()));
bot.send(bot.getChannel(),
NumberFormat.getCurrencyInstance(Locale.US).format(amt).substring(1) + ' '
+
cmds[1].toUpperCase() + " = " +
NumberFormat.getCurrencyInstance(Locale.US).format((amt * to) / from)
.substring(1) + ' ' + cmds[3].toUpperCase());
}
catch (NullPointerException ignored)
{
bot.send(sender,
"The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
}
}
}
}
}
else if (query.equals(Commands.CURRENCY_RATES_KEYWORD))
{
bot.send(sender, "Last Update: " + pubDate);
final Iterator<String> it = EXCHANGE_RATES.keySet().iterator();
String rate;
final StringBuilder buff = new StringBuilder(0);
while (it.hasNext())
else if (query.equals(Commands.CURRENCY_RATES_KEYWORD))
{
rate = it.next();
if (buff.length() > 0)
bot.send(sender, "Last Update: " + pubDate);
final Iterator<String> it = EXCHANGE_RATES.keySet().iterator();
String rate;
final StringBuilder buff = new StringBuilder(0);
while (it.hasNext())
{
buff.append(", ");
rate = it.next();
if (buff.length() > 0)
{
buff.append(", ");
}
buff.append(rate).append(": ").append(EXCHANGE_RATES.get(rate));
}
buff.append(rate).append(": ").append(EXCHANGE_RATES.get(rate));
bot.send(sender, buff.toString());
}
bot.send(sender, buff.toString());
}
else
{
bot.helpResponse(sender, Commands.CURRENCY_CMD + ' ' + query);
bot.send(sender, "The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
}
}
else
{
bot.getLogger().debug("The exchange rate table is empty.");
bot.send(sender, "Sorry, but the exchange rate table is empty.");
bot.helpResponse(sender, Commands.CURRENCY_CMD + ' ' + query);
bot.send(sender, "The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
}
}
}
@ -215,6 +220,7 @@ public class CurrencyConverter implements Runnable
* @param sender The nick of the person who sent the message.
* @param query The currency query.
*/
public synchronized void setQuery(String sender, String query)
{
this.query = query;

View file

@ -1211,6 +1211,8 @@ public class Mobibot extends PircBot
if (lcTopic.endsWith(Commands.CURRENCY_CMD))
{
send(sender, "For a listing of currency rates:");
send(sender, DOUBLE_INDENT + Utils.bold(getNick() + ": " + Commands.CURRENCY_CMD) + ' ' + Commands.CURRENCY_RATES_KEYWORD);
send(sender, "For a listing of supported currencies:");
send(sender, DOUBLE_INDENT + Utils.bold(getNick() + ": " + Commands.CURRENCY_CMD));
}

View file

@ -1,115 +1,59 @@
/*
* ReleaseInfo.java
*
* Copyright (c) 2004-2015, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Created by JReleaseInfo AntTask from Open Source Competence Group */
/* Creation date Fri Apr 25 18:08:16 PDT 2014 */
/* Creation date Sat Aug 29 12:08:20 PDT 2015 */
package net.thauvin.erik.mobibot;
import java.util.Date;
/**
* This class provides information gathered from the build environment.
*
*
* @author JReleaseInfo AntTask
*/
public class ReleaseInfo
{
public class ReleaseInfo {
/**
* buildDate (set during build process to 1398474496363L).
*/
private static final Date buildDate = new Date(1398474496363L);
/**
* Disables the default constructor.
* @throws UnsupportedOperationException if the constructor is called.
*/
private ReleaseInfo() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Illegal constructor call.");
}
/**
* project (set during build process to "mobibot").
*/
private static final String project = "mobibot";
/**
* version (set during build process to "0.6").
*/
private static final String version = "0.6";
/** buildDate (set during build process to 1440875300470L). */
private static final Date buildDate = new Date(1440875300470L);
/**
* Disables the default constructor.
*
* @throws UnsupportedOperationException if the constructor is called.
*/
private ReleaseInfo()
throws UnsupportedOperationException
{
throw new UnsupportedOperationException("Illegal constructor call.");
}
/**
* Get buildDate (set during build process to Sat Aug 29 12:08:20 PDT 2015).
* @return Date buildDate
*/
public static Date getBuildDate() { return buildDate; }
/**
* Get buildDate (set during build process to Fri Apr 25 18:08:16 PDT 2014).
*
* @return Date buildDate
*/
public static Date getBuildDate()
{
return buildDate;
}
/**
* Get buildNumber (set during build process to 0).
*
* @return int buildNumber
*/
public static int getBuildNumber()
{
return 0;
}
/** project (set during build process to "mobibot"). */
private static final String project = "mobibot";
/**
* Get project (set during build process to "mobibot").
*
* @return String project
*/
public static String getProject()
{
return project;
}
/**
* Get project (set during build process to "mobibot").
* @return String project
*/
public static String getProject() { return project; }
/**
* Get version (set during build process to "0.6").
*
* @return String version
*/
public static String getVersion()
{
return version;
}
/** version (set during build process to "0.6"). */
private static final String version = "0.6";
/**
* Get version (set during build process to "0.6").
* @return String version
*/
public static String getVersion() { return version; }
/**
* Get buildNumber (set during build process to 0).
* @return int buildNumber
*/
public static int getBuildNumber() { return 0; }
}