而 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;
}
}
}