Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Sana Khan
- * Assignment 3 (Bank Project v. 1)
- * Date: 2nd February 2012
- * Instructor: Volodymyr Voytenko
- */
- package mypackage;
- import javax.microedition.global.Formatter;
- import net.rim.device.api.system.Bitmap;
- import net.rim.device.api.ui.Field;
- import net.rim.device.api.ui.FieldChangeListener;
- import net.rim.device.api.ui.MenuItem;
- import net.rim.device.api.ui.UiApplication;
- import net.rim.device.api.ui.component.BitmapField;
- import net.rim.device.api.ui.component.ButtonField;
- import net.rim.device.api.ui.component.CheckboxField;
- import net.rim.device.api.ui.component.Dialog;
- import net.rim.device.api.ui.component.EditField;
- import net.rim.device.api.ui.component.LabelField;
- import net.rim.device.api.ui.component.Menu;
- import net.rim.device.api.ui.component.ObjectChoiceField;
- import net.rim.device.api.ui.component.PasswordEditField;
- import net.rim.device.api.ui.component.SeparatorField;
- import net.rim.device.api.ui.container.HorizontalFieldManager;
- import net.rim.device.api.ui.container.MainScreen;
- import net.rim.device.api.ui.component.*;
- import net.rim.device.api.ui.container.*;
- public class BankInterest extends MainScreen implements FieldChangeListener {
- // Screen Title section.
- protected LabelField title = new LabelField("Calculate Interest" ,
- LabelField.ELLIPSIS | LabelField.FIELD_HCENTER);
- Formatter f = new Formatter();
- BitmapField bitmap;
- Bitmap logo;
- EditField amountField; // editable user field
- // EditField interestField; // editable user field
- EditField yearField; // editable user field
- ButtonField interestButton; // interest button
- ButtonField totalButton; // Total button
- String myArray[];
- ObjectChoiceField choice;
- // setTitle("Select Interest Rate");
- /**
- * Declare LoginMenuItem class
- **/
- class InterestMenuItem extends MenuItem {
- public InterestMenuItem() {
- super("Interest", 10, 20);
- }
- public void run() {
- Dialog.inform("Your Interest is: " + Double.toString(interestCalc()));
- }
- }
- /**
- * Declare ClearMenuItem class
- **/
- class TotalMenuItem extends MenuItem {
- public TotalMenuItem() {
- super("Total", 27, 1);
- }
- public void run() {
- Dialog.inform("Your Total is: " + Double.toString(totalInt()));
- }
- }
- MenuItem _result = new MenuItem("Exit" , 1000,1000)
- {
- public void run()
- {
- Dialog.alert("Have a nice day :) ! "+Double.toString(exit()));
- }
- };
- public double exit()
- {
- Dialog.alert("Have a nice day :) !");
- System.exit(0);
- return 0;
- }
- public BankInterest() {
- // this sets the title
- setTitle(title);
- logo = Bitmap.getBitmapResource("logo.png");
- bitmap = new BitmapField(logo, Field.FIELD_HCENTER);
- bitmap.setBitmap(logo);
- add(bitmap);
- add(new SeparatorField());
- // define three Field control elements
- amountField = new EditField("Enter amount: $", "");
- // interestField = new EditField("Enter % interest:", "");
- yearField = new EditField("Enter year(s):", "");
- add(new SeparatorField());
- myArray = new String[]{"5","4","3","2","1"};
- choice = new ObjectChoiceField("Select Interest Rate: ",myArray);
- // add three Field control elements
- add(amountField);
- // add(interestField);
- add(choice);
- add(yearField);
- add(new SeparatorField());
- // define and setchangelistener for buttons Interest
- interestButton = new ButtonField("Interest Only", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER );
- add(interestButton);
- interestButton.setChangeListener(new FieldChangeListener(){
- public void fieldChanged( Field arg0, int arg1 ) {
- Dialog.alert("The interest is $"+Double.toString(interestCalc()));
- f.formatCurrency(interestCalc(),"CAD");
- }
- });
- // define and setchangelistener for buttons Total
- totalButton = new ButtonField("Total", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER );
- add(totalButton);
- totalButton.setChangeListener(new FieldChangeListener(){
- public void fieldChanged( Field arg0, int arg1 ) {
- Dialog.alert("The total is $"+Double.toString(totalInt()));
- f.formatCurrency(totalInt(),"CAD");
- }
- });
- interestButton = new ButtonField("Interest", ButtonField.CONSUME_CLICK );
- interestButton.setChangeListener(this);
- totalButton = new ButtonField("Total", ButtonField.CONSUME_CLICK);
- totalButton.setChangeListener(this);
- RadioButtonGroup rbg = new RadioButtonGroup();
- add(new RadioButtonField("Option 1",rbg,true));
- add(new RadioButtonField("Option 2",rbg,false));
- }
- // define menu
- protected void makeMenu(Menu menu, int instance) {
- super.makeMenu(menu, instance);
- menu.add(new InterestMenuItem());
- menu.add(new TotalMenuItem());
- menu.add(_result);
- }
- //Declaring instance variables
- double cInterest = 0;
- double pInterest = 0;
- double tTotal = 0;
- /* Calculation for the total interest */
- /* I = prt */
- protected double interestCalc()
- {
- try {
- double tAmount = Double.parseDouble(amountField.getText());
- // double tInterest = Double.parseDouble(choice.getSelectedIndex());
- double tYear = Double.parseDouble(yearField.getText());
- int rate = choice.getSelectedIndex();
- pInterest = rate/100;
- cInterest = (double)((tAmount * pInterest) * tYear);
- }
- catch (NumberFormatException ex)
- {
- Dialog.alert("Invalid input");
- }
- return cInterest;
- }
- /* Calculating total */
- /* Interest + amount */
- protected double totalInt()
- {
- try {
- double tAmount = Double.parseDouble(amountField.getText());
- tTotal =(double)(tAmount + cInterest);
- }
- catch (NumberFormatException ex)
- {
- Dialog.alert("Invalid input");
- }
- return tTotal;
- }
- public boolean onSavePrompt()
- {
- return false;
- }
- public void fieldChanged(Field arg0, int arg1) {
- // TODO Auto-generated method stub
- }
- }
Add Comment
Please, Sign In to add comment