/*
* Description:
* This is a Converter application, which lets a user choose from a number
* of different conversion categories. Within each category, the user can
* further select the type of units to convert from and to, after which the user
* is prompted to enter a value to convert. The result is then displayed to
* the screen.
*
*
* Date: December 3rd, 2010
*
* Authors:
* Mirela Petkova 260139527
* Robert Stein
* Richard Martinez
*
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.Date;
import java.io.*;
import java.lang.*;
public class UnitConverter extends MIDlet implements CommandListener,
Runnable {
private Display display = null;
private final int MAX_CHARS = 20;
//menu strings
private static String TemperatureConverter = "Convert Temperature";
private static String PressureConverter = "Convert Pressure";
private static String EnergyConverter = "Convert Energy";
private static String SpeedConverter = "Convert Speed";
private static String DistanceConverter = "Convert Distance";
private static String WeightConverter = "Convert Weight";
private static String LengthConverter = "Convert Length";
//submenu strings
private static String TempCF = "C to F";
private static String TempFC = "F to C";
private static String convertKM = "Km to Miles";
private static String convertMK = "Miles to Km";
private static String convertCJ = "Calories to Joules";
private static String convertJC = "Joules to Calories";
private static String PressureBP = "Bars to PSI";
private static String PressurePB = "PSI to Bars";
private static String convertKMs = "Km to Miles";
private static String convertMKs = "Miles to Km";
private static String convertKP = "Kilograms to Pounds";
private static String convertPK = "Pounds to Kilograms";
private static String convertCI = "Cm to Inches";
private static String convertIC= "Inches to Cm";
//Declaring the menu
List menu = null;
Form ui_form = null;
List menuTemp = null;
Image temperature=null;
Image pressure=null;
Image energy=null;
Image speed=null;
Image distance=null;
//Welcome message
Alert welcome;
//declaring Commands
Command Done, Convert, Quit, Back, BackSub;
//String and double variable declarations
String str, res;
double numUnits = 0.0;
double result = 0.0;
//declaring some objects
Alert timeAlert;
Image i = null;
TextField num;
Ticker ui_ticker = null;
//constructor
public UnitConverter()
{
display = Display.getDisplay(this);
Done = new Command("Done", Command.SCREEN, 1);
Quit = new Command("Quit", Command.SCREEN, 2);
Convert = new Command("Convert", Command.ITEM, 1);
Back = new Command("Back", Command.ITEM, 2);
BackSub = new Command("Back", Command.ITEM, 2);
}
public void startApp()
{
//creating the first menu
Display.getDisplay(this).setCurrent(timeAlert);
createAlert();
menu = new List("Term Project - Converter Application", List.IMPLICIT);
try {
temperature = Image.createImage("images/thermometer.png");
pressure = Image.createImage("images/pressure.png");
energy = Image.createImage("images/energy.png");
speed = Image.createImage("images/speed.png");
distance = Image.createImage("images/distance.png");
}
catch (IOException ioe){}
//placing images on the menu
menu.append(TemperatureConverter, temperature );
menu.append(PressureConverter, pressure);
menu.append(EnergyConverter, energy);
menu.append(SpeedConverter, speed);
menu.append(DistanceConverter, distance);
// initializing the ui_ticker
ui_ticker = new Ticker("Welcome to the Converter");
menu.setTicker(ui_ticker);
menu.addCommand(Quit);
menu.setCommandListener(this);
display.setCurrent(timeAlert, menu);
}//End startApp()
//creating the welcome alert
public void createAlert() {
try {
i = Image.createImage("images/ampere-meter.jpg");
}
catch (IOException ioe) {}
// Initializing the alert with date display and no timeout
timeAlert = new Alert("Welcome to the Converter", null, i, AlertType.INFO);
timeAlert.setString(new Date().toString());
timeAlert.setTimeout(timeAlert.FOREVER);
}
//creating the welcome message
public void welcome()
{
display = Display.getDisplay(this);
welcome = new Alert("Unit Conversion", "", null, AlertType.INFO);
welcome.setString("Let's convert some units");
welcome.setTimeout(Alert.FOREVER);
welcome.addCommand(Done);
welcome.setCommandListener(this);
display.setCurrent(welcome);
}//END welcome()
public void pauseApp()
{
}//End pauseApp()
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}//End destroyApp()
//processing commands from the device
public void commandAction(Command c, Displayable d)
{
if ( c == Done )
showMenu();
if ( c == Quit )
destroyApp(true);
if (c == Convert)
conversion();
else if(c == Back)
display.setCurrent(menu);
else if (c == List.SELECT_COMMAND)
run();
}//End commandAction
public void showMenu()
{
menu = new List("Converter", List.IMPLICIT);
try {
temperature = Image.createImage("images/thermometer.png");
pressure = Image.createImage("images/pressure.png");
energy = Image.createImage("images/energy.png");
speed = Image.createImage("images/speed.png");
distance = Image.createImage("images/distance.png");
} catch (IOException ioe){}
menu.append(TemperatureConverter, temperature);
menu.append(SpeedConverter, pressure);
menu.append(PressureConverter, energy);
menu.append(EnergyConverter, speed);
menu.append(DistanceConverter, distance);
// putting the ticker in the menu
menu.setTicker(ui_ticker);
menu.addCommand(Quit);
menu.setCommandListener(this);
display.setCurrent(menu);
}//END showMenu();
public void run()
{
//processing selected converter
int index = menu.getSelectedIndex();
if(index == -1) return;
String command = menu.getString(index);
if (command.equals(TemperatureConverter))
{
customForm("Convert Temperature");
convertSub(TemperatureConverter);
}
else if (command.equals(TempCF))
{
customForm("Convert from C to F");
convertTemperatureCF();
}
else if (command.equals(TempFC))
{
customForm("Convert from F to C");
convertTemperatureFC();
}
else if (command.equals(PressureConverter))
{
customForm("Convert Pressure");
convertSub(PressureConverter);
}
else if (command.equals(PressurePB))
{
customForm("Convert from PSI to Bars");
convertPressurePB();
}
else if (command.equals(PressureBP))
{
customForm("Convert from Bars to PSI");
convertPressureBP();
}
else if (command.equals(EnergyConverter))
{
customForm("Convert Energy");
convertSub(EnergyConverter);
}
else if (command.equals(convertJC))
{
customForm("Convert from J to Cals");
convertEnergyJC();
}
else if (command.equals(convertCJ))
{
customForm("Convert from Cals to J");
convertEnergyCJ();
}
else if (command.equals(SpeedConverter))
{
customForm("Convert Speed");
convertSub(SpeedConverter);
}
else if (command.equals(convertKMs))
{
customForm("Convert from Km to Mi");
convertSpeedMK();
}
else if (command.equals(convertMKs))
{
customForm("Convert from Mi to Km");
convertSpeedKM();
}
else if (command.equals(DistanceConverter))
{
customForm("Convert Distance");
convertSub(DistanceConverter);
}
else if (command.equals(convertKM))
{
customForm("Convert from Km to Mi");
convertDistanceKM();
}
else if (command.equals(convertMK))
{
customForm("Convert from Mi to Km");
convertDistanceMK();
}
else if (command.equals(WeightConverter))
{
customForm("Convert Weight");
convertSub(WeightConverter);
}
else if (command.equals(convertKP))
{
customForm("Convert from Kg to Lb");
convertWeightKP();
}
else if (command.equals(convertPK))
{
customForm("Convert from Lb to Kg");
convertWeightPK();
}
else if (command.equals(LengthConverter))
{
customForm("Convert Length");
convertSub(LengthConverter);
}
else if (command.equals(convertCI))
{
customForm("Convert from Cm to In");
convertLengthCI();
}
else if (command.equals(convertMK))
{
customForm("Convert from In to Cm");
convertLengthIC();
}
}
//create a custom convert unit entry form
public double customForm (String fName)
{
ui_form = new Form(fName);
ui_form.addCommand(Convert);
ui_form.addCommand(Back);
display.setCurrent(ui_form);
num = new TextField("Enter the number of units you would like to convert", "", MAX_CHARS, TextField.ANY);
ui_form.append(num);
ui_form.setCommandListener(this);
/***********************/
/* THIS IS NOT WORKING*/
// str = num.getString();
str = "The number is: " + num.getString();
try
{
numUnits = Double.parseDouble(str);
}
catch (NumberFormatException nfe)
{
}
finally
{
return numUnits;
}
//return str;
}
public void convertSub(String s)
{
menu = new List("Converter", List.IMPLICIT);
if (s.equals(TemperatureConverter))
{
try {
pressure = Image.createImage("images/pressure.png");
}
catch (IOException ioe){}
menu.append(TempCF, pressure);
menu.append(TempFC, pressure);
}
if (s.equals(PressureConverter))
{
try {
temperature = Image.createImage("images/thermometer.png");
pressure = Image.createImage("images/pressure.png");
}
catch (IOException ioe){}
menu.append(PressureBP, temperature);
menu.append(PressurePB, pressure);
}
if (s.equals(EnergyConverter))
{
try {
temperature = Image.createImage("images/distance.png");
pressure = Image.createImage("images/distance.png");
} catch (IOException ioe){}
menu.append(convertCJ, temperature);
menu.append(convertJC, pressure);
}
if (s.equals(SpeedConverter))
{
try {
pressure = Image.createImage("images/pressure.png");
}
catch (IOException ioe){}
menu.append(convertKMs, pressure);
menu.append(convertMKs, pressure);
}
if (s.equals(DistanceConverter))
{
try {
temperature = Image.createImage("images/distance.png");
pressure = Image.createImage("images/distance.png");
} catch (IOException ioe){}
menu.append(convertKM, temperature);
menu.append(convertMK, pressure);
}
if (s.equals(WeightConverter))
{
menu.append(convertKP, null);
menu.append(convertPK, null);
}
if (s.equals(LengthConverter))
{
menu.append(convertCI, null);
menu.append(convertIC, null);
}
// putting the ticker in the menu
menu.setTicker(ui_ticker);
menu.addCommand(Back);
menu.addCommand(Quit);
menu.setCommandListener(this);
display.setCurrent(menu);
}
/**********************************************Pressure converter *************************************** */
public String convertPressureBP()
{
PressureConverter p = new PressureConverter("Bars", "PSI", "This is a Bar-PSI pressure converter",
"TPressure converter", "Pres");
result = p.convert(numUnits, p.getConversionConstant());
return (res = Double.toString(result));
}
public String convertPressurePB()
{
PressureConverter p = new PressureConverter("PSI", "Bars", "This is a PSI-Bar pressure converter",
"TPressure converter", "Pres");
result = p.convert(numUnits, 1/(p.getConversionConstant()));
return (res = Double.toString(result));
}
/**********************************************Temperature converter *************************************** */
public String convertTemperatureCF()
{
TemperatureConverter c = new TemperatureConverter("Celsius", "Fahrenheit", "This is a C-F temp converter",
"Temperature converter", "Temp");
result = c.convertCF(numUnits);
ui_form.append("The num units in convertTemperatureCF in line 503 is: " + numUnits);
ui_form.append("The result in convertTemperatureCF in line 504 is: " + result);
return (res = Double.toString(result));
}
public String convertTemperatureFC()
{
TemperatureConverter c = new TemperatureConverter("Fahrenheit", "Celsius", "This is an F-C temp converter",
"Temperature converter", "Temp");
result = c.convertFC(numUnits);
return (res = Double.toString(result));
}
/**********************************************Speed converter *************************************** */
public String convertSpeedKM()
{
SpeedConverter s = new SpeedConverter("Miles", "Kilometers", "This is a Km-Mi speed converter",
"Speed converter", "Speed");
result = s.convert(numUnits, s.getConversionConstant());
return (res = Double.toString(result));
}
public String convertSpeedMK()
{
SpeedConverter s = new SpeedConverter("Kilometers", "Miles", "This is a Mi-Km speed converter",
"Speed converter", "Speed");
result = s.convert(numUnits, 1/(s.getConversionConstant()));
return (res = Double.toString(result));
}
/**********************************************Distance converter *************************************** */
public String convertDistanceMK()
{
DistanceConverter d = new DistanceConverter("Miles", "Kilometers", "This is a Mi-Km distance converter",
"Distance converter", "Dist");
//result = d.convertKM(numUnits);
result = d.convert(numUnits, (1/d.getConversionConstant()));
return (res = Double.toString(result));
}
public String convertDistanceKM()
{
DistanceConverter d = new DistanceConverter("Kilometers", "Miles", "This is a Km-Mi distance converter",
"Distance converter", "Dist");
result = d.convert(numUnits, d.getConversionConstant());
return (res = Double.toString(result));
}
/**********************************************Energy converter *************************************** */
public String convertEnergyJC()
{
EnergyConverter e = new EnergyConverter("Joules", "Calories", "This is a Joules-Cal energy converter",
"Energy converter", "En");
result = e.convert(numUnits, 1/(e.getConversionConstant()));
return (res = Double.toString(result));
}
public String convertEnergyCJ()
{
EnergyConverter e = new EnergyConverter("Calories", "Joules", "This is a Cal-Joules energy converter",
"Energy converter", "En");
result = e.convert(numUnits, e.getConversionConstant());
return (res = Double.toString(result));
}
/**********************************************Weight converter *************************************** */
public String convertWeightKP()
{
WeightConverter w = new WeightConverter("Kilograms", "Pounds", "This is a Kg-Lb weight converter",
"Weight converter", "Weight");
result = w.convert(numUnits, w.getConversionConstant());
return (res = Double.toString(result));
}
public String convertWeightPK()
{
WeightConverter w = new WeightConverter("Pounds", "Kilograms", "This is an Lb-Kg weight converter",
"Weight converter", "Weight");
result = w.convert(numUnits, 1/(w.getConversionConstant()));
return (res = Double.toString(result));
}
/**********************************************Length converter *************************************** */
public String convertLengthCI()
{
LengthConverter l = new LengthConverter("Centimeters", "Inches", "This is a Cm-In length converter",
"Length converter", "Len");
result = l.convert(numUnits, l.getConversionConstant());
return (res = Double.toString(result));
}
public String convertLengthIC()
{
LengthConverter l = new LengthConverter("Inches", "Centimeters", "This is an In-Cm length converter",
"Length converter", "Len");
result = l.convert(numUnits, 1/(l.getConversionConstant()));
return (res = Double.toString(result));
}
//display results
public void conversion()
{
ui_form.append("The value of res parameter in line 621 is: " + res);
ui_form.append("The num units in conversion method line 622 is: " + numUnits);
//ui_form.append(str);
}
}