Guest User

Untitled

a guest
Jun 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. /*
  2. * Name: Sana Khan
  3. * Assignment 3 (Bank Project v. 1)
  4. * Date: 2nd February 2012
  5. * Instructor: Volodymyr Voytenko
  6. */
  7.  
  8. package mypackage;
  9.  
  10. import javax.microedition.global.Formatter;
  11.  
  12. import net.rim.device.api.system.Bitmap;
  13. import net.rim.device.api.ui.Field;
  14. import net.rim.device.api.ui.FieldChangeListener;
  15. import net.rim.device.api.ui.MenuItem;
  16. import net.rim.device.api.ui.UiApplication;
  17. import net.rim.device.api.ui.component.BitmapField;
  18. import net.rim.device.api.ui.component.ButtonField;
  19. import net.rim.device.api.ui.component.CheckboxField;
  20. import net.rim.device.api.ui.component.Dialog;
  21. import net.rim.device.api.ui.component.EditField;
  22. import net.rim.device.api.ui.component.LabelField;
  23. import net.rim.device.api.ui.component.Menu;
  24. import net.rim.device.api.ui.component.ObjectChoiceField;
  25. import net.rim.device.api.ui.component.PasswordEditField;
  26. import net.rim.device.api.ui.component.SeparatorField;
  27. import net.rim.device.api.ui.container.HorizontalFieldManager;
  28. import net.rim.device.api.ui.container.MainScreen;
  29. import net.rim.device.api.ui.component.*;
  30. import net.rim.device.api.ui.container.*;
  31.  
  32.  
  33. public class BankInterest extends MainScreen implements FieldChangeListener {
  34.  
  35. // Screen Title section.
  36. protected LabelField title = new LabelField("Calculate Interest" ,
  37. LabelField.ELLIPSIS | LabelField.FIELD_HCENTER);
  38.  
  39. Formatter f = new Formatter();
  40.  
  41. BitmapField bitmap;
  42. Bitmap logo;
  43.  
  44. EditField amountField; // editable user field
  45. // EditField interestField; // editable user field
  46. EditField yearField; // editable user field
  47.  
  48. ButtonField interestButton; // interest button
  49. ButtonField totalButton; // Total button
  50. String myArray[];
  51.  
  52. ObjectChoiceField choice;
  53.  
  54.  
  55.  
  56. // setTitle("Select Interest Rate");
  57.  
  58.  
  59. /**
  60. * Declare LoginMenuItem class
  61. **/
  62. class InterestMenuItem extends MenuItem {
  63. public InterestMenuItem() {
  64. super("Interest", 10, 20);
  65. }
  66. public void run() {
  67. Dialog.inform("Your Interest is: " + Double.toString(interestCalc()));
  68. }
  69. }
  70.  
  71. /**
  72. * Declare ClearMenuItem class
  73. **/
  74. class TotalMenuItem extends MenuItem {
  75. public TotalMenuItem() {
  76. super("Total", 27, 1);
  77. }
  78.  
  79. public void run() {
  80. Dialog.inform("Your Total is: " + Double.toString(totalInt()));
  81. }
  82. }
  83.  
  84. MenuItem _result = new MenuItem("Exit" , 1000,1000)
  85. {
  86. public void run()
  87.  
  88. {
  89.  
  90. Dialog.alert("Have a nice day :) ! "+Double.toString(exit()));
  91.  
  92. }
  93. };
  94.  
  95.  
  96. public double exit()
  97.  
  98. {
  99. Dialog.alert("Have a nice day :) !");
  100. System.exit(0);
  101. return 0;
  102. }
  103.  
  104. public BankInterest() {
  105.  
  106.  
  107.  
  108. // this sets the title
  109. setTitle(title);
  110.  
  111. logo = Bitmap.getBitmapResource("logo.png");
  112.  
  113. bitmap = new BitmapField(logo, Field.FIELD_HCENTER);
  114. bitmap.setBitmap(logo);
  115. add(bitmap);
  116.  
  117.  
  118. add(new SeparatorField());
  119.  
  120.  
  121.  
  122. // define three Field control elements
  123. amountField = new EditField("Enter amount: $", "");
  124. // interestField = new EditField("Enter % interest:", "");
  125. yearField = new EditField("Enter year(s):", "");
  126. add(new SeparatorField());
  127.  
  128.  
  129. myArray = new String[]{"5","4","3","2","1"};
  130. choice = new ObjectChoiceField("Select Interest Rate: ",myArray);
  131.  
  132.  
  133. // add three Field control elements
  134. add(amountField);
  135. // add(interestField);
  136. add(choice);
  137. add(yearField);
  138.  
  139. add(new SeparatorField());
  140.  
  141.  
  142. // define and setchangelistener for buttons Interest
  143. interestButton = new ButtonField("Interest Only", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER );
  144. add(interestButton);
  145. interestButton.setChangeListener(new FieldChangeListener(){
  146. public void fieldChanged( Field arg0, int arg1 ) {
  147. Dialog.alert("The interest is $"+Double.toString(interestCalc()));
  148. f.formatCurrency(interestCalc(),"CAD");
  149. }
  150. });
  151.  
  152. // define and setchangelistener for buttons Total
  153. totalButton = new ButtonField("Total", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER );
  154. add(totalButton);
  155. totalButton.setChangeListener(new FieldChangeListener(){
  156. public void fieldChanged( Field arg0, int arg1 ) {
  157. Dialog.alert("The total is $"+Double.toString(totalInt()));
  158. f.formatCurrency(totalInt(),"CAD");
  159. }
  160. });
  161.  
  162.  
  163. interestButton = new ButtonField("Interest", ButtonField.CONSUME_CLICK );
  164. interestButton.setChangeListener(this);
  165.  
  166. totalButton = new ButtonField("Total", ButtonField.CONSUME_CLICK);
  167. totalButton.setChangeListener(this);
  168.  
  169.  
  170. RadioButtonGroup rbg = new RadioButtonGroup();
  171. add(new RadioButtonField("Option 1",rbg,true));
  172. add(new RadioButtonField("Option 2",rbg,false));
  173.  
  174.  
  175.  
  176.  
  177. }
  178.  
  179.  
  180. // define menu
  181. protected void makeMenu(Menu menu, int instance) {
  182. super.makeMenu(menu, instance);
  183.  
  184. menu.add(new InterestMenuItem());
  185. menu.add(new TotalMenuItem());
  186. menu.add(_result);
  187.  
  188. }
  189.  
  190. //Declaring instance variables
  191. double cInterest = 0;
  192. double pInterest = 0;
  193. double tTotal = 0;
  194.  
  195. /* Calculation for the total interest */
  196. /* I = prt */
  197.  
  198. protected double interestCalc()
  199. {
  200. try {
  201. double tAmount = Double.parseDouble(amountField.getText());
  202. // double tInterest = Double.parseDouble(choice.getSelectedIndex());
  203. double tYear = Double.parseDouble(yearField.getText());
  204. int rate = choice.getSelectedIndex();
  205.  
  206.  
  207. pInterest = rate/100;
  208. cInterest = (double)((tAmount * pInterest) * tYear);
  209.  
  210.  
  211. }
  212. catch (NumberFormatException ex)
  213. {
  214. Dialog.alert("Invalid input");
  215.  
  216. }
  217. return cInterest;
  218. }
  219.  
  220.  
  221. /* Calculating total */
  222. /* Interest + amount */
  223. protected double totalInt()
  224. {
  225. try {
  226.  
  227. double tAmount = Double.parseDouble(amountField.getText());
  228.  
  229. tTotal =(double)(tAmount + cInterest);
  230. }
  231.  
  232. catch (NumberFormatException ex)
  233. {
  234. Dialog.alert("Invalid input");
  235.  
  236. }
  237.  
  238.  
  239. return tTotal;
  240. }
  241.  
  242. public boolean onSavePrompt()
  243. {
  244. return false;
  245. }
  246.  
  247.  
  248.  
  249.  
  250. public void fieldChanged(Field arg0, int arg1) {
  251. // TODO Auto-generated method stub
  252.  
  253. }
  254. }
Add Comment
Please, Sign In to add comment