-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
226 lines (180 loc) · 8.04 KB
/
Program.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
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
using System.Diagnostics;
using System.Text.Json;
namespace SubscriptionCalculator
{
internal class Program
{
private static List<Subscription>? SubscriptionsList { get; set; }
public static T LoadJsonFromFile<T>(string path)
{
if (!File.Exists(path))
return (T)Activator.CreateInstance(typeof(T))!;
string json = File.ReadAllText(path);
T jsonObj = JsonSerializer.Deserialize<T>(json)!;
return jsonObj;
}
public static void SaveJsonToFile<T>(T type, string path)
{
if (File.Exists(path))
File.Delete(path);
string json = JsonSerializer.Serialize(type);
File.WriteAllText(path, json);
}
static void Main()
{
SubscriptionsList = LoadJsonFromFile<List<Subscription>>("subscriptions.json");
bool mainLoop = true;
while (mainLoop)
{
Console.Clear();
Console.WriteLine("--- Subscription Calculator ---");
Console.WriteLine("(Press enter to exit)");
Console.WriteLine();
Console.WriteLine("1. Add a subscription");
Console.WriteLine("2. Remove a subscription");
Console.WriteLine("3. List subscriptions");
Console.WriteLine("4. Calculate total");
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.KeyChar)
{
case '1':
AddSubscription();
break;
case '2':
RemoveSubscription();
break;
case '3':
ListSubscriptions();
break;
case '4':
CalculateTotal();
break;
case (char)13:
case '\0':
case ' ':
mainLoop = false;
break;
}
}
SaveJsonToFile(SubscriptionsList, "subscriptions.json");
}
static void AddSubscription()
{
Console.Clear();
Console.Write("What is the name of the subscription? ");
string subscriptionName = Console.ReadLine()!;
Subscription subscription = new()
{
SubscriptionName = subscriptionName,
};
bool isMoreSubscriptionTypes = true;
while (isMoreSubscriptionTypes)
{
SubscriptionType currentSubscriptionType;
SubscriptionData? currentSubscriptionData;
SubscriptionType? tempSubType = Subscription.PromptSubscriptionType("What time period of subscription is it? (Or press enter to return to the menu)");
if (tempSubType == null)
{
if (subscription.SubscriptionDataList == null)
return;
isMoreSubscriptionTypes = false;
continue;
}
subscription.SubscriptionDataList ??= new();
currentSubscriptionType = tempSubType.Value;
Console.Write("\nWhat is the price of the subscription? ");
double price = double.Parse(Console.ReadLine()!);
// set some stuff (current subscription type, current subscription data), and add the current subscription data to a list
currentSubscriptionData = new(currentSubscriptionType, price);
subscription.SubscriptionDataList.Add(currentSubscriptionData);
// ask if owned, if y, set the owned flag to true and the ownedindex to the indexof currentsubscriptiondata
Console.Write("Do you own this subscription type? (y/N) ");
ConsoleKeyInfo doesOwn = Console.ReadKey(true);
switch ($"{doesOwn.KeyChar}".ToLower())
{
case "y":
subscription.IsOwned = true;
subscription.OwnedIndex = subscription.SubscriptionDataList.IndexOf(currentSubscriptionData);
break;
}
}
SubscriptionsList!.Add(subscription);
}
static void RemoveSubscription()
{
Console.Clear();
Console.Write("What subscription would you like to remove? (or press enter to exit) ");
string subscription = Console.ReadLine()!;
if (subscription == null || subscription == "" || subscription == " " || SubscriptionsList!.Find(pred => pred.SubscriptionName == subscription) == null)
return;
SubscriptionsList.Remove(SubscriptionsList.Find(pred => pred.SubscriptionName == subscription)!);
Console.WriteLine($"Removed {subscription}!");
}
static void ListSubscriptions()
{
Console.Clear();
Console.WriteLine("What subscriptions would you like to list?");
Console.WriteLine();
Console.WriteLine("1. Owned");
Console.WriteLine("2. Not Owned");
Console.WriteLine("3. All");
ConsoleKeyInfo key = Console.ReadKey(true);
switch (key.KeyChar)
{
case '1':
Console.Clear();
Console.WriteLine("Owned Subscriptions: ");
Console.WriteLine();
foreach (Subscription sub in SubscriptionsList!)
{
if (!sub.IsOwned)
{
continue;
}
SubscriptionData subData = sub.SubscriptionDataList![sub.OwnedIndex!.Value];
Console.WriteLine($" {sub.SubscriptionName}, {subData.SubscriptionType.ToFriendlyString()}");
}
Console.WriteLine();
Console.WriteLine("Press any key to return to the menu...");
Console.ReadKey(true);
break;
case '2':
break;
case '3':
break;
case (char)13:
case '\0':
case ' ':
break;
}
}
static void CalculateTotal()
{
SubscriptionType currentSubscriptionType;
SubscriptionType? tempSubType = Subscription.PromptSubscriptionType("What billing period would you like to view your prices as?");
if (tempSubType == null) // if they want to leave, return
return;
currentSubscriptionType = tempSubType.Value;
Console.Clear();
Console.WriteLine($"Subscriptions as {currentSubscriptionType.ToFriendlyString()}: ");
Console.WriteLine();
double total = 0;
foreach (Subscription sub in SubscriptionsList!)
{
if (!sub.IsOwned)
{
continue;
}
SubscriptionData subData = sub.SubscriptionDataList![sub.OwnedIndex!.Value];
double price = subData.ToPriceType(currentSubscriptionType);
Console.WriteLine($" {price:C} ({sub.SubscriptionName}, {subData.SubscriptionType.ToComparisonString(currentSubscriptionType)})");
total += price;
}
Console.WriteLine("----------");
Console.WriteLine($"Total: {total:C}");
Console.WriteLine();
Console.WriteLine("Press any key to return to the menu...");
Console.ReadKey(true);
}
}
}