-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditGoal.xaml.cs
136 lines (127 loc) · 4.54 KB
/
EditGoal.xaml.cs
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
using Goalie.Lib;
using Goalie.Lib.Models;
using System.Globalization;
using System.Windows;
namespace Goalie
{
/// <summary>
/// Interaction logic for EditGoal.xaml
/// </summary>
public partial class EditGoal : Window, IShouldSave, IShouldDelete
{
public Account Account { get; set; }
public bool IsNewMode { get; set; }
public bool ShouldSave { get; set; }
public bool ShouldDelete { get; set; }
public EditGoal()
{
InitializeComponent();
IsNewMode = true;
ShouldSave = false;
ShouldDelete = false;
Account = new Account();
Account.Type = AccountType.Goal;
Account.SavingsAmount = 5;
Account.SavingsType = GoalSavingsType.Percentage;
Account.Name = "My New Goal";
Account.ContinueSavingAfterGoalMet = true;
DisplayAccount();
}
public EditGoal(Account account)
{
InitializeComponent();
IsNewMode = false;
ShouldSave = false;
ShouldDelete = false;
Account = account;
DisplayAccount();
}
protected void DisplayAccount()
{
if (IsNewMode)
{
Title = "New Goal";
GoalLabel.Text = "New Goal";
DeleteButton.Visibility = Visibility.Collapsed;
}
else
{
if(Account.Type == AccountType.GeneralSavings)
{
AmountPerPaycheck.Visibility = Visibility.Collapsed;
GoalSettings.Visibility = Visibility.Collapsed;
ContinueSettings.Visibility = Visibility.Collapsed;
Title = $"Edit Account \"{Account.Name}\"";
GoalLabel.Text = $"Edit Account \"{Account.Name}\"";
DeleteButton.Visibility = Visibility.Collapsed;
}
else
{
Title = $"Edit Goal \"{Account.Name}\"";
GoalLabel.Text = $"Edit Goal \"{Account.Name}\"";
}
}
SavingsType.ItemsSource = DropDownItems.GoalSavingsTypes;
AccountName.Text = Account.Name;
InitialBalance.Text = Account.Balance.ToString("C");
SavingsAmount.Text = Account.SavingsAmount.ToString();
SavingsType.SelectedValue = Account.SavingsType;
Goal.Text = (Account.FixedGoal ?? (decimal)0.0).ToString("C");
ContinueAfterGoalMet.IsChecked = Account.ContinueSavingAfterGoalMet;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
decimal balance;
decimal? goal = null, savingsAmount = null;
try
{
balance = decimal.Parse(InitialBalance.Text, NumberStyles.Currency);
}
catch
{
MessageBox.Show("\"Balance\" must be a numerical value");
return;
}
try
{
goal = decimal.Parse(Goal.Text, NumberStyles.Currency);
if (goal == 0)
goal = null;
}
catch { }
try
{
savingsAmount = decimal.Parse(SavingsAmount.Text, NumberStyles.Currency);
if (savingsAmount == 0)
savingsAmount = null;
}
catch { }
Account.Name = AccountName.Text;
Account.Balance = balance;
if (Account.Type != AccountType.GeneralSavings)
{
Account.SavingsAmount = savingsAmount;
Account.SavingsType = (GoalSavingsType)SavingsType.SelectedValue;
Account.FixedGoal = goal;
Account.ContinueSavingAfterGoalMet = ContinueAfterGoalMet.IsChecked ?? true;
}
if (IsNewMode)
Account.NewID();
ShouldSave = true;
Close();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if(MessageBox.Show("Delete this goal? The balance will be transferred to the general savings account",
"Confirm", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
ShouldDelete = true;
Close();
}
}
}
}