-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subscription.cs
120 lines (104 loc) · 4.03 KB
/
Subscription.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
namespace SubscriptionCalculator
{
public class Subscription
{
public string? SubscriptionName { get; set; }
public bool IsOwned { get; set; }
public int? OwnedIndex { get; set; }
public List<SubscriptionData>? SubscriptionDataList { get; set; } // Subscription type, price per billing period
public static SubscriptionType? PromptSubscriptionType(string header)
{
Console.Clear();
Console.WriteLine(header);
Console.WriteLine();
// get a list of every subscription type
SubscriptionType[] subscriptionTypes = (SubscriptionType[])Enum.GetValues(typeof(SubscriptionType));
// list all subscription types
int i = 1;
foreach (SubscriptionType subscriptionType in subscriptionTypes)
{
Console.WriteLine($"{i}. {subscriptionType.ToFriendlyString()}");
i++;
}
// read a key for finding the subscription type
ConsoleKeyInfo subType = Console.ReadKey(true);
i = 1;
// for every subscription type
foreach (SubscriptionType subscriptionType in subscriptionTypes)
{
// if i is the same as the pressed character (because of how i was used earlier)
if (subType.KeyChar == i.ToString().ToCharArray().Single())
{
// return the subscription type
return subscriptionType;
}
i++;
}
return null;
}
}
public class SubscriptionData
{
public SubscriptionType SubscriptionType { get; set; }
public double Price { get; set; }
public SubscriptionData(SubscriptionType subscriptionType, double price)
{
SubscriptionType = subscriptionType;
Price = price;
}
}
public enum SubscriptionType
{
Monthly, // 1 Month
Quarterly, // 3 Month
Yearly // 12 Month
}
public static class SubscriptionTypeExtensions
{
public static string ToFriendlyString(this SubscriptionType type)
{
return type switch
{
SubscriptionType.Monthly => "Monthly",
SubscriptionType.Quarterly => "Quarterly",
SubscriptionType.Yearly => "Yearly",
_ => "Monthly",
};
}
public static SubscriptionType ToSubscriptionType(this string type)
{
return type switch
{
"Monthly" => SubscriptionType.Monthly,
"Quarterly" => SubscriptionType.Quarterly,
"Yearly" => SubscriptionType.Yearly,
_ => SubscriptionType.Monthly,
};
}
public static double ToPriceType(this SubscriptionData subData, SubscriptionType targetType)
{
double priceAsMonthly;
// get any subscription type down to a monthly
priceAsMonthly = subData.SubscriptionType switch
{
SubscriptionType.Monthly => subData.Price,
SubscriptionType.Quarterly => subData.Price / 3,
SubscriptionType.Yearly => subData.Price / 12,
_ => subData.Price,
};
// bring it to the target type
return targetType switch
{
SubscriptionType.Monthly => priceAsMonthly,
SubscriptionType.Quarterly => priceAsMonthly * 3,
SubscriptionType.Yearly => priceAsMonthly * 12,
_ => priceAsMonthly,
};
}
public static string ToComparisonString(this SubscriptionType type, SubscriptionType target)
{
if (type == target) return type.ToFriendlyString();
return $"{type.ToFriendlyString()} -> {target.ToFriendlyString()}";
}
}
}