房贷计算器代码2.0

  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 }

    经过两三天的修改,代码进一步完善。但是鉴于自己水平有限,代码错误之处在所难免。不足之处,希望大家提出宝贵意见,使代码进一步完善。

猜你喜欢

转载自www.cnblogs.com/xiaochen0409/p/11741798.html
今日推荐