⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mortgage.java

📁 calculator for calculating interest rate in a mobile phone
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import java.io.*;
import javax.microedition.io.*;

public class Mortgage extends MIDlet implements CommandListener {
  private Command exitCommand, goCommand;
  private Display display;
  private Form screen;
  private TextField amountField, rateField, termField, tniField;

  public Mortgage() {
    // Get the Display object for the MIDlet
    display = Display.getDisplay(this);

    // Create the Exit and Go commands
    exitCommand = new Command("Exit", Command.EXIT, 2);
    goCommand = new Command("Go", Command.OK, 2);

    // Create the screen form
    screen = new Form("Enter Loan Info");
    amountField = new TextField("Loan Amount ($)", "100000", 7, TextField.NUMERIC);
    screen.append(amountField);
    rateField = new TextField("Interest Rate (%)", "8.5", 5, TextField.ANY);
    screen.append(rateField);
    termField = new TextField("Loan Term (years)", "15", 2, TextField.NUMERIC);
    screen.append(termField);
    tniField = new TextField("Taxes & Ins. ($)", "1200", 5, TextField.NUMERIC);
    screen.append(tniField);

    // Set the Exit and Go commands for the screen
    screen.addCommand(exitCommand);
    screen.addCommand(goCommand);
    screen.setCommandListener(this);
  }

  public void startApp() throws MIDletStateChangeException {
    // Set the current display to the screen
    display.setCurrent(screen);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (c == goCommand) {
      // Calculate the monthly payment
      int amount = Integer.parseInt(amountField.getString());
      int rate1K = parseRate(rateField.getString());
      int term = Integer.parseInt(termField.getString());
      int tni = Integer.parseInt(tniField.getString());
      int payment = calcPayment(amount, rate1K, term, tni);

      // Display the monthly payment
      Alert paymentAlert = new Alert("Mortgage", "Monthly Payment: $" +
        String.valueOf(payment), null, AlertType.INFO);
      paymentAlert.setTimeout(Alert.FOREVER);
      display.setCurrent(paymentAlert);
    }
  }

  private int parseRate(String strRate) {
    // Convert the string to a string buffer
    StringBuffer str = new StringBuffer(strRate);

    // See if there is a decimal point in the rate, and delete it
    int i;
    for (i = 0; i < str.length(); i++)
      if (str.charAt(i) == '.') {
        str.deleteCharAt(i);
        break;
      }

    // Pad the rate string so that it is exactly 4 digits
    for (i = str.length(); i < 4; i++)
      str.append("0");

    return Integer.parseInt(str.toString());
  }

  private int calcPayment(int amount, int rate1K, int term, int tni) {
    int monthlyPayment;
    long precision = 1000000l;
    int termMonths = term * 12;
    long tmp1 = (precision * 1200000l) / (1200000l + rate1K);
    long tmp2 = tmp1;

    // Calculate the principal and interest
    for (int i = 0; i < termMonths; i++)
      tmp2 = (tmp1 * tmp2) / precision;
    monthlyPayment = (int)(((precision * amount * rate1K) / 1200000l) /
      (precision - tmp2));

    // Add in the monthly taxes and insurance
    monthlyPayment += tni / 12;

    return monthlyPayment;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -