程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C#寫個人住房貸款計算器(2)

用C#寫個人住房貸款計算器(2)

編輯:關於C語言

你可以自行修改這個文件,以適應不同銀行的貸款利率。

這個文件由 Config.cs 文件中的 Config 類讀取:

using System;
using System.XML;
using System.Drawing;
using System.Collections.Generic;

namespace Skyiv.Ben.LoanCalculator
{
 sealed class Config
 {
  static readonly string ElmOption = "option";
  static readonly string ElmItems = "items";
  static readonly string AttrBalance = "balance";
  static readonly string AttrMonths = "months";
  static readonly string AttrDate = "date";
  static readonly string AttrMethod = "method";
  static readonly string AttrItem = "item";
  static readonly string AttrTitle = "title";
  static readonly string AttrLow = "low";
  static readonly string AttrHigh = "high";

  public decimal Balance { get; private set; } // 貸款金額(萬元)
  public int Months { get; private set; }   // 貸款期數(月)
  public DateTime Date { get; private set; }  // 貸款日期
  public bool IsEq { get; private set; }    // 還款方式: true:等本息 false: 等本金
  public string Item { get; private set; }   // 貸款種類
  public string[] Items { get; private set; } // 貸款種類列表
  public KeyValuePair<DateTime, PointF>[] Rates { get; private set; } // 貸款利率

  KeyValuePair<DateTime, PointF>[][] ratesArray; // 各種類的“貸款利率”列表

  public Config(string fileName)
  {
   try
   {
    var doc = new XMLDocument();
    doc.Load(fileName);
    var elm = doc.DocumentElement[ElmOption];
    if (elm == null) throw new Exception("未能找到 <" + ElmOption + "> 元素");
    Balance = GetDecimal(elm, AttrBalance);
    Months = GetInt32(elm, AttrMonths);
    Date = GetDateTime(elm, AttrDate);
    IsEq = GetBooleanFromMethod(elm, AttrMethod);
    Item = GetString(elm, AttrItem);
    Items = GetItemsAndLoadRatesArray(doc);
    SetRates(Item);
   }
   catch (Exception ex)
   {
    throw new Exception("讀配置文件(" + fileName + ")", ex);
   }
  }

  // 根據貸款種類設置貸款利率
  public void SetRates(string key)
  {
   var idx = Array.IndexOf(Items, key);
   if (idx < 0) throw new Exception("無此貸款種類: " + key);
   Rates = ratesArray[idx];
  }

  string[] GetItemsAndLoadRatesArray(XMLDocument doc)
  {
   var elm = doc.DocumentElement[ElmItems];
   if (elm == null) throw new Exception("未能找到 <" + ElmItems + "> 元素");
   var elms = elm.ChildNodes;
   var items = new string[elms.Count];
   ratesArray = new KeyValuePair<DateTime, PointF>[elms.Count][];
   for (var i = 0; i < elms.Count; i++)
   {
    items[i] = GetString(elms[i], AttrTitle);
    ratesArray[i] = GetRates(elms[i]);
   }
   return items;
  }

  KeyValuePair<DateTime, PointF>[] GetRates(XMLNode elm)
  {
   var elms = elm.ChildNodes;
   var rates = new KeyValuePair<DateTime, PointF>[elms.Count];
   for (var i = 0; i < elms.Count; i++)
    rates[i] = new KeyValuePair<DateTime, PointF>(GetDateTime(elms[i], AttrDate),
     new PointF(GetFloat(elms[i], AttrLow), GetFloat(elms[i], AttrHigh)));
   return rates;
  }

  string GetString(XMLNode elm, string key)
  {
   if (elm.Attributes[key] == null) throw new Exception("未能找到 <" + elm.Name + "> 元素的 " + key + " 屬性");
   return elm.Attributes[key].Value;
  }

  float GetFloat(XMLNode elm, string key)
  {
   float value;
   if (!float.TryParse(GetString(elm, key), out value))
    throw new Exception("<" + elm.Name + "> 元素的 " + key + " 屬性的值必須為浮點數");
   return value;
  }

  decimal GetDecimal(XMLNode elm, string key)
  {
   decimal value;
   if (!decimal.TryParse(GetString(elm, key), out value))
    throw new Exception("<" + elm.Name + "> 元素的 " + key + " 屬性的值必須為實數");
   return value;
  }

  int GetInt32(XMLNode elm, string key)
  {
   int value;
   if (!int.TryParse(GetString(elm, key), out value))
    throw new Exception("<" + elm.Name + "> 元素的 " + key + " 屬性的值必須為整數");
   return value;
  }

  DateTime GetDateTime(XMLNode elm, string key)
  {
   DateTime value;
   if (!DateTime.TryParseExact(GetString(elm, key), "yyyy-MM-dd", null, System.Globalization.DateTimeStyles.None, out value))
    throw new Exception("<" + elm.Name + "> 元素的 " + key + " 屬性的值必須為日期值");
   return value;
  }

  bool GetBooleanFromMethod(XMLNode elm, string key)
  {
   var value = GetString(elm, key);
   if (value == "等本息") return true;
   if (value == "等本金") return false;
   throw new Exception("<" + elm.Name + "> 元素的 " + key + " 屬性的值必須為“等本息”或者“等本金”");
  }
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved