close
Skip to content

Commit 6103f0b

Browse files
committed
Moved all budgeting month functions from ReconciliationIntro to new BudgetingMonthService class.
1 parent 7ab95f2 commit 6103f0b

8 files changed

Lines changed: 554 additions & 414 deletions

File tree

‎Console/Console.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
</ItemGroup>
4141
<ItemGroup>
4242
<Compile Include="Reconciliation\Files\ExpectedIncomeFile.cs" />
43+
<Compile Include="Reconciliation\Loaders\BudgetingMonthService.cs" />
4344
<Compile Include="Reconciliation\Loaders\DummyLoader.cs" />
4445
<Compile Include="Reconciliation\Loaders\FileLoader.cs" />
4546
<Compile Include="Reconciliation\ReconciliationType.cs" />
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
using System;
2+
using System.Globalization;
3+
using ConsoleCatchall.Console.Reconciliation.Spreadsheets;
4+
using Interfaces;
5+
using Interfaces.Constants;
6+
using Interfaces.DTOs;
7+
8+
namespace ConsoleCatchall.Console.Reconciliation.Loaders
9+
{
10+
internal class BudgetingMonthService
11+
{
12+
private readonly IInputOutput _input_output;
13+
14+
public BudgetingMonthService(IInputOutput input_output)
15+
{
16+
_input_output = input_output;
17+
}
18+
19+
public BudgetingMonths Recursively_ask_for_budgeting_months(ISpreadsheet spreadsheet)
20+
{
21+
DateTime next_unplanned_month = Get_next_unplanned_month(spreadsheet);
22+
int last_month_for_budget_planning = Get_last_month_for_budget_planning(spreadsheet, next_unplanned_month.Month);
23+
var budgeting_months = new BudgetingMonths
24+
{
25+
Next_unplanned_month = next_unplanned_month.Month,
26+
Last_month_for_budget_planning = last_month_for_budget_planning,
27+
Start_year = next_unplanned_month.Year
28+
};
29+
if (last_month_for_budget_planning != 0)
30+
{
31+
budgeting_months.Last_month_for_budget_planning = Confirm_budgeting_month_choices_with_user(budgeting_months, spreadsheet);
32+
}
33+
return budgeting_months;
34+
}
35+
36+
private DateTime Get_next_unplanned_month(ISpreadsheet spreadsheet)
37+
{
38+
DateTime default_month = DateTime.Today;
39+
DateTime next_unplanned_month = default_month;
40+
bool bad_input = false;
41+
try
42+
{
43+
next_unplanned_month = spreadsheet.Get_next_unplanned_month();
44+
}
45+
catch (Exception)
46+
{
47+
string new_month = _input_output.Get_input(ReconConsts.CantFindMortgageRow);
48+
try
49+
{
50+
if (!String.IsNullOrEmpty(new_month) && Char.IsDigit(new_month[0]))
51+
{
52+
int actual_month = Convert.ToInt32(new_month);
53+
if (actual_month < 1 || actual_month > 12)
54+
{
55+
bad_input = true;
56+
}
57+
else
58+
{
59+
var year = default_month.Year;
60+
if (actual_month < default_month.Month)
61+
{
62+
year++;
63+
}
64+
next_unplanned_month = new DateTime(year, actual_month, 1);
65+
}
66+
}
67+
else
68+
{
69+
bad_input = true;
70+
}
71+
}
72+
catch (Exception)
73+
{
74+
bad_input = true;
75+
}
76+
}
77+
78+
if (bad_input)
79+
{
80+
_input_output.Output_line(ReconConsts.DefaultUnplannedMonth);
81+
next_unplanned_month = default_month;
82+
}
83+
84+
return next_unplanned_month;
85+
}
86+
87+
private int Get_last_month_for_budget_planning(ISpreadsheet spreadsheet, int next_unplanned_month)
88+
{
89+
string next_unplanned_month_as_string = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(next_unplanned_month);
90+
var request_to_enter_month = String.Format(ReconConsts.EnterMonths, next_unplanned_month_as_string);
91+
string month = _input_output.Get_input(request_to_enter_month);
92+
int result = 0;
93+
94+
try
95+
{
96+
if (!String.IsNullOrEmpty(month) && Char.IsDigit(month[0]))
97+
{
98+
result = Convert.ToInt32(month);
99+
if (result < 1 || result > 12)
100+
{
101+
result = 0;
102+
}
103+
}
104+
}
105+
catch (Exception)
106+
{
107+
// Ignore it and return zero by default.
108+
}
109+
110+
result = Handle_zero_month_choice_result(result, spreadsheet, next_unplanned_month);
111+
return result;
112+
}
113+
114+
private int Confirm_budgeting_month_choices_with_user(BudgetingMonths budgeting_months, ISpreadsheet spreadsheet)
115+
{
116+
var new_result = budgeting_months.Last_month_for_budget_planning;
117+
string input = Get_response_to_budgeting_months_confirmation_message(budgeting_months);
118+
119+
if (!String.IsNullOrEmpty(input) && input.ToUpper() == "Y")
120+
{
121+
// I know this doesn't really do anything but I found the if statement easier to parse this way round.
122+
new_result = budgeting_months.Last_month_for_budget_planning;
123+
}
124+
else
125+
{
126+
// Recursion ftw!
127+
new_result = Get_last_month_for_budget_planning(spreadsheet, budgeting_months.Next_unplanned_month);
128+
}
129+
130+
return new_result;
131+
}
132+
133+
private string Get_response_to_budgeting_months_confirmation_message(BudgetingMonths budgeting_months)
134+
{
135+
string first_month = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(budgeting_months.Next_unplanned_month);
136+
string second_month = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(budgeting_months.Last_month_for_budget_planning);
137+
138+
int month_span = budgeting_months.Num_budgeting_months();
139+
140+
var confirmation_text = String.Format(ReconConsts.ConfirmMonthInterval, first_month, second_month, month_span);
141+
142+
return _input_output.Get_input(confirmation_text);
143+
}
144+
145+
private int Handle_zero_month_choice_result(int chosen_month, ISpreadsheet spreadsheet, int next_unplanned_month)
146+
{
147+
var new_result = chosen_month;
148+
if (chosen_month == 0)
149+
{
150+
var input = _input_output.Get_input(ReconConsts.ConfirmBadMonth);
151+
152+
if (!String.IsNullOrEmpty(input) && input.ToUpper() == "Y")
153+
{
154+
new_result = 0;
155+
_input_output.Output_line(ReconConsts.ConfirmNoMonthlyBudgeting);
156+
}
157+
else
158+
{
159+
// Recursion ftw!
160+
new_result = Get_last_month_for_budget_planning(spreadsheet, next_unplanned_month);
161+
}
162+
}
163+
return new_result;
164+
}
165+
}
166+
}

‎Console/Reconciliation/Loaders/FileLoader.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public ReconciliationInterface Load_specific_files_for_reconciliation_type(
4949
// WriteBackToMainSpreadsheet. Between now and then, everything is done using csv files.
5050
var spreadsheet_repo = _spreadsheet_factory.Create_spreadsheet_repo();
5151
var spreadsheet = new Spreadsheet(spreadsheet_repo);
52-
var reconciliation_intro = new ReconciliationIntro(_input_output);
53-
BudgetingMonths budgeting_months = reconciliation_intro.Recursively_ask_for_budgeting_months(spreadsheet);
52+
var budgeting_month_service = new BudgetingMonthService(_input_output);
53+
BudgetingMonths budgeting_months = budgeting_month_service.Recursively_ask_for_budgeting_months(spreadsheet);
5454

5555
switch (reconciliation_type)
5656
{

‎Console/Reconciliation/ReconciliationIntro.cs‎

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -509,156 +509,5 @@ public void Inject_spreadsheet_factory(ISpreadsheetRepoFactory spreadsheet_facto
509509
}
510510

511511
#endregion Debug Spreadsheet Operations
512-
513-
#region Get budgeting months
514-
515-
public BudgetingMonths Recursively_ask_for_budgeting_months(ISpreadsheet spreadsheet)
516-
{
517-
DateTime next_unplanned_month = Get_next_unplanned_month(spreadsheet);
518-
int last_month_for_budget_planning = Get_last_month_for_budget_planning(spreadsheet, next_unplanned_month.Month);
519-
var budgeting_months = new BudgetingMonths
520-
{
521-
Next_unplanned_month = next_unplanned_month.Month,
522-
Last_month_for_budget_planning = last_month_for_budget_planning,
523-
Start_year = next_unplanned_month.Year
524-
};
525-
if (last_month_for_budget_planning != 0)
526-
{
527-
budgeting_months.Last_month_for_budget_planning = Confirm_budgeting_month_choices_with_user(budgeting_months, spreadsheet);
528-
}
529-
return budgeting_months;
530-
}
531-
532-
private DateTime Get_next_unplanned_month(ISpreadsheet spreadsheet)
533-
{
534-
DateTime default_month = DateTime.Today;
535-
DateTime next_unplanned_month = default_month;
536-
bool bad_input = false;
537-
try
538-
{
539-
next_unplanned_month = spreadsheet.Get_next_unplanned_month();
540-
}
541-
catch (Exception)
542-
{
543-
string new_month = _input_output.Get_input(ReconConsts.CantFindMortgageRow);
544-
try
545-
{
546-
if (!String.IsNullOrEmpty(new_month) && Char.IsDigit(new_month[0]))
547-
{
548-
int actual_month = Convert.ToInt32(new_month);
549-
if (actual_month < 1 || actual_month > 12)
550-
{
551-
bad_input = true;
552-
}
553-
else
554-
{
555-
var year = default_month.Year;
556-
if (actual_month < default_month.Month)
557-
{
558-
year++;
559-
}
560-
next_unplanned_month = new DateTime(year, actual_month, 1);
561-
}
562-
}
563-
else
564-
{
565-
bad_input = true;
566-
}
567-
}
568-
catch (Exception)
569-
{
570-
bad_input = true;
571-
}
572-
}
573-
574-
if (bad_input)
575-
{
576-
_input_output.Output_line(ReconConsts.DefaultUnplannedMonth);
577-
next_unplanned_month = default_month;
578-
}
579-
580-
return next_unplanned_month;
581-
}
582-
583-
private int Get_last_month_for_budget_planning(ISpreadsheet spreadsheet, int next_unplanned_month)
584-
{
585-
string next_unplanned_month_as_string = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(next_unplanned_month);
586-
var request_to_enter_month = String.Format(ReconConsts.EnterMonths, next_unplanned_month_as_string);
587-
string month = _input_output.Get_input(request_to_enter_month);
588-
int result = 0;
589-
590-
try
591-
{
592-
if (!String.IsNullOrEmpty(month) && Char.IsDigit(month[0]))
593-
{
594-
result = Convert.ToInt32(month);
595-
if (result < 1 || result > 12)
596-
{
597-
result = 0;
598-
}
599-
}
600-
}
601-
catch (Exception)
602-
{
603-
// Ignore it and return zero by default.
604-
}
605-
606-
result = Handle_zero_month_choice_result(result, spreadsheet, next_unplanned_month);
607-
return result;
608-
}
609-
610-
private int Confirm_budgeting_month_choices_with_user(BudgetingMonths budgeting_months, ISpreadsheet spreadsheet)
611-
{
612-
var new_result = budgeting_months.Last_month_for_budget_planning;
613-
string input = Get_response_to_budgeting_months_confirmation_message(budgeting_months);
614-
615-
if (!String.IsNullOrEmpty(input) && input.ToUpper() == "Y")
616-
{
617-
// I know this doesn't really do anything but I found the if statement easier to parse this way round.
618-
new_result = budgeting_months.Last_month_for_budget_planning;
619-
}
620-
else
621-
{
622-
// Recursion ftw!
623-
new_result = Get_last_month_for_budget_planning(spreadsheet, budgeting_months.Next_unplanned_month);
624-
}
625-
626-
return new_result;
627-
}
628-
629-
private string Get_response_to_budgeting_months_confirmation_message(BudgetingMonths budgeting_months)
630-
{
631-
string first_month = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(budgeting_months.Next_unplanned_month);
632-
string second_month = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(budgeting_months.Last_month_for_budget_planning);
633-
634-
int month_span = budgeting_months.Num_budgeting_months();
635-
636-
var confirmation_text = String.Format(ReconConsts.ConfirmMonthInterval, first_month, second_month, month_span);
637-
638-
return _input_output.Get_input(confirmation_text);
639-
}
640-
641-
private int Handle_zero_month_choice_result(int chosen_month, ISpreadsheet spreadsheet, int next_unplanned_month)
642-
{
643-
var new_result = chosen_month;
644-
if (chosen_month == 0)
645-
{
646-
var input = _input_output.Get_input(ReconConsts.ConfirmBadMonth);
647-
648-
if (!String.IsNullOrEmpty(input) && input.ToUpper() == "Y")
649-
{
650-
new_result = 0;
651-
_input_output.Output_line(ReconConsts.ConfirmNoMonthlyBudgeting);
652-
}
653-
else
654-
{
655-
// Recursion ftw!
656-
new_result = Get_last_month_for_budget_planning(spreadsheet, next_unplanned_month);
657-
}
658-
}
659-
return new_result;
660-
}
661-
662-
#endregion Get budgeting months
663512
}
664513
}

‎ConsoleCatchallTests/ConsoleCatchallTests.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@
7676
<ItemGroup>
7777
<Compile Include="Reconciliation\Files\ExpectedIncomeFileTests.cs" />
7878
<Compile Include="Reconciliation\Loaders\FileLoaderTests.cs" />
79+
<Compile Include="Reconciliation\Loaders\BudgetingMonthServiceTests.cs" />
7980
<Compile Include="Reconciliation\Loaders\ReconciliationIntroTests.cs" />
81+
<Compile Include="Reconciliation\Loaders\BudgetingMonthServiceTestsSelfShunt.cs" />
8082
<Compile Include="Reconciliation\Loaders\ReconciliationIntroTestsSelfShunt.cs" />
8183
<Compile Include="Reconciliation\Matchers\BankAndBankInMatcherTests.cs" />
8284
<Compile Include="Reconciliation\Matchers\BankAndBankInMatcherTestsSelfShunt.cs" />

0 commit comments

Comments
 (0)