I made this small system that checks if you're old enough to drive. I'm new to C#, but I do know most of the basics since I also know Java. I'm trying to make my code more efficient. Any suggestions or help would be appreciated.
using System;
using System.Threading;
class EfficientCode
{
static int age = 0;
static void Main(string[] args)
{
Console.WriteLine("This is just a simple project that takes simple and small information from you and uses it to process the information and checks whether you're allowed to drive or not. It also tells you how long is left till you drive.");
Thread.Sleep(8000); // adds a 8 second delay so that the user could first read the introduction
Start(); // after the 8 second delay it starts the system
}
static void Start()
{
Console.WriteLine("Enter your first name");
String firstName = Console.ReadLine();
Console.WriteLine("Enter your middle name");
String middleName = Console.ReadLine();
Console.WriteLine("Enter your last name");
String lastName = Console.ReadLine();
Console.WriteLine("Enter your age");
try
{
age = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("ERROR: Input AGE must be a number!");
Console.WriteLine("Reinitializing questions.");
Thread.Sleep(3000); // this adds a delay of 3 seconds
Start(); // restarts the questions
}
if (ConfirmInformation(firstName, middleName, lastName, age))
{
if (age >= 18)
{
Console.WriteLine("Congratulations! You're already old enough to drive.");
Environment.Exit(0); // Ends the system
}
else if (age == 17)
{
Console.WriteLine("You're almost there! You can get a learners permit.\nYou have 1 more year left to get an official drivers license!");
Environment.Exit(0); // Ends the system
}
else
{
Console.WriteLine("You'll get there. You're not old enough to have a license.");
int yearsLeft = 18 - age; // gets the years left to drive
Console.WriteLine("You still have " + yearsLeft + " years left to get a license.");
Environment.Exit(0); // Ends the system
}
}
else
{
Console.WriteLine("Reinitializing questions.");
Thread.Sleep(3000); // this adds a delay of 3 seconds
Start(); // restarts the questions
}
}
static bool ConfirmInformation(String firstName, String middleName, String lastName, int age)
{
Console.WriteLine("======================================");
Console.WriteLine("INFORMATION CHECK:");
Console.WriteLine("");
Console.WriteLine("FIRST NAME: " + firstName);
Console.WriteLine("MIDDLE NAME: " + middleName);
Console.WriteLine("LAST NAME: " + lastName);
Console.WriteLine("AGE: " + age);
Console.WriteLine("");
Console.WriteLine("Are all those information correct? (Y/N)");
Console.WriteLine("======================================");
String booleanInput = Console.ReadLine();
if (booleanInput.Equals("yes", StringComparison.OrdinalIgnoreCase) || booleanInput.Equals("y", StringComparison.OrdinalIgnoreCase))
{
return true;
}
else if (booleanInput.Equals("no", StringComparison.OrdinalIgnoreCase) || booleanInput.Equals("n", StringComparison.OrdinalIgnoreCase))
{
return false;
}
else
{
Console.WriteLine("ERROR: Input must be either YES or NO.");
return false;
}
return false;
}
}