經過兩三天的修改,代碼進一步完善。但是鑒於自己水平有限,代碼錯誤之處在所難免。不足之處,希望大家提出寶貴意見,使代碼進一步完善。 ...
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Drawing; 7 using System.Reflection; 8 using System.Net.NetworkInformation; 9 10 namespace play 11 { 12 class Program 13 { 14 //短期利率與長期利率 15 double lowRate = 0; 16 double highRate = 0; 17 18 //KeyValuePair<DateTime, >; 19 DateTime dt1 = new DateTime(2015, 3, 1); 20 DateTime dt2 = new DateTime(2015, 5, 11); 21 DateTime dt3 = new DateTime(2015, 6, 28); 22 DateTime dt4 = new DateTime(2015, 8, 26); 23 //貸款金額 24 static double totalMoney, balance; 25 26 static string choice; 27 static string newChoice; 28 //期數 29 int n; 30 //月供 31 double payMonth; 32 double rate; 33 double interest; 34 /// <summary> 35 /// 用來構建歡迎界面 36 /// </summary> 37 void Paint() 38 { 39 Console.WriteLine("***************************"); 40 Console.WriteLine("***************************"); 41 Console.WriteLine("***************************"); 42 Console.WriteLine("歡迎使用房貸計算器"); 43 Console.WriteLine("***************************"); 44 Console.WriteLine("***************************"); 45 Console.WriteLine("***************************"); 46 47 Console.WriteLine("請按任意鍵繼續"); 48 Console.ReadKey(); 49 Console.Clear(); 50 } 51 /// <summary> 52 /// 按日期選擇利率 53 /// </summary> 54 /// <param name="dt">輸入的日期</param> 55 void RateChoose(DateTime dt) 56 { 57 if (dt >= dt4) 58 { 59 lowRate = 0.0275; 60 highRate = 0.0325; 61 } 62 else if (dt >= dt3) 63 { 64 lowRate = 0.03; 65 highRate = 0.035; 66 } 67 else if (dt >= dt2) 68 { 69 lowRate = 0.0325; 70 highRate = 0.0375; 71 } 72 else if (dt >= dt1) 73 { 74 lowRate = 0.035; 75 highRate = 0.04; 76 } 77 else 78 { 79 Console.WriteLine("你輸入的日期不在範圍內"); 80 Console.ReadKey(); 81 Environment.Exit(0); 82 } 83 } 84 /// <summary> 85 /// 計算月供 86 /// </summary> 87 void Payment(DateTime dt) 88 { 89 //設置緩衝區高度,即設置顯示的最大行數 90 Console.BufferHeight = 1000; 91 RateChoose(dt); 92 switch (choice) 93 { 94 case "1": 95 { 96 //等額本息,月供計算方法 97 for (n = 1; n <= 360; n++) 98 { 99 100 101 if (n <= 60) 102 { 103 rate = lowRate; 104 } 105 else 106 { 107 rate = highRate; 108 } 109 //計算月供 110 double p; 111 p = rate / 12; 112 payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1)); 113 //利息 114 interest = n * payMonth; 115 if (n % 2 == 0) 116 { 117 Console.ForegroundColor = ConsoleColor.Blue; 118 } 119 else 120 { 121 Console.ForegroundColor = ConsoleColor.White; 122 } 123 Console.WriteLine("{0}期按揭貸款的月供是{1:0.00}元,總利息是{2:0.00}元", n, payMonth, interest); 124 125 } 126 } 127 break; 128 case "2": 129 { 130 //等額本金,月供計算方法 131 for (n = 1; n <= 360; n++) 132 { 133 if (n <= 60) 134 { 135 rate = lowRate; 136 } 137 else 138 { 139 rate = highRate; 140 } 141 //計算月供 142 double p; 143 p = rate / 12; 144 payMonth = (totalMoney / n) + totalMoney * p; 145 interest = ((totalMoney / n + totalMoney * p) + totalMoney / n * (1 + p)) / 2 * n - totalMoney; 146 Console.WriteLine("{0}期按揭貸款的首月還款額是{1:0.00}元,總利息是{2:0.00}元", n, payMonth, interest); 147 } 148 } 149 break; 150 } 151 } 152 153 /// <summary> 154 /// 月貸計算方法 155 /// </summary> 156 /// <param name="n">還款期數</param> 157 void Compute(int n) 158 { 159 Console.WriteLine("期次\t\t償還利息\t\t償還本金\t\t還款額\t\t剩餘本金"); 160 switch (choice) 161 { 162 163 case "1": 164 //等額本息法計算房貸 165 { 166 if (n <= 60) 167 { 168 rate = lowRate; 169 } 170 else 171 { 172 rate = highRate; 173 } 174 175 double p; 176 p = rate / 12; 177 payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1)); 178 for (int i = 1; i <= n; i++) 179 { 180 //string s = i.ToString(); 181 interest = totalMoney * p; 182 double capital = payMonth - interest; 183 totalMoney -= capital; 184 Console.WriteLine("{0}\t\t{1:0.00}\t\t{2:0.00}\t{3:0.00}\t{4:0.00}", i, interest, capital, payMonth, totalMoney); 185 } 186 } 187 break; 188 case "2": 189 //等額本金法計算房貸 190 { 191 if (n <= 60) 192 { 193 rate = lowRate; 194 } 195 else 196 { 197 rate = highRate; 198 } 199 double p; 200 p = rate / 12; 201 double capital = totalMoney / n; 202 for (int i = 1; i <= n; i++) 203 { 204 205 interest = totalMoney * p; 206 payMonth = capital + interest; 207 totalMoney -= capital; 208 209 Console.WriteLine("{0}\t\t{1:0.00}\t\t\t{2:0.00}\t\t{3:0.00}\t{4:0.00}", i, interest, capital, payMonth, totalMoney); 210 } 211 } 212 break; 213 214 } 215 } 216 /// <summary> 217 /// 計算還款狀況 218 /// </summary> 219 /// <param name="n0">還款期數</param> 220 /// <param name="dt">首次還款時間</param> 221 void Repayment(int n0, DateTime dt) 222 { 223 balance = totalMoney; 224 DateTime dt0 = dt; 225 double rate0; 226 double capital; 227 double p; 228 Console.WriteLine("還款時間\t\t償還利息\t\t償還本金\t\t還款額\t\t剩餘本金"); 229 switch (choice) 230 { 231 case "1": 232 { 233 234 RateChoose(dt0); 235 if (n0 <= 60) 236 { 237 rate0 = lowRate; 238 } 239 else 240 { 241 rate0 = highRate; 242 } 243 n = n0; 244 for (int i = 1; i <=n0; i++) 245 { 246 RateChoose(dt); 247 rate = (n0 <= 60 ? lowRate : highRate); 248 249 if (dt.Year != dt0.Year && rate != rate0)//這裡應該有問題? 250 { 251 double p0 = rate0 / 12; 252 double payMonth0= (p0 * totalMoney * Math.Pow((1 + p0), n)) / ((Math.Pow(1 + p0, n) - 1)); 253 double interest0 = balance * p0; 254 double capital0 = payMonth0 - interest0; 255 256 rate0 = rate; 257 258 interest = balance * (rate0/12); 259 totalMoney = balance; 260 balance -= capital0; 261 262 payMonth = capital0 + interest; 263 n =n0 - i+1; 264 Console.WriteLine("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital0, payMonth, balance); 265 dt = dt.AddMonths(1); 266 continue; 267 } 268 269 270 271 p = rate0 / 12; 272 payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1)); 273 274 interest = balance * p; 275 capital = payMonth - interest; 276 balance -= capital; 277 Console.WriteLine("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital, payMonth, balance); 278 dt = dt.AddMonths(1); 279 280 } 281 282 } 283 break; 284 case "2": 285 { 286 RateChoose(dt0); 287 rate0 = (n0 <= 60 ? lowRate : highRate); 288 n = n0; 289 for (int i = 0; i < n0; i++) 290 { 291 RateChoose(dt); 292 rate = (n0 <= 60 ? lowRate : highRate); 293 if (dt.Year!=dt0.Year&&rate!=rate0) 294 { 295 rate0 = rate; 296 } 297 p = rate0 / 12; 298 capital = totalMoney / n0; 299 interest = balance * p; 300 payMonth = capital + interest; 301 balance -= capital; 302 Console.WriteLine("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt, interest, capital, payMonth, balance); 303 dt = dt.AddMonths(1); 304 } 305 } 306 break; 307 } 308 } 309 /// <summary> 310 ///實現提前還款功能 311 /// </summary> 312 /// <param name="n">還款總期數</param> 313 /// <param name="dt1">首次還款時間</param> 314 /// <param name="dt2">提前還款時間</param> 315 void Prepayment(int n0,DateTime dt1,DateTime dt2,double money) 316 { 317 balance = totalMoney; 318 DateTime dt0 = dt1; 319 double rate0; 320 double capital; 321 double p; 322 bool flag = false; 323 Console.WriteLine("還款時間\t\t償還利息\t\t償還本金\t\t還款額\t\t剩餘本金"); 324 switch (choice) 325 { 326 case "1": 327 { 328 RateChoose(dt0); 329 rate0 = (n0 <= 60 ? lowRate : highRate); 330 n = n0; 331 for(int i=1;i<=n0;i++) 332 { 333 RateChoose(dt1); 334 rate = (n0 <= 60 ? lowRate : highRate); 335 336 337 if (dt1.Year != dt0.Year && rate != rate0)//這裡應該有問題? 338 { 339 double p0 = rate0 / 12; 340 double payMonth0 = (p0 * totalMoney * Math.Pow((1 + p0), n)) / ((Math.Pow(1 + p0, n) - 1)); 341 double interest0 = balance * p0; 342 double capital0 = payMonth0 - interest0; 343 344 rate0 = rate; 345 346 interest = balance * (rate0 / 12); 347 totalMoney = balance; 348 balance -= capital0; 349 350 payMonth = capital0 + interest; 351 n = n0 - i + 1; 352 Console.WriteLine("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt1, interest, capital0, payMonth, balance); 353 dt1 = dt1.AddMonths(1); 354 continue; 355 } 356 357 358 359 p = rate0 / 12; 360 if (dt2.Year == dt1.Year && dt2.Month == dt1.Month) 361 { 362 balance -= money; 363 totalMoney = balance; 364 switch (newChoice) 365 { 366 case "1": 367 { 368 369 n = n0 - i + 1; 370 flag = true; 371 } 372 break; 373 case "2": 374 { 375 double temp = Math.Log((payMonth / (payMonth - totalMoney * p)), (1 + p)); 376 n= (int)Math.Ceiling(temp); 377 n0 = n + i - 1; 378 } 379 break; 380 } 381 382 } 383 payMonth = (p * totalMoney * Math.Pow((1 + p), n)) / ((Math.Pow(1 + p, n) - 1)); 384 385 386 interest = balance * p; 387 capital = payMonth - interest; 388 balance -= capital; 389 if (flag) 390 { 391 payMonth += money; 392 capital += money; 393 flag = false; 394 } 395 Console.WriteLine("{0}\t{1:0.00}\t\t\t{2:0.00}\t\t\t{3:0.00}\t\t{4:0.00}", dt1, interest, capital, payMonth, balance); 396 dt1 = dt1.AddMonths(1); 397 } 398 } 399 break; 400 case "2": 401 { 402 403 } 404 break; 405 } 406 } 407 static void Main(string[] args) 408 { 409 410 { 411 Program p = new Program(); 412 p.Paint(); 413 Console.WriteLine("請選擇您的操作!1.房貸計算 2.月供速算 3.還款狀況 4.提前還款"); 414 Console.WriteLine("請選擇您的還款方式?1.等額本息 2.等額本金"); 415 choice = Console.ReadLine(); 416 Console.WriteLine("請輸入你的貸款金額"); 417 totalMoney = Convert.ToDouble(Console.ReadLine()); 418 Console.WriteLine("請輸入您貸款的期數"); 419 int number = Convert.ToInt32(Console.ReadLine()); 420 Console.WriteLine("請輸入你的首次貸款時間"); 421 DateTime dT = Convert.ToDateTime(Console.ReadLine()); 422 Console.WriteLine("請輸入你的提前還款時間"); 423 DateTime dTR = Convert.ToDateTime(Console.ReadLine()); 424 Console.WriteLine("請輸入你的還款金額"); 425 double partMoney = Convert.ToDouble(Console.ReadLine()); 426 Console.WriteLine("請選擇你要的後續操作!1.減少月供 2.縮短年限"); 427 newChoice = Console.ReadLine(); 428 //Console.WriteLine("你輸入的日期是{0},短期利率是{1},長期利率是{2}",dT,p.lowRate,p.highRate); 429 //dT= dT.AddMonths(1); 430 // Console.WriteLine(dT); 431 // p.Repayment(number, dT); 432 // p.Compute(number); 433 // p.Payment(dTR); 434 p.Prepayment(number, dT, dTR, partMoney); 435 Console.ReadKey(); 436 } 437 } 438 } 439 }
經過兩三天的修改,代碼進一步完善。但是鑒於自己水平有限,代碼錯誤之處在所難免。不足之處,希望大家提出寶貴意見,使代碼進一步完善。