1. 程式人生 > >python for android : 貸款每月還款額計算

python for android : 貸款每月還款額計算

功能: 1.等額本息 每月還款額計算 

2.等額本金 每月還款額計算

dkjs3.py

# -*- coding: utf-8 -*-
import android
import os,sys
reload(sys)
sys.setdefaultencoding('utf-8')
droid = android.Android()

# 等額本息 每月還款額計算公式如下:
# =(貸款本金*月利率*(1+月利率)^還款月數)/((1+月利率)^還款月數-1)
def compute1():
    rate= droid.fullQueryDetail("editText1").result["text"]
    cap = droid.fullQueryDetail("editText2").result["text"]
    months= droid.fullQueryDetail("editText3").result["text"]
    print rate,cap,months
    try:
        c = float(cap)
        r = float(rate)
        m = float(months)
        if m >360.0: return
        mhk = (c*(r/1200)*(1+r/1200)**m)/((1+r/1200)**m-1)
        total = mhk*m
        print 'total: %.2f' % (total)
        out = "每月還款額: %.2f元\n還款總利息= %.2f元\n" % (mhk,total-c)
        droid.fullSetProperty("Text2","text",out)
    except:
        droid.makeToast('Error: 輸入數字有錯誤')
        return

# 等額本金 每月還款額計算公式如下:
# 每月本金 = 貸款本金/總月數
def compute2():
    rate= droid.fullQueryDetail("editText1").result["text"]
    cap = droid.fullQueryDetail("editText2").result["text"]
    months= droid.fullQueryDetail("editText3").result["text"]
    print rate,cap,months
    try:
        c = float(cap)
        r = float(rate)
        m = int(months)
        if m >360: return
        cm = c/m
        out = '每月本金: %.2f元\n期數 每月利息 每月還款額\n' % (cm)
        total =0.0
        for i in range(0,m):
            mint = (c-cm*i)*r/1200
            total += mint
            out += '%2d期: %.2f元 %.2f元\n' % (i+1,mint,cm+mint)
        out += '還款總利息= %.2f元\n' % ((m+1)*c*r/1200/2)        
        droid.fullSetProperty("Text2","text",out)
        print 'total: %.2f' % (c+total)
    except:
        droid.makeToast('Error: 輸入數字有錯誤')
        return

def eventloop():
  while True:
    event=droid.eventWait().result
    if event["name"]=="click":
      id=event["data"]["id"]
      if id=="button1":
        compute1()
      if id=="button2":
        compute2()
      if id=="Exit":
        return
    elif event["name"]=="screen":
      if event["data"]=="destroy":
        return

layout = """<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/background"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent" android:background="#ff000000">
  <LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
        <Button
            android:id="@+id/Exit"
            android:layout_width="60dip"
            android:layout_height="wrap_content"
            android:text="退出"
            />
        <Button
            android:id="@+id/button1"
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:text="等額本息計算"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:text="等額本金計算"
            />
  </LinearLayout>
  
  <LinearLayout android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
        <EditText
            android:id="@+id/editText1"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:hint="年利率"
            android:inputType="textPhonetic|number">
            <requestFocus></requestFocus>
        </EditText>
        <EditText
            android:id="@+id/editText2"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:hint="貸款本金"
            android:inputType="number">
            
        </EditText>
        <EditText
            android:id="@+id/editText3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="月數"
            android:inputType="number">
            
        </EditText>
  </LinearLayout>
    
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" 
        android:fadingEdge="vertical" >
    <TextView
        android:id="@+id/Text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="false"
        android:textSize="16"
        android:textColor="#004000"
        android:background="#FFFFF0"
        android:padding="10dip"
        android:hint="輸出"
        />
    </ScrollView>
</LinearLayout>
"""

droid.fullShow(layout)
eventloop()
droid.fullDismiss()


參考 https://code.google.com/p/android-scripting/wiki/FullScreenUI