Advertisement
Guest User

KapitalRechner-Advanced

a guest
Mar 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Kapitalrechner_3
  12. {
  13.     public partial class Form_1 : Form
  14.     {
  15.         public Form_1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.  
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.             ClearAll(this);
  23.         }
  24.  
  25.         private void button_exit_Click(object sender, EventArgs e)
  26.         {
  27.             Environment.Exit(0);
  28.         }
  29.  
  30.         private void button_Clipboard_Click(object sender, EventArgs e)
  31.         {
  32.             string clip = "";
  33.             foreach (string line in listBox_out.Items)
  34.             {
  35.                 clip += line.Replace(" ", string.Empty);
  36.                 clip += Environment.NewLine;
  37.             }
  38.  
  39.             try
  40.             {
  41.                 Clipboard.SetText(clip);
  42.             }
  43.             catch
  44.             {
  45.                 label_info.Text = "Data is null";
  46.                 return;
  47.             }
  48.  
  49.             label_info.Text = "Copied";
  50.         }
  51.  
  52.         void ClearAll(Control con)
  53.         {
  54.             ClearList();
  55.  
  56.             foreach (Control c in con.Controls)
  57.             {
  58.                 if (c is TextBox)
  59.                     ((TextBox)c).Clear();
  60.                 else
  61.                     ClearAll(c);
  62.             }
  63.         }
  64.  
  65.         void ClearList()
  66.         {
  67.             listBox_out.Items.Clear();
  68.         }
  69.  
  70.         private void button_clearAll_Click(object sender, EventArgs e)
  71.         {
  72.             ClearAll(this);
  73.             textBox_k0.Focus();
  74.         }
  75.  
  76.         private void button_clearList_Click(object sender, EventArgs e)
  77.         {
  78.             ClearList();
  79.         }
  80.  
  81.         float GetDays(DateTime startD, DateTime endD)
  82.         {
  83.             float days = 0;
  84.  
  85.             days = (30 - startD.Date.Day) + 30 * ((endD.Date.Month + (endD.Date.Year - startD.Date.Year) * 12) - startD.Date.Month - 1) - 1 + endD.Date.Day;
  86.  
  87.             return days;
  88.         }
  89.  
  90.         float GetYears(DateTime startD, DateTime endD)
  91.         {
  92.             float years = 0;
  93.  
  94.             years = (endD.Date.Year - startD.Date.Year) - 1;
  95.             if (years < 0)
  96.             {
  97.                 years = 0;
  98.             }
  99.  
  100.  
  101.             Console.WriteLine("Years: " + years);
  102.             return years;
  103.         }
  104.  
  105.         float GetDaysFirst(DateTime startD, DateTime endD)
  106.         {
  107.             float days = 0;
  108.  
  109.             days = (30 - startD.Date.Day) + 30 * (12 - startD.Date.Month - 1);
  110.  
  111.             Console.WriteLine("First Days: " + days);
  112.             return days;
  113.         }
  114.  
  115.         float GetDaysLast(DateTime startD, DateTime endD)
  116.         {
  117.             float days = 0;
  118.  
  119.             days = 30 * endD.Date.Month + endD.Date.Day;
  120.  
  121.             Console.WriteLine("Last Days: " + days);
  122.             return days;
  123.         }
  124.  
  125.         private void button_go_Click(object sender, EventArgs e)
  126.         {
  127.             ClearList();
  128.  
  129.             button_go.Enabled = false;
  130.  
  131.             double k0 = 0;
  132.             double n = 0;
  133.             double p = 0;
  134.             double peff = 0;
  135.  
  136.             double pFormular = 0;
  137.  
  138.             DateTime startDate;
  139.             DateTime endDate;
  140.  
  141.             double kFirst = 0;
  142.             double kMiddle = 0;
  143.             double kLast = 0;
  144.  
  145.             double kAll = 0;
  146.  
  147.             try
  148.             {
  149.                 startDate = dateTimePicker_start.Value.Date;
  150.                 endDate = dateTimePicker_end.Value.Date;
  151.  
  152.                 k0 = Convert.ToDouble(textBox_k0.Text);
  153.                 p = Convert.ToDouble(textBox_p.Text);
  154.             }
  155.             catch
  156.             {
  157.                 label_info.Text = "Check your input";
  158.                 return;
  159.             }
  160.  
  161.             try
  162.             {
  163.                 n = Convert.ToDouble(textBox_n.Text);
  164.             }
  165.             catch
  166.             {
  167.                 try
  168.                 {
  169.                     n = Convert.ToDouble(new DataTable().Compute(textBox_n.Text, null));
  170.                 }
  171.                 catch
  172.                 {
  173.                     try
  174.                     {
  175.                         n = GetDays(startDate, endDate);
  176.                         textBox_n.Text = n + " / 360";
  177.                     }
  178.                     catch
  179.                     {
  180.                         label_info.Text = "Check your input";
  181.                         return;
  182.                     }
  183.                 }
  184.             }
  185.  
  186.             //Peff
  187.             peff = p * 0.75;
  188.             textBox_peff.Text = peff.ToString();
  189.  
  190.             //Check if Peff should be used
  191.             if (checkBox_usePeff.Checked)
  192.             {
  193.                 pFormular = peff / 100;
  194.             }
  195.             else
  196.             {
  197.                 pFormular = p / 100;
  198.             }
  199.  
  200.             //Display day difference
  201.             label_days.Text = n.ToString();
  202.  
  203.             //First Action:
  204.             kFirst = k0 * (1 + pFormular * (GetDaysFirst(startDate, endDate) / 360));
  205.  
  206.             //Second Action:
  207.             kMiddle = kFirst * Math.Pow(1 + pFormular, (GetYears(startDate, endDate)));
  208.  
  209.             //Last Action:
  210.             kLast = kMiddle * (1 + pFormular * (GetDaysLast(startDate, endDate) / 360));
  211.  
  212.             kAll = kFirst + kMiddle + kLast;
  213.  
  214.             if (startDate.Date.Year == endDate.Date.Year)
  215.             {
  216.                 kAll = k0 * (1 + pFormular * (GetDays(startDate, endDate) / 360));
  217.  
  218.                 kFirst = 0;
  219.                 kMiddle = 0;
  220.                 kLast = 0;
  221.             }
  222.  
  223.             //Display output
  224.             listBox_out.Items.Add("K" + startDate.Date.Year.ToString() + ":\t" + kFirst);
  225.             listBox_out.Items.Add("K" + (startDate.Date.Year + GetYears(startDate, endDate)) + ":\t" + kMiddle);
  226.             listBox_out.Items.Add("K" + endDate.Date.Year.ToString() + ":\t" + kLast);
  227.             listBox_out.Items.Add("Kall:\t" + kAll);
  228.  
  229.             button_go.Enabled = true;
  230.         }
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement