-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeathdate.cs
More file actions
185 lines (149 loc) · 5.56 KB
/
deathdate.cs
File metadata and controls
185 lines (149 loc) · 5.56 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
using System;
namespace RhinoS
{
public class Program
{
static readonly Random Rng = new Random();
static DateTime GetBirthday()
{
int month = ReadInt("Month [1-12]]: ", 1, 12);
int year = ReadYear("Year: ");
int day = ReadDay(month, year);
return new DateTime(year, month, day);
}
static void PrintHeader()
{
Console.WriteLine("========================================");
Console.WriteLine(" created by Chloe Jane ");
Console.WriteLine("========================================");
Console.WriteLine();
}
static void ShowBirthdayInfo(DateTime birthday)
{
DateTime today = DateTime.Today;
Console.WriteLine();
Console.WriteLine($" Your birthday: {birthday:MMMM d, yyyy}");
if (birthday <= today)
{
int age = CalculateAge(birthday, today);
Console.WriteLine($" Age: {age} year{(age == 1 ? "" : "s")} old");
ShowNextBirthday(birthday, today);
}
else
{
int daysUntil = (birthday - today).Days;
Console.WriteLine($" Arrives in: {daysUntil} day{(daysUntil == 1 ? "" : "s")} from today");
}
}
static void ShowNextBirthday(DateTime birthday, DateTime today)
{
DateTime next = NextBirthdayFrom(birthday, today);
int daysLeft = (next - today).Days;
if (daysLeft == 0)
{
Console.WriteLine(" Today is your birthday!");
}
else
{
Console.WriteLine($" Next birthday: {next:MMMM d, yyyy} ({daysLeft} day{(daysLeft == 1 ? "" : "s")} away)");
}
}
static void ShowFate(DateTime birthday)
{
Console.WriteLine();
Console.Write(" Press Enter...\n");
Console.ReadLine();
DateTime today = DateTime.Today;
DateTime startDate = birthday > today ? birthday : today;
DateTime fateDate = GenerateFutureDate(startDate, startDate.AddYears(50));
Console.WriteLine($" Death date: {fateDate:MMMM d, yyyy}");
Console.WriteLine();
}
static bool AskPlayAgain()
{
Console.Write(" Play again? (y/n): ");
string answer = (Console.ReadLine() ?? "").Trim().ToLower();
return answer == "y" || answer == "yes";
}
static int ReadInt(string prompt, int min, int max)
{
while (true)
{
Console.Write($" {prompt}");
string input = Console.ReadLine() ?? "";
//C
if (int.TryParse(input.Trim(), out int value) && value >= min && value <= max)
return value;
//C
Console.WriteLine($" Invalid. Enter a number between {min} and {max}.\n");
}
}
static int ReadYear(string prompt)
{
int currentYear = DateTime.Today.Year;
while (true)
{
Console.Write($" {prompt}");
string input = (Console.ReadLine() ?? "").Trim();
//C
if (input.Length == 4 && int.TryParse(input, out int year) && year >= 1900 && year <= currentYear)
return year;
//C
Console.WriteLine($" Invalid Input.\n");
}
}
static int ReadDay(int month, int year)
{
int maxDay = DateTime.DaysInMonth(year, month);
while (true)
{
Console.Write($" Day (1-{maxDay}): ");
string input = (Console.ReadLine() ?? "").Trim();
//C
if (int.TryParse(input, out int day) && day >= 1 && day <= maxDay)
return day;
//C
Console.WriteLine($" Invalid Input.\n");
}
}
static DateTime GenerateFutureDate(DateTime start, DateTime end)
{
if (start >= end)
{
return start;
}
int range = (end - start).Days;
return start.AddDays(Rng.Next(0, range + 1));
}
static int CalculateAge(DateTime birthday, DateTime today)
{
int age = today.Year - birthday.Year;
if (today < birthday.AddYears(age)) age--;
return age;
}
static DateTime NextBirthdayFrom(DateTime birthday, DateTime today)
{
DateTime next = new DateTime(today.Year, birthday.Month, birthday.Day);
if (next < today) next = next.AddYears(1);
{
return next;
}
}
public static void Main(string[] args)
{
while (true)
{
Console.Clear();
PrintHeader();
DateTime birthday = GetBirthday();
ShowBirthdayInfo(birthday);
ShowFate(birthday);
if(!AskPlayAgain())
{
break;
}
}
Console.WriteLine("\nProgram Ended.\n");
}
}
}