A cleaner way to approach the given output

32 Views Asked by At

monthlyArrearWithRemainder

output

I have managed to code the output in the image. But I want to know the other way to do it or a cleaner way.

My code is like this

if (number > 0)
 {
     dateEnd = dateStart.AddMonths((int)-(monthsCount + 1));
 }
 else
 {
     dateEnd = dateStart.AddMonths((int)-monthsCount);
 }
 while(dateEnd < dateStart)
 {
     dateEnd = dateEnd.AddMonths(1);               
     monthsArrears.Add(string.Format("{0:C}", Convert.ToDouble(moneyRent)) + " " + dateEnd.ToString("MMMM yyyy") + "\n");             
 }
 if (number > 0)
 {
     var leastMonthArrear = string.Format("{0:C}", Convert.ToDouble(number)) + " " + dateEnd.AddMonths(-(int)monthsCount).ToString("MMMM yyyy") + Environment.NewLine;
     var removed = monthsArrears.Remove(monthsArrears.FirstOrDefault());
     var monthArrear = string.Join(Environment.NewLine, monthsArrears);
     var insert = monthArrear.Insert(0, leastMonthArrear + "\n");
     MessageBox.Show(insert.Replace(" ", " - "));
 }
 else
 {
     var monthArrear = string.Join(Environment.NewLine, monthsArrears);
     MessageBox.Show(monthArrear.Replace(" ", " - "));
 }

What I want to do is if user put the money owed, the system will show the month arrears along with monthly rent and put it in the database. And if there is a remainder from the money owed it will be on the least arrear month.

Thank you.

0

There are 0 best solutions below