首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

用 C# 寫個人住房貸款計算機(三)

     編輯:關於C語言

而 Pub.cs 檔案中的 Pub 靜態類別提供的 GetMessage 方法用於顯示錯誤訊息:

using System;
using System.Text;

namespace Skyiv.Ben.LoanCalculator
{
    static class Pub
    {
        public static string GetMessage(Exception ex)
        {
            var sb = new StringBuilder("錯誤: ");
            for (var e = ex; e != null; e = e.InnerException) sb.Append(e.Message + ": ");
            sb.Length -= 2;
            return sb.ToString();
        }
    }
}

接著,就是 LoanBase.cs 檔案中的抽象基類 LoanBase 了:

using System;
using System.Data;
using System.Drawing;
using System.Collections.Generic;

namespace Skyiv.Ben.LoanCalculator
{
    abstract class LoanBase
    {
        public DataTable Table { get; private set; }

        public LoanBase(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
        {
            Table = GetTable();
            Calculate(balance, months, date, rates);
        }

        protected virtual void Calculate(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
        {
        }

        protected decimal GetMonthRate(DateTime date, int months, KeyValuePair<DateTime, PointF>[] rates)
        {
            int i;
            for (i = rates.Length - 1; i >= 0; i--) if (date >= rates[i].Key) break;
            return (decimal)(months <= 60 ? rates[i].Value.X : rates[i].Value.Y) / 100 / 12;
        }

        protected decimal Round(decimal dec)
        {
            return decimal.Round(dec, 2);
        }

        DataTable GetTable()
        {
            var dt = new DataTable();
            dt.Columns.Add("期數", typeof(int));
            dt.Columns.Add("還款日期", typeof(DateTime));
            dt.Columns.Add("本金", typeof(decimal));
            dt.Columns.Add("利息", typeof(decimal));
            dt.Columns.Add("還款", typeof(decimal));
            dt.Columns.Add("貸款餘額", typeof(decimal));
            dt.Columns.Add("累計還款", typeof(decimal));
            dt.Columns.Add("累計利息", typeof(decimal));
            return dt;
        }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved