C# Lesson 12
Matt Bauer 2/19/2020
How to use Methods.
In this lesson we are going to learn about Methods.
using System;
namespace Lesson12
{
class Methods
{
// A method needs to be delared inside a class
static void Main()
{
RunProgram();
}
static void RunProgram()
{
string name = PromptTheUser("What is your name?");
// Make a method to ask the user for their age
// int age = PromptTheUserForInt("What is your age?");
int age = PromptTheUserForInt("What is your age?");
Console.WriteLine($"Your name is {name} and you are {age} years old");
}
static int PromptTheUserForInt(string prompt)
{
Console.WriteLine(prompt);
int userInput = Convert.ToInt32(Console.ReadLine());
return userInput;
}
static string PromptTheUser(string prompt)
{
Console.WriteLine(prompt);
string userInput = Console.ReadLine();
return userInput;
}
}
}
/*
* TASK
*
* You are creating a password creation tool.
* A password has to meet 4 requirements...
* -Contain at least 1 upper case letter.
* -Contain at least 1 lower case letter.
* -Contain one of these special characters !#$%&
* -Be more than 8 characters long.
*
* Create 4 methods to check if the password is 'Valid' or 'Invalid'.
* One method for each of the requirements.
*
* I have started the program by prompting the user and checking the first requirement.
*
*/
using System;
namespace Lesson12Task
{
class MethodsTask
{
static void Main(string[] args)
{
Console.WriteLine("A password must be...");
Console.WriteLine("Contain at least 1 upper case letter.");
Console.WriteLine("Contain at least 1 lower case letter.");
Console.WriteLine("Contain one of these special characters !#$%&");
Console.WriteLine("Be more than 8 characters long.");
Console.WriteLine();
Console.Write("Enter a password: ");
string password = Console.ReadLine();
bool containsUpperCase = CheckUpperCase(password);
if (containsUpperCase)
{
Console.WriteLine("Valid");
}
else
{
Console.WriteLine("Rejected");
}
}
static bool CheckUpperCase(string password)
{
for (int i = 0; i < password.Length; i++)
{
if (password[i] >= 65 && password[i] <= 'Z')
{
return true;
}
}
return false;
}
}
}