Skip to content

Instantly share code, notes, and snippets.

@grantwinney
Created August 3, 2018 21:07
Show Gist options
  • Save grantwinney/f108565bc6a829ddc1d759b50c163212 to your computer and use it in GitHub Desktop.
Save grantwinney/f108565bc6a829ddc1d759b50c163212 to your computer and use it in GitHub Desktop.
an example for a blog post - comments on what, revisited
public static void Main(string[] args)
{
var accountId = "1234";
ValidateUser(accountId);
var action = GetTransactionType(accountId);
var amount = GetTransactionAmount(accountId);
ShowTransactionAndBalance(accountId, action, amount);
}
private static void ShowTransactionAndBalance(string accountId, string action, decimal amount)
{
var name = GetUserName(accountId);
var balance = GetBalance(accountId);
Console.WriteLine($"\nThanks {name}, we recorded your {(action == "D" ? "deposit" : "withdrawal")} of ${amount}.");
Console.WriteLine($"For your records, you now have ${(balance + (action == "D" ? amount : -amount))} in your account.");
}
private static bool ValidateUser(string accountId)
{
var secretAnswer = GetSecret(accountId);
GetUserInput("Provide your secret answer before continuing:",
(answer) => answer == secretAnswer,
"WRONG!!!");
return true;
}
private static string GetTransactionType(string accountId)
{
var name = GetUserName(accountId);
return GetUserInput($"Hello {name}, [W]ithdrawal or [D]eposit?",
(tranType) => tranType == "D" || tranType == "W",
"Invalid option!");
}
private static decimal GetTransactionAmount(string accountId)
{
var balance = GetBalance(accountId);
var amount = GetUserInput($"How much is this transaction for? (You have ${balance})",
(amt) => Decimal.TryParse(amt, out decimal a) && Decimal.Parse(amt) > 0,
"Invalid amount!");
return Decimal.Parse(amount);
}
private static string GetSecret(string accountId)
{
return "42";
}
private static string GetUserName(string accountId)
{
return "Grant";
}
private static decimal GetBalance(string accountId)
{
return 100m;
}
private static string GetUserInput(string prompt, Func<string, bool> isValidAnswer, string errorMessage)
{
var answer = "";
while (!isValidAnswer(answer))
{
Console.Write(prompt + " ");
answer = Console.ReadLine();
if (!isValidAnswer(answer))
Console.WriteLine($"\n{errorMessage}");
}
return answer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment