-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
1347 lines (1214 loc) · 52 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
1347 lines (1214 loc) · 52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace AcctISGenerator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
//changed to a class from struct because we want shallow copies
public class AccountVal
{
public readonly int Amount;
public bool Visiting, //this value is used mainly when setting needsSolving
NeedsSolving, //as in, this value was not given in the initial problem
SolvingSet; //this value is used when setting needsSolving
public readonly List<AccountVal[]> Substitutes;
public string Name { get; set; }
public AccountVal()
{
Name = "Not set";
Substitutes = new List<AccountVal[]>(3);
Amount = 0;
SolvingSet = _isSolvableVisiting = Visiting = false;
NeedsSolving = true; //defaulting to true because I'm lazy
}
public AccountVal(int amount)
{
Name = "Not set";
Substitutes = new List<AccountVal[]>(3); //did 3 because i am lazy
this.Amount = amount;
SolvingSet = NeedsSolving = false; //defaulting
_isSolvableVisiting = Visiting = false;
}
public AccountVal(int amount, bool needsSolving)
{
Name = "Not set";
Substitutes = new List<AccountVal[]>(3);
this.Amount = amount;
SolvingSet = false;
this.NeedsSolving = needsSolving;
_isSolvableVisiting = Visiting = false;
}
private bool _isSolvableVisiting;
public bool IsSolvableRecursive()
{
_isSolvableVisiting = true;
if (!NeedsSolving)
{
_isSolvableVisiting = false;
return true;
}
//iterate through substitutes
/*//mmm it went past the forbidden line in my ide so i split it up a bit
if ((from substituteGroup in substitutes
where !substituteGroup.Any(x => x._isSolvableVisiting)
select substituteGroup.All(
t => t is not null && t.IsSolvableRecursive())).Any(groupSolvable => groupSolvable))
{
_isSolvableVisiting = false;
return true;
}*/
if (Substitutes.Any(substituteGroup => substituteGroup.Where(substitute => !substitute._isSolvableVisiting)
.All(substitute => substitute.IsSolvableRecursive())))
{
_isSolvableVisiting = false;
return true;
}
_isSolvableVisiting = false;
return false;
}
/*Only returns an expected value is SolvingSet is true*/
public bool IsSolvable()
{
//either it doesn't need solving and the value has been set, or there is a substitute group where all values do not need to be solved for and the value has been properly set
return (!this.NeedsSolving && this.SolvingSet) ||
Substitutes.Any(subGroup =>
subGroup.All(acct => (!acct.NeedsSolving && acct.SolvingSet) || acct.UsableSubstitute()));
}
public bool UsableSubstitute() //Essentially IsSolvableRecursive, but it doesn't check itself for anything
{
_isSolvableVisiting = true;
/*if ((from substituteGroup in substitutes
where substituteGroup.Any(x => !x._isSolvableVisiting) //where none of them are currently being checked in the recursive functions
select substituteGroup.All(//select the groups where the substitute is not null and it is solvable recursively
t => t is not null && t.IsSolvableRecursive())).Any(groupSolvable => groupSolvable))
{
_isSolvableVisiting = false;
return true;
}*/
if (Substitutes.Any( //select the substitute groups that
substituteGroup => substituteGroup
.Where( //do not have a variable that is currently being visited by the recursive function calls
substitute => !substitute._isSolvableVisiting)
.All( //and then check if that group is solvable recursively
substitute => substitute.IsSolvableRecursive())
))
_isSolvableVisiting = false;
return false;
}
//the stupid operator overloads. this takes up too much space
//we are not overloading the bitshift operators (there is literally no need for something like this)
//it was taking up too much space and was a pain to scroll through so i removed some of the line breaks
//this was 90+ lines before...
public static AccountVal operator +(AccountVal a, AccountVal b)
{
return new AccountVal(a.Amount + b.Amount);
}
public static AccountVal operator -(AccountVal a, AccountVal b)
{
return new AccountVal(a.Amount - b.Amount);
}
public static AccountVal operator /(AccountVal a, AccountVal b)
{
return new AccountVal(a.Amount / b.Amount);
}
public static AccountVal operator *(AccountVal a, AccountVal b)
{
return new AccountVal(a.Amount * b.Amount);
}
public static AccountVal operator %(AccountVal a, AccountVal b)
{
return new AccountVal(a.Amount % b.Amount);
}
public static AccountVal operator +(AccountVal a, int b)
{
return new AccountVal(a.Amount + b);
}
public static AccountVal operator -(AccountVal a, int b)
{
return new AccountVal(a.Amount - b);
}
public static AccountVal operator /(AccountVal a, int b)
{
return new AccountVal(a.Amount / b);
}
public static AccountVal operator *(AccountVal a, int b)
{
return new AccountVal(a.Amount * b);
}
public static AccountVal operator %(AccountVal a, int b)
{
return new AccountVal(a.Amount % b);
}
public static AccountVal operator +(int b, AccountVal a)
{
return new AccountVal(a.Amount + b);
}
public static AccountVal operator -(int b, AccountVal a)
{
return new AccountVal(b - a.Amount);
}
public static AccountVal operator /(int b, AccountVal a)
{
return new AccountVal(b / a.Amount);
}
public static AccountVal operator *(int b, AccountVal a)
{
return new AccountVal(a.Amount * b);
}
public static AccountVal operator %(int b, AccountVal a)
{
return new AccountVal(b % a.Amount);
}
public static bool operator ==(AccountVal a, AccountVal b)
{
return b != null && a != null && a.Amount == b.Amount;
}
public static bool operator !=(AccountVal a, AccountVal b)
{
return a != null && b != null && a.Amount != b.Amount;
}
public static bool operator ==(AccountVal a, int b)
{
return a != null && a.Amount == b;
}
public static bool operator !=(AccountVal a, int b)
{
return a != null && a.Amount != b;
}
public static bool operator !=(int b, AccountVal a)
{
return a != null && a.Amount != b;
}
public static bool operator ==(int b, AccountVal a)
{
return a != null && a.Amount == b;
}
public static bool operator >=(AccountVal a, AccountVal b)
{
return a.Amount >= b.Amount;
}
public static bool operator <=(AccountVal a, AccountVal b)
{
return a.Amount <= b.Amount;
}
public static bool operator >=(AccountVal a, int b)
{
return a.Amount >= b;
}
public static bool operator <=(AccountVal a, int b)
{
return a.Amount <= b;
}
public static bool operator >=(int b, AccountVal a)
{
return a.Amount >= b;
}
public static bool operator <=(int b, AccountVal a)
{
return a.Amount <= b;
}
public static bool operator >(AccountVal a, AccountVal b)
{
return a.Amount > b.Amount;
}
public static bool operator <(AccountVal a, AccountVal b)
{
return a.Amount < b.Amount;
}
public static bool operator >(AccountVal a, int b)
{
return a.Amount > b;
}
public static bool operator <(AccountVal a, int b)
{
return a.Amount < b;
}
public static bool operator >(int b, AccountVal a)
{
return b > a.Amount;
}
public static bool operator <(int b, AccountVal a)
{
return b < a.Amount;
}
public override bool Equals(object o)
{
AccountVal test = o as AccountVal;
if (test is null)
return false;
return
test.Amount == Amount && test.Name == Name; //not sure if i want this to also check the substitutes...
}
public override int GetHashCode()
{
return (Amount + Substitutes.GetHashCode()) * 7;
}
public override string ToString()
{
return $"{Name}: {Amount:C0}";
}
}
public class RandomDateTime //from stackov erflow.com/a/ 262 63669
//without the spaces because i don't like search engine indexing
{
readonly DateTime _start;
readonly Random _gen;
private readonly int _range;
public RandomDateTime()
{
_start = new DateTime(1995, 1, 1);
_gen = new Random();
_range = (DateTime.Today - _start).Days;
}
public DateTime Next()
{
return _start.AddDays(_gen.Next(_range)).AddHours(_gen.Next(0, 24)).AddMinutes(_gen.Next(0, 60))
.AddSeconds(_gen.Next(0, 60));
}
}
public partial class MainWindow
{
private bool _showAnswerFlag;
private bool _restartFlag;
private bool _backToMainFlag;
private bool _questionsCompleteFlag;
private bool _isIncomeStatementProblem;
private int _amountSolved;
//there are 15 AccountVal in this list
private AccountVal _beginningInventory,
_costOfDeliveredMerchandise,
_costOfMerchandiseSold,
_costOfMerchandiseAvaForSale,
_endingInventory,
_grossProfit,
_netPurchases,
_netSales,
_purchases,
_purchasesDiscounts,
_purchasesRetAndAllow,
_sales,
_salesDiscounts,
_salesRetAndAllow,
_transportationIn;
private AccountVal[] _accounts;
private readonly List<AccountVal> _givenAccounts;
private HelpWindow _helpWindow;
private readonly Dictionary<Button, TextBox> _submitButtonToInputBox;
//input text box to AccountVal
private readonly Dictionary<TextBox, AccountVal> _notGivenAccts;
private readonly Dictionary<TextBox, TextBox> _inputToWarningBox;
private readonly List<TextBox> _inputTextBoxes;
private readonly Dictionary<AccountVal, TextBox> _ISAcctToTB;
//constant reference means we don't need the resizing capabilities of List
private readonly TextBox[] _incomeStatementValueTextBoxes;
private DateTime _fiscalYearStart, _fiscalYearEnd;
private readonly RandomDateTime _randomDateTime;
private void InitializeAccounts()
{
//sets accounts' values to a random amount using RandomNumberGenerator
//Sales must be at least 3/4 of Purchases to prevent an extremely low and unrealistic gross profit
_transportationIn = new AccountVal(500 + RandomNumberGenerator.GetInt32(500, 10000), true);
_purchasesRetAndAllow = new AccountVal(RandomNumberGenerator.GetInt32(300, 8700), true);
_purchasesDiscounts = new AccountVal(RandomNumberGenerator.GetInt32(300, 16500), true);
_salesRetAndAllow = new AccountVal(RandomNumberGenerator.GetInt32(300, 14750), true);
_salesDiscounts = new AccountVal(RandomNumberGenerator.GetInt32(300, 13440), true);
_beginningInventory = new AccountVal(RandomNumberGenerator.GetInt32(10000, 30000), true);
_endingInventory = new AccountVal(RandomNumberGenerator.GetInt32(10000, 30000), true);
_purchases = new AccountVal(RandomNumberGenerator.GetInt32(80000, 300000), true);
_sales = new AccountVal(RandomNumberGenerator.GetInt32(_purchases.Amount * 3 / 4, 375000), true);
//these AccountVals are based on the previous set values
_costOfDeliveredMerchandise = _purchases + _transportationIn;
_netPurchases = _costOfDeliveredMerchandise - _purchasesRetAndAllow - _purchasesDiscounts;
_netSales = _sales - _salesDiscounts - _salesRetAndAllow;
_costOfMerchandiseAvaForSale = _beginningInventory + _netPurchases;
_costOfMerchandiseSold = _costOfMerchandiseAvaForSale - _endingInventory;
_grossProfit = _netSales - _costOfMerchandiseSold;
//setting up substitutes
_purchases.Substitutes.Add(new[] { _transportationIn, _costOfDeliveredMerchandise });
_transportationIn.Substitutes.Add(new[] { _purchases, _costOfDeliveredMerchandise });
_costOfDeliveredMerchandise.Substitutes.Add(new[] { _transportationIn, _purchases });
_costOfDeliveredMerchandise.Substitutes.Add(new[] { _purchasesRetAndAllow, _purchasesDiscounts, _netPurchases });
_purchasesRetAndAllow.Substitutes.Add(new[] { _costOfDeliveredMerchandise, _purchasesDiscounts, _netPurchases });
_purchasesDiscounts.Substitutes.Add(new[] { _costOfDeliveredMerchandise, _purchasesRetAndAllow, _netPurchases });
_netPurchases.Substitutes.Add(new[] { _costOfDeliveredMerchandise, _purchasesRetAndAllow, _purchasesDiscounts });
_netPurchases.Substitutes.Add(new[] { _beginningInventory, _costOfMerchandiseAvaForSale });
_beginningInventory.Substitutes.Add(new[] { _netPurchases, _costOfMerchandiseAvaForSale });
_costOfMerchandiseAvaForSale.Substitutes.Add(new[] { _beginningInventory, _netPurchases });
_costOfMerchandiseAvaForSale.Substitutes.Add(new[] { _endingInventory, _costOfMerchandiseSold });
_endingInventory.Substitutes.Add(new[] { _costOfMerchandiseAvaForSale, _costOfMerchandiseSold });
_costOfMerchandiseSold.Substitutes.Add(new[] { _costOfMerchandiseAvaForSale, _endingInventory });
_costOfMerchandiseSold.Substitutes.Add(new[] { _netSales, _grossProfit });
_sales.Substitutes.Add(new[] { _salesRetAndAllow, _salesDiscounts, _netSales });
_salesRetAndAllow.Substitutes.Add(new[] { _sales, _salesDiscounts, _netSales });
_salesDiscounts.Substitutes.Add(new[] { _sales, _salesRetAndAllow, _netSales });
_netSales.Substitutes.Add(new[] { _sales, _salesRetAndAllow, _salesDiscounts });
_netSales.Substitutes.Add(new[] { _costOfMerchandiseSold, _grossProfit });
_grossProfit.Substitutes.Add(new[] { _netSales, _costOfMerchandiseSold });
//setting the name of each AccountVal
_beginningInventory.Name = "Beginning Inventory";
_costOfDeliveredMerchandise.Name = "Cost Of Delivered Merchandise";
_costOfMerchandiseSold.Name = "Cost Of Merchandise Sold";
_costOfMerchandiseAvaForSale.Name = "Cost Of Merchandise Available For Sale";
_endingInventory.Name = "Ending Inventory";
_grossProfit.Name = "Gross Profit";
_netPurchases.Name = "Net Purchases";
_netSales.Name = "Net Sales";
_purchases.Name = "Purchases";
_purchasesDiscounts.Name = "Purchases Discounts";
_purchasesRetAndAllow.Name = "Purchases Returns and Allowances";
_sales.Name = "Sales";
_salesDiscounts.Name = "Sales Discounts";
_salesRetAndAllow.Name = "Sales Returns and Allowances";
_transportationIn.Name = "Transportation In";
//Add them to the list containing all of the AccountVals
_accounts = new[]
{
_beginningInventory, _costOfDeliveredMerchandise, _costOfMerchandiseSold,
_costOfMerchandiseAvaForSale, _endingInventory, _grossProfit, _netPurchases, _netSales, _purchases,
_purchasesDiscounts, _purchasesRetAndAllow, _sales, _salesDiscounts, _salesRetAndAllow,
_transportationIn
};
//priming read to ensure at least one account needs to be solved for
List<AccountVal> tempArr = _accounts.ToList();
int tempRandIndex = RandomNumberGenerator.GetInt32(0, tempArr.Count);
AccountVal tempRand = tempArr[tempRandIndex];
tempRand.SolvingSet = true;
tempArr.RemoveAt(tempRandIndex);
while (tempArr.Count > 0)
{
int rand = RandomNumberGenerator.GetInt32(0, tempArr.Count);
if (!tempArr[rand].SolvingSet)
{
SetSolveStatesAlternative(tempArr[rand]);
}
tempArr.RemoveAt(rand);
}
}
//returns true if the account needs to be solved for, false if it does not
//assumes all NeedsSolving values are initially false
[Obsolete(
"Consider using setSolveStatesAlternative(AccountVal). This method assumes that initial value of NeedsSolving is false.")]
private bool SetSolveStates(AccountVal a)
{
if (a.SolvingSet)
return a.NeedsSolving;
a.Visiting = true;
//method may not function with this commented out
//randomize this because otherwise it results in the same accounts being set to needs solving
if (RandomNumberGenerator.GetInt32(1,4)==2) //1/3 chance to set the account value as given
{
//this value does not need to be solved for and will be given in the problem
a.NeedsSolving = false;
a.SolvingSet = true;
a.Visiting = false;
return true;
}
//check if it can be set to unsolved state
foreach (var substituteArr in a.Substitutes)
{
//check to prevent infinite recursions
if (substituteArr.Any(accountVal => accountVal.Visiting))
continue;
//if any substitute is not solvable check the next substitute group
if (substituteArr.Any(account => !account.IsSolvable())) continue;
a.NeedsSolving = true;
a.SolvingSet = true;
a.Visiting = false;
return false;
}
//reaching this point means the program could not find a suitable substitute with the given substitutes
a.NeedsSolving = false;
a.SolvingSet = true;
a.Visiting = false;
return true;
}
//assumes all NeedsSolving values are initially true
//results in 5-6 questions
private void SetSolveStatesAlternative(AccountVal a)
{
if (a.SolvingSet) return;
a.Visiting = true;
//check if it can be set to unsolved state
if (a.Substitutes.Any(substituteArr => substituteArr.All(substitute => substitute.IsSolvable())))
{
a.NeedsSolving = true;
a.SolvingSet = true;
a.Visiting = false;
return;
}
//reaching this point means the program could not find a suitable substitute with the given substitutes
a.NeedsSolving = false;
a.SolvingSet = true;
a.Visiting = false;
}
public MainWindow()
{
//with 15 accounts, and 5-6 almost always being in the questions, this only needs 11 slots
_givenAccounts = new List<AccountVal>(11);
_submitButtonToInputBox = new Dictionary<Button, TextBox>(6);
_notGivenAccts = new Dictionary<TextBox, AccountVal>(6);
_inputToWarningBox = new Dictionary<TextBox, TextBox>(6);
_cancellationTokens = new Dictionary<TextBox, CancellationTokenSource>(6);
_warningCTS = new Dictionary<TextBox, CancellationTokenSource>(6);
_inputTextBoxes = new List<TextBox>(6);
_ISAcctToTB = new Dictionary<AccountVal, TextBox>(15);
_randomDateTime = new RandomDateTime();
InitializeComponent();
_incomeStatementValueTextBoxes = new[]
{
BeginInventoryISValueTextBox, CoDMISValueTextBox, COMSISValueTextBox, COMAFSISValueTextBox,
EndingInventoryISValueTextBox, GrossProfitISValueTextBox, NetPurchasesISValueTextBox, NetSalesISValueTextBox,
PurchasesISValueTextBox, PurchasesDiscountsISValueTextBox, PurchasesReturnsISValueTextBox, SalesISValueTextBox,
SalesDiscountsISValueTextBox, SalesReturnsISValueTextBox, TransportationInISValueTextBox
};
StartButton.Click += delegate {OpenSelectModeMenu();};
ExitButton.Click += delegate {Application.Current.Shutdown();};
ShowAnswersButton.Click += ShowAnswersButtonPressed;
HelpButton.Click += HelpButtonPressed;
FunctionHelpButton.Click += HelpButtonPressed;
BackButton.Click += QuitButtonPressed;
RestartQuitGridBackButton.Click += QuitButtonPressed;
RestartButton.Click += RestartButtonPressed;
YesButton.Click += YesConfirmationButtonPressed;
NoButton.Click += NoConfirmationButtonPressed;
StartModeButton.Click += CheckModeSelected;
BackModeButton.Click += BackModeButtonPressed;
}
private void OpenSelectModeMenu()
{
RestartQuitGrid.Visibility = StartGrid.Visibility = ShowAnswersTextBox.Visibility =
FunctionButtonsGrid.Visibility = AllCorrectAnswersTextBox.Visibility = QuestionGrid.Visibility =
IncomeStatementGrid.Visibility = AccountListGrid.Visibility = AccountListBorder.Visibility =
Visibility.Hidden;
ModeSelectGrid.Visibility = Visibility.Visible;
}
private void ReturnToMainMenu()
{
StartGrid.Visibility = Visibility.Visible;
FunctionGrid.Visibility = AccountListGrid.Visibility = AccountListBorder.Visibility =
QuestionGrid.Visibility =
Visibility.Hidden;
HideConfirmationButtons();
}
private void AllQuestionsFinished()
{
_questionsCompleteFlag = true;
HideConfirmationButtons();
RestartQuitGrid.Visibility = Visibility.Visible;
if (_showAnswerFlag) ShowAnswersTextBox.Visibility = Visibility.Visible;
else AllCorrectAnswersTextBox.Visibility = Visibility.Visible;
}
private void SetInitialState()
{
_givenAccounts.Clear();
_submitButtonToInputBox.Clear();
_notGivenAccts.Clear();
_inputTextBoxes.Clear();
AccountListGrid.RowDefinitions.Clear();
AccountListGrid.Children.Clear();
QuestionGrid.Children.Clear();
QuestionGrid.RowDefinitions.Clear();
_amountSolved = 0;
_isIncomeStatementProblem = _showAnswerFlag = _restartFlag = _backToMainFlag = _questionsCompleteFlag =
false;
InitializeAccounts();
}
//adds all the information to the grid that holds the account information
private void InitializeAccountList()
{
SetInitialState();
foreach (AccountVal acct in _accounts)
if (!acct.NeedsSolving)
AddToAccountList(acct);
else
AddToQuestionList(acct);
FixBottomBorderAList();
FinishInitialization();
AccountListGrid.Visibility = AccountListBorder.Visibility = Visibility.Visible;
}
private void FinishInitialization()
{
//add one last row definition to the QuestionGrid to reduce the sizes of the other rows
QuestionGrid.RowDefinitions.Add(new RowDefinition()
{ Height = new GridLength(14 - QuestionGrid.RowDefinitions.Count, GridUnitType.Star) });
RowDefinition endRow = new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) };
QuestionGrid.RowDefinitions.Add(endRow);
FunctionButtonsGrid.Visibility = FunctionGrid.Visibility = QuestionGrid.Visibility = Visibility.Visible;
ShowFunctionButtons();
HideConfirmationButtons();
}
private void InitializeIncomeStatement()
{
SetInitialState();
_isIncomeStatementProblem = true;
_fiscalYearStart = _randomDateTime.Next();
if (_fiscalYearStart.Month == 2 && _fiscalYearStart.Day == 29)
{
_fiscalYearStart = _fiscalYearStart.AddMonths(1);
}
_fiscalYearEnd = _fiscalYearStart.AddYears(1).AddDays(-1);
CompanyNameISTextBox.Text = GetRandomCompanyName();
DateTextISBox.Text = $"For the Year Ended {_fiscalYearEnd:MMMM dd, yyyy}";
_beginningInventory.Name = $"Inventory on {_fiscalYearStart:d}";
_endingInventory.Name = $"Inventory on {_fiscalYearEnd:d}";
BeginInventoryISNameTextBox.Text = $"\t{_beginningInventory.Name}";
EndingInventoryISNameTextBox.Text = $"\t{_endingInventory.Name}";
//first add the accounts to the dictionary correlating themselves to the income statement
_ISAcctToTB.Add(_beginningInventory, BeginInventoryISValueTextBox);
_ISAcctToTB.Add(_costOfDeliveredMerchandise, CoDMISValueTextBox);
_ISAcctToTB.Add(_costOfMerchandiseSold, COMSISValueTextBox);
_ISAcctToTB.Add(_costOfMerchandiseAvaForSale, COMAFSISValueTextBox);
_ISAcctToTB.Add(_endingInventory, EndingInventoryISValueTextBox);
_ISAcctToTB.Add(_grossProfit, GrossProfitISValueTextBox);
_ISAcctToTB.Add(_netPurchases, NetPurchasesISValueTextBox);
_ISAcctToTB.Add(_netSales, NetSalesISValueTextBox);
_ISAcctToTB.Add(_purchases, PurchasesISValueTextBox);
_ISAcctToTB.Add(_purchasesDiscounts, PurchasesDiscountsISValueTextBox);
_ISAcctToTB.Add(_purchasesRetAndAllow, PurchasesReturnsISValueTextBox);
_ISAcctToTB.Add(_sales, SalesISValueTextBox);
_ISAcctToTB.Add(_salesDiscounts, SalesDiscountsISValueTextBox);
_ISAcctToTB.Add(_salesRetAndAllow, SalesReturnsISValueTextBox);
_ISAcctToTB.Add(_transportationIn, TransportationInISValueTextBox);
//set the font color since it sometimes changes
BeginInventoryISValueTextBox.Foreground = CoDMISValueTextBox.Foreground = COMSISValueTextBox.Foreground =
COMAFSISValueTextBox.Foreground = EndingInventoryISValueTextBox.Foreground =
GrossProfitISValueTextBox.Foreground = NetPurchasesISValueTextBox.Foreground =
NetSalesISValueTextBox.Foreground = PurchasesISValueTextBox.Foreground =
PurchasesISValueTextBox.Foreground = PurchasesDiscountsISValueTextBox.Foreground =
PurchasesReturnsISValueTextBox.Foreground = SalesISValueTextBox.Foreground =
SalesDiscountsISValueTextBox.Foreground = SalesReturnsISValueTextBox.Foreground =
TransportationInISValueTextBox.Foreground =
Brushes.Black;
foreach (AccountVal acct in _accounts)
if (!acct.NeedsSolving)
AddToIncomeStatement(acct);
else
AddToQuestionList(acct);
FinishInitialization();
IncomeStatementGrid.Visibility = Visibility.Visible;
}
//adds the account to the Dictionary, _notGivenAccounts, and puts a question on the question grid
private void AddToQuestionList(AccountVal acct)
{
QuestionGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
int rowIndex = QuestionGrid.RowDefinitions.Count - 1;
TextBox questionBox = new TextBox
{
VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
BorderThickness = new Thickness(0), TextWrapping = TextWrapping.Wrap, IsReadOnly = true,
Text = $"What is the amount of {acct.Name}?"
};
TextBox inputBox = new TextBox
{
TextWrapping = TextWrapping.NoWrap, Margin = new Thickness(1, 0, 1, 0), CaretBrush = Brushes.Black,
MinWidth = 125
};
inputBox.KeyDown += EnterOrReturnPressed;
Button submitButton = new Button
{
Content = new TextBox()
{
Background = Brushes.Transparent, BorderThickness = new Thickness(0),
TextWrapping = TextWrapping.Wrap, TextAlignment = TextAlignment.Center, IsHitTestVisible = false,
IsReadOnly = true, Text = "Submit"
}
};
submitButton.Click += SubmitButtonPressed;
TextBox warningBox = new TextBox
{
TextWrapping = TextWrapping.Wrap, BorderThickness = new Thickness(0), FontSize = 10,
Foreground = Brushes.Red, IsHitTestVisible = false, IsReadOnly = true
};
Grid inputGrid = new Grid
{
RowDefinitions =
{
new RowDefinition() { Height = new GridLength(3, GridUnitType.Star) },
new RowDefinition() { Height = new GridLength(3, GridUnitType.Star) }
},
Children = { inputBox, warningBox }
};
Grid.SetRow(inputBox, 0);
Grid.SetRow(warningBox, 1);
QuestionGrid.Children.Add(inputGrid);
QuestionGrid.Children.Add(questionBox);
QuestionGrid.Children.Add(submitButton);
Grid.SetRow(questionBox, rowIndex);
Grid.SetRow(inputGrid, rowIndex);
Grid.SetRow(submitButton, rowIndex);
Grid.SetColumn(questionBox, 0);
Grid.SetColumn(inputGrid, 1);
Grid.SetColumn(submitButton, 2);
_notGivenAccts.Add(inputBox, acct);
_submitButtonToInputBox.Add(submitButton, inputBox);
_inputToWarningBox.Add(inputBox, warningBox);
_inputTextBoxes.Add(inputBox);
}
private void AddToAccountList(AccountVal acct)
{
_givenAccounts.Add(acct);
AccountListGrid.RowDefinitions.Add(new RowDefinition());
TextBox acctNameTextBox = new TextBox
{
Text = acct.Name,
Margin = new Thickness(1, 0, 0, 1),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Bottom,
TextAlignment = TextAlignment.Left,
BorderThickness = new Thickness(0),
IsReadOnly = true
};
Border boarder = new Border
{
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(0, 0, 1, 1),
Child = acctNameTextBox
};
AccountListGrid.Children.Add(boarder);
Grid.SetRow(boarder, _givenAccounts.Count - 1);
Grid.SetColumn(boarder, 0);
TextBox acctValTextBox = new TextBox()
{
Text = acct.Amount.ToString("N"),
Margin = new Thickness(0, 0, 1, 1),
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Bottom,
TextAlignment = TextAlignment.Right,
BorderThickness = new Thickness(0),
IsReadOnly = true
};
Border acctValBorder = new Border
{
BorderBrush = Brushes.Black,
BorderThickness = new Thickness(0, 0, 0, 1),
Child = acctValTextBox
};
AccountListGrid.Children.Add(acctValBorder);
Grid.SetRow(acctValBorder, _givenAccounts.Count - 1);
Grid.SetColumn(acctValBorder, 1);
}
private void AddToIncomeStatement(AccountVal acct)
{
TextBox incomeStatementValueTextBox = _ISAcctToTB[acct];
incomeStatementValueTextBox.Text = acct.Amount.ToString("C0");
/*incomeStatementValueTextBox.VerticalAlignment = VerticalAlignment.Bottom;*/
_givenAccounts.Add(acct);
}
private string GetRandomCompanyName()
{
string name = "";
name += CompanyNames[RandomNumberGenerator.GetInt32(0, CompanyNames.Length)];
if (RandomNumberGenerator.GetInt32(1, 21) < 7)
{
name += $" {CompanyNames[RandomNumberGenerator.GetInt32(0, CompanyNames.Length)]}";
if (RandomNumberGenerator.GetInt32(1, 21) == 1)
{
name += $" {CompanyNames[RandomNumberGenerator.GetInt32(0, CompanyNames.Length)]}";
}
}
name += $" {CompanyTypes[RandomNumberGenerator.GetInt32(0, CompanyTypes.Length)]}";
return name;
}
//setting the border thickness properly for the bottom row. These should be the last two indices of the list
private void FixBottomBorderAList()
{
int setCount = 0;
for (var index = AccountListGrid.Children.Count - 1;
index >= 0;
index--)
{
UIElement gridChild = AccountListGrid.Children[index];
if (gridChild is not Border gridChildBorder ||
Grid.GetRow(gridChild) != AccountListGrid.RowDefinitions.Count - 1) continue;
gridChildBorder.BorderThickness = new Thickness(0, 0, gridChildBorder.BorderThickness.Right, 0);
if (++setCount == 2)
break;
}
}
private CancellationTokenSource _modeWarningCTS;
private async void CheckModeSelected(object sender, RoutedEventArgs e)
{
_modeWarningCTS?.Cancel();
if (ISModeSelect.IsChecked == true)
{
ModeSelectGrid.Visibility = Visibility.Hidden;
ISModeSelect.IsChecked = false;
InitializeIncomeStatement();
}
else if (ALModeSelect.IsChecked == true)
{
ModeSelectGrid.Visibility = Visibility.Hidden;
ALModeSelect.IsChecked = false;
InitializeAccountList();
}
else
{
_modeWarningCTS = new CancellationTokenSource();
await AsyncDisplayWarningMessage(ModeWarningTextBox, _modeWarningCTS.Token, "Please select a mode.");
}
}
private void BackModeButtonPressed(object sender, RoutedEventArgs e)
{
ModeSelectGrid.Visibility = Visibility.Hidden;
StartGrid.Visibility = Visibility.Visible;
ISModeSelect.IsChecked = false;
ALModeSelect.IsChecked = false;
_modeWarningCTS?.Cancel();
}
private static readonly CultureInfo CultureInfo = new("en-US");
private readonly Dictionary<TextBox, CancellationTokenSource> _cancellationTokens;
private readonly Dictionary<TextBox, CancellationTokenSource> _warningCTS;
private async void SubmitButtonPressed(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
TextBox input = _submitButtonToInputBox[button];
CancelThisTasks(input);
await AsyncCheckAnswer(input);
}
private async void EnterOrReturnPressed(object sender, KeyEventArgs e)
{
if (e.Key != Key.Return && e.Key != Key.Enter)
return;
TextBox input = sender as TextBox;
CancelThisTasks(input);
await AsyncCheckAnswer(input);
}
private async Task AsyncCheckAnswer(TextBox input)
{
if (_warningCTS.TryGetValue(input, out var tempCTS))
{
tempCTS.Cancel();
_warningCTS.Remove(input);
}
if (!int.TryParse(input.Text, NumberStyles.Currency, CultureInfo,
out var givenAnswer))
{
CancellationTokenSource cts = new CancellationTokenSource();
_warningCTS.Add(input, cts);
await AsyncDisplayWarningMessage(_inputToWarningBox[input], cts.Token, "Input is not a number.");
return;
}
AccountVal notGivenAcct = _notGivenAccts[input];
int actualAnswer = notGivenAcct.Amount;
if (givenAnswer == actualAnswer)
{
input.Background = new SolidColorBrush(Color.FromRgb(127, 255, 0));
input.IsReadOnly = true;
if (_isIncomeStatementProblem)
PutAnswerIncomeStatement(_ISAcctToTB[notGivenAcct], givenAnswer, Brushes.YellowGreen);
if (++_amountSolved == _notGivenAccts.Count)
AllQuestionsFinished();
}
else //answer is incorrect
await AsyncIncorrectAnimation(input);
}
private void ShowAnswersButtonPressed(object sender, RoutedEventArgs e)
{
ShowConfirmationButtons();
ConfirmationTextBox.Text = "Are you sure you want to show the answers?";
_showAnswerFlag = true;
}
private void ShowConfirmationButtons()
{
RestartQuitGrid.Visibility = ShowAnswersButton.Visibility =
FunctionHelpButton.Visibility = BackButton.Visibility =
Visibility.Hidden;
YesButton.Visibility = NoButton.Visibility = ConfirmationTextBox.Visibility = Visibility.Visible;
}
private void HelpButtonPressed(object sender, RoutedEventArgs e)
{
_helpWindow = new HelpWindow();
_helpWindow.Show();
}
private void QuitButtonPressed(object sender, RoutedEventArgs e)
{
ShowConfirmationButtons();
ConfirmationTextBox.Text =
"Are you sure you want to return to the main menu? Questions and answers will not be saved.";
_backToMainFlag = true;
}
private void RestartButtonPressed(object sender, RoutedEventArgs e)
{
ShowConfirmationButtons();
ConfirmationTextBox.Text = "Are you sure you want to restart?";
_restartFlag = true;
}
private void YesConfirmationButtonPressed(object sender, RoutedEventArgs e)
{
if (_backToMainFlag)
{
if (_showAnswerFlag)
ResetShowAnswerState();
ReturnToMainMenu();
return;
}
if (_restartFlag)
{
if (_showAnswerFlag)
ResetShowAnswerState();
OpenSelectModeMenu();
return;
}
if (_showAnswerFlag)
{
if (_isIncomeStatementProblem)
{
foreach (TextBox inputTextBox in _inputTextBoxes)
{
AccountVal notGivenAcct = _notGivenAccts[inputTextBox];
int expectedAnswer = notGivenAcct.Amount;
inputTextBox.IsReadOnly = true;
if (Int32.TryParse(inputTextBox.Text, NumberStyles.Currency, CultureInfo, out var answer)
&& answer == expectedAnswer)
continue;
inputTextBox.Foreground = Brushes.DarkOrange;
inputTextBox.Text = expectedAnswer.ToString("C", CultureInfo);
PutAnswerIncomeStatement(_ISAcctToTB[notGivenAcct], expectedAnswer,
Brushes.DarkOrange);
}
}
else
{
foreach (TextBox inputTextBox in _inputTextBoxes)
{
int expectedAnswer = _notGivenAccts[inputTextBox].Amount;